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