| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1 | /** | 
|  | 2 | * \file psa/crypto_se_driver.h | 
|  | 3 | * \brief PSA external cryptoprocessor driver module | 
|  | 4 | * | 
|  | 5 | * This header declares types and function signatures for cryptography | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 6 | * drivers that access key material via opaque references. | 
|  | 7 | * This is meant for cryptoprocessors that have a separate key storage from the | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 8 | * space in which the PSA Crypto implementation runs, typically secure | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 9 | * elements (SEs). | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 10 | * | 
| Gilles Peskine | b6cadea | 2019-06-24 13:46:37 +0200 | [diff] [blame] | 11 | * This file is part of the PSA Crypto Driver HAL (hardware abstraction layer), | 
|  | 12 | * containing functions for driver developers to implement to enable hardware | 
|  | 13 | * to be called in a standardized way by a PSA Cryptography API | 
|  | 14 | * implementation. The functions comprising the driver HAL, which driver | 
|  | 15 | * authors implement, are not intended to be called by application developers. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 16 | */ | 
|  | 17 |  | 
|  | 18 | /* | 
| Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 19 | *  Copyright The Mbed TLS Contributors | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 20 | *  SPDX-License-Identifier: Apache-2.0 | 
|  | 21 | * | 
|  | 22 | *  Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 23 | *  not use this file except in compliance with the License. | 
|  | 24 | *  You may obtain a copy of the License at | 
|  | 25 | * | 
|  | 26 | *  http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 27 | * | 
|  | 28 | *  Unless required by applicable law or agreed to in writing, software | 
|  | 29 | *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 30 | *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 31 | *  See the License for the specific language governing permissions and | 
|  | 32 | *  limitations under the License. | 
|  | 33 | */ | 
|  | 34 | #ifndef PSA_CRYPTO_SE_DRIVER_H | 
|  | 35 | #define PSA_CRYPTO_SE_DRIVER_H | 
|  | 36 |  | 
|  | 37 | #include "crypto_driver_common.h" | 
|  | 38 |  | 
|  | 39 | #ifdef __cplusplus | 
|  | 40 | extern "C" { | 
|  | 41 | #endif | 
|  | 42 |  | 
| Gilles Peskine | 7a86da1 | 2019-07-12 23:25:38 +0200 | [diff] [blame] | 43 | /** \defgroup se_init Secure element driver initialization | 
|  | 44 | */ | 
|  | 45 | /**@{*/ | 
|  | 46 |  | 
|  | 47 | /** \brief Driver context structure | 
|  | 48 | * | 
|  | 49 | * Driver functions receive a pointer to this structure. | 
|  | 50 | * Each registered driver has one instance of this structure. | 
|  | 51 | * | 
|  | 52 | * Implementations must include the fields specified here and | 
|  | 53 | * may include other fields. | 
|  | 54 | */ | 
|  | 55 | typedef struct { | 
|  | 56 | /** A read-only pointer to the driver's persistent data. | 
|  | 57 | * | 
| Gilles Peskine | 0c3ae1f | 2019-07-25 14:04:38 +0200 | [diff] [blame] | 58 | * Drivers typically use this persistent data to keep track of | 
|  | 59 | * which slot numbers are available. This is only a guideline: | 
|  | 60 | * drivers may use the persistent data for any purpose, keeping | 
|  | 61 | * in mind the restrictions on when the persistent data is saved | 
|  | 62 | * to storage: the persistent data is only saved after calling | 
|  | 63 | * certain functions that receive a writable pointer to the | 
|  | 64 | * persistent data. | 
| Gilles Peskine | 7a86da1 | 2019-07-12 23:25:38 +0200 | [diff] [blame] | 65 | * | 
|  | 66 | * The core allocates a memory buffer for the persistent data. | 
| Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 67 | * The pointer is guaranteed to be suitably aligned for any data type, | 
| Gilles Peskine | 7a86da1 | 2019-07-12 23:25:38 +0200 | [diff] [blame] | 68 | * like a pointer returned by `malloc` (but the core can use any | 
|  | 69 | * method to allocate the buffer, not necessarily `malloc`). | 
|  | 70 | * | 
| Gilles Peskine | 9dd125d | 2019-07-23 18:26:43 +0200 | [diff] [blame] | 71 | * The size of this buffer is in the \c persistent_data_size field of | 
|  | 72 | * this structure. | 
| Gilles Peskine | 7a86da1 | 2019-07-12 23:25:38 +0200 | [diff] [blame] | 73 | * | 
|  | 74 | * Before the driver is initialized for the first time, the content of | 
|  | 75 | * the persistent data is all-bits-zero. After a driver upgrade, if the | 
|  | 76 | * size of the persistent data has increased, the original data is padded | 
|  | 77 | * on the right with zeros; if the size has decreased, the original data | 
|  | 78 | * is truncated to the new size. | 
|  | 79 | * | 
|  | 80 | * This pointer is to read-only data. Only a few driver functions are | 
|  | 81 | * allowed to modify the persistent data. These functions receive a | 
| Gilles Peskine | 0c3ae1f | 2019-07-25 14:04:38 +0200 | [diff] [blame] | 82 | * writable pointer. These functions are: | 
|  | 83 | * - psa_drv_se_t::p_init | 
|  | 84 | * - psa_drv_se_key_management_t::p_allocate | 
|  | 85 | * - psa_drv_se_key_management_t::p_destroy | 
|  | 86 | * | 
|  | 87 | * The PSA Cryptography core saves the persistent data from one | 
|  | 88 | * session to the next. It does this before returning from API functions | 
|  | 89 | * that call a driver method that is allowed to modify the persistent | 
|  | 90 | * data, specifically: | 
|  | 91 | * - psa_crypto_init() causes a call to psa_drv_se_t::p_init, and may call | 
|  | 92 | *   psa_drv_se_key_management_t::p_destroy to complete an action | 
|  | 93 | *   that was interrupted by a power failure. | 
|  | 94 | * - Key creation functions cause a call to | 
|  | 95 | *   psa_drv_se_key_management_t::p_allocate, and may cause a call to | 
|  | 96 | *   psa_drv_se_key_management_t::p_destroy in case an error occurs. | 
|  | 97 | * - psa_destroy_key() causes a call to | 
|  | 98 | *   psa_drv_se_key_management_t::p_destroy. | 
| Gilles Peskine | 7a86da1 | 2019-07-12 23:25:38 +0200 | [diff] [blame] | 99 | */ | 
|  | 100 | const void *const persistent_data; | 
|  | 101 |  | 
|  | 102 | /** The size of \c persistent_data in bytes. | 
|  | 103 | * | 
| Gilles Peskine | 9dd125d | 2019-07-23 18:26:43 +0200 | [diff] [blame] | 104 | * This is always equal to the value of the `persistent_data_size` field | 
|  | 105 | * of the ::psa_drv_se_t structure when the driver is registered. | 
| Gilles Peskine | 7a86da1 | 2019-07-12 23:25:38 +0200 | [diff] [blame] | 106 | */ | 
|  | 107 | const size_t persistent_data_size; | 
|  | 108 |  | 
|  | 109 | /** Driver transient data. | 
|  | 110 | * | 
|  | 111 | * The core initializes this value to 0 and does not read or modify it | 
|  | 112 | * afterwards. The driver may store whatever it wants in this field. | 
|  | 113 | */ | 
|  | 114 | uintptr_t transient_data; | 
|  | 115 | } psa_drv_se_context_t; | 
|  | 116 |  | 
|  | 117 | /** \brief A driver initialization function. | 
|  | 118 | * | 
|  | 119 | * \param[in,out] drv_context       The driver context structure. | 
| Gilles Peskine | 94cc42c | 2019-07-12 23:34:20 +0200 | [diff] [blame] | 120 | * \param[in,out] persistent_data   A pointer to the persistent data | 
|  | 121 | *                                  that allows writing. | 
| Gilles Peskine | 52ac958 | 2020-05-10 00:39:18 +0200 | [diff] [blame] | 122 | * \param location                  The location value for which this driver | 
|  | 123 | *                                  is registered. The driver will be invoked | 
|  | 124 | *                                  for all keys whose lifetime is in this | 
|  | 125 | *                                  location. | 
| Gilles Peskine | 7a86da1 | 2019-07-12 23:25:38 +0200 | [diff] [blame] | 126 | * | 
|  | 127 | * \retval #PSA_SUCCESS | 
|  | 128 | *         The driver is operational. | 
|  | 129 | *         The core will update the persistent data in storage. | 
|  | 130 | * \return | 
|  | 131 | *         Any other return value prevents the driver from being used in | 
|  | 132 | *         this session. | 
|  | 133 | *         The core will NOT update the persistent data in storage. | 
|  | 134 | */ | 
|  | 135 | typedef psa_status_t (*psa_drv_se_init_t)(psa_drv_se_context_t *drv_context, | 
| Gilles Peskine | 94cc42c | 2019-07-12 23:34:20 +0200 | [diff] [blame] | 136 | void *persistent_data, | 
| Gilles Peskine | 52ac958 | 2020-05-10 00:39:18 +0200 | [diff] [blame] | 137 | psa_key_location_t location); | 
| Gilles Peskine | 7a86da1 | 2019-07-12 23:25:38 +0200 | [diff] [blame] | 138 |  | 
| Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 139 | #if defined(__DOXYGEN_ONLY__) || !defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 140 | /* Mbed Crypto with secure element support enabled defines this type in | 
|  | 141 | * crypto_types.h because it is also visible to applications through an | 
|  | 142 | * implementation-specific extension. | 
|  | 143 | * For the PSA Cryptography specification, this type is only visible | 
|  | 144 | * via crypto_se_driver.h. */ | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 145 | /** An internal designation of a key slot between the core part of the | 
|  | 146 | * PSA Crypto implementation and the driver. The meaning of this value | 
|  | 147 | * is driver-dependent. */ | 
| Gilles Peskine | f03143a | 2019-07-12 23:18:29 +0200 | [diff] [blame] | 148 | typedef uint64_t psa_key_slot_number_t; | 
| Gilles Peskine | c8000c0 | 2019-08-02 20:15:51 +0200 | [diff] [blame] | 149 | #endif /* __DOXYGEN_ONLY__ || !MBEDTLS_PSA_CRYPTO_SE_C */ | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 150 |  | 
| Gilles Peskine | 7a86da1 | 2019-07-12 23:25:38 +0200 | [diff] [blame] | 151 | /**@}*/ | 
|  | 152 |  | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 153 | /** \defgroup se_mac Secure Element Message Authentication Codes | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 154 | * Generation and authentication of Message Authentication Codes (MACs) using | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 155 | * a secure element can be done either as a single function call (via the | 
|  | 156 | * `psa_drv_se_mac_generate_t` or `psa_drv_se_mac_verify_t` functions), or in | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 157 | * parts using the following sequence: | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 158 | * - `psa_drv_se_mac_setup_t` | 
|  | 159 | * - `psa_drv_se_mac_update_t` | 
|  | 160 | * - `psa_drv_se_mac_update_t` | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 161 | * - ... | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 162 | * - `psa_drv_se_mac_finish_t` or `psa_drv_se_mac_finish_verify_t` | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 163 | * | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 164 | * If a previously started secure element MAC operation needs to be terminated, | 
|  | 165 | * it should be done so by the `psa_drv_se_mac_abort_t`. Failure to do so may | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 166 | * result in allocated resources not being freed or in other undefined | 
|  | 167 | * behavior. | 
|  | 168 | */ | 
|  | 169 | /**@{*/ | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 170 | /** \brief A function that starts a secure element  MAC operation for a PSA | 
|  | 171 | * Crypto Driver implementation | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 172 | * | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 173 | * \param[in,out] drv_context   The driver context structure. | 
|  | 174 | * \param[in,out] op_context    A structure that will contain the | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 175 | *                              hardware-specific MAC context | 
|  | 176 | * \param[in] key_slot          The slot of the key to be used for the | 
|  | 177 | *                              operation | 
|  | 178 | * \param[in] algorithm         The algorithm to be used to underly the MAC | 
|  | 179 | *                              operation | 
|  | 180 | * | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 181 | * \retval  #PSA_SUCCESS | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 182 | *          Success. | 
|  | 183 | */ | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 184 | typedef psa_status_t (*psa_drv_se_mac_setup_t)(psa_drv_se_context_t *drv_context, | 
|  | 185 | void *op_context, | 
| Derek Miller | b2a1cce | 2019-02-15 17:03:42 -0600 | [diff] [blame] | 186 | psa_key_slot_number_t key_slot, | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 187 | psa_algorithm_t algorithm); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 188 |  | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 189 | /** \brief A function that continues a previously started secure element MAC | 
|  | 190 | * operation | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 191 | * | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 192 | * \param[in,out] op_context    A hardware-specific structure for the | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 193 | *                              previously-established MAC operation to be | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 194 | *                              updated | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 195 | * \param[in] p_input           A buffer containing the message to be appended | 
|  | 196 | *                              to the MAC operation | 
| Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 197 | * \param[in] input_length      The size in bytes of the input message buffer | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 198 | */ | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 199 | typedef psa_status_t (*psa_drv_se_mac_update_t)(void *op_context, | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 200 | const uint8_t *p_input, | 
|  | 201 | size_t input_length); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 202 |  | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 203 | /** \brief a function that completes a previously started secure element MAC | 
|  | 204 | * operation by returning the resulting MAC. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 205 | * | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 206 | * \param[in,out] op_context    A hardware-specific structure for the | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 207 | *                              previously started MAC operation to be | 
|  | 208 | *                              finished | 
|  | 209 | * \param[out] p_mac            A buffer where the generated MAC will be | 
|  | 210 | *                              placed | 
|  | 211 | * \param[in] mac_size          The size in bytes of the buffer that has been | 
|  | 212 | *                              allocated for the `output` buffer | 
|  | 213 | * \param[out] p_mac_length     After completion, will contain the number of | 
|  | 214 | *                              bytes placed in the `p_mac` buffer | 
|  | 215 | * | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 216 | * \retval  #PSA_SUCCESS | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 217 | *          Success. | 
|  | 218 | */ | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 219 | typedef psa_status_t (*psa_drv_se_mac_finish_t)(void *op_context, | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 220 | uint8_t *p_mac, | 
|  | 221 | size_t mac_size, | 
|  | 222 | size_t *p_mac_length); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 223 |  | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 224 | /** \brief A function that completes a previously started secure element MAC | 
|  | 225 | * operation by comparing the resulting MAC against a provided value | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 226 | * | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 227 | * \param[in,out] op_context    A hardware-specific structure for the previously | 
| Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 228 | *                              started MAC operation to be fiinished | 
|  | 229 | * \param[in] p_mac             The MAC value against which the resulting MAC | 
|  | 230 | *                              will be compared against | 
|  | 231 | * \param[in] mac_length        The size in bytes of the value stored in `p_mac` | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 232 | * | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 233 | * \retval #PSA_SUCCESS | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 234 | *         The operation completed successfully and the MACs matched each | 
|  | 235 | *         other | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 236 | * \retval #PSA_ERROR_INVALID_SIGNATURE | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 237 | *         The operation completed successfully, but the calculated MAC did | 
|  | 238 | *         not match the provided MAC | 
|  | 239 | */ | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 240 | typedef psa_status_t (*psa_drv_se_mac_finish_verify_t)(void *op_context, | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 241 | const uint8_t *p_mac, | 
|  | 242 | size_t mac_length); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 243 |  | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 244 | /** \brief A function that aborts a previous started secure element MAC | 
|  | 245 | * operation | 
| Gilles Peskine | 32668ce | 2019-03-06 18:29:57 +0100 | [diff] [blame] | 246 | * | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 247 | * \param[in,out] op_context    A hardware-specific structure for the previously | 
| Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 248 | *                              started MAC operation to be aborted | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 249 | */ | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 250 | typedef psa_status_t (*psa_drv_se_mac_abort_t)(void *op_context); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 251 |  | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 252 | /** \brief A function that performs a secure element MAC operation in one | 
|  | 253 | * command and returns the calculated MAC | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 254 | * | 
| Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 255 | * \param[in,out] drv_context   The driver context structure. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 256 | * \param[in] p_input           A buffer containing the message to be MACed | 
|  | 257 | * \param[in] input_length      The size in bytes of `p_input` | 
|  | 258 | * \param[in] key_slot          The slot of the key to be used | 
|  | 259 | * \param[in] alg               The algorithm to be used to underlie the MAC | 
|  | 260 | *                              operation | 
|  | 261 | * \param[out] p_mac            A buffer where the generated MAC will be | 
|  | 262 | *                              placed | 
|  | 263 | * \param[in] mac_size          The size in bytes of the `p_mac` buffer | 
|  | 264 | * \param[out] p_mac_length     After completion, will contain the number of | 
|  | 265 | *                              bytes placed in the `output` buffer | 
|  | 266 | * | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 267 | * \retval #PSA_SUCCESS | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 268 | *         Success. | 
|  | 269 | */ | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 270 | typedef psa_status_t (*psa_drv_se_mac_generate_t)(psa_drv_se_context_t *drv_context, | 
|  | 271 | const uint8_t *p_input, | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 272 | size_t input_length, | 
| Derek Miller | b2a1cce | 2019-02-15 17:03:42 -0600 | [diff] [blame] | 273 | psa_key_slot_number_t key_slot, | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 274 | psa_algorithm_t alg, | 
|  | 275 | uint8_t *p_mac, | 
|  | 276 | size_t mac_size, | 
|  | 277 | size_t *p_mac_length); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 278 |  | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 279 | /** \brief A function that performs a secure element MAC operation in one | 
|  | 280 | * command and compares the resulting MAC against a provided value | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 281 | * | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 282 | * \param[in,out] drv_context       The driver context structure. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 283 | * \param[in] p_input       A buffer containing the message to be MACed | 
|  | 284 | * \param[in] input_length  The size in bytes of `input` | 
|  | 285 | * \param[in] key_slot      The slot of the key to be used | 
|  | 286 | * \param[in] alg           The algorithm to be used to underlie the MAC | 
|  | 287 | *                          operation | 
|  | 288 | * \param[in] p_mac         The MAC value against which the resulting MAC will | 
|  | 289 | *                          be compared against | 
|  | 290 | * \param[in] mac_length   The size in bytes of `mac` | 
|  | 291 | * | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 292 | * \retval #PSA_SUCCESS | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 293 | *         The operation completed successfully and the MACs matched each | 
|  | 294 | *         other | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 295 | * \retval #PSA_ERROR_INVALID_SIGNATURE | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 296 | *         The operation completed successfully, but the calculated MAC did | 
|  | 297 | *         not match the provided MAC | 
|  | 298 | */ | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 299 | typedef psa_status_t (*psa_drv_se_mac_verify_t)(psa_drv_se_context_t *drv_context, | 
|  | 300 | const uint8_t *p_input, | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 301 | size_t input_length, | 
| Derek Miller | b2a1cce | 2019-02-15 17:03:42 -0600 | [diff] [blame] | 302 | psa_key_slot_number_t key_slot, | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 303 | psa_algorithm_t alg, | 
|  | 304 | const uint8_t *p_mac, | 
|  | 305 | size_t mac_length); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 306 |  | 
|  | 307 | /** \brief A struct containing all of the function pointers needed to | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 308 | * perform secure element MAC operations | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 309 | * | 
|  | 310 | * PSA Crypto API implementations should populate the table as appropriate | 
|  | 311 | * upon startup. | 
|  | 312 | * | 
|  | 313 | * If one of the functions is not implemented (such as | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 314 | * `psa_drv_se_mac_generate_t`), it should be set to NULL. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 315 | * | 
|  | 316 | * Driver implementers should ensure that they implement all of the functions | 
|  | 317 | * that make sense for their hardware, and that they provide a full solution | 
|  | 318 | * (for example, if they support `p_setup`, they should also support | 
|  | 319 | * `p_update` and at least one of `p_finish` or `p_finish_verify`). | 
|  | 320 | * | 
|  | 321 | */ | 
|  | 322 | typedef struct { | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 323 | /**The size in bytes of the hardware-specific secure element MAC context | 
|  | 324 | * structure | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 325 | */ | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 326 | size_t                    context_size; | 
|  | 327 | /** Function that performs a MAC setup operation | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 328 | */ | 
| Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 329 | psa_drv_se_mac_setup_t          p_setup; | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 330 | /** Function that performs a MAC update operation | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 331 | */ | 
| Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 332 | psa_drv_se_mac_update_t         p_update; | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 333 | /** Function that completes a MAC operation | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 334 | */ | 
| Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 335 | psa_drv_se_mac_finish_t         p_finish; | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 336 | /** Function that completes a MAC operation with a verify check | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 337 | */ | 
| Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 338 | psa_drv_se_mac_finish_verify_t  p_finish_verify; | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 339 | /** Function that aborts a previoustly started MAC operation | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 340 | */ | 
| Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 341 | psa_drv_se_mac_abort_t          p_abort; | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 342 | /** Function that performs a MAC operation in one call | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 343 | */ | 
| Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 344 | psa_drv_se_mac_generate_t       p_mac; | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 345 | /** Function that performs a MAC and verify operation in one call | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 346 | */ | 
| Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 347 | psa_drv_se_mac_verify_t         p_mac_verify; | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 348 | } psa_drv_se_mac_t; | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 349 | /**@}*/ | 
|  | 350 |  | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 351 | /** \defgroup se_cipher Secure Element Symmetric Ciphers | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 352 | * | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 353 | * Encryption and Decryption using secure element keys in block modes other | 
|  | 354 | * than ECB must be done in multiple parts, using the following flow: | 
|  | 355 | * - `psa_drv_se_cipher_setup_t` | 
|  | 356 | * - `psa_drv_se_cipher_set_iv_t` (optional depending upon block mode) | 
|  | 357 | * - `psa_drv_se_cipher_update_t` | 
|  | 358 | * - `psa_drv_se_cipher_update_t` | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 359 | * - ... | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 360 | * - `psa_drv_se_cipher_finish_t` | 
| Gilles Peskine | 32668ce | 2019-03-06 18:29:57 +0100 | [diff] [blame] | 361 | * | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 362 | * If a previously started secure element Cipher operation needs to be | 
|  | 363 | * terminated, it should be done so by the `psa_drv_se_cipher_abort_t`. Failure | 
|  | 364 | * to do so may result in allocated resources not being freed or in other | 
|  | 365 | * undefined behavior. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 366 | * | 
|  | 367 | * In situations where a PSA Cryptographic API implementation is using a block | 
|  | 368 | * mode not-supported by the underlying hardware or driver, it can construct | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 369 | * the block mode itself, while calling the `psa_drv_se_cipher_ecb_t` function | 
|  | 370 | * for the cipher operations. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 371 | */ | 
|  | 372 | /**@{*/ | 
|  | 373 |  | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 374 | /** \brief A function that provides the cipher setup function for a | 
|  | 375 | * secure element driver | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 376 | * | 
| Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 377 | * \param[in,out] drv_context   The driver context structure. | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 378 | * \param[in,out] op_context    A structure that will contain the | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 379 | *                              hardware-specific cipher context. | 
|  | 380 | * \param[in] key_slot          The slot of the key to be used for the | 
|  | 381 | *                              operation | 
|  | 382 | * \param[in] algorithm         The algorithm to be used in the cipher | 
|  | 383 | *                              operation | 
|  | 384 | * \param[in] direction         Indicates whether the operation is an encrypt | 
|  | 385 | *                              or decrypt | 
|  | 386 | * | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 387 | * \retval #PSA_SUCCESS | 
|  | 388 | * \retval #PSA_ERROR_NOT_SUPPORTED | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 389 | */ | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 390 | typedef psa_status_t (*psa_drv_se_cipher_setup_t)(psa_drv_se_context_t *drv_context, | 
|  | 391 | void *op_context, | 
| Derek Miller | b2a1cce | 2019-02-15 17:03:42 -0600 | [diff] [blame] | 392 | psa_key_slot_number_t key_slot, | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 393 | psa_algorithm_t algorithm, | 
|  | 394 | psa_encrypt_or_decrypt_t direction); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 395 |  | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 396 | /** \brief A function that sets the initialization vector (if | 
|  | 397 | * necessary) for an secure element cipher operation | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 398 | * | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 399 | * Rationale: The `psa_se_cipher_*` operation in the PSA Cryptographic API has | 
|  | 400 | * two IV functions: one to set the IV, and one to generate it internally. The | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 401 | * generate function is not necessary for the drivers to implement as the PSA | 
|  | 402 | * Crypto implementation can do the generation using its RNG features. | 
|  | 403 | * | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 404 | * \param[in,out] op_context    A structure that contains the previously set up | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 405 | *                              hardware-specific cipher context | 
|  | 406 | * \param[in] p_iv              A buffer containing the initialization vector | 
|  | 407 | * \param[in] iv_length         The size (in bytes) of the `p_iv` buffer | 
|  | 408 | * | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 409 | * \retval #PSA_SUCCESS | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 410 | */ | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 411 | typedef psa_status_t (*psa_drv_se_cipher_set_iv_t)(void *op_context, | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 412 | const uint8_t *p_iv, | 
|  | 413 | size_t iv_length); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 414 |  | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 415 | /** \brief A function that continues a previously started secure element cipher | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 416 | * operation | 
|  | 417 | * | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 418 | * \param[in,out] op_context        A hardware-specific structure for the | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 419 | *                                  previously started cipher operation | 
|  | 420 | * \param[in] p_input               A buffer containing the data to be | 
|  | 421 | *                                  encrypted/decrypted | 
|  | 422 | * \param[in] input_size            The size in bytes of the buffer pointed to | 
|  | 423 | *                                  by `p_input` | 
|  | 424 | * \param[out] p_output             The caller-allocated buffer where the | 
|  | 425 | *                                  output will be placed | 
|  | 426 | * \param[in] output_size           The allocated size in bytes of the | 
|  | 427 | *                                  `p_output` buffer | 
|  | 428 | * \param[out] p_output_length      After completion, will contain the number | 
|  | 429 | *                                  of bytes placed in the `p_output` buffer | 
|  | 430 | * | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 431 | * \retval #PSA_SUCCESS | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 432 | */ | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 433 | typedef psa_status_t (*psa_drv_se_cipher_update_t)(void *op_context, | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 434 | const uint8_t *p_input, | 
|  | 435 | size_t input_size, | 
|  | 436 | uint8_t *p_output, | 
|  | 437 | size_t output_size, | 
|  | 438 | size_t *p_output_length); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 439 |  | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 440 | /** \brief A function that completes a previously started secure element cipher | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 441 | * operation | 
|  | 442 | * | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 443 | * \param[in,out] op_context    A hardware-specific structure for the | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 444 | *                              previously started cipher operation | 
|  | 445 | * \param[out] p_output         The caller-allocated buffer where the output | 
|  | 446 | *                              will be placed | 
|  | 447 | * \param[in] output_size       The allocated size in bytes of the `p_output` | 
|  | 448 | *                              buffer | 
|  | 449 | * \param[out] p_output_length  After completion, will contain the number of | 
|  | 450 | *                              bytes placed in the `p_output` buffer | 
|  | 451 | * | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 452 | * \retval #PSA_SUCCESS | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 453 | */ | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 454 | typedef psa_status_t (*psa_drv_se_cipher_finish_t)(void *op_context, | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 455 | uint8_t *p_output, | 
|  | 456 | size_t output_size, | 
|  | 457 | size_t *p_output_length); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 458 |  | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 459 | /** \brief A function that aborts a previously started secure element cipher | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 460 | * operation | 
|  | 461 | * | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 462 | * \param[in,out] op_context    A hardware-specific structure for the | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 463 | *                              previously started cipher operation | 
|  | 464 | */ | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 465 | typedef psa_status_t (*psa_drv_se_cipher_abort_t)(void *op_context); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 466 |  | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 467 | /** \brief A function that performs the ECB block mode for secure element | 
|  | 468 | * cipher operations | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 469 | * | 
|  | 470 | * Note: this function should only be used with implementations that do not | 
|  | 471 | * provide a needed higher-level operation. | 
|  | 472 | * | 
| Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 473 | * \param[in,out] drv_context   The driver context structure. | 
|  | 474 | * \param[in] key_slot          The slot of the key to be used for the operation | 
|  | 475 | * \param[in] algorithm         The algorithm to be used in the cipher operation | 
|  | 476 | * \param[in] direction         Indicates whether the operation is an encrypt or | 
|  | 477 | *                              decrypt | 
|  | 478 | * \param[in] p_input           A buffer containing the data to be | 
|  | 479 | *                              encrypted/decrypted | 
|  | 480 | * \param[in] input_size        The size in bytes of the buffer pointed to by | 
|  | 481 | *                              `p_input` | 
|  | 482 | * \param[out] p_output         The caller-allocated buffer where the output | 
|  | 483 | *                              will be placed | 
|  | 484 | * \param[in] output_size       The allocated size in bytes of the `p_output` | 
|  | 485 | *                              buffer | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 486 | * | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 487 | * \retval #PSA_SUCCESS | 
|  | 488 | * \retval #PSA_ERROR_NOT_SUPPORTED | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 489 | */ | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 490 | typedef psa_status_t (*psa_drv_se_cipher_ecb_t)(psa_drv_se_context_t *drv_context, | 
|  | 491 | psa_key_slot_number_t key_slot, | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 492 | psa_algorithm_t algorithm, | 
|  | 493 | psa_encrypt_or_decrypt_t direction, | 
|  | 494 | const uint8_t *p_input, | 
|  | 495 | size_t input_size, | 
|  | 496 | uint8_t *p_output, | 
|  | 497 | size_t output_size); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 498 |  | 
|  | 499 | /** | 
|  | 500 | * \brief A struct containing all of the function pointers needed to implement | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 501 | * cipher operations using secure elements. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 502 | * | 
|  | 503 | * PSA Crypto API implementations should populate instances of the table as | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 504 | * appropriate upon startup or at build time. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 505 | * | 
|  | 506 | * If one of the functions is not implemented (such as | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 507 | * `psa_drv_se_cipher_ecb_t`), it should be set to NULL. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 508 | */ | 
|  | 509 | typedef struct { | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 510 | /** The size in bytes of the hardware-specific secure element cipher | 
|  | 511 | * context structure | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 512 | */ | 
| Derek Miller | 34b33f1 | 2019-02-15 17:13:54 -0600 | [diff] [blame] | 513 | size_t               context_size; | 
|  | 514 | /** Function that performs a cipher setup operation */ | 
| Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 515 | psa_drv_se_cipher_setup_t  p_setup; | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 516 | /** Function that sets a cipher IV (if necessary) */ | 
| Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 517 | psa_drv_se_cipher_set_iv_t p_set_iv; | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 518 | /** Function that performs a cipher update operation */ | 
| Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 519 | psa_drv_se_cipher_update_t p_update; | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 520 | /** Function that completes a cipher operation */ | 
| Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 521 | psa_drv_se_cipher_finish_t p_finish; | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 522 | /** Function that aborts a cipher operation */ | 
| Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 523 | psa_drv_se_cipher_abort_t  p_abort; | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 524 | /** Function that performs ECB mode for a cipher operation | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 525 | * (Danger: ECB mode should not be used directly by clients of the PSA | 
|  | 526 | * Crypto Client API) | 
|  | 527 | */ | 
| Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 528 | psa_drv_se_cipher_ecb_t    p_ecb; | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 529 | } psa_drv_se_cipher_t; | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 530 |  | 
|  | 531 | /**@}*/ | 
|  | 532 |  | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 533 | /** \defgroup se_asymmetric Secure Element Asymmetric Cryptography | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 534 | * | 
|  | 535 | * Since the amount of data that can (or should) be encrypted or signed using | 
|  | 536 | * asymmetric keys is limited by the key size, asymmetric key operations using | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 537 | * keys in a secure element must be done in single function calls. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 538 | */ | 
|  | 539 | /**@{*/ | 
|  | 540 |  | 
|  | 541 | /** | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 542 | * \brief A function that signs a hash or short message with a private key in | 
|  | 543 | * a secure element | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 544 | * | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 545 | * \param[in,out] drv_context       The driver context structure. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 546 | * \param[in] key_slot              Key slot of an asymmetric key pair | 
|  | 547 | * \param[in] alg                   A signature algorithm that is compatible | 
|  | 548 | *                                  with the type of `key` | 
|  | 549 | * \param[in] p_hash                The hash to sign | 
|  | 550 | * \param[in] hash_length           Size of the `p_hash` buffer in bytes | 
|  | 551 | * \param[out] p_signature          Buffer where the signature is to be written | 
|  | 552 | * \param[in] signature_size        Size of the `p_signature` buffer in bytes | 
|  | 553 | * \param[out] p_signature_length   On success, the number of bytes | 
|  | 554 | *                                  that make up the returned signature value | 
|  | 555 | * | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 556 | * \retval #PSA_SUCCESS | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 557 | */ | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 558 | typedef psa_status_t (*psa_drv_se_asymmetric_sign_t)(psa_drv_se_context_t *drv_context, | 
|  | 559 | psa_key_slot_number_t key_slot, | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 560 | psa_algorithm_t alg, | 
|  | 561 | const uint8_t *p_hash, | 
|  | 562 | size_t hash_length, | 
|  | 563 | uint8_t *p_signature, | 
|  | 564 | size_t signature_size, | 
|  | 565 | size_t *p_signature_length); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 566 |  | 
|  | 567 | /** | 
|  | 568 | * \brief A function that verifies the signature a hash or short message using | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 569 | * an asymmetric public key in a secure element | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 570 | * | 
| Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 571 | * \param[in,out] drv_context   The driver context structure. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 572 | * \param[in] key_slot          Key slot of a public key or an asymmetric key | 
|  | 573 | *                              pair | 
|  | 574 | * \param[in] alg               A signature algorithm that is compatible with | 
|  | 575 | *                              the type of `key` | 
|  | 576 | * \param[in] p_hash            The hash whose signature is to be verified | 
|  | 577 | * \param[in] hash_length       Size of the `p_hash` buffer in bytes | 
|  | 578 | * \param[in] p_signature       Buffer containing the signature to verify | 
|  | 579 | * \param[in] signature_length  Size of the `p_signature` buffer in bytes | 
|  | 580 | * | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 581 | * \retval #PSA_SUCCESS | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 582 | *         The signature is valid. | 
|  | 583 | */ | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 584 | typedef psa_status_t (*psa_drv_se_asymmetric_verify_t)(psa_drv_se_context_t *drv_context, | 
|  | 585 | psa_key_slot_number_t key_slot, | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 586 | psa_algorithm_t alg, | 
|  | 587 | const uint8_t *p_hash, | 
|  | 588 | size_t hash_length, | 
|  | 589 | const uint8_t *p_signature, | 
|  | 590 | size_t signature_length); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 591 |  | 
|  | 592 | /** | 
|  | 593 | * \brief A function that encrypts a short message with an asymmetric public | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 594 | * key in a secure element | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 595 | * | 
| Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 596 | * \param[in,out] drv_context   The driver context structure. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 597 | * \param[in] key_slot          Key slot of a public key or an asymmetric key | 
|  | 598 | *                              pair | 
|  | 599 | * \param[in] alg               An asymmetric encryption algorithm that is | 
|  | 600 | *                              compatible with the type of `key` | 
|  | 601 | * \param[in] p_input           The message to encrypt | 
|  | 602 | * \param[in] input_length      Size of the `p_input` buffer in bytes | 
|  | 603 | * \param[in] p_salt            A salt or label, if supported by the | 
|  | 604 | *                              encryption algorithm | 
|  | 605 | *                              If the algorithm does not support a | 
|  | 606 | *                              salt, pass `NULL`. | 
|  | 607 | *                              If the algorithm supports an optional | 
|  | 608 | *                              salt and you do not want to pass a salt, | 
|  | 609 | *                              pass `NULL`. | 
|  | 610 | *                              For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is | 
|  | 611 | *                              supported. | 
|  | 612 | * \param[in] salt_length       Size of the `p_salt` buffer in bytes | 
|  | 613 | *                              If `p_salt` is `NULL`, pass 0. | 
|  | 614 | * \param[out] p_output         Buffer where the encrypted message is to | 
|  | 615 | *                              be written | 
|  | 616 | * \param[in] output_size       Size of the `p_output` buffer in bytes | 
|  | 617 | * \param[out] p_output_length  On success, the number of bytes that make up | 
|  | 618 | *                              the returned output | 
|  | 619 | * | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 620 | * \retval #PSA_SUCCESS | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 621 | */ | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 622 | typedef psa_status_t (*psa_drv_se_asymmetric_encrypt_t)(psa_drv_se_context_t *drv_context, | 
|  | 623 | psa_key_slot_number_t key_slot, | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 624 | psa_algorithm_t alg, | 
|  | 625 | const uint8_t *p_input, | 
|  | 626 | size_t input_length, | 
|  | 627 | const uint8_t *p_salt, | 
|  | 628 | size_t salt_length, | 
|  | 629 | uint8_t *p_output, | 
|  | 630 | size_t output_size, | 
|  | 631 | size_t *p_output_length); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 632 |  | 
|  | 633 | /** | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 634 | * \brief A function that decrypts a short message with an asymmetric private | 
|  | 635 | * key in a secure element. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 636 | * | 
| Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 637 | * \param[in,out] drv_context   The driver context structure. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 638 | * \param[in] key_slot          Key slot of an asymmetric key pair | 
|  | 639 | * \param[in] alg               An asymmetric encryption algorithm that is | 
|  | 640 | *                              compatible with the type of `key` | 
|  | 641 | * \param[in] p_input           The message to decrypt | 
|  | 642 | * \param[in] input_length      Size of the `p_input` buffer in bytes | 
|  | 643 | * \param[in] p_salt            A salt or label, if supported by the | 
|  | 644 | *                              encryption algorithm | 
|  | 645 | *                              If the algorithm does not support a | 
|  | 646 | *                              salt, pass `NULL`. | 
|  | 647 | *                              If the algorithm supports an optional | 
|  | 648 | *                              salt and you do not want to pass a salt, | 
|  | 649 | *                              pass `NULL`. | 
|  | 650 | *                              For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is | 
|  | 651 | *                              supported. | 
|  | 652 | * \param[in] salt_length       Size of the `p_salt` buffer in bytes | 
|  | 653 | *                              If `p_salt` is `NULL`, pass 0. | 
|  | 654 | * \param[out] p_output         Buffer where the decrypted message is to | 
|  | 655 | *                              be written | 
|  | 656 | * \param[in] output_size       Size of the `p_output` buffer in bytes | 
|  | 657 | * \param[out] p_output_length  On success, the number of bytes | 
|  | 658 | *                              that make up the returned output | 
|  | 659 | * | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 660 | * \retval #PSA_SUCCESS | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 661 | */ | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 662 | typedef psa_status_t (*psa_drv_se_asymmetric_decrypt_t)(psa_drv_se_context_t *drv_context, | 
|  | 663 | psa_key_slot_number_t key_slot, | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 664 | psa_algorithm_t alg, | 
|  | 665 | const uint8_t *p_input, | 
|  | 666 | size_t input_length, | 
|  | 667 | const uint8_t *p_salt, | 
|  | 668 | size_t salt_length, | 
|  | 669 | uint8_t *p_output, | 
|  | 670 | size_t output_size, | 
|  | 671 | size_t *p_output_length); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 672 |  | 
|  | 673 | /** | 
|  | 674 | * \brief A struct containing all of the function pointers needed to implement | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 675 | * asymmetric cryptographic operations using secure elements. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 676 | * | 
|  | 677 | * PSA Crypto API implementations should populate instances of the table as | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 678 | * appropriate upon startup or at build time. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 679 | * | 
|  | 680 | * If one of the functions is not implemented, it should be set to NULL. | 
|  | 681 | */ | 
|  | 682 | typedef struct { | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 683 | /** Function that performs an asymmetric sign operation */ | 
| Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 684 | psa_drv_se_asymmetric_sign_t    p_sign; | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 685 | /** Function that performs an asymmetric verify operation */ | 
| Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 686 | psa_drv_se_asymmetric_verify_t  p_verify; | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 687 | /** Function that performs an asymmetric encrypt operation */ | 
| Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 688 | psa_drv_se_asymmetric_encrypt_t p_encrypt; | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 689 | /** Function that performs an asymmetric decrypt operation */ | 
| Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 690 | psa_drv_se_asymmetric_decrypt_t p_decrypt; | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 691 | } psa_drv_se_asymmetric_t; | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 692 |  | 
|  | 693 | /**@}*/ | 
|  | 694 |  | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 695 | /** \defgroup se_aead Secure Element Authenticated Encryption with Additional Data | 
|  | 696 | * Authenticated Encryption with Additional Data (AEAD) operations with secure | 
|  | 697 | * elements must be done in one function call. While this creates a burden for | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 698 | * implementers as there must be sufficient space in memory for the entire | 
|  | 699 | * message, it prevents decrypted data from being made available before the | 
|  | 700 | * authentication operation is complete and the data is known to be authentic. | 
|  | 701 | */ | 
|  | 702 | /**@{*/ | 
|  | 703 |  | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 704 | /** \brief A function that performs a secure element authenticated encryption | 
|  | 705 | * operation | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 706 | * | 
| Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 707 | * \param[in,out] drv_context           The driver context structure. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 708 | * \param[in] key_slot                  Slot containing the key to use. | 
|  | 709 | * \param[in] algorithm                 The AEAD algorithm to compute | 
|  | 710 | *                                      (\c PSA_ALG_XXX value such that | 
|  | 711 | *                                      #PSA_ALG_IS_AEAD(`alg`) is true) | 
|  | 712 | * \param[in] p_nonce                   Nonce or IV to use | 
|  | 713 | * \param[in] nonce_length              Size of the `p_nonce` buffer in bytes | 
|  | 714 | * \param[in] p_additional_data         Additional data that will be | 
|  | 715 | *                                      authenticated but not encrypted | 
|  | 716 | * \param[in] additional_data_length    Size of `p_additional_data` in bytes | 
|  | 717 | * \param[in] p_plaintext               Data that will be authenticated and | 
|  | 718 | *                                      encrypted | 
|  | 719 | * \param[in] plaintext_length          Size of `p_plaintext` in bytes | 
|  | 720 | * \param[out] p_ciphertext             Output buffer for the authenticated and | 
|  | 721 | *                                      encrypted data. The additional data is | 
|  | 722 | *                                      not part of this output. For algorithms | 
|  | 723 | *                                      where the encrypted data and the | 
|  | 724 | *                                      authentication tag are defined as | 
|  | 725 | *                                      separate outputs, the authentication | 
|  | 726 | *                                      tag is appended to the encrypted data. | 
|  | 727 | * \param[in] ciphertext_size           Size of the `p_ciphertext` buffer in | 
|  | 728 | *                                      bytes | 
|  | 729 | * \param[out] p_ciphertext_length      On success, the size of the output in | 
|  | 730 | *                                      the `p_ciphertext` buffer | 
|  | 731 | * | 
|  | 732 | * \retval #PSA_SUCCESS | 
|  | 733 | *         Success. | 
|  | 734 | */ | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 735 | typedef psa_status_t (*psa_drv_se_aead_encrypt_t)(psa_drv_se_context_t *drv_context, | 
|  | 736 | psa_key_slot_number_t key_slot, | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 737 | psa_algorithm_t algorithm, | 
|  | 738 | const uint8_t *p_nonce, | 
|  | 739 | size_t nonce_length, | 
|  | 740 | const uint8_t *p_additional_data, | 
|  | 741 | size_t additional_data_length, | 
|  | 742 | const uint8_t *p_plaintext, | 
|  | 743 | size_t plaintext_length, | 
|  | 744 | uint8_t *p_ciphertext, | 
|  | 745 | size_t ciphertext_size, | 
|  | 746 | size_t *p_ciphertext_length); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 747 |  | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 748 | /** A function that peforms a secure element authenticated decryption operation | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 749 | * | 
| Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 750 | * \param[in,out] drv_context           The driver context structure. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 751 | * \param[in] key_slot                  Slot containing the key to use | 
|  | 752 | * \param[in] algorithm                 The AEAD algorithm to compute | 
|  | 753 | *                                      (\c PSA_ALG_XXX value such that | 
|  | 754 | *                                      #PSA_ALG_IS_AEAD(`alg`) is true) | 
|  | 755 | * \param[in] p_nonce                   Nonce or IV to use | 
|  | 756 | * \param[in] nonce_length              Size of the `p_nonce` buffer in bytes | 
|  | 757 | * \param[in] p_additional_data         Additional data that has been | 
|  | 758 | *                                      authenticated but not encrypted | 
|  | 759 | * \param[in] additional_data_length    Size of `p_additional_data` in bytes | 
|  | 760 | * \param[in] p_ciphertext              Data that has been authenticated and | 
|  | 761 | *                                      encrypted. | 
|  | 762 | *                                      For algorithms where the encrypted data | 
|  | 763 | *                                      and the authentication tag are defined | 
|  | 764 | *                                      as separate inputs, the buffer must | 
|  | 765 | *                                      contain the encrypted data followed by | 
|  | 766 | *                                      the authentication tag. | 
|  | 767 | * \param[in] ciphertext_length         Size of `p_ciphertext` in bytes | 
|  | 768 | * \param[out] p_plaintext              Output buffer for the decrypted data | 
|  | 769 | * \param[in] plaintext_size            Size of the `p_plaintext` buffer in | 
|  | 770 | *                                      bytes | 
|  | 771 | * \param[out] p_plaintext_length       On success, the size of the output in | 
|  | 772 | *                                      the `p_plaintext` buffer | 
|  | 773 | * | 
|  | 774 | * \retval #PSA_SUCCESS | 
|  | 775 | *         Success. | 
|  | 776 | */ | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 777 | typedef psa_status_t (*psa_drv_se_aead_decrypt_t)(psa_drv_se_context_t *drv_context, | 
|  | 778 | psa_key_slot_number_t key_slot, | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 779 | psa_algorithm_t algorithm, | 
|  | 780 | const uint8_t *p_nonce, | 
|  | 781 | size_t nonce_length, | 
|  | 782 | const uint8_t *p_additional_data, | 
|  | 783 | size_t additional_data_length, | 
|  | 784 | const uint8_t *p_ciphertext, | 
|  | 785 | size_t ciphertext_length, | 
|  | 786 | uint8_t *p_plaintext, | 
|  | 787 | size_t plaintext_size, | 
|  | 788 | size_t *p_plaintext_length); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 789 |  | 
|  | 790 | /** | 
|  | 791 | * \brief A struct containing all of the function pointers needed to implement | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 792 | * secure element Authenticated Encryption with Additional Data operations | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 793 | * | 
|  | 794 | * PSA Crypto API implementations should populate instances of the table as | 
|  | 795 | * appropriate upon startup. | 
|  | 796 | * | 
|  | 797 | * If one of the functions is not implemented, it should be set to NULL. | 
|  | 798 | */ | 
|  | 799 | typedef struct { | 
|  | 800 | /** Function that performs the AEAD encrypt operation */ | 
| Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 801 | psa_drv_se_aead_encrypt_t p_encrypt; | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 802 | /** Function that performs the AEAD decrypt operation */ | 
| Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 803 | psa_drv_se_aead_decrypt_t p_decrypt; | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 804 | } psa_drv_se_aead_t; | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 805 | /**@}*/ | 
|  | 806 |  | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 807 | /** \defgroup se_key_management Secure Element Key Management | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 808 | * Currently, key management is limited to importing keys in the clear, | 
|  | 809 | * destroying keys, and exporting keys in the clear. | 
|  | 810 | * Whether a key may be exported is determined by the key policies in place | 
|  | 811 | * on the key slot. | 
|  | 812 | */ | 
|  | 813 | /**@{*/ | 
|  | 814 |  | 
| Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 815 | /** An enumeration indicating how a key is created. | 
|  | 816 | */ | 
|  | 817 | typedef enum | 
|  | 818 | { | 
|  | 819 | PSA_KEY_CREATION_IMPORT, /**< During psa_import_key() */ | 
|  | 820 | PSA_KEY_CREATION_GENERATE, /**< During psa_generate_key() */ | 
|  | 821 | PSA_KEY_CREATION_DERIVE, /**< During psa_key_derivation_output_key() */ | 
|  | 822 | PSA_KEY_CREATION_COPY, /**< During psa_copy_key() */ | 
| Gilles Peskine | a5f8749 | 2019-08-05 16:46:18 +0200 | [diff] [blame] | 823 |  | 
|  | 824 | #ifndef __DOXYGEN_ONLY__ | 
|  | 825 | /** A key is being registered with mbedtls_psa_register_se_key(). | 
|  | 826 | * | 
|  | 827 | * The core only passes this value to | 
|  | 828 | * psa_drv_se_key_management_t::p_validate_slot_number, not to | 
|  | 829 | * psa_drv_se_key_management_t::p_allocate. The call to | 
|  | 830 | * `p_validate_slot_number` is not followed by any other call to the | 
|  | 831 | * driver: the key is considered successfully registered if the call to | 
|  | 832 | * `p_validate_slot_number` succeeds, or if `p_validate_slot_number` is | 
|  | 833 | * null. | 
|  | 834 | * | 
|  | 835 | * With this creation method, the driver must return #PSA_SUCCESS if | 
|  | 836 | * the given attributes are compatible with the existing key in the slot, | 
|  | 837 | * and #PSA_ERROR_DOES_NOT_EXIST if the driver can determine that there | 
|  | 838 | * is no key with the specified slot number. | 
|  | 839 | * | 
|  | 840 | * This is an Mbed Crypto extension. | 
|  | 841 | */ | 
|  | 842 | PSA_KEY_CREATION_REGISTER, | 
|  | 843 | #endif | 
| Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 844 | } psa_key_creation_method_t; | 
|  | 845 |  | 
| Gilles Peskine | f2223c8 | 2019-07-12 23:33:02 +0200 | [diff] [blame] | 846 | /** \brief A function that allocates a slot for a key. | 
|  | 847 | * | 
| Gilles Peskine | 9d75202 | 2019-08-09 11:33:48 +0200 | [diff] [blame] | 848 | * To create a key in a specific slot in a secure element, the core | 
|  | 849 | * first calls this function to determine a valid slot number, | 
|  | 850 | * then calls a function to create the key material in that slot. | 
| Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 851 | * In nominal conditions (that is, if no error occurs), | 
|  | 852 | * the effect of a call to a key creation function in the PSA Cryptography | 
|  | 853 | * API with a lifetime that places the key in a secure element is the | 
|  | 854 | * following: | 
| Gilles Peskine | 9d75202 | 2019-08-09 11:33:48 +0200 | [diff] [blame] | 855 | * -# The core calls psa_drv_se_key_management_t::p_allocate | 
|  | 856 | *    (or in some implementations | 
|  | 857 | *    psa_drv_se_key_management_t::p_validate_slot_number). The driver | 
|  | 858 | *    selects (or validates) a suitable slot number given the key attributes | 
|  | 859 | *    and the state of the secure element. | 
| Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 860 | * -# The core calls a key creation function in the driver. | 
| Gilles Peskine | 9d75202 | 2019-08-09 11:33:48 +0200 | [diff] [blame] | 861 | * | 
| Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 862 | * The key creation functions in the PSA Cryptography API are: | 
|  | 863 | * - psa_import_key(), which causes | 
|  | 864 | *   a call to `p_allocate` with \p method = #PSA_KEY_CREATION_IMPORT | 
|  | 865 | *   then a call to psa_drv_se_key_management_t::p_import. | 
|  | 866 | * - psa_generate_key(), which causes | 
|  | 867 | *   a call to `p_allocate` with \p method = #PSA_KEY_CREATION_GENERATE | 
|  | 868 | *   then a call to psa_drv_se_key_management_t::p_import. | 
|  | 869 | * - psa_key_derivation_output_key(), which causes | 
|  | 870 | *   a call to `p_allocate` with \p method = #PSA_KEY_CREATION_DERIVE | 
|  | 871 | *   then a call to psa_drv_se_key_derivation_t::p_derive. | 
|  | 872 | * - psa_copy_key(), which causes | 
|  | 873 | *   a call to `p_allocate` with \p method = #PSA_KEY_CREATION_COPY | 
|  | 874 | *   then a call to psa_drv_se_key_management_t::p_export. | 
| Gilles Peskine | 9d75202 | 2019-08-09 11:33:48 +0200 | [diff] [blame] | 875 | * | 
|  | 876 | * In case of errors, other behaviors are possible. | 
|  | 877 | * - If the PSA Cryptography subsystem dies after the first step, | 
|  | 878 | *   for example because the device has lost power abruptly, | 
|  | 879 | *   the second step may never happen, or may happen after a reset | 
|  | 880 | *   and re-initialization. Alternatively, after a reset and | 
|  | 881 | *   re-initialization, the core may call | 
|  | 882 | *   psa_drv_se_key_management_t::p_destroy on the slot number that | 
|  | 883 | *   was allocated (or validated) instead of calling a key creation function. | 
|  | 884 | * - If an error occurs, the core may call | 
|  | 885 | *   psa_drv_se_key_management_t::p_destroy on the slot number that | 
|  | 886 | *   was allocated (or validated) instead of calling a key creation function. | 
|  | 887 | * | 
|  | 888 | * Errors and system resets also have an impact on the driver's persistent | 
|  | 889 | * data. If a reset happens before the overall key creation process is | 
|  | 890 | * completed (before or after the second step above), it is unspecified | 
|  | 891 | * whether the persistent data after the reset is identical to what it | 
|  | 892 | * was before or after the call to `p_allocate` (or `p_validate_slot_number`). | 
|  | 893 | * | 
| Gilles Peskine | f2223c8 | 2019-07-12 23:33:02 +0200 | [diff] [blame] | 894 | * \param[in,out] drv_context       The driver context structure. | 
| Gilles Peskine | 94cc42c | 2019-07-12 23:34:20 +0200 | [diff] [blame] | 895 | * \param[in,out] persistent_data   A pointer to the persistent data | 
|  | 896 | *                                  that allows writing. | 
| Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 897 | * \param[in] attributes            Attributes of the key. | 
| Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 898 | * \param method                    The way in which the key is being created. | 
| Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 899 | * \param[out] key_slot             Slot where the key will be stored. | 
|  | 900 | *                                  This must be a valid slot for a key of the | 
|  | 901 | *                                  chosen type. It must be unoccupied. | 
| Gilles Peskine | f2223c8 | 2019-07-12 23:33:02 +0200 | [diff] [blame] | 902 | * | 
|  | 903 | * \retval #PSA_SUCCESS | 
|  | 904 | *         Success. | 
|  | 905 | *         The core will record \c *key_slot as the key slot where the key | 
|  | 906 | *         is stored and will update the persistent data in storage. | 
|  | 907 | * \retval #PSA_ERROR_NOT_SUPPORTED | 
|  | 908 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE | 
|  | 909 | */ | 
|  | 910 | typedef psa_status_t (*psa_drv_se_allocate_key_t)( | 
|  | 911 | psa_drv_se_context_t *drv_context, | 
| Gilles Peskine | 94cc42c | 2019-07-12 23:34:20 +0200 | [diff] [blame] | 912 | void *persistent_data, | 
| Gilles Peskine | f2223c8 | 2019-07-12 23:33:02 +0200 | [diff] [blame] | 913 | const psa_key_attributes_t *attributes, | 
| Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 914 | psa_key_creation_method_t method, | 
| Gilles Peskine | f2223c8 | 2019-07-12 23:33:02 +0200 | [diff] [blame] | 915 | psa_key_slot_number_t *key_slot); | 
|  | 916 |  | 
| Gilles Peskine | ae9964d | 2019-08-05 14:55:14 +0200 | [diff] [blame] | 917 | /** \brief A function that determines whether a slot number is valid | 
|  | 918 | * for a key. | 
|  | 919 | * | 
| Gilles Peskine | 9d75202 | 2019-08-09 11:33:48 +0200 | [diff] [blame] | 920 | * To create a key in a specific slot in a secure element, the core | 
|  | 921 | * first calls this function to validate the choice of slot number, | 
|  | 922 | * then calls a function to create the key material in that slot. | 
|  | 923 | * See the documentation of #psa_drv_se_allocate_key_t for more details. | 
|  | 924 | * | 
|  | 925 | * As of the PSA Cryptography API specification version 1.0, there is no way | 
|  | 926 | * for applications to trigger a call to this function. However some | 
|  | 927 | * implementations offer the capability to create or declare a key in | 
|  | 928 | * a specific slot via implementation-specific means, generally for the | 
|  | 929 | * sake of initial device provisioning or onboarding. Such a mechanism may | 
|  | 930 | * be added to a future version of the PSA Cryptography API specification. | 
|  | 931 | * | 
| Gilles Peskine | 5ec3a30 | 2019-10-01 14:27:23 +0200 | [diff] [blame] | 932 | * This function may update the driver's persistent data through | 
|  | 933 | * \p persistent_data. The core will save the updated persistent data at the | 
|  | 934 | * end of the key creation process. See the description of | 
|  | 935 | * ::psa_drv_se_allocate_key_t for more information. | 
|  | 936 | * | 
| Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 937 | * \param[in,out] drv_context   The driver context structure. | 
| Gilles Peskine | 5ec3a30 | 2019-10-01 14:27:23 +0200 | [diff] [blame] | 938 | * \param[in,out] persistent_data   A pointer to the persistent data | 
|  | 939 | *                                  that allows writing. | 
| Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 940 | * \param[in] attributes        Attributes of the key. | 
|  | 941 | * \param method                The way in which the key is being created. | 
|  | 942 | * \param[in] key_slot          Slot where the key is to be stored. | 
| Gilles Peskine | ae9964d | 2019-08-05 14:55:14 +0200 | [diff] [blame] | 943 | * | 
|  | 944 | * \retval #PSA_SUCCESS | 
|  | 945 | *         The given slot number is valid for a key with the given | 
|  | 946 | *         attributes. | 
|  | 947 | * \retval #PSA_ERROR_INVALID_ARGUMENT | 
|  | 948 | *         The given slot number is not valid for a key with the | 
|  | 949 | *         given attributes. This includes the case where the slot | 
|  | 950 | *         number is not valid at all. | 
|  | 951 | * \retval #PSA_ERROR_ALREADY_EXISTS | 
|  | 952 | *         There is already a key with the specified slot number. | 
|  | 953 | *         Drivers may choose to return this error from the key | 
|  | 954 | *         creation function instead. | 
|  | 955 | */ | 
|  | 956 | typedef psa_status_t (*psa_drv_se_validate_slot_number_t)( | 
|  | 957 | psa_drv_se_context_t *drv_context, | 
| Gilles Peskine | 5ec3a30 | 2019-10-01 14:27:23 +0200 | [diff] [blame] | 958 | void *persistent_data, | 
| Gilles Peskine | ae9964d | 2019-08-05 14:55:14 +0200 | [diff] [blame] | 959 | const psa_key_attributes_t *attributes, | 
| Gilles Peskine | e88c2c1 | 2019-08-05 16:44:14 +0200 | [diff] [blame] | 960 | psa_key_creation_method_t method, | 
| Gilles Peskine | ae9964d | 2019-08-05 14:55:14 +0200 | [diff] [blame] | 961 | psa_key_slot_number_t key_slot); | 
|  | 962 |  | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 963 | /** \brief A function that imports a key into a secure element in binary format | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 964 | * | 
|  | 965 | * This function can support any output from psa_export_key(). Refer to the | 
|  | 966 | * documentation of psa_export_key() for the format for each key type. | 
|  | 967 | * | 
| Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 968 | * \param[in,out] drv_context   The driver context structure. | 
| Gilles Peskine | f3801ff | 2019-08-06 17:32:04 +0200 | [diff] [blame] | 969 | * \param key_slot              Slot where the key will be stored. | 
| Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 970 | *                              This must be a valid slot for a key of the | 
|  | 971 | *                              chosen type. It must be unoccupied. | 
| Gilles Peskine | f3801ff | 2019-08-06 17:32:04 +0200 | [diff] [blame] | 972 | * \param[in] attributes        The key attributes, including the lifetime, | 
|  | 973 | *                              the key type and the usage policy. | 
|  | 974 | *                              Drivers should not access the key size stored | 
|  | 975 | *                              in the attributes: it may not match the | 
|  | 976 | *                              data passed in \p data. | 
|  | 977 | *                              Drivers can call psa_get_key_lifetime(), | 
|  | 978 | *                              psa_get_key_type(), | 
|  | 979 | *                              psa_get_key_usage_flags() and | 
|  | 980 | *                              psa_get_key_algorithm() to access this | 
|  | 981 | *                              information. | 
|  | 982 | * \param[in] data              Buffer containing the key data. | 
|  | 983 | * \param[in] data_length       Size of the \p data buffer in bytes. | 
| Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame] | 984 | * \param[out] bits             On success, the key size in bits. The driver | 
|  | 985 | *                              must determine this value after parsing the | 
|  | 986 | *                              key according to the key type. | 
|  | 987 | *                              This value is not used if the function fails. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 988 | * | 
|  | 989 | * \retval #PSA_SUCCESS | 
|  | 990 | *         Success. | 
|  | 991 | */ | 
| Gilles Peskine | f3801ff | 2019-08-06 17:32:04 +0200 | [diff] [blame] | 992 | typedef psa_status_t (*psa_drv_se_import_key_t)( | 
|  | 993 | psa_drv_se_context_t *drv_context, | 
|  | 994 | psa_key_slot_number_t key_slot, | 
|  | 995 | const psa_key_attributes_t *attributes, | 
|  | 996 | const uint8_t *data, | 
|  | 997 | size_t data_length, | 
|  | 998 | size_t *bits); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 999 |  | 
|  | 1000 | /** | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1001 | * \brief A function that destroys a secure element key and restore the slot to | 
|  | 1002 | * its default state | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1003 | * | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1004 | * This function destroys the content of the key from a secure element. | 
|  | 1005 | * Implementations shall make a best effort to ensure that any previous content | 
|  | 1006 | * of the slot is unrecoverable. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1007 | * | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1008 | * This function returns the specified slot to its default state. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1009 | * | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 1010 | * \param[in,out] drv_context       The driver context structure. | 
| Gilles Peskine | 94cc42c | 2019-07-12 23:34:20 +0200 | [diff] [blame] | 1011 | * \param[in,out] persistent_data   A pointer to the persistent data | 
|  | 1012 | *                                  that allows writing. | 
| Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 1013 | * \param key_slot                  The key slot to erase. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1014 | * | 
|  | 1015 | * \retval #PSA_SUCCESS | 
|  | 1016 | *         The slot's content, if any, has been erased. | 
|  | 1017 | */ | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 1018 | typedef psa_status_t (*psa_drv_se_destroy_key_t)( | 
|  | 1019 | psa_drv_se_context_t *drv_context, | 
| Gilles Peskine | 94cc42c | 2019-07-12 23:34:20 +0200 | [diff] [blame] | 1020 | void *persistent_data, | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 1021 | psa_key_slot_number_t key_slot); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1022 |  | 
|  | 1023 | /** | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1024 | * \brief A function that exports a secure element key in binary format | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1025 | * | 
|  | 1026 | * The output of this function can be passed to psa_import_key() to | 
|  | 1027 | * create an equivalent object. | 
|  | 1028 | * | 
|  | 1029 | * If a key is created with `psa_import_key()` and then exported with | 
|  | 1030 | * this function, it is not guaranteed that the resulting data is | 
|  | 1031 | * identical: the implementation may choose a different representation | 
|  | 1032 | * of the same key if the format permits it. | 
|  | 1033 | * | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1034 | * This function should generate output in the same format that | 
|  | 1035 | * `psa_export_key()` does. Refer to the | 
|  | 1036 | * documentation of `psa_export_key()` for the format for each key type. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1037 | * | 
| Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 1038 | * \param[in,out] drv_context   The driver context structure. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1039 | * \param[in] key               Slot whose content is to be exported. This must | 
|  | 1040 | *                              be an occupied key slot. | 
|  | 1041 | * \param[out] p_data           Buffer where the key data is to be written. | 
|  | 1042 | * \param[in] data_size         Size of the `p_data` buffer in bytes. | 
|  | 1043 | * \param[out] p_data_length    On success, the number of bytes | 
|  | 1044 | *                              that make up the key data. | 
|  | 1045 | * | 
|  | 1046 | * \retval #PSA_SUCCESS | 
| David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 1047 | * \retval #PSA_ERROR_DOES_NOT_EXIST | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1048 | * \retval #PSA_ERROR_NOT_PERMITTED | 
|  | 1049 | * \retval #PSA_ERROR_NOT_SUPPORTED | 
|  | 1050 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE | 
|  | 1051 | * \retval #PSA_ERROR_HARDWARE_FAILURE | 
| Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 1052 | * \retval #PSA_ERROR_CORRUPTION_DETECTED | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1053 | */ | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 1054 | typedef psa_status_t (*psa_drv_se_export_key_t)(psa_drv_se_context_t *drv_context, | 
|  | 1055 | psa_key_slot_number_t key, | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 1056 | uint8_t *p_data, | 
|  | 1057 | size_t data_size, | 
|  | 1058 | size_t *p_data_length); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1059 |  | 
|  | 1060 | /** | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1061 | * \brief A function that generates a symmetric or asymmetric key on a secure | 
|  | 1062 | * element | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1063 | * | 
| Gilles Peskine | 364d12c | 2021-03-08 17:23:47 +0100 | [diff] [blame] | 1064 | * If the key type \c type recorded in \p attributes | 
|  | 1065 | * is asymmetric (#PSA_KEY_TYPE_IS_ASYMMETRIC(\c type) = 1), | 
| Gilles Peskine | f3801ff | 2019-08-06 17:32:04 +0200 | [diff] [blame] | 1066 | * the driver may export the public key at the time of generation, | 
|  | 1067 | * in the format documented for psa_export_public_key() by writing it | 
|  | 1068 | * to the \p pubkey buffer. | 
|  | 1069 | * This is optional, intended for secure elements that output the | 
|  | 1070 | * public key at generation time and that cannot export the public key | 
|  | 1071 | * later. Drivers that do not need this feature should leave | 
|  | 1072 | * \p *pubkey_length set to 0 and should | 
|  | 1073 | * implement the psa_drv_key_management_t::p_export_public function. | 
|  | 1074 | * Some implementations do not support this feature, in which case | 
|  | 1075 | * \p pubkey is \c NULL and \p pubkey_size is 0. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1076 | * | 
| Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 1077 | * \param[in,out] drv_context   The driver context structure. | 
| Gilles Peskine | f3801ff | 2019-08-06 17:32:04 +0200 | [diff] [blame] | 1078 | * \param key_slot              Slot where the key will be stored. | 
|  | 1079 | *                              This must be a valid slot for a key of the | 
|  | 1080 | *                              chosen type. It must be unoccupied. | 
|  | 1081 | * \param[in] attributes        The key attributes, including the lifetime, | 
|  | 1082 | *                              the key type and size, and the usage policy. | 
|  | 1083 | *                              Drivers can call psa_get_key_lifetime(), | 
|  | 1084 | *                              psa_get_key_type(), psa_get_key_bits(), | 
|  | 1085 | *                              psa_get_key_usage_flags() and | 
|  | 1086 | *                              psa_get_key_algorithm() to access this | 
|  | 1087 | *                              information. | 
|  | 1088 | * \param[out] pubkey           A buffer where the driver can write the | 
|  | 1089 | *                              public key, when generating an asymmetric | 
|  | 1090 | *                              key pair. | 
|  | 1091 | *                              This is \c NULL when generating a symmetric | 
|  | 1092 | *                              key or if the core does not support | 
|  | 1093 | *                              exporting the public key at generation time. | 
|  | 1094 | * \param pubkey_size           The size of the `pubkey` buffer in bytes. | 
|  | 1095 | *                              This is 0 when generating a symmetric | 
|  | 1096 | *                              key or if the core does not support | 
|  | 1097 | *                              exporting the public key at generation time. | 
|  | 1098 | * \param[out] pubkey_length    On entry, this is always 0. | 
|  | 1099 | *                              On success, the number of bytes written to | 
|  | 1100 | *                              \p pubkey. If this is 0 or unchanged on return, | 
|  | 1101 | *                              the core will not read the \p pubkey buffer, | 
|  | 1102 | *                              and will instead call the driver's | 
|  | 1103 | *                              psa_drv_key_management_t::p_export_public | 
|  | 1104 | *                              function to export the public key when needed. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1105 | */ | 
| Gilles Peskine | f3801ff | 2019-08-06 17:32:04 +0200 | [diff] [blame] | 1106 | typedef psa_status_t (*psa_drv_se_generate_key_t)( | 
|  | 1107 | psa_drv_se_context_t *drv_context, | 
|  | 1108 | psa_key_slot_number_t key_slot, | 
|  | 1109 | const psa_key_attributes_t *attributes, | 
|  | 1110 | uint8_t *pubkey, size_t pubkey_size, size_t *pubkey_length); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1111 |  | 
|  | 1112 | /** | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1113 | * \brief A struct containing all of the function pointers needed to for secure | 
|  | 1114 | * element key management | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1115 | * | 
|  | 1116 | * PSA Crypto API implementations should populate instances of the table as | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1117 | * appropriate upon startup or at build time. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1118 | * | 
|  | 1119 | * If one of the functions is not implemented, it should be set to NULL. | 
|  | 1120 | */ | 
|  | 1121 | typedef struct { | 
| Gilles Peskine | 9d75202 | 2019-08-09 11:33:48 +0200 | [diff] [blame] | 1122 | /** Function that allocates a slot for a key. */ | 
| Gilles Peskine | f2223c8 | 2019-07-12 23:33:02 +0200 | [diff] [blame] | 1123 | psa_drv_se_allocate_key_t   p_allocate; | 
| Gilles Peskine | 9d75202 | 2019-08-09 11:33:48 +0200 | [diff] [blame] | 1124 | /** Function that checks the validity of a slot for a key. */ | 
| Gilles Peskine | ae9964d | 2019-08-05 14:55:14 +0200 | [diff] [blame] | 1125 | psa_drv_se_validate_slot_number_t p_validate_slot_number; | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1126 | /** Function that performs a key import operation */ | 
|  | 1127 | psa_drv_se_import_key_t     p_import; | 
|  | 1128 | /** Function that performs a generation */ | 
| Derek Miller | 0b3098a | 2019-02-15 17:10:49 -0600 | [diff] [blame] | 1129 | psa_drv_se_generate_key_t   p_generate; | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1130 | /** Function that performs a key destroy operation */ | 
| Derek Miller | 0b3098a | 2019-02-15 17:10:49 -0600 | [diff] [blame] | 1131 | psa_drv_se_destroy_key_t    p_destroy; | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1132 | /** Function that performs a key export operation */ | 
| Derek Miller | 0b3098a | 2019-02-15 17:10:49 -0600 | [diff] [blame] | 1133 | psa_drv_se_export_key_t     p_export; | 
| Gilles Peskine | e62b74e | 2019-06-25 15:25:09 +0200 | [diff] [blame] | 1134 | /** Function that performs a public key export operation */ | 
|  | 1135 | psa_drv_se_export_key_t     p_export_public; | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 1136 | } psa_drv_se_key_management_t; | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1137 |  | 
|  | 1138 | /**@}*/ | 
|  | 1139 |  | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1140 | /** \defgroup driver_derivation Secure Element Key Derivation and Agreement | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1141 | * Key derivation is the process of generating new key material using an | 
|  | 1142 | * existing key and additional parameters, iterating through a basic | 
|  | 1143 | * cryptographic function, such as a hash. | 
|  | 1144 | * Key agreement is a part of cryptographic protocols that allows two parties | 
|  | 1145 | * to agree on the same key value, but starting from different original key | 
|  | 1146 | * material. | 
|  | 1147 | * The flows are similar, and the PSA Crypto Driver Model uses the same functions | 
|  | 1148 | * for both of the flows. | 
|  | 1149 | * | 
|  | 1150 | * There are two different final functions for the flows, | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1151 | * `psa_drv_se_key_derivation_derive` and `psa_drv_se_key_derivation_export`. | 
|  | 1152 | * `psa_drv_se_key_derivation_derive` is used when the key material should be | 
|  | 1153 | * placed in a slot on the hardware and not exposed to the caller. | 
|  | 1154 | * `psa_drv_se_key_derivation_export` is used when the key material should be | 
|  | 1155 | * returned to the PSA Cryptographic API implementation. | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1156 | * | 
|  | 1157 | * Different key derivation algorithms require a different number of inputs. | 
|  | 1158 | * Instead of having an API that takes as input variable length arrays, which | 
|  | 1159 | * can be problemmatic to manage on embedded platforms, the inputs are passed | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1160 | * to the driver via a function, `psa_drv_se_key_derivation_collateral`, that | 
|  | 1161 | * is called multiple times with different `collateral_id`s. Thus, for a key | 
| Tobias Nießen | 1e8ca12 | 2021-05-10 19:53:15 +0200 | [diff] [blame] | 1162 | * derivation algorithm that required 3 parameter inputs, the flow would look | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1163 | * something like: | 
|  | 1164 | * ~~~~~~~~~~~~~{.c} | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1165 | * psa_drv_se_key_derivation_setup(kdf_algorithm, source_key, dest_key_size_bytes); | 
|  | 1166 | * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_0, | 
|  | 1167 | *                                      p_collateral_0, | 
|  | 1168 | *                                      collateral_0_size); | 
|  | 1169 | * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_1, | 
|  | 1170 | *                                      p_collateral_1, | 
|  | 1171 | *                                      collateral_1_size); | 
|  | 1172 | * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_2, | 
|  | 1173 | *                                      p_collateral_2, | 
|  | 1174 | *                                      collateral_2_size); | 
|  | 1175 | * psa_drv_se_key_derivation_derive(); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1176 | * ~~~~~~~~~~~~~ | 
|  | 1177 | * | 
|  | 1178 | * key agreement example: | 
|  | 1179 | * ~~~~~~~~~~~~~{.c} | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1180 | * psa_drv_se_key_derivation_setup(alg, source_key. dest_key_size_bytes); | 
|  | 1181 | * psa_drv_se_key_derivation_collateral(DHE_PUBKEY, p_pubkey, pubkey_size); | 
|  | 1182 | * psa_drv_se_key_derivation_export(p_session_key, | 
|  | 1183 | *                                  session_key_size, | 
|  | 1184 | *                                  &session_key_length); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1185 | * ~~~~~~~~~~~~~ | 
|  | 1186 | */ | 
|  | 1187 | /**@{*/ | 
|  | 1188 |  | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1189 | /** \brief A function that Sets up a secure element key derivation operation by | 
|  | 1190 | * specifying the algorithm and the source key sot | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1191 | * | 
| Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 1192 | * \param[in,out] drv_context   The driver context structure. | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 1193 | * \param[in,out] op_context    A hardware-specific structure containing any | 
| Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 1194 | *                              context information for the implementation | 
|  | 1195 | * \param[in] kdf_alg           The algorithm to be used for the key derivation | 
|  | 1196 | * \param[in] source_key        The key to be used as the source material for | 
|  | 1197 | *                              the key derivation | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1198 | * | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 1199 | * \retval #PSA_SUCCESS | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1200 | */ | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 1201 | typedef psa_status_t (*psa_drv_se_key_derivation_setup_t)(psa_drv_se_context_t *drv_context, | 
|  | 1202 | void *op_context, | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 1203 | psa_algorithm_t kdf_alg, | 
| Derek Miller | b2a1cce | 2019-02-15 17:03:42 -0600 | [diff] [blame] | 1204 | psa_key_slot_number_t source_key); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1205 |  | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1206 | /** \brief A function that provides collateral (parameters) needed for a secure | 
|  | 1207 | * element key derivation or key agreement operation | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1208 | * | 
|  | 1209 | * Since many key derivation algorithms require multiple parameters, it is | 
| Tobias Nießen | 1e8ca12 | 2021-05-10 19:53:15 +0200 | [diff] [blame] | 1210 | * expected that this function may be called multiple times for the same | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1211 | * operation, each with a different algorithm-specific `collateral_id` | 
|  | 1212 | * | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 1213 | * \param[in,out] op_context    A hardware-specific structure containing any | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1214 | *                              context information for the implementation | 
|  | 1215 | * \param[in] collateral_id     An ID for the collateral being provided | 
|  | 1216 | * \param[in] p_collateral      A buffer containing the collateral data | 
|  | 1217 | * \param[in] collateral_size   The size in bytes of the collateral | 
|  | 1218 | * | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 1219 | * \retval #PSA_SUCCESS | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1220 | */ | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 1221 | typedef psa_status_t (*psa_drv_se_key_derivation_collateral_t)(void *op_context, | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 1222 | uint32_t collateral_id, | 
|  | 1223 | const uint8_t *p_collateral, | 
|  | 1224 | size_t collateral_size); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1225 |  | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1226 | /** \brief A function that performs the final secure element key derivation | 
|  | 1227 | * step and place the generated key material in a slot | 
| Gilles Peskine | c3044a6 | 2019-03-06 17:56:28 +0100 | [diff] [blame] | 1228 | * | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 1229 | * \param[in,out] op_context    A hardware-specific structure containing any | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1230 | *                              context information for the implementation | 
|  | 1231 | * \param[in] dest_key          The slot where the generated key material | 
|  | 1232 | *                              should be placed | 
|  | 1233 | * | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 1234 | * \retval #PSA_SUCCESS | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1235 | */ | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 1236 | typedef psa_status_t (*psa_drv_se_key_derivation_derive_t)(void *op_context, | 
| Derek Miller | 6211726 | 2019-02-15 17:12:26 -0600 | [diff] [blame] | 1237 | psa_key_slot_number_t dest_key); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1238 |  | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1239 | /** \brief A function that performs the final step of a secure element key | 
|  | 1240 | * agreement and place the generated key material in a buffer | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1241 | * | 
|  | 1242 | * \param[out] p_output         Buffer in which to place the generated key | 
|  | 1243 | *                              material | 
|  | 1244 | * \param[in] output_size       The size in bytes of `p_output` | 
|  | 1245 | * \param[out] p_output_length  Upon success, contains the number of bytes of | 
|  | 1246 | *                              key material placed in `p_output` | 
|  | 1247 | * | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 1248 | * \retval #PSA_SUCCESS | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1249 | */ | 
| Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 1250 | typedef psa_status_t (*psa_drv_se_key_derivation_export_t)(void *op_context, | 
| Derek Miller | 6211726 | 2019-02-15 17:12:26 -0600 | [diff] [blame] | 1251 | uint8_t *p_output, | 
|  | 1252 | size_t output_size, | 
|  | 1253 | size_t *p_output_length); | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1254 |  | 
|  | 1255 | /** | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1256 | * \brief A struct containing all of the function pointers needed to for secure | 
|  | 1257 | * element key derivation and agreement | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1258 | * | 
|  | 1259 | * PSA Crypto API implementations should populate instances of the table as | 
|  | 1260 | * appropriate upon startup. | 
|  | 1261 | * | 
|  | 1262 | * If one of the functions is not implemented, it should be set to NULL. | 
|  | 1263 | */ | 
|  | 1264 | typedef struct { | 
| Derek Miller | 6211726 | 2019-02-15 17:12:26 -0600 | [diff] [blame] | 1265 | /** The driver-specific size of the key derivation context */ | 
|  | 1266 | size_t                           context_size; | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1267 | /** Function that performs a key derivation setup */ | 
| Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 1268 | psa_drv_se_key_derivation_setup_t      p_setup; | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1269 | /** Function that sets key derivation collateral */ | 
| Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 1270 | psa_drv_se_key_derivation_collateral_t p_collateral; | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1271 | /** Function that performs a final key derivation step */ | 
| Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 1272 | psa_drv_se_key_derivation_derive_t     p_derive; | 
| Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1273 | /** Function that perforsm a final key derivation or agreement and | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1274 | * exports the key */ | 
| Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 1275 | psa_drv_se_key_derivation_export_t     p_export; | 
| Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 1276 | } psa_drv_se_key_derivation_t; | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1277 |  | 
|  | 1278 | /**@}*/ | 
|  | 1279 |  | 
| Gilles Peskine | b6cadea | 2019-06-24 13:46:37 +0200 | [diff] [blame] | 1280 | /** \defgroup se_registration Secure element driver registration | 
|  | 1281 | */ | 
|  | 1282 | /**@{*/ | 
|  | 1283 |  | 
|  | 1284 | /** A structure containing pointers to all the entry points of a | 
|  | 1285 | * secure element driver. | 
|  | 1286 | * | 
|  | 1287 | * Future versions of this specification may add extra substructures at | 
|  | 1288 | * the end of this structure. | 
|  | 1289 | */ | 
|  | 1290 | typedef struct { | 
| Gilles Peskine | c93a43b | 2019-06-26 11:21:41 +0200 | [diff] [blame] | 1291 | /** The version of the driver HAL that this driver implements. | 
|  | 1292 | * This is a protection against loading driver binaries built against | 
| Gilles Peskine | b6cadea | 2019-06-24 13:46:37 +0200 | [diff] [blame] | 1293 | * a different version of this specification. | 
|  | 1294 | * Use #PSA_DRV_SE_HAL_VERSION. | 
|  | 1295 | */ | 
|  | 1296 | uint32_t hal_version; | 
| Gilles Peskine | 7a86da1 | 2019-07-12 23:25:38 +0200 | [diff] [blame] | 1297 |  | 
| Gilles Peskine | 0c3ae1f | 2019-07-25 14:04:38 +0200 | [diff] [blame] | 1298 | /** The size of the driver's persistent data in bytes. | 
|  | 1299 | * | 
|  | 1300 | * This can be 0 if the driver does not need persistent data. | 
|  | 1301 | * | 
|  | 1302 | * See the documentation of psa_drv_se_context_t::persistent_data | 
|  | 1303 | * for more information about why and how a driver can use | 
|  | 1304 | * persistent data. | 
|  | 1305 | */ | 
| Gilles Peskine | 7a86da1 | 2019-07-12 23:25:38 +0200 | [diff] [blame] | 1306 | size_t persistent_data_size; | 
|  | 1307 |  | 
|  | 1308 | /** The driver initialization function. | 
|  | 1309 | * | 
|  | 1310 | * This function is called once during the initialization of the | 
|  | 1311 | * PSA Cryptography subsystem, before any other function of the | 
|  | 1312 | * driver is called. If this function returns a failure status, | 
|  | 1313 | * the driver will be unusable, at least until the next system reset. | 
|  | 1314 | * | 
|  | 1315 | * If this field is \c NULL, it is equivalent to a function that does | 
|  | 1316 | * nothing and returns #PSA_SUCCESS. | 
|  | 1317 | */ | 
|  | 1318 | psa_drv_se_init_t p_init; | 
|  | 1319 |  | 
| Gilles Peskine | 6e59c42 | 2019-06-26 19:06:52 +0200 | [diff] [blame] | 1320 | const psa_drv_se_key_management_t *key_management; | 
|  | 1321 | const psa_drv_se_mac_t *mac; | 
|  | 1322 | const psa_drv_se_cipher_t *cipher; | 
|  | 1323 | const psa_drv_se_aead_t *aead; | 
|  | 1324 | const psa_drv_se_asymmetric_t *asymmetric; | 
|  | 1325 | const psa_drv_se_key_derivation_t *derivation; | 
| Gilles Peskine | b6cadea | 2019-06-24 13:46:37 +0200 | [diff] [blame] | 1326 | } psa_drv_se_t; | 
|  | 1327 |  | 
| Gilles Peskine | c93a43b | 2019-06-26 11:21:41 +0200 | [diff] [blame] | 1328 | /** The current version of the secure element driver HAL. | 
| Gilles Peskine | b6cadea | 2019-06-24 13:46:37 +0200 | [diff] [blame] | 1329 | */ | 
|  | 1330 | /* 0.0.0 patchlevel 5 */ | 
|  | 1331 | #define PSA_DRV_SE_HAL_VERSION 0x00000005 | 
|  | 1332 |  | 
| Gilles Peskine | c93a43b | 2019-06-26 11:21:41 +0200 | [diff] [blame] | 1333 | /** Register an external cryptoprocessor (secure element) driver. | 
| Gilles Peskine | d910e92 | 2019-06-24 13:47:07 +0200 | [diff] [blame] | 1334 | * | 
|  | 1335 | * This function is only intended to be used by driver code, not by | 
|  | 1336 | * application code. In implementations with separation between the | 
|  | 1337 | * PSA cryptography module and applications, this function should | 
|  | 1338 | * only be available to callers that run in the same memory space as | 
|  | 1339 | * the cryptography module, and should not be exposed to applications | 
|  | 1340 | * running in a different memory space. | 
|  | 1341 | * | 
|  | 1342 | * This function may be called before psa_crypto_init(). It is | 
|  | 1343 | * implementation-defined whether this function may be called | 
|  | 1344 | * after psa_crypto_init(). | 
|  | 1345 | * | 
| Gilles Peskine | c93a43b | 2019-06-26 11:21:41 +0200 | [diff] [blame] | 1346 | * \note Implementations store metadata about keys including the lifetime | 
| Gilles Peskine | 52ac958 | 2020-05-10 00:39:18 +0200 | [diff] [blame] | 1347 | *       value, which contains the driver's location indicator. Therefore, | 
|  | 1348 | *       from one instantiation of the PSA Cryptography | 
| Gilles Peskine | c93a43b | 2019-06-26 11:21:41 +0200 | [diff] [blame] | 1349 | *       library to the next one, if there is a key in storage with a certain | 
|  | 1350 | *       lifetime value, you must always register the same driver (or an | 
|  | 1351 | *       updated version that communicates with the same secure element) | 
| Gilles Peskine | 52ac958 | 2020-05-10 00:39:18 +0200 | [diff] [blame] | 1352 | *       with the same location value. | 
| Gilles Peskine | c93a43b | 2019-06-26 11:21:41 +0200 | [diff] [blame] | 1353 | * | 
| Gilles Peskine | 52ac958 | 2020-05-10 00:39:18 +0200 | [diff] [blame] | 1354 | * \param location      The location value through which this driver will | 
| Gilles Peskine | d910e92 | 2019-06-24 13:47:07 +0200 | [diff] [blame] | 1355 | *                      be exposed to applications. | 
| Gilles Peskine | 52ac958 | 2020-05-10 00:39:18 +0200 | [diff] [blame] | 1356 | *                      This driver will be used for all keys such that | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 1357 | *                      `location == #PSA_KEY_LIFETIME_GET_LOCATION( lifetime )`. | 
| Gilles Peskine | 52ac958 | 2020-05-10 00:39:18 +0200 | [diff] [blame] | 1358 | *                      The value #PSA_KEY_LOCATION_LOCAL_STORAGE is reserved | 
|  | 1359 | *                      and may not be used for drivers. Implementations | 
| Gilles Peskine | d910e92 | 2019-06-24 13:47:07 +0200 | [diff] [blame] | 1360 | *                      may reserve other values. | 
|  | 1361 | * \param[in] methods   The method table of the driver. This structure must | 
|  | 1362 | *                      remain valid for as long as the cryptography | 
|  | 1363 | *                      module keeps running. It is typically a global | 
|  | 1364 | *                      constant. | 
|  | 1365 | * | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 1366 | * \return #PSA_SUCCESS | 
| Gilles Peskine | d910e92 | 2019-06-24 13:47:07 +0200 | [diff] [blame] | 1367 | *         The driver was successfully registered. Applications can now | 
| Gilles Peskine | 7ef23be | 2021-03-08 17:19:47 +0100 | [diff] [blame] | 1368 | *         use \p location to access keys through the methods passed to | 
| Gilles Peskine | d910e92 | 2019-06-24 13:47:07 +0200 | [diff] [blame] | 1369 | *         this function. | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 1370 | * \return #PSA_ERROR_BAD_STATE | 
| Gilles Peskine | d910e92 | 2019-06-24 13:47:07 +0200 | [diff] [blame] | 1371 | *         This function was called after the initialization of the | 
|  | 1372 | *         cryptography module, and this implementation does not support | 
|  | 1373 | *         driver registration at this stage. | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 1374 | * \return #PSA_ERROR_ALREADY_EXISTS | 
| Gilles Peskine | 7ef23be | 2021-03-08 17:19:47 +0100 | [diff] [blame] | 1375 | *         There is already a registered driver for this value of \p location. | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 1376 | * \return #PSA_ERROR_INVALID_ARGUMENT | 
| Gilles Peskine | 7ef23be | 2021-03-08 17:19:47 +0100 | [diff] [blame] | 1377 | *         \p location is a reserved value. | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 1378 | * \return #PSA_ERROR_NOT_SUPPORTED | 
| Gilles Peskine | c93a43b | 2019-06-26 11:21:41 +0200 | [diff] [blame] | 1379 | *         `methods->hal_version` is not supported by this implementation. | 
| Ronald Cron | 9678355 | 2020-10-19 12:06:30 +0200 | [diff] [blame] | 1380 | * \return #PSA_ERROR_INSUFFICIENT_MEMORY | 
|  | 1381 | * \return #PSA_ERROR_NOT_PERMITTED | 
| gabor-mezei-arm | 452b0a3 | 2020-11-09 17:42:55 +0100 | [diff] [blame] | 1382 | * \return #PSA_ERROR_STORAGE_FAILURE | 
|  | 1383 | * \return #PSA_ERROR_DATA_CORRUPT | 
| Gilles Peskine | d910e92 | 2019-06-24 13:47:07 +0200 | [diff] [blame] | 1384 | */ | 
|  | 1385 | psa_status_t psa_register_se_driver( | 
| Gilles Peskine | 344e15b | 2020-05-10 00:44:30 +0200 | [diff] [blame] | 1386 | psa_key_location_t location, | 
| Gilles Peskine | d910e92 | 2019-06-24 13:47:07 +0200 | [diff] [blame] | 1387 | const psa_drv_se_t *methods); | 
|  | 1388 |  | 
| Gilles Peskine | b6cadea | 2019-06-24 13:46:37 +0200 | [diff] [blame] | 1389 | /**@}*/ | 
|  | 1390 |  | 
| Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1391 | #ifdef __cplusplus | 
|  | 1392 | } | 
|  | 1393 | #endif | 
|  | 1394 |  | 
|  | 1395 | #endif /* PSA_CRYPTO_SE_DRIVER_H */ |