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 | /* |
| 19 | * Copyright (C) 2018, ARM Limited, All Rights Reserved |
| 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 | 7a86da1 | 2019-07-12 23:25:38 +0200 | [diff] [blame] | 122 | * \param lifetime The lifetime value for which this driver |
| 123 | * is registered. |
| 124 | * |
| 125 | * \retval #PSA_SUCCESS |
| 126 | * The driver is operational. |
| 127 | * The core will update the persistent data in storage. |
| 128 | * \return |
| 129 | * Any other return value prevents the driver from being used in |
| 130 | * this session. |
| 131 | * The core will NOT update the persistent data in storage. |
| 132 | */ |
| 133 | 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] | 134 | void *persistent_data, |
Gilles Peskine | 7a86da1 | 2019-07-12 23:25:38 +0200 | [diff] [blame] | 135 | psa_key_lifetime_t lifetime); |
| 136 | |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 137 | /** An internal designation of a key slot between the core part of the |
| 138 | * PSA Crypto implementation and the driver. The meaning of this value |
| 139 | * is driver-dependent. */ |
Gilles Peskine | f03143a | 2019-07-12 23:18:29 +0200 | [diff] [blame] | 140 | typedef uint64_t psa_key_slot_number_t; |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 141 | |
Gilles Peskine | 7a86da1 | 2019-07-12 23:25:38 +0200 | [diff] [blame] | 142 | /**@}*/ |
| 143 | |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 144 | /** \defgroup se_mac Secure Element Message Authentication Codes |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 145 | * Generation and authentication of Message Authentication Codes (MACs) using |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 146 | * a secure element can be done either as a single function call (via the |
| 147 | * `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] | 148 | * parts using the following sequence: |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 149 | * - `psa_drv_se_mac_setup_t` |
| 150 | * - `psa_drv_se_mac_update_t` |
| 151 | * - `psa_drv_se_mac_update_t` |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 152 | * - ... |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 153 | * - `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] | 154 | * |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 155 | * If a previously started secure element MAC operation needs to be terminated, |
| 156 | * 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] | 157 | * result in allocated resources not being freed or in other undefined |
| 158 | * behavior. |
| 159 | */ |
| 160 | /**@{*/ |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 161 | /** \brief A function that starts a secure element MAC operation for a PSA |
| 162 | * Crypto Driver implementation |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 163 | * |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 164 | * \param[in,out] drv_context The driver context structure. |
| 165 | * \param[in,out] op_context A structure that will contain the |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 166 | * hardware-specific MAC context |
| 167 | * \param[in] key_slot The slot of the key to be used for the |
| 168 | * operation |
| 169 | * \param[in] algorithm The algorithm to be used to underly the MAC |
| 170 | * operation |
| 171 | * |
| 172 | * \retval PSA_SUCCESS |
| 173 | * Success. |
| 174 | */ |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 175 | typedef psa_status_t (*psa_drv_se_mac_setup_t)(psa_drv_se_context_t *drv_context, |
| 176 | void *op_context, |
Derek Miller | b2a1cce | 2019-02-15 17:03:42 -0600 | [diff] [blame] | 177 | psa_key_slot_number_t key_slot, |
Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 178 | psa_algorithm_t algorithm); |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 179 | |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 180 | /** \brief A function that continues a previously started secure element MAC |
| 181 | * operation |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 182 | * |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 183 | * \param[in,out] op_context A hardware-specific structure for the |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 184 | * previously-established MAC operation to be |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 185 | * updated |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 186 | * \param[in] p_input A buffer containing the message to be appended |
| 187 | * to the MAC operation |
Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 188 | * \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] | 189 | */ |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 190 | 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] | 191 | const uint8_t *p_input, |
| 192 | size_t input_length); |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 193 | |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 194 | /** \brief a function that completes a previously started secure element MAC |
| 195 | * operation by returning the resulting MAC. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 196 | * |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 197 | * \param[in,out] op_context A hardware-specific structure for the |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 198 | * previously started MAC operation to be |
| 199 | * finished |
| 200 | * \param[out] p_mac A buffer where the generated MAC will be |
| 201 | * placed |
| 202 | * \param[in] mac_size The size in bytes of the buffer that has been |
| 203 | * allocated for the `output` buffer |
| 204 | * \param[out] p_mac_length After completion, will contain the number of |
| 205 | * bytes placed in the `p_mac` buffer |
| 206 | * |
| 207 | * \retval PSA_SUCCESS |
| 208 | * Success. |
| 209 | */ |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 210 | 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] | 211 | uint8_t *p_mac, |
| 212 | size_t mac_size, |
| 213 | size_t *p_mac_length); |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 214 | |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 215 | /** \brief A function that completes a previously started secure element MAC |
| 216 | * operation by comparing the resulting MAC against a provided value |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 217 | * |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 218 | * \param[in,out] op_context A hardware-specific structure for the previously |
Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 219 | * started MAC operation to be fiinished |
| 220 | * \param[in] p_mac The MAC value against which the resulting MAC |
| 221 | * will be compared against |
| 222 | * \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] | 223 | * |
| 224 | * \retval PSA_SUCCESS |
| 225 | * The operation completed successfully and the MACs matched each |
| 226 | * other |
| 227 | * \retval PSA_ERROR_INVALID_SIGNATURE |
| 228 | * The operation completed successfully, but the calculated MAC did |
| 229 | * not match the provided MAC |
| 230 | */ |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 231 | 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] | 232 | const uint8_t *p_mac, |
| 233 | size_t mac_length); |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 234 | |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 235 | /** \brief A function that aborts a previous started secure element MAC |
| 236 | * operation |
Gilles Peskine | 32668ce | 2019-03-06 18:29:57 +0100 | [diff] [blame] | 237 | * |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 238 | * \param[in,out] op_context A hardware-specific structure for the previously |
Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 239 | * started MAC operation to be aborted |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 240 | */ |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 241 | 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] | 242 | |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 243 | /** \brief A function that performs a secure element MAC operation in one |
| 244 | * command and returns the calculated MAC |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 245 | * |
Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 246 | * \param[in,out] drv_context The driver context structure. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 247 | * \param[in] p_input A buffer containing the message to be MACed |
| 248 | * \param[in] input_length The size in bytes of `p_input` |
| 249 | * \param[in] key_slot The slot of the key to be used |
| 250 | * \param[in] alg The algorithm to be used to underlie the MAC |
| 251 | * operation |
| 252 | * \param[out] p_mac A buffer where the generated MAC will be |
| 253 | * placed |
| 254 | * \param[in] mac_size The size in bytes of the `p_mac` buffer |
| 255 | * \param[out] p_mac_length After completion, will contain the number of |
| 256 | * bytes placed in the `output` buffer |
| 257 | * |
| 258 | * \retval PSA_SUCCESS |
| 259 | * Success. |
| 260 | */ |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 261 | typedef psa_status_t (*psa_drv_se_mac_generate_t)(psa_drv_se_context_t *drv_context, |
| 262 | const uint8_t *p_input, |
Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 263 | size_t input_length, |
Derek Miller | b2a1cce | 2019-02-15 17:03:42 -0600 | [diff] [blame] | 264 | psa_key_slot_number_t key_slot, |
Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 265 | psa_algorithm_t alg, |
| 266 | uint8_t *p_mac, |
| 267 | size_t mac_size, |
| 268 | size_t *p_mac_length); |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 269 | |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 270 | /** \brief A function that performs a secure element MAC operation in one |
| 271 | * command and compares the resulting MAC against a provided value |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 272 | * |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 273 | * \param[in,out] drv_context The driver context structure. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 274 | * \param[in] p_input A buffer containing the message to be MACed |
| 275 | * \param[in] input_length The size in bytes of `input` |
| 276 | * \param[in] key_slot The slot of the key to be used |
| 277 | * \param[in] alg The algorithm to be used to underlie the MAC |
| 278 | * operation |
| 279 | * \param[in] p_mac The MAC value against which the resulting MAC will |
| 280 | * be compared against |
| 281 | * \param[in] mac_length The size in bytes of `mac` |
| 282 | * |
| 283 | * \retval PSA_SUCCESS |
| 284 | * The operation completed successfully and the MACs matched each |
| 285 | * other |
| 286 | * \retval PSA_ERROR_INVALID_SIGNATURE |
| 287 | * The operation completed successfully, but the calculated MAC did |
| 288 | * not match the provided MAC |
| 289 | */ |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 290 | typedef psa_status_t (*psa_drv_se_mac_verify_t)(psa_drv_se_context_t *drv_context, |
| 291 | const uint8_t *p_input, |
Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 292 | size_t input_length, |
Derek Miller | b2a1cce | 2019-02-15 17:03:42 -0600 | [diff] [blame] | 293 | psa_key_slot_number_t key_slot, |
Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 294 | psa_algorithm_t alg, |
| 295 | const uint8_t *p_mac, |
| 296 | size_t mac_length); |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 297 | |
| 298 | /** \brief A struct containing all of the function pointers needed to |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 299 | * perform secure element MAC operations |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 300 | * |
| 301 | * PSA Crypto API implementations should populate the table as appropriate |
| 302 | * upon startup. |
| 303 | * |
| 304 | * If one of the functions is not implemented (such as |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 305 | * `psa_drv_se_mac_generate_t`), it should be set to NULL. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 306 | * |
| 307 | * Driver implementers should ensure that they implement all of the functions |
| 308 | * that make sense for their hardware, and that they provide a full solution |
| 309 | * (for example, if they support `p_setup`, they should also support |
| 310 | * `p_update` and at least one of `p_finish` or `p_finish_verify`). |
| 311 | * |
| 312 | */ |
| 313 | typedef struct { |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 314 | /**The size in bytes of the hardware-specific secure element MAC context |
| 315 | * structure |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 316 | */ |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 317 | size_t context_size; |
| 318 | /** Function that performs a MAC setup operation |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 319 | */ |
Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 320 | psa_drv_se_mac_setup_t p_setup; |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 321 | /** Function that performs a MAC update operation |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 322 | */ |
Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 323 | psa_drv_se_mac_update_t p_update; |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 324 | /** Function that completes a MAC operation |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 325 | */ |
Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 326 | psa_drv_se_mac_finish_t p_finish; |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 327 | /** Function that completes a MAC operation with a verify check |
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_finish_verify_t p_finish_verify; |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 330 | /** Function that aborts a previoustly started MAC 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_abort_t p_abort; |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 333 | /** Function that performs a MAC operation in one call |
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_generate_t p_mac; |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 336 | /** Function that performs a MAC and verify operation in one call |
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_verify_t p_mac_verify; |
Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 339 | } psa_drv_se_mac_t; |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 340 | /**@}*/ |
| 341 | |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 342 | /** \defgroup se_cipher Secure Element Symmetric Ciphers |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 343 | * |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 344 | * Encryption and Decryption using secure element keys in block modes other |
| 345 | * than ECB must be done in multiple parts, using the following flow: |
| 346 | * - `psa_drv_se_cipher_setup_t` |
| 347 | * - `psa_drv_se_cipher_set_iv_t` (optional depending upon block mode) |
| 348 | * - `psa_drv_se_cipher_update_t` |
| 349 | * - `psa_drv_se_cipher_update_t` |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 350 | * - ... |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 351 | * - `psa_drv_se_cipher_finish_t` |
Gilles Peskine | 32668ce | 2019-03-06 18:29:57 +0100 | [diff] [blame] | 352 | * |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 353 | * If a previously started secure element Cipher operation needs to be |
| 354 | * terminated, it should be done so by the `psa_drv_se_cipher_abort_t`. Failure |
| 355 | * to do so may result in allocated resources not being freed or in other |
| 356 | * undefined behavior. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 357 | * |
| 358 | * In situations where a PSA Cryptographic API implementation is using a block |
| 359 | * mode not-supported by the underlying hardware or driver, it can construct |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 360 | * the block mode itself, while calling the `psa_drv_se_cipher_ecb_t` function |
| 361 | * for the cipher operations. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 362 | */ |
| 363 | /**@{*/ |
| 364 | |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 365 | /** \brief A function that provides the cipher setup function for a |
| 366 | * secure element driver |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 367 | * |
Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 368 | * \param[in,out] drv_context The driver context structure. |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 369 | * \param[in,out] op_context A structure that will contain the |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 370 | * hardware-specific cipher context. |
| 371 | * \param[in] key_slot The slot of the key to be used for the |
| 372 | * operation |
| 373 | * \param[in] algorithm The algorithm to be used in the cipher |
| 374 | * operation |
| 375 | * \param[in] direction Indicates whether the operation is an encrypt |
| 376 | * or decrypt |
| 377 | * |
| 378 | * \retval PSA_SUCCESS |
| 379 | * \retval PSA_ERROR_NOT_SUPPORTED |
| 380 | */ |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 381 | typedef psa_status_t (*psa_drv_se_cipher_setup_t)(psa_drv_se_context_t *drv_context, |
| 382 | void *op_context, |
Derek Miller | b2a1cce | 2019-02-15 17:03:42 -0600 | [diff] [blame] | 383 | psa_key_slot_number_t key_slot, |
Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 384 | psa_algorithm_t algorithm, |
| 385 | psa_encrypt_or_decrypt_t direction); |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 386 | |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 387 | /** \brief A function that sets the initialization vector (if |
| 388 | * necessary) for an secure element cipher operation |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 389 | * |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 390 | * Rationale: The `psa_se_cipher_*` operation in the PSA Cryptographic API has |
| 391 | * 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] | 392 | * generate function is not necessary for the drivers to implement as the PSA |
| 393 | * Crypto implementation can do the generation using its RNG features. |
| 394 | * |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 395 | * \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] | 396 | * hardware-specific cipher context |
| 397 | * \param[in] p_iv A buffer containing the initialization vector |
| 398 | * \param[in] iv_length The size (in bytes) of the `p_iv` buffer |
| 399 | * |
| 400 | * \retval PSA_SUCCESS |
| 401 | */ |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 402 | 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] | 403 | const uint8_t *p_iv, |
| 404 | size_t iv_length); |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 405 | |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 406 | /** \brief A function that continues a previously started secure element cipher |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 407 | * operation |
| 408 | * |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 409 | * \param[in,out] op_context A hardware-specific structure for the |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 410 | * previously started cipher operation |
| 411 | * \param[in] p_input A buffer containing the data to be |
| 412 | * encrypted/decrypted |
| 413 | * \param[in] input_size The size in bytes of the buffer pointed to |
| 414 | * by `p_input` |
| 415 | * \param[out] p_output The caller-allocated buffer where the |
| 416 | * output will be placed |
| 417 | * \param[in] output_size The allocated size in bytes of the |
| 418 | * `p_output` buffer |
| 419 | * \param[out] p_output_length After completion, will contain the number |
| 420 | * of bytes placed in the `p_output` buffer |
| 421 | * |
| 422 | * \retval PSA_SUCCESS |
| 423 | */ |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 424 | 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] | 425 | const uint8_t *p_input, |
| 426 | size_t input_size, |
| 427 | uint8_t *p_output, |
| 428 | size_t output_size, |
| 429 | size_t *p_output_length); |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 430 | |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 431 | /** \brief A function that completes a previously started secure element cipher |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 432 | * operation |
| 433 | * |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 434 | * \param[in,out] op_context A hardware-specific structure for the |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 435 | * previously started cipher operation |
| 436 | * \param[out] p_output The caller-allocated buffer where the output |
| 437 | * will be placed |
| 438 | * \param[in] output_size The allocated size in bytes of the `p_output` |
| 439 | * buffer |
| 440 | * \param[out] p_output_length After completion, will contain the number of |
| 441 | * bytes placed in the `p_output` buffer |
| 442 | * |
| 443 | * \retval PSA_SUCCESS |
| 444 | */ |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 445 | 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] | 446 | uint8_t *p_output, |
| 447 | size_t output_size, |
| 448 | size_t *p_output_length); |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 449 | |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 450 | /** \brief A function that aborts a previously started secure element cipher |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 451 | * operation |
| 452 | * |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 453 | * \param[in,out] op_context A hardware-specific structure for the |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 454 | * previously started cipher operation |
| 455 | */ |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 456 | 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] | 457 | |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 458 | /** \brief A function that performs the ECB block mode for secure element |
| 459 | * cipher operations |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 460 | * |
| 461 | * Note: this function should only be used with implementations that do not |
| 462 | * provide a needed higher-level operation. |
| 463 | * |
Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 464 | * \param[in,out] drv_context The driver context structure. |
| 465 | * \param[in] key_slot The slot of the key to be used for the operation |
| 466 | * \param[in] algorithm The algorithm to be used in the cipher operation |
| 467 | * \param[in] direction Indicates whether the operation is an encrypt or |
| 468 | * decrypt |
| 469 | * \param[in] p_input A buffer containing the data to be |
| 470 | * encrypted/decrypted |
| 471 | * \param[in] input_size The size in bytes of the buffer pointed to by |
| 472 | * `p_input` |
| 473 | * \param[out] p_output The caller-allocated buffer where the output |
| 474 | * will be placed |
| 475 | * \param[in] output_size The allocated size in bytes of the `p_output` |
| 476 | * buffer |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 477 | * |
| 478 | * \retval PSA_SUCCESS |
| 479 | * \retval PSA_ERROR_NOT_SUPPORTED |
| 480 | */ |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 481 | typedef psa_status_t (*psa_drv_se_cipher_ecb_t)(psa_drv_se_context_t *drv_context, |
| 482 | psa_key_slot_number_t key_slot, |
Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 483 | psa_algorithm_t algorithm, |
| 484 | psa_encrypt_or_decrypt_t direction, |
| 485 | const uint8_t *p_input, |
| 486 | size_t input_size, |
| 487 | uint8_t *p_output, |
| 488 | size_t output_size); |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 489 | |
| 490 | /** |
| 491 | * \brief A struct containing all of the function pointers needed to implement |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 492 | * cipher operations using secure elements. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 493 | * |
| 494 | * PSA Crypto API implementations should populate instances of the table as |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 495 | * appropriate upon startup or at build time. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 496 | * |
| 497 | * If one of the functions is not implemented (such as |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 498 | * `psa_drv_se_cipher_ecb_t`), it should be set to NULL. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 499 | */ |
| 500 | typedef struct { |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 501 | /** The size in bytes of the hardware-specific secure element cipher |
| 502 | * context structure |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 503 | */ |
Derek Miller | 34b33f1 | 2019-02-15 17:13:54 -0600 | [diff] [blame] | 504 | size_t context_size; |
| 505 | /** Function that performs a cipher setup operation */ |
Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 506 | psa_drv_se_cipher_setup_t p_setup; |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 507 | /** Function that sets a cipher IV (if necessary) */ |
Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 508 | psa_drv_se_cipher_set_iv_t p_set_iv; |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 509 | /** Function that performs a cipher update operation */ |
Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 510 | psa_drv_se_cipher_update_t p_update; |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 511 | /** Function that completes a cipher operation */ |
Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 512 | psa_drv_se_cipher_finish_t p_finish; |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 513 | /** Function that aborts a cipher operation */ |
Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 514 | psa_drv_se_cipher_abort_t p_abort; |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 515 | /** Function that performs ECB mode for a cipher operation |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 516 | * (Danger: ECB mode should not be used directly by clients of the PSA |
| 517 | * Crypto Client API) |
| 518 | */ |
Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 519 | psa_drv_se_cipher_ecb_t p_ecb; |
Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 520 | } psa_drv_se_cipher_t; |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 521 | |
| 522 | /**@}*/ |
| 523 | |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 524 | /** \defgroup se_asymmetric Secure Element Asymmetric Cryptography |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 525 | * |
| 526 | * Since the amount of data that can (or should) be encrypted or signed using |
| 527 | * asymmetric keys is limited by the key size, asymmetric key operations using |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 528 | * keys in a secure element must be done in single function calls. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 529 | */ |
| 530 | /**@{*/ |
| 531 | |
| 532 | /** |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 533 | * \brief A function that signs a hash or short message with a private key in |
| 534 | * a secure element |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 535 | * |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 536 | * \param[in,out] drv_context The driver context structure. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 537 | * \param[in] key_slot Key slot of an asymmetric key pair |
| 538 | * \param[in] alg A signature algorithm that is compatible |
| 539 | * with the type of `key` |
| 540 | * \param[in] p_hash The hash to sign |
| 541 | * \param[in] hash_length Size of the `p_hash` buffer in bytes |
| 542 | * \param[out] p_signature Buffer where the signature is to be written |
| 543 | * \param[in] signature_size Size of the `p_signature` buffer in bytes |
| 544 | * \param[out] p_signature_length On success, the number of bytes |
| 545 | * that make up the returned signature value |
| 546 | * |
| 547 | * \retval PSA_SUCCESS |
| 548 | */ |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 549 | typedef psa_status_t (*psa_drv_se_asymmetric_sign_t)(psa_drv_se_context_t *drv_context, |
| 550 | psa_key_slot_number_t key_slot, |
Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 551 | psa_algorithm_t alg, |
| 552 | const uint8_t *p_hash, |
| 553 | size_t hash_length, |
| 554 | uint8_t *p_signature, |
| 555 | size_t signature_size, |
| 556 | size_t *p_signature_length); |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 557 | |
| 558 | /** |
| 559 | * \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] | 560 | * an asymmetric public key in a secure element |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 561 | * |
Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 562 | * \param[in,out] drv_context The driver context structure. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 563 | * \param[in] key_slot Key slot of a public key or an asymmetric key |
| 564 | * pair |
| 565 | * \param[in] alg A signature algorithm that is compatible with |
| 566 | * the type of `key` |
| 567 | * \param[in] p_hash The hash whose signature is to be verified |
| 568 | * \param[in] hash_length Size of the `p_hash` buffer in bytes |
| 569 | * \param[in] p_signature Buffer containing the signature to verify |
| 570 | * \param[in] signature_length Size of the `p_signature` buffer in bytes |
| 571 | * |
| 572 | * \retval PSA_SUCCESS |
| 573 | * The signature is valid. |
| 574 | */ |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 575 | typedef psa_status_t (*psa_drv_se_asymmetric_verify_t)(psa_drv_se_context_t *drv_context, |
| 576 | psa_key_slot_number_t key_slot, |
Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 577 | psa_algorithm_t alg, |
| 578 | const uint8_t *p_hash, |
| 579 | size_t hash_length, |
| 580 | const uint8_t *p_signature, |
| 581 | size_t signature_length); |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 582 | |
| 583 | /** |
| 584 | * \brief A function that encrypts a short message with an asymmetric public |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 585 | * key in a secure element |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 586 | * |
Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 587 | * \param[in,out] drv_context The driver context structure. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 588 | * \param[in] key_slot Key slot of a public key or an asymmetric key |
| 589 | * pair |
| 590 | * \param[in] alg An asymmetric encryption algorithm that is |
| 591 | * compatible with the type of `key` |
| 592 | * \param[in] p_input The message to encrypt |
| 593 | * \param[in] input_length Size of the `p_input` buffer in bytes |
| 594 | * \param[in] p_salt A salt or label, if supported by the |
| 595 | * encryption algorithm |
| 596 | * If the algorithm does not support a |
| 597 | * salt, pass `NULL`. |
| 598 | * If the algorithm supports an optional |
| 599 | * salt and you do not want to pass a salt, |
| 600 | * pass `NULL`. |
| 601 | * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is |
| 602 | * supported. |
| 603 | * \param[in] salt_length Size of the `p_salt` buffer in bytes |
| 604 | * If `p_salt` is `NULL`, pass 0. |
| 605 | * \param[out] p_output Buffer where the encrypted message is to |
| 606 | * be written |
| 607 | * \param[in] output_size Size of the `p_output` buffer in bytes |
| 608 | * \param[out] p_output_length On success, the number of bytes that make up |
| 609 | * the returned output |
| 610 | * |
| 611 | * \retval PSA_SUCCESS |
| 612 | */ |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 613 | typedef psa_status_t (*psa_drv_se_asymmetric_encrypt_t)(psa_drv_se_context_t *drv_context, |
| 614 | psa_key_slot_number_t key_slot, |
Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 615 | psa_algorithm_t alg, |
| 616 | const uint8_t *p_input, |
| 617 | size_t input_length, |
| 618 | const uint8_t *p_salt, |
| 619 | size_t salt_length, |
| 620 | uint8_t *p_output, |
| 621 | size_t output_size, |
| 622 | size_t *p_output_length); |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 623 | |
| 624 | /** |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 625 | * \brief A function that decrypts a short message with an asymmetric private |
| 626 | * key in a secure element. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 627 | * |
Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 628 | * \param[in,out] drv_context The driver context structure. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 629 | * \param[in] key_slot Key slot of an asymmetric key pair |
| 630 | * \param[in] alg An asymmetric encryption algorithm that is |
| 631 | * compatible with the type of `key` |
| 632 | * \param[in] p_input The message to decrypt |
| 633 | * \param[in] input_length Size of the `p_input` buffer in bytes |
| 634 | * \param[in] p_salt A salt or label, if supported by the |
| 635 | * encryption algorithm |
| 636 | * If the algorithm does not support a |
| 637 | * salt, pass `NULL`. |
| 638 | * If the algorithm supports an optional |
| 639 | * salt and you do not want to pass a salt, |
| 640 | * pass `NULL`. |
| 641 | * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is |
| 642 | * supported. |
| 643 | * \param[in] salt_length Size of the `p_salt` buffer in bytes |
| 644 | * If `p_salt` is `NULL`, pass 0. |
| 645 | * \param[out] p_output Buffer where the decrypted message is to |
| 646 | * be written |
| 647 | * \param[in] output_size Size of the `p_output` buffer in bytes |
| 648 | * \param[out] p_output_length On success, the number of bytes |
| 649 | * that make up the returned output |
| 650 | * |
| 651 | * \retval PSA_SUCCESS |
| 652 | */ |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 653 | typedef psa_status_t (*psa_drv_se_asymmetric_decrypt_t)(psa_drv_se_context_t *drv_context, |
| 654 | psa_key_slot_number_t key_slot, |
Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 655 | psa_algorithm_t alg, |
| 656 | const uint8_t *p_input, |
| 657 | size_t input_length, |
| 658 | const uint8_t *p_salt, |
| 659 | size_t salt_length, |
| 660 | uint8_t *p_output, |
| 661 | size_t output_size, |
| 662 | size_t *p_output_length); |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 663 | |
| 664 | /** |
| 665 | * \brief A struct containing all of the function pointers needed to implement |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 666 | * asymmetric cryptographic operations using secure elements. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 667 | * |
| 668 | * PSA Crypto API implementations should populate instances of the table as |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 669 | * appropriate upon startup or at build time. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 670 | * |
| 671 | * If one of the functions is not implemented, it should be set to NULL. |
| 672 | */ |
| 673 | typedef struct { |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 674 | /** Function that performs an asymmetric sign operation */ |
Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 675 | psa_drv_se_asymmetric_sign_t p_sign; |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 676 | /** Function that performs an asymmetric verify operation */ |
Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 677 | psa_drv_se_asymmetric_verify_t p_verify; |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 678 | /** Function that performs an asymmetric encrypt operation */ |
Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 679 | psa_drv_se_asymmetric_encrypt_t p_encrypt; |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 680 | /** Function that performs an asymmetric decrypt operation */ |
Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 681 | psa_drv_se_asymmetric_decrypt_t p_decrypt; |
Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 682 | } psa_drv_se_asymmetric_t; |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 683 | |
| 684 | /**@}*/ |
| 685 | |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 686 | /** \defgroup se_aead Secure Element Authenticated Encryption with Additional Data |
| 687 | * Authenticated Encryption with Additional Data (AEAD) operations with secure |
| 688 | * 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] | 689 | * implementers as there must be sufficient space in memory for the entire |
| 690 | * message, it prevents decrypted data from being made available before the |
| 691 | * authentication operation is complete and the data is known to be authentic. |
| 692 | */ |
| 693 | /**@{*/ |
| 694 | |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 695 | /** \brief A function that performs a secure element authenticated encryption |
| 696 | * operation |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 697 | * |
Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 698 | * \param[in,out] drv_context The driver context structure. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 699 | * \param[in] key_slot Slot containing the key to use. |
| 700 | * \param[in] algorithm The AEAD algorithm to compute |
| 701 | * (\c PSA_ALG_XXX value such that |
| 702 | * #PSA_ALG_IS_AEAD(`alg`) is true) |
| 703 | * \param[in] p_nonce Nonce or IV to use |
| 704 | * \param[in] nonce_length Size of the `p_nonce` buffer in bytes |
| 705 | * \param[in] p_additional_data Additional data that will be |
| 706 | * authenticated but not encrypted |
| 707 | * \param[in] additional_data_length Size of `p_additional_data` in bytes |
| 708 | * \param[in] p_plaintext Data that will be authenticated and |
| 709 | * encrypted |
| 710 | * \param[in] plaintext_length Size of `p_plaintext` in bytes |
| 711 | * \param[out] p_ciphertext Output buffer for the authenticated and |
| 712 | * encrypted data. The additional data is |
| 713 | * not part of this output. For algorithms |
| 714 | * where the encrypted data and the |
| 715 | * authentication tag are defined as |
| 716 | * separate outputs, the authentication |
| 717 | * tag is appended to the encrypted data. |
| 718 | * \param[in] ciphertext_size Size of the `p_ciphertext` buffer in |
| 719 | * bytes |
| 720 | * \param[out] p_ciphertext_length On success, the size of the output in |
| 721 | * the `p_ciphertext` buffer |
| 722 | * |
| 723 | * \retval #PSA_SUCCESS |
| 724 | * Success. |
| 725 | */ |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 726 | typedef psa_status_t (*psa_drv_se_aead_encrypt_t)(psa_drv_se_context_t *drv_context, |
| 727 | psa_key_slot_number_t key_slot, |
Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 728 | psa_algorithm_t algorithm, |
| 729 | const uint8_t *p_nonce, |
| 730 | size_t nonce_length, |
| 731 | const uint8_t *p_additional_data, |
| 732 | size_t additional_data_length, |
| 733 | const uint8_t *p_plaintext, |
| 734 | size_t plaintext_length, |
| 735 | uint8_t *p_ciphertext, |
| 736 | size_t ciphertext_size, |
| 737 | size_t *p_ciphertext_length); |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 738 | |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 739 | /** A function that peforms a secure element authenticated decryption operation |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 740 | * |
Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 741 | * \param[in,out] drv_context The driver context structure. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 742 | * \param[in] key_slot Slot containing the key to use |
| 743 | * \param[in] algorithm The AEAD algorithm to compute |
| 744 | * (\c PSA_ALG_XXX value such that |
| 745 | * #PSA_ALG_IS_AEAD(`alg`) is true) |
| 746 | * \param[in] p_nonce Nonce or IV to use |
| 747 | * \param[in] nonce_length Size of the `p_nonce` buffer in bytes |
| 748 | * \param[in] p_additional_data Additional data that has been |
| 749 | * authenticated but not encrypted |
| 750 | * \param[in] additional_data_length Size of `p_additional_data` in bytes |
| 751 | * \param[in] p_ciphertext Data that has been authenticated and |
| 752 | * encrypted. |
| 753 | * For algorithms where the encrypted data |
| 754 | * and the authentication tag are defined |
| 755 | * as separate inputs, the buffer must |
| 756 | * contain the encrypted data followed by |
| 757 | * the authentication tag. |
| 758 | * \param[in] ciphertext_length Size of `p_ciphertext` in bytes |
| 759 | * \param[out] p_plaintext Output buffer for the decrypted data |
| 760 | * \param[in] plaintext_size Size of the `p_plaintext` buffer in |
| 761 | * bytes |
| 762 | * \param[out] p_plaintext_length On success, the size of the output in |
| 763 | * the `p_plaintext` buffer |
| 764 | * |
| 765 | * \retval #PSA_SUCCESS |
| 766 | * Success. |
| 767 | */ |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 768 | typedef psa_status_t (*psa_drv_se_aead_decrypt_t)(psa_drv_se_context_t *drv_context, |
| 769 | psa_key_slot_number_t key_slot, |
Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 770 | psa_algorithm_t algorithm, |
| 771 | const uint8_t *p_nonce, |
| 772 | size_t nonce_length, |
| 773 | const uint8_t *p_additional_data, |
| 774 | size_t additional_data_length, |
| 775 | const uint8_t *p_ciphertext, |
| 776 | size_t ciphertext_length, |
| 777 | uint8_t *p_plaintext, |
| 778 | size_t plaintext_size, |
| 779 | size_t *p_plaintext_length); |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 780 | |
| 781 | /** |
| 782 | * \brief A struct containing all of the function pointers needed to implement |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 783 | * secure element Authenticated Encryption with Additional Data operations |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 784 | * |
| 785 | * PSA Crypto API implementations should populate instances of the table as |
| 786 | * appropriate upon startup. |
| 787 | * |
| 788 | * If one of the functions is not implemented, it should be set to NULL. |
| 789 | */ |
| 790 | typedef struct { |
| 791 | /** Function that performs the AEAD encrypt operation */ |
Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 792 | psa_drv_se_aead_encrypt_t p_encrypt; |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 793 | /** Function that performs the AEAD decrypt operation */ |
Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 794 | psa_drv_se_aead_decrypt_t p_decrypt; |
Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 795 | } psa_drv_se_aead_t; |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 796 | /**@}*/ |
| 797 | |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 798 | /** \defgroup se_key_management Secure Element Key Management |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 799 | * Currently, key management is limited to importing keys in the clear, |
| 800 | * destroying keys, and exporting keys in the clear. |
| 801 | * Whether a key may be exported is determined by the key policies in place |
| 802 | * on the key slot. |
| 803 | */ |
| 804 | /**@{*/ |
| 805 | |
Gilles Peskine | f2223c8 | 2019-07-12 23:33:02 +0200 | [diff] [blame] | 806 | /** \brief A function that allocates a slot for a key. |
| 807 | * |
| 808 | * \param[in,out] drv_context The driver context structure. |
Gilles Peskine | 94cc42c | 2019-07-12 23:34:20 +0200 | [diff] [blame] | 809 | * \param[in,out] persistent_data A pointer to the persistent data |
| 810 | * that allows writing. |
Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 811 | * \param[in] attributes Attributes of the key. |
| 812 | * \param[out] key_slot Slot where the key will be stored. |
| 813 | * This must be a valid slot for a key of the |
| 814 | * chosen type. It must be unoccupied. |
Gilles Peskine | f2223c8 | 2019-07-12 23:33:02 +0200 | [diff] [blame] | 815 | * |
| 816 | * \retval #PSA_SUCCESS |
| 817 | * Success. |
| 818 | * The core will record \c *key_slot as the key slot where the key |
| 819 | * is stored and will update the persistent data in storage. |
| 820 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 821 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 822 | */ |
| 823 | typedef psa_status_t (*psa_drv_se_allocate_key_t)( |
| 824 | psa_drv_se_context_t *drv_context, |
Gilles Peskine | 94cc42c | 2019-07-12 23:34:20 +0200 | [diff] [blame] | 825 | void *persistent_data, |
Gilles Peskine | f2223c8 | 2019-07-12 23:33:02 +0200 | [diff] [blame] | 826 | const psa_key_attributes_t *attributes, |
| 827 | psa_key_slot_number_t *key_slot); |
| 828 | |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 829 | /** \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] | 830 | * |
| 831 | * This function can support any output from psa_export_key(). Refer to the |
| 832 | * documentation of psa_export_key() for the format for each key type. |
| 833 | * |
Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 834 | * \param[in,out] drv_context The driver context structure. |
| 835 | * \param[in] key_slot Slot where the key will be stored |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame^] | 836 | * This must be a valid slot for a key of the |
| 837 | * chosen type. It must be unoccupied. |
Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 838 | * \param[in] lifetime The required lifetime of the key storage |
| 839 | * \param[in] type Key type (a \c PSA_KEY_TYPE_XXX value) |
| 840 | * \param[in] algorithm Key algorithm (a \c PSA_ALG_XXX value) |
| 841 | * \param[in] usage The allowed uses of the key |
| 842 | * \param[in] p_data Buffer containing the key data |
| 843 | * \param[in] data_length Size of the `data` buffer in bytes |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame^] | 844 | * \param[out] bits On success, the key size in bits. The driver |
| 845 | * must determine this value after parsing the |
| 846 | * key according to the key type. |
| 847 | * This value is not used if the function fails. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 848 | * |
| 849 | * \retval #PSA_SUCCESS |
| 850 | * Success. |
| 851 | */ |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 852 | typedef psa_status_t (*psa_drv_se_import_key_t)(psa_drv_se_context_t *drv_context, |
| 853 | psa_key_slot_number_t key_slot, |
Derek Miller | 0972fe5 | 2019-02-15 17:08:27 -0600 | [diff] [blame] | 854 | psa_key_lifetime_t lifetime, |
Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 855 | psa_key_type_t type, |
| 856 | psa_algorithm_t algorithm, |
| 857 | psa_key_usage_t usage, |
| 858 | const uint8_t *p_data, |
Gilles Peskine | 1801740 | 2019-07-24 20:25:59 +0200 | [diff] [blame^] | 859 | size_t data_length, |
| 860 | size_t *bits); |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 861 | |
| 862 | /** |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 863 | * \brief A function that destroys a secure element key and restore the slot to |
| 864 | * its default state |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 865 | * |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 866 | * This function destroys the content of the key from a secure element. |
| 867 | * Implementations shall make a best effort to ensure that any previous content |
| 868 | * of the slot is unrecoverable. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 869 | * |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 870 | * This function returns the specified slot to its default state. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 871 | * |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 872 | * \param[in,out] drv_context The driver context structure. |
Gilles Peskine | 94cc42c | 2019-07-12 23:34:20 +0200 | [diff] [blame] | 873 | * \param[in,out] persistent_data A pointer to the persistent data |
| 874 | * that allows writing. |
Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 875 | * \param key_slot The key slot to erase. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 876 | * |
| 877 | * \retval #PSA_SUCCESS |
| 878 | * The slot's content, if any, has been erased. |
| 879 | */ |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 880 | typedef psa_status_t (*psa_drv_se_destroy_key_t)( |
| 881 | psa_drv_se_context_t *drv_context, |
Gilles Peskine | 94cc42c | 2019-07-12 23:34:20 +0200 | [diff] [blame] | 882 | void *persistent_data, |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 883 | psa_key_slot_number_t key_slot); |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 884 | |
| 885 | /** |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 886 | * \brief A function that exports a secure element key in binary format |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 887 | * |
| 888 | * The output of this function can be passed to psa_import_key() to |
| 889 | * create an equivalent object. |
| 890 | * |
| 891 | * If a key is created with `psa_import_key()` and then exported with |
| 892 | * this function, it is not guaranteed that the resulting data is |
| 893 | * identical: the implementation may choose a different representation |
| 894 | * of the same key if the format permits it. |
| 895 | * |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 896 | * This function should generate output in the same format that |
| 897 | * `psa_export_key()` does. Refer to the |
| 898 | * documentation of `psa_export_key()` for the format for each key type. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 899 | * |
Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 900 | * \param[in,out] drv_context The driver context structure. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 901 | * \param[in] key Slot whose content is to be exported. This must |
| 902 | * be an occupied key slot. |
| 903 | * \param[out] p_data Buffer where the key data is to be written. |
| 904 | * \param[in] data_size Size of the `p_data` buffer in bytes. |
| 905 | * \param[out] p_data_length On success, the number of bytes |
| 906 | * that make up the key data. |
| 907 | * |
| 908 | * \retval #PSA_SUCCESS |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 909 | * \retval #PSA_ERROR_DOES_NOT_EXIST |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 910 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 911 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 912 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 913 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
Gilles Peskine | 4b3eb69 | 2019-05-16 21:35:18 +0200 | [diff] [blame] | 914 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 915 | */ |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 916 | typedef psa_status_t (*psa_drv_se_export_key_t)(psa_drv_se_context_t *drv_context, |
| 917 | psa_key_slot_number_t key, |
Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 918 | uint8_t *p_data, |
| 919 | size_t data_size, |
| 920 | size_t *p_data_length); |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 921 | |
| 922 | /** |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 923 | * \brief A function that generates a symmetric or asymmetric key on a secure |
| 924 | * element |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 925 | * |
Gilles Peskine | 9dd125d | 2019-07-23 18:26:43 +0200 | [diff] [blame] | 926 | * If \p type is asymmetric (#PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) = 1), |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 927 | * the public component of the generated key will be placed in `p_pubkey_out`. |
| 928 | * The format of the public key information will match the format specified for |
Gilles Peskine | e5c025c | 2019-03-06 18:01:43 +0100 | [diff] [blame] | 929 | * the psa_export_key() function for the key type. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 930 | * |
Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 931 | * \param[in,out] drv_context The driver context structure. |
| 932 | * \param[in] key_slot Slot where the generated key will be placed |
| 933 | * \param[in] type The type of the key to be generated |
| 934 | * \param[in] usage The prescribed usage of the generated key |
| 935 | * Note: Not all Secure Elements support the same |
| 936 | * restrictions that PSA Crypto does (and vice |
| 937 | * versa). |
| 938 | * Driver developers should endeavor to match the |
| 939 | * usages as close as possible. |
| 940 | * \param[in] bits The size in bits of the key to be generated. |
| 941 | * \param[in] extra Extra parameters for key generation. The |
| 942 | * interpretation of this parameter should match |
| 943 | * the interpretation in the `extra` parameter is |
| 944 | * the `psa_generate_key` function |
| 945 | * \param[in] extra_size The size in bytes of the \p extra buffer |
| 946 | * \param[out] p_pubkey_out The buffer where the public key information will |
| 947 | * be placed |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 948 | * \param[in] pubkey_out_size The size in bytes of the `p_pubkey_out` buffer |
| 949 | * \param[out] p_pubkey_length Upon successful completion, will contain the |
| 950 | * size of the data placed in `p_pubkey_out`. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 951 | */ |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 952 | typedef psa_status_t (*psa_drv_se_generate_key_t)(psa_drv_se_context_t *drv_context, |
| 953 | psa_key_slot_number_t key_slot, |
Gilles Peskine | 32668ce | 2019-03-06 18:29:57 +0100 | [diff] [blame] | 954 | psa_key_type_t type, |
| 955 | psa_key_usage_t usage, |
| 956 | size_t bits, |
| 957 | const void *extra, |
| 958 | size_t extra_size, |
| 959 | uint8_t *p_pubkey_out, |
| 960 | size_t pubkey_out_size, |
| 961 | size_t *p_pubkey_length); |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 962 | |
| 963 | /** |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 964 | * \brief A struct containing all of the function pointers needed to for secure |
| 965 | * element key management |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 966 | * |
| 967 | * PSA Crypto API implementations should populate instances of the table as |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 968 | * appropriate upon startup or at build time. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 969 | * |
| 970 | * If one of the functions is not implemented, it should be set to NULL. |
| 971 | */ |
| 972 | typedef struct { |
Gilles Peskine | f2223c8 | 2019-07-12 23:33:02 +0200 | [diff] [blame] | 973 | /** Function that allocates a slot. */ |
| 974 | psa_drv_se_allocate_key_t p_allocate; |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 975 | /** Function that performs a key import operation */ |
| 976 | psa_drv_se_import_key_t p_import; |
| 977 | /** Function that performs a generation */ |
Derek Miller | 0b3098a | 2019-02-15 17:10:49 -0600 | [diff] [blame] | 978 | psa_drv_se_generate_key_t p_generate; |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 979 | /** Function that performs a key destroy operation */ |
Derek Miller | 0b3098a | 2019-02-15 17:10:49 -0600 | [diff] [blame] | 980 | psa_drv_se_destroy_key_t p_destroy; |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 981 | /** Function that performs a key export operation */ |
Derek Miller | 0b3098a | 2019-02-15 17:10:49 -0600 | [diff] [blame] | 982 | psa_drv_se_export_key_t p_export; |
Gilles Peskine | e62b74e | 2019-06-25 15:25:09 +0200 | [diff] [blame] | 983 | /** Function that performs a public key export operation */ |
| 984 | psa_drv_se_export_key_t p_export_public; |
Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 985 | } psa_drv_se_key_management_t; |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 986 | |
| 987 | /**@}*/ |
| 988 | |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 989 | /** \defgroup driver_derivation Secure Element Key Derivation and Agreement |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 990 | * Key derivation is the process of generating new key material using an |
| 991 | * existing key and additional parameters, iterating through a basic |
| 992 | * cryptographic function, such as a hash. |
| 993 | * Key agreement is a part of cryptographic protocols that allows two parties |
| 994 | * to agree on the same key value, but starting from different original key |
| 995 | * material. |
| 996 | * The flows are similar, and the PSA Crypto Driver Model uses the same functions |
| 997 | * for both of the flows. |
| 998 | * |
| 999 | * There are two different final functions for the flows, |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1000 | * `psa_drv_se_key_derivation_derive` and `psa_drv_se_key_derivation_export`. |
| 1001 | * `psa_drv_se_key_derivation_derive` is used when the key material should be |
| 1002 | * placed in a slot on the hardware and not exposed to the caller. |
| 1003 | * `psa_drv_se_key_derivation_export` is used when the key material should be |
| 1004 | * returned to the PSA Cryptographic API implementation. |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1005 | * |
| 1006 | * Different key derivation algorithms require a different number of inputs. |
| 1007 | * Instead of having an API that takes as input variable length arrays, which |
| 1008 | * can be problemmatic to manage on embedded platforms, the inputs are passed |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1009 | * to the driver via a function, `psa_drv_se_key_derivation_collateral`, that |
| 1010 | * is called multiple times with different `collateral_id`s. Thus, for a key |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1011 | * derivation algorithm that required 3 paramter inputs, the flow would look |
| 1012 | * something like: |
| 1013 | * ~~~~~~~~~~~~~{.c} |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1014 | * psa_drv_se_key_derivation_setup(kdf_algorithm, source_key, dest_key_size_bytes); |
| 1015 | * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_0, |
| 1016 | * p_collateral_0, |
| 1017 | * collateral_0_size); |
| 1018 | * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_1, |
| 1019 | * p_collateral_1, |
| 1020 | * collateral_1_size); |
| 1021 | * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_2, |
| 1022 | * p_collateral_2, |
| 1023 | * collateral_2_size); |
| 1024 | * psa_drv_se_key_derivation_derive(); |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1025 | * ~~~~~~~~~~~~~ |
| 1026 | * |
| 1027 | * key agreement example: |
| 1028 | * ~~~~~~~~~~~~~{.c} |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1029 | * psa_drv_se_key_derivation_setup(alg, source_key. dest_key_size_bytes); |
| 1030 | * psa_drv_se_key_derivation_collateral(DHE_PUBKEY, p_pubkey, pubkey_size); |
| 1031 | * psa_drv_se_key_derivation_export(p_session_key, |
| 1032 | * session_key_size, |
| 1033 | * &session_key_length); |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1034 | * ~~~~~~~~~~~~~ |
| 1035 | */ |
| 1036 | /**@{*/ |
| 1037 | |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1038 | /** \brief A function that Sets up a secure element key derivation operation by |
| 1039 | * specifying the algorithm and the source key sot |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1040 | * |
Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 1041 | * \param[in,out] drv_context The driver context structure. |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 1042 | * \param[in,out] op_context A hardware-specific structure containing any |
Gilles Peskine | 6a3dd89 | 2019-07-25 10:56:39 +0200 | [diff] [blame] | 1043 | * context information for the implementation |
| 1044 | * \param[in] kdf_alg The algorithm to be used for the key derivation |
| 1045 | * \param[in] source_key The key to be used as the source material for |
| 1046 | * the key derivation |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1047 | * |
| 1048 | * \retval PSA_SUCCESS |
| 1049 | */ |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 1050 | typedef psa_status_t (*psa_drv_se_key_derivation_setup_t)(psa_drv_se_context_t *drv_context, |
| 1051 | void *op_context, |
Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 1052 | psa_algorithm_t kdf_alg, |
Derek Miller | b2a1cce | 2019-02-15 17:03:42 -0600 | [diff] [blame] | 1053 | psa_key_slot_number_t source_key); |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1054 | |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1055 | /** \brief A function that provides collateral (parameters) needed for a secure |
| 1056 | * element key derivation or key agreement operation |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1057 | * |
| 1058 | * Since many key derivation algorithms require multiple parameters, it is |
| 1059 | * expeced that this function may be called multiple times for the same |
| 1060 | * operation, each with a different algorithm-specific `collateral_id` |
| 1061 | * |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 1062 | * \param[in,out] op_context A hardware-specific structure containing any |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1063 | * context information for the implementation |
| 1064 | * \param[in] collateral_id An ID for the collateral being provided |
| 1065 | * \param[in] p_collateral A buffer containing the collateral data |
| 1066 | * \param[in] collateral_size The size in bytes of the collateral |
| 1067 | * |
| 1068 | * \retval PSA_SUCCESS |
| 1069 | */ |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 1070 | 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] | 1071 | uint32_t collateral_id, |
| 1072 | const uint8_t *p_collateral, |
| 1073 | size_t collateral_size); |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1074 | |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1075 | /** \brief A function that performs the final secure element key derivation |
| 1076 | * step and place the generated key material in a slot |
Gilles Peskine | c3044a6 | 2019-03-06 17:56:28 +0100 | [diff] [blame] | 1077 | * |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 1078 | * \param[in,out] op_context A hardware-specific structure containing any |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1079 | * context information for the implementation |
| 1080 | * \param[in] dest_key The slot where the generated key material |
| 1081 | * should be placed |
| 1082 | * |
| 1083 | * \retval PSA_SUCCESS |
| 1084 | */ |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 1085 | 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] | 1086 | psa_key_slot_number_t dest_key); |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1087 | |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1088 | /** \brief A function that performs the final step of a secure element key |
| 1089 | * agreement and place the generated key material in a buffer |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1090 | * |
| 1091 | * \param[out] p_output Buffer in which to place the generated key |
| 1092 | * material |
| 1093 | * \param[in] output_size The size in bytes of `p_output` |
| 1094 | * \param[out] p_output_length Upon success, contains the number of bytes of |
| 1095 | * key material placed in `p_output` |
| 1096 | * |
| 1097 | * \retval PSA_SUCCESS |
| 1098 | */ |
Gilles Peskine | 8597bc1 | 2019-07-12 23:28:46 +0200 | [diff] [blame] | 1099 | 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] | 1100 | uint8_t *p_output, |
| 1101 | size_t output_size, |
| 1102 | size_t *p_output_length); |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1103 | |
| 1104 | /** |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1105 | * \brief A struct containing all of the function pointers needed to for secure |
| 1106 | * element key derivation and agreement |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1107 | * |
| 1108 | * PSA Crypto API implementations should populate instances of the table as |
| 1109 | * appropriate upon startup. |
| 1110 | * |
| 1111 | * If one of the functions is not implemented, it should be set to NULL. |
| 1112 | */ |
| 1113 | typedef struct { |
Derek Miller | 6211726 | 2019-02-15 17:12:26 -0600 | [diff] [blame] | 1114 | /** The driver-specific size of the key derivation context */ |
| 1115 | size_t context_size; |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1116 | /** Function that performs a key derivation setup */ |
Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 1117 | psa_drv_se_key_derivation_setup_t p_setup; |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1118 | /** Function that sets key derivation collateral */ |
Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 1119 | psa_drv_se_key_derivation_collateral_t p_collateral; |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1120 | /** Function that performs a final key derivation step */ |
Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 1121 | psa_drv_se_key_derivation_derive_t p_derive; |
Derek Miller | f0c1d0d | 2019-02-15 17:23:42 -0600 | [diff] [blame] | 1122 | /** Function that perforsm a final key derivation or agreement and |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1123 | * exports the key */ |
Derek Miller | ea743cf | 2019-02-15 17:06:29 -0600 | [diff] [blame] | 1124 | psa_drv_se_key_derivation_export_t p_export; |
Derek Miller | 83d2662 | 2019-02-15 16:41:22 -0600 | [diff] [blame] | 1125 | } psa_drv_se_key_derivation_t; |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1126 | |
| 1127 | /**@}*/ |
| 1128 | |
Gilles Peskine | b6cadea | 2019-06-24 13:46:37 +0200 | [diff] [blame] | 1129 | /** \defgroup se_registration Secure element driver registration |
| 1130 | */ |
| 1131 | /**@{*/ |
| 1132 | |
| 1133 | /** A structure containing pointers to all the entry points of a |
| 1134 | * secure element driver. |
| 1135 | * |
| 1136 | * Future versions of this specification may add extra substructures at |
| 1137 | * the end of this structure. |
| 1138 | */ |
| 1139 | typedef struct { |
Gilles Peskine | c93a43b | 2019-06-26 11:21:41 +0200 | [diff] [blame] | 1140 | /** The version of the driver HAL that this driver implements. |
| 1141 | * This is a protection against loading driver binaries built against |
Gilles Peskine | b6cadea | 2019-06-24 13:46:37 +0200 | [diff] [blame] | 1142 | * a different version of this specification. |
| 1143 | * Use #PSA_DRV_SE_HAL_VERSION. |
| 1144 | */ |
| 1145 | uint32_t hal_version; |
Gilles Peskine | 7a86da1 | 2019-07-12 23:25:38 +0200 | [diff] [blame] | 1146 | |
Gilles Peskine | 0c3ae1f | 2019-07-25 14:04:38 +0200 | [diff] [blame] | 1147 | /** The size of the driver's persistent data in bytes. |
| 1148 | * |
| 1149 | * This can be 0 if the driver does not need persistent data. |
| 1150 | * |
| 1151 | * See the documentation of psa_drv_se_context_t::persistent_data |
| 1152 | * for more information about why and how a driver can use |
| 1153 | * persistent data. |
| 1154 | */ |
Gilles Peskine | 7a86da1 | 2019-07-12 23:25:38 +0200 | [diff] [blame] | 1155 | size_t persistent_data_size; |
| 1156 | |
| 1157 | /** The driver initialization function. |
| 1158 | * |
| 1159 | * This function is called once during the initialization of the |
| 1160 | * PSA Cryptography subsystem, before any other function of the |
| 1161 | * driver is called. If this function returns a failure status, |
| 1162 | * the driver will be unusable, at least until the next system reset. |
| 1163 | * |
| 1164 | * If this field is \c NULL, it is equivalent to a function that does |
| 1165 | * nothing and returns #PSA_SUCCESS. |
| 1166 | */ |
| 1167 | psa_drv_se_init_t p_init; |
| 1168 | |
Gilles Peskine | 6e59c42 | 2019-06-26 19:06:52 +0200 | [diff] [blame] | 1169 | const psa_drv_se_key_management_t *key_management; |
| 1170 | const psa_drv_se_mac_t *mac; |
| 1171 | const psa_drv_se_cipher_t *cipher; |
| 1172 | const psa_drv_se_aead_t *aead; |
| 1173 | const psa_drv_se_asymmetric_t *asymmetric; |
| 1174 | const psa_drv_se_key_derivation_t *derivation; |
Gilles Peskine | b6cadea | 2019-06-24 13:46:37 +0200 | [diff] [blame] | 1175 | } psa_drv_se_t; |
| 1176 | |
Gilles Peskine | c93a43b | 2019-06-26 11:21:41 +0200 | [diff] [blame] | 1177 | /** The current version of the secure element driver HAL. |
Gilles Peskine | b6cadea | 2019-06-24 13:46:37 +0200 | [diff] [blame] | 1178 | */ |
| 1179 | /* 0.0.0 patchlevel 5 */ |
| 1180 | #define PSA_DRV_SE_HAL_VERSION 0x00000005 |
| 1181 | |
Gilles Peskine | c93a43b | 2019-06-26 11:21:41 +0200 | [diff] [blame] | 1182 | /** Register an external cryptoprocessor (secure element) driver. |
Gilles Peskine | d910e92 | 2019-06-24 13:47:07 +0200 | [diff] [blame] | 1183 | * |
| 1184 | * This function is only intended to be used by driver code, not by |
| 1185 | * application code. In implementations with separation between the |
| 1186 | * PSA cryptography module and applications, this function should |
| 1187 | * only be available to callers that run in the same memory space as |
| 1188 | * the cryptography module, and should not be exposed to applications |
| 1189 | * running in a different memory space. |
| 1190 | * |
| 1191 | * This function may be called before psa_crypto_init(). It is |
| 1192 | * implementation-defined whether this function may be called |
| 1193 | * after psa_crypto_init(). |
| 1194 | * |
Gilles Peskine | c93a43b | 2019-06-26 11:21:41 +0200 | [diff] [blame] | 1195 | * \note Implementations store metadata about keys including the lifetime |
| 1196 | * value. Therefore, from one instantiation of the PSA Cryptography |
| 1197 | * library to the next one, if there is a key in storage with a certain |
| 1198 | * lifetime value, you must always register the same driver (or an |
| 1199 | * updated version that communicates with the same secure element) |
| 1200 | * with the same lifetime value. |
| 1201 | * |
Gilles Peskine | d910e92 | 2019-06-24 13:47:07 +0200 | [diff] [blame] | 1202 | * \param lifetime The lifetime value through which this driver will |
| 1203 | * be exposed to applications. |
| 1204 | * The values #PSA_KEY_LIFETIME_VOLATILE and |
| 1205 | * #PSA_KEY_LIFETIME_PERSISTENT are reserved and |
Gilles Peskine | c93a43b | 2019-06-26 11:21:41 +0200 | [diff] [blame] | 1206 | * may not be used for drivers. Implementations |
Gilles Peskine | d910e92 | 2019-06-24 13:47:07 +0200 | [diff] [blame] | 1207 | * may reserve other values. |
| 1208 | * \param[in] methods The method table of the driver. This structure must |
| 1209 | * remain valid for as long as the cryptography |
| 1210 | * module keeps running. It is typically a global |
| 1211 | * constant. |
| 1212 | * |
| 1213 | * \return PSA_SUCCESS |
| 1214 | * The driver was successfully registered. Applications can now |
| 1215 | * use \p lifetime to access keys through the methods passed to |
| 1216 | * this function. |
| 1217 | * \return PSA_ERROR_BAD_STATE |
| 1218 | * This function was called after the initialization of the |
| 1219 | * cryptography module, and this implementation does not support |
| 1220 | * driver registration at this stage. |
| 1221 | * \return PSA_ERROR_ALREADY_EXISTS |
| 1222 | * There is already a registered driver for this value of \p lifetime. |
| 1223 | * \return PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | c93a43b | 2019-06-26 11:21:41 +0200 | [diff] [blame] | 1224 | * \p lifetime is a reserved value. |
Gilles Peskine | d910e92 | 2019-06-24 13:47:07 +0200 | [diff] [blame] | 1225 | * \return PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | c93a43b | 2019-06-26 11:21:41 +0200 | [diff] [blame] | 1226 | * `methods->hal_version` is not supported by this implementation. |
Gilles Peskine | d910e92 | 2019-06-24 13:47:07 +0200 | [diff] [blame] | 1227 | * \return PSA_ERROR_INSUFFICIENT_MEMORY |
| 1228 | * \return PSA_ERROR_NOT_PERMITTED |
| 1229 | */ |
| 1230 | psa_status_t psa_register_se_driver( |
| 1231 | psa_key_lifetime_t lifetime, |
| 1232 | const psa_drv_se_t *methods); |
| 1233 | |
Gilles Peskine | b6cadea | 2019-06-24 13:46:37 +0200 | [diff] [blame] | 1234 | /**@}*/ |
| 1235 | |
Gilles Peskine | 7597689 | 2018-12-12 15:55:09 +0100 | [diff] [blame] | 1236 | #ifdef __cplusplus |
| 1237 | } |
| 1238 | #endif |
| 1239 | |
| 1240 | #endif /* PSA_CRYPTO_SE_DRIVER_H */ |