Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file psa/crypto.h |
| 3 | * \brief Platform Security Architecture cryptography module |
| 4 | */ |
Jaeden Amero | cab5494 | 2018-07-25 13:26:13 +0100 | [diff] [blame] | 5 | /* |
| 6 | * Copyright (C) 2018, ARM Limited, All Rights Reserved |
| 7 | * SPDX-License-Identifier: Apache-2.0 |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | * not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
| 20 | */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 21 | |
| 22 | #ifndef PSA_CRYPTO_H |
| 23 | #define PSA_CRYPTO_H |
| 24 | |
| 25 | #include "crypto_platform.h" |
| 26 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 27 | #include <stddef.h> |
| 28 | |
Gilles Peskine | 62a7e7e | 2018-02-07 21:54:47 +0100 | [diff] [blame] | 29 | #ifdef __DOXYGEN_ONLY__ |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 30 | /* This __DOXYGEN_ONLY__ block contains mock definitions for things that |
| 31 | * must be defined in the crypto_platform.h header. These mock definitions |
| 32 | * are present in this file as a convenience to generate pretty-printed |
| 33 | * documentation that includes those definitions. */ |
| 34 | |
Gilles Peskine | 62a7e7e | 2018-02-07 21:54:47 +0100 | [diff] [blame] | 35 | /** \defgroup platform Implementation-specific definitions |
| 36 | * @{ |
| 37 | */ |
| 38 | |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 39 | /** \brief Key handle. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 40 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 41 | * This type represents open handles to keys. It must be an unsigned integral |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 42 | * type. The choice of type is implementation-dependent. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 43 | * |
Gilles Peskine | 23fd2bd | 2018-12-11 15:51:32 +0100 | [diff] [blame] | 44 | * 0 is not a valid key handle. How other handle values are assigned is |
| 45 | * implementation-dependent. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 46 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 47 | typedef _unsigned_integral_type_ psa_key_handle_t; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 48 | |
Gilles Peskine | 62a7e7e | 2018-02-07 21:54:47 +0100 | [diff] [blame] | 49 | /**@}*/ |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 50 | #endif /* __DOXYGEN_ONLY__ */ |
Gilles Peskine | 62a7e7e | 2018-02-07 21:54:47 +0100 | [diff] [blame] | 51 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 52 | #ifdef __cplusplus |
| 53 | extern "C" { |
| 54 | #endif |
| 55 | |
Gilles Peskine | f3b731e | 2018-12-12 13:38:31 +0100 | [diff] [blame] | 56 | /* The file "crypto_types.h" declares types that encode errors, |
| 57 | * algorithms, key types, policies, etc. */ |
| 58 | #include "crypto_types.h" |
| 59 | |
| 60 | /* The file "crypto_values.h" declares macros to build and analyze values |
| 61 | * of integral types defined in "crypto_types.h". */ |
| 62 | #include "crypto_values.h" |
| 63 | |
| 64 | /** \defgroup initialization Library initialization |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 65 | * @{ |
| 66 | */ |
| 67 | |
| 68 | /** |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 69 | * \brief Library initialization. |
| 70 | * |
| 71 | * Applications must call this function before calling any other |
| 72 | * function in this module. |
| 73 | * |
| 74 | * Applications may call this function more than once. Once a call |
| 75 | * succeeds, subsequent calls are guaranteed to succeed. |
| 76 | * |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 77 | * If the application calls other functions before calling psa_crypto_init(), |
| 78 | * the behavior is undefined. Implementations are encouraged to either perform |
| 79 | * the operation as if the library had been initialized or to return |
| 80 | * #PSA_ERROR_BAD_STATE or some other applicable error. In particular, |
| 81 | * implementations should not return a success status if the lack of |
| 82 | * initialization may have security implications, for example due to improper |
| 83 | * seeding of the random number generator. |
| 84 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 85 | * \retval #PSA_SUCCESS |
| 86 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 87 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 88 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 89 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 90 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 91 | */ |
| 92 | psa_status_t psa_crypto_init(void); |
| 93 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 94 | /**@}*/ |
| 95 | |
| 96 | /** \defgroup key_management Key management |
| 97 | * @{ |
| 98 | */ |
| 99 | |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 100 | /** \brief Retrieve the lifetime of an open key. |
Gilles Peskine | 3cac8c4 | 2018-11-30 14:07:45 +0100 | [diff] [blame] | 101 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 102 | * \param handle Handle to query. |
Gilles Peskine | 3cac8c4 | 2018-11-30 14:07:45 +0100 | [diff] [blame] | 103 | * \param[out] lifetime On success, the lifetime value. |
| 104 | * |
| 105 | * \retval #PSA_SUCCESS |
| 106 | * Success. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 107 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 3cac8c4 | 2018-11-30 14:07:45 +0100 | [diff] [blame] | 108 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 109 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 110 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 111 | * \retval #PSA_ERROR_BAD_STATE |
| 112 | * The library has not been previously initialized by psa_crypto_init(). |
| 113 | * It is implementation-dependent whether a failure to initialize |
| 114 | * results in this error code. |
| 115 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 116 | psa_status_t psa_get_key_lifetime(psa_key_handle_t handle, |
Gilles Peskine | 3cac8c4 | 2018-11-30 14:07:45 +0100 | [diff] [blame] | 117 | psa_key_lifetime_t *lifetime); |
| 118 | |
Gilles Peskine | 3cac8c4 | 2018-11-30 14:07:45 +0100 | [diff] [blame] | 119 | |
Gilles Peskine | f535eb2 | 2018-11-30 14:08:36 +0100 | [diff] [blame] | 120 | /** Allocate a key slot for a transient key, i.e. a key which is only stored |
| 121 | * in volatile memory. |
| 122 | * |
| 123 | * The allocated key slot and its handle remain valid until the |
| 124 | * application calls psa_close_key() or psa_destroy_key() or until the |
| 125 | * application terminates. |
| 126 | * |
| 127 | * This function takes a key type and maximum size as arguments so that |
| 128 | * the implementation can reserve a corresponding amount of memory. |
| 129 | * Implementations are not required to enforce this limit: if the application |
| 130 | * later tries to create a larger key or a key of a different type, it |
| 131 | * is implementation-defined whether this may succeed. |
| 132 | * |
| 133 | * \param type The type of key that the slot will contain. |
| 134 | * \param max_bits The maximum key size that the slot will contain. |
| 135 | * \param[out] handle On success, a handle to a volatile key slot. |
| 136 | * |
| 137 | * \retval #PSA_SUCCESS |
| 138 | * Success. The application can now use the value of `*handle` |
| 139 | * to access the newly allocated key slot. |
| 140 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 141 | * There was not enough memory, or the maximum number of key slots |
| 142 | * has been reached. |
| 143 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 144 | * This implementation does not support this key type. |
| 145 | */ |
| 146 | |
| 147 | psa_status_t psa_allocate_key(psa_key_type_t type, |
| 148 | size_t max_bits, |
| 149 | psa_key_handle_t *handle); |
| 150 | |
| 151 | /** Open a handle to an existing persistent key. |
| 152 | * |
| 153 | * Open a handle to a key which was previously created with psa_create_key(). |
| 154 | * |
| 155 | * \param lifetime The lifetime of the key. This designates a storage |
| 156 | * area where the key material is stored. This must not |
| 157 | * be #PSA_KEY_LIFETIME_VOLATILE. |
| 158 | * \param id The persistent identifier of the key. |
| 159 | * \param[out] handle On success, a handle to a key slot which contains |
| 160 | * the data and metadata loaded from the specified |
| 161 | * persistent location. |
| 162 | * |
| 163 | * \retval #PSA_SUCCESS |
| 164 | * Success. The application can now use the value of `*handle` |
| 165 | * to access the newly allocated key slot. |
| 166 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 167 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 168 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 169 | * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE. |
| 170 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 171 | * \p id is invalid for the specified lifetime. |
| 172 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 173 | * \p lifetime is not supported. |
| 174 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 175 | * The specified key exists, but the application does not have the |
| 176 | * permission to access it. Note that this specification does not |
| 177 | * define any way to create such a key, but it may be possible |
| 178 | * through implementation-specific means. |
| 179 | */ |
| 180 | psa_status_t psa_open_key(psa_key_lifetime_t lifetime, |
| 181 | psa_key_id_t id, |
| 182 | psa_key_handle_t *handle); |
| 183 | |
| 184 | /** Create a new persistent key slot. |
| 185 | * |
| 186 | * Create a new persistent key slot and return a handle to it. The handle |
| 187 | * remains valid until the application calls psa_close_key() or terminates. |
| 188 | * The application can open the key again with psa_open_key() until it |
| 189 | * removes the key by calling psa_destroy_key(). |
| 190 | * |
| 191 | * \param lifetime The lifetime of the key. This designates a storage |
| 192 | * area where the key material is stored. This must not |
| 193 | * be #PSA_KEY_LIFETIME_VOLATILE. |
| 194 | * \param id The persistent identifier of the key. |
| 195 | * \param type The type of key that the slot will contain. |
| 196 | * \param max_bits The maximum key size that the slot will contain. |
| 197 | * \param[out] handle On success, a handle to the newly created key slot. |
| 198 | * When key material is later created in this key slot, |
| 199 | * it will be saved to the specified persistent location. |
| 200 | * |
| 201 | * \retval #PSA_SUCCESS |
| 202 | * Success. The application can now use the value of `*handle` |
| 203 | * to access the newly allocated key slot. |
| 204 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 205 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 206 | * \retval #PSA_ERROR_OCCUPIED_SLOT |
| 207 | * There is already a key with the identifier \p id in the storage |
| 208 | * area designated by \p lifetime. |
| 209 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 210 | * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE. |
| 211 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 212 | * \p id is invalid for the specified lifetime. |
| 213 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 214 | * \p lifetime is not supported. |
| 215 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 216 | * \p lifetime is valid, but the application does not have the |
| 217 | * permission to create a key there. |
| 218 | */ |
| 219 | psa_status_t psa_create_key(psa_key_lifetime_t lifetime, |
| 220 | psa_key_id_t id, |
| 221 | psa_key_type_t type, |
| 222 | size_t max_bits, |
| 223 | psa_key_handle_t *handle); |
| 224 | |
| 225 | /** Close a key handle. |
| 226 | * |
| 227 | * If the handle designates a volatile key, destroy the key material and |
| 228 | * free all associated resources, just like psa_destroy_key(). |
| 229 | * |
| 230 | * If the handle designates a persistent key, free all resources associated |
| 231 | * with the key in volatile memory. The key slot in persistent storage is |
| 232 | * not affected and can be opened again later with psa_open_key(). |
| 233 | * |
Gilles Peskine | 5f25dd0 | 2019-01-14 18:24:53 +0100 | [diff] [blame] | 234 | * If the key is currently in use in a multipart operation, |
| 235 | * the multipart operation is aborted. |
| 236 | * |
Gilles Peskine | f535eb2 | 2018-11-30 14:08:36 +0100 | [diff] [blame] | 237 | * \param handle The key handle to close. |
| 238 | * |
| 239 | * \retval #PSA_SUCCESS |
| 240 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 241 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
Gilles Peskine | f535eb2 | 2018-11-30 14:08:36 +0100 | [diff] [blame] | 242 | */ |
| 243 | psa_status_t psa_close_key(psa_key_handle_t handle); |
| 244 | |
Gilles Peskine | 3cac8c4 | 2018-11-30 14:07:45 +0100 | [diff] [blame] | 245 | /**@}*/ |
| 246 | |
| 247 | /** \defgroup import_export Key import and export |
| 248 | * @{ |
| 249 | */ |
| 250 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 251 | /** |
| 252 | * \brief Import a key in binary format. |
| 253 | * |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 254 | * This function supports any output from psa_export_key(). Refer to the |
Gilles Peskine | f793393 | 2018-10-31 14:07:52 +0100 | [diff] [blame] | 255 | * documentation of psa_export_public_key() for the format of public keys |
| 256 | * and to the documentation of psa_export_key() for the format for |
| 257 | * other key types. |
| 258 | * |
| 259 | * This specification supports a single format for each key type. |
| 260 | * Implementations may support other formats as long as the standard |
| 261 | * format is supported. Implementations that support other formats |
| 262 | * should ensure that the formats are clearly unambiguous so as to |
| 263 | * minimize the risk that an invalid input is accidentally interpreted |
| 264 | * according to a different format. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 265 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 266 | * \param handle Handle to the slot where the key will be stored. |
| 267 | * This must be a valid slot for a key of the chosen |
| 268 | * type: it must have been obtained by calling |
| 269 | * psa_allocate_key() or psa_create_key() with the |
| 270 | * correct \p type and with a maximum size that is |
| 271 | * compatible with \p data. |
Gilles Peskine | f793393 | 2018-10-31 14:07:52 +0100 | [diff] [blame] | 272 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). On a successful |
| 273 | * import, the key slot will contain a key of this type. |
| 274 | * \param[in] data Buffer containing the key data. The content of this |
| 275 | * buffer is interpreted according to \p type. It must |
| 276 | * contain the format described in the documentation |
| 277 | * of psa_export_key() or psa_export_public_key() for |
| 278 | * the chosen type. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 279 | * \param data_length Size of the \p data buffer in bytes. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 280 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 281 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 282 | * Success. |
Gilles Peskine | 23fd2bd | 2018-12-11 15:51:32 +0100 | [diff] [blame] | 283 | * If the key is persistent, the key material and the key's metadata |
| 284 | * have been saved to persistent storage. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 285 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 286 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 287 | * The key type or key size is not supported, either by the |
| 288 | * implementation in general or in this particular slot. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 289 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 290 | * The key slot is invalid, |
| 291 | * or the key data is not correctly formatted. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 292 | * \retval #PSA_ERROR_OCCUPIED_SLOT |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 293 | * There is already a key in the specified slot. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 294 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 295 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 296 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 297 | * \retval #PSA_ERROR_STORAGE_FAILURE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 298 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 299 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 300 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 301 | * The library has not been previously initialized by psa_crypto_init(). |
| 302 | * It is implementation-dependent whether a failure to initialize |
| 303 | * results in this error code. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 304 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 305 | psa_status_t psa_import_key(psa_key_handle_t handle, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 306 | psa_key_type_t type, |
| 307 | const uint8_t *data, |
| 308 | size_t data_length); |
| 309 | |
| 310 | /** |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 311 | * \brief Destroy a key. |
Gilles Peskine | 154bd95 | 2018-04-19 08:38:16 +0200 | [diff] [blame] | 312 | * |
| 313 | * This function destroys the content of the key slot from both volatile |
| 314 | * memory and, if applicable, non-volatile storage. Implementations shall |
| 315 | * make a best effort to ensure that any previous content of the slot is |
| 316 | * unrecoverable. |
| 317 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 318 | * This function also erases any metadata such as policies and frees all |
| 319 | * resources associated with the key. |
Gilles Peskine | 154bd95 | 2018-04-19 08:38:16 +0200 | [diff] [blame] | 320 | * |
Gilles Peskine | 5f25dd0 | 2019-01-14 18:24:53 +0100 | [diff] [blame] | 321 | * If the key is currently in use in a multipart operation, |
| 322 | * the multipart operation is aborted. |
| 323 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 324 | * \param handle Handle to the key slot to erase. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 325 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 326 | * \retval #PSA_SUCCESS |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 327 | * The slot's content, if any, has been erased. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 328 | * \retval #PSA_ERROR_NOT_PERMITTED |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 329 | * The slot holds content and cannot be erased because it is |
| 330 | * read-only, either due to a policy or due to physical restrictions. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 331 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 332 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 333 | * There was an failure in communication with the cryptoprocessor. |
| 334 | * The key material may still be present in the cryptoprocessor. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 335 | * \retval #PSA_ERROR_STORAGE_FAILURE |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 336 | * The storage is corrupted. Implementations shall make a best effort |
| 337 | * to erase key material even in this stage, however applications |
| 338 | * should be aware that it may be impossible to guarantee that the |
| 339 | * key material is not recoverable in such cases. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 340 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 341 | * An unexpected condition which is not a storage corruption or |
| 342 | * a communication failure occurred. The cryptoprocessor may have |
| 343 | * been compromised. |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 344 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 345 | * The library has not been previously initialized by psa_crypto_init(). |
| 346 | * It is implementation-dependent whether a failure to initialize |
| 347 | * results in this error code. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 348 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 349 | psa_status_t psa_destroy_key(psa_key_handle_t handle); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 350 | |
| 351 | /** |
| 352 | * \brief Get basic metadata about a key. |
| 353 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 354 | * \param handle Handle to the key slot to query. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 355 | * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value). |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 356 | * This may be a null pointer, in which case the key type |
| 357 | * is not written. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 358 | * \param[out] bits On success, the key size in bits. |
Gilles Peskine | 9a1ba0d | 2018-03-21 20:49:16 +0100 | [diff] [blame] | 359 | * This may be a null pointer, in which case the key size |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 360 | * is not written. |
| 361 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 362 | * \retval #PSA_SUCCESS |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 363 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 364 | * \retval #PSA_ERROR_EMPTY_SLOT |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 365 | * The handle is to a key slot which does not contain key material yet. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 366 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 367 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 368 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 369 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 370 | * The library has not been previously initialized by psa_crypto_init(). |
| 371 | * It is implementation-dependent whether a failure to initialize |
| 372 | * results in this error code. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 373 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 374 | psa_status_t psa_get_key_information(psa_key_handle_t handle, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 375 | psa_key_type_t *type, |
| 376 | size_t *bits); |
| 377 | |
| 378 | /** |
| 379 | * \brief Export a key in binary format. |
| 380 | * |
| 381 | * The output of this function can be passed to psa_import_key() to |
| 382 | * create an equivalent object. |
| 383 | * |
Gilles Peskine | f793393 | 2018-10-31 14:07:52 +0100 | [diff] [blame] | 384 | * If the implementation of psa_import_key() supports other formats |
| 385 | * beyond the format specified here, the output from psa_export_key() |
| 386 | * must use the representation specified here, not the original |
| 387 | * representation. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 388 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 389 | * For standard key types, the output format is as follows: |
| 390 | * |
| 391 | * - For symmetric keys (including MAC keys), the format is the |
| 392 | * raw bytes of the key. |
| 393 | * - For DES, the key data consists of 8 bytes. The parity bits must be |
| 394 | * correct. |
| 395 | * - For Triple-DES, the format is the concatenation of the |
| 396 | * two or three DES keys. |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 397 | * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 398 | * is the non-encrypted DER encoding of the representation defined by |
| 399 | * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0. |
| 400 | * ``` |
| 401 | * RSAPrivateKey ::= SEQUENCE { |
Gilles Peskine | 4f6c77b | 2018-08-11 01:17:53 +0200 | [diff] [blame] | 402 | * version INTEGER, -- must be 0 |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 403 | * modulus INTEGER, -- n |
| 404 | * publicExponent INTEGER, -- e |
| 405 | * privateExponent INTEGER, -- d |
| 406 | * prime1 INTEGER, -- p |
| 407 | * prime2 INTEGER, -- q |
| 408 | * exponent1 INTEGER, -- d mod (p-1) |
| 409 | * exponent2 INTEGER, -- d mod (q-1) |
| 410 | * coefficient INTEGER, -- (inverse of q) mod p |
| 411 | * } |
| 412 | * ``` |
| 413 | * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format |
| 414 | * is the non-encrypted DER encoding of the representation used by |
Gilles Peskine | c6290c0 | 2018-08-13 17:24:59 +0200 | [diff] [blame] | 415 | * OpenSSL and OpenSSH, whose structure is described in ASN.1 as follows: |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 416 | * ``` |
| 417 | * DSAPrivateKey ::= SEQUENCE { |
Gilles Peskine | 4f6c77b | 2018-08-11 01:17:53 +0200 | [diff] [blame] | 418 | * version INTEGER, -- must be 0 |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 419 | * prime INTEGER, -- p |
| 420 | * subprime INTEGER, -- q |
| 421 | * generator INTEGER, -- g |
| 422 | * public INTEGER, -- y |
| 423 | * private INTEGER, -- x |
| 424 | * } |
| 425 | * ``` |
| 426 | * - For elliptic curve key pairs (key types for which |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 427 | * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is |
Gilles Peskine | 6c6a023 | 2018-11-15 17:44:43 +0100 | [diff] [blame] | 428 | * a representation of the private value as a `ceiling(m/8)`-byte string |
| 429 | * where `m` is the bit size associated with the curve, i.e. the bit size |
| 430 | * of the order of the curve's coordinate field. This byte string is |
| 431 | * in little-endian order for Montgomery curves (curve types |
| 432 | * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass |
| 433 | * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX` |
| 434 | * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`). |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 435 | * This is the content of the `privateKey` field of the `ECPrivateKey` |
| 436 | * format defined by RFC 5915. |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 437 | * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is |
| 438 | * true), the format is the same as for psa_export_public_key(). |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 439 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 440 | * \param handle Handle to the key to export. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 441 | * \param[out] data Buffer where the key data is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 442 | * \param data_size Size of the \p data buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 443 | * \param[out] data_length On success, the number of bytes |
| 444 | * that make up the key data. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 445 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 446 | * \retval #PSA_SUCCESS |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 447 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 448 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 449 | * \retval #PSA_ERROR_NOT_PERMITTED |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 450 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 451 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 452 | * The size of the \p data buffer is too small. You can determine a |
| 453 | * sufficient buffer size by calling |
| 454 | * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits) |
| 455 | * where \c type is the key type |
| 456 | * and \c bits is the key size in bits. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 457 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 458 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 459 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 460 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 461 | * The library has not been previously initialized by psa_crypto_init(). |
| 462 | * It is implementation-dependent whether a failure to initialize |
| 463 | * results in this error code. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 464 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 465 | psa_status_t psa_export_key(psa_key_handle_t handle, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 466 | uint8_t *data, |
| 467 | size_t data_size, |
| 468 | size_t *data_length); |
| 469 | |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 470 | /** |
| 471 | * \brief Export a public key or the public part of a key pair in binary format. |
| 472 | * |
| 473 | * The output of this function can be passed to psa_import_key() to |
| 474 | * create an object that is equivalent to the public key. |
| 475 | * |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 476 | * The format is the DER representation defined by RFC 5280 as |
| 477 | * `SubjectPublicKeyInfo`, with the `subjectPublicKey` format |
| 478 | * specified below. |
| 479 | * ``` |
| 480 | * SubjectPublicKeyInfo ::= SEQUENCE { |
| 481 | * algorithm AlgorithmIdentifier, |
| 482 | * subjectPublicKey BIT STRING } |
| 483 | * AlgorithmIdentifier ::= SEQUENCE { |
| 484 | * algorithm OBJECT IDENTIFIER, |
| 485 | * parameters ANY DEFINED BY algorithm OPTIONAL } |
| 486 | * ``` |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 487 | * |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 488 | * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), |
| 489 | * the `subjectPublicKey` format is defined by RFC 3279 §2.3.1 as |
| 490 | * `RSAPublicKey`, |
| 491 | * with the OID `rsaEncryption`, |
| 492 | * and with the parameters `NULL`. |
| 493 | * ``` |
| 494 | * pkcs-1 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840) |
| 495 | * rsadsi(113549) pkcs(1) 1 } |
| 496 | * rsaEncryption OBJECT IDENTIFIER ::= { pkcs-1 1 } |
| 497 | * |
| 498 | * RSAPublicKey ::= SEQUENCE { |
| 499 | * modulus INTEGER, -- n |
| 500 | * publicExponent INTEGER } -- e |
| 501 | * ``` |
| 502 | * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), |
| 503 | * the `subjectPublicKey` format is defined by RFC 3279 §2.3.2 as |
| 504 | * `DSAPublicKey`, |
| 505 | * with the OID `id-dsa`, |
| 506 | * and with the parameters `DSS-Parms`. |
| 507 | * ``` |
| 508 | * id-dsa OBJECT IDENTIFIER ::= { |
| 509 | * iso(1) member-body(2) us(840) x9-57(10040) x9cm(4) 1 } |
| 510 | * |
| 511 | * Dss-Parms ::= SEQUENCE { |
| 512 | * p INTEGER, |
| 513 | * q INTEGER, |
| 514 | * g INTEGER } |
| 515 | * DSAPublicKey ::= INTEGER -- public key, Y |
| 516 | * ``` |
| 517 | * - For elliptic curve public keys (key types for which |
| 518 | * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), |
| 519 | * the `subjectPublicKey` format is defined by RFC 3279 §2.3.5 as |
Gilles Peskine | 4f6c77b | 2018-08-11 01:17:53 +0200 | [diff] [blame] | 520 | * `ECPoint`, which contains the uncompressed |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 521 | * representation defined by SEC1 §2.3.3. |
| 522 | * The OID is `id-ecPublicKey`, |
Gilles Peskine | 4f6c77b | 2018-08-11 01:17:53 +0200 | [diff] [blame] | 523 | * and the parameters must be given as a `namedCurve` OID as specified in |
Gilles Peskine | c6290c0 | 2018-08-13 17:24:59 +0200 | [diff] [blame] | 524 | * RFC 5480 §2.1.1.1 or other applicable standards. |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 525 | * ``` |
| 526 | * ansi-X9-62 OBJECT IDENTIFIER ::= |
| 527 | * { iso(1) member-body(2) us(840) 10045 } |
| 528 | * id-public-key-type OBJECT IDENTIFIER ::= { ansi-X9.62 2 } |
| 529 | * id-ecPublicKey OBJECT IDENTIFIER ::= { id-publicKeyType 1 } |
| 530 | * |
Gilles Peskine | 4f6c77b | 2018-08-11 01:17:53 +0200 | [diff] [blame] | 531 | * ECPoint ::= ... |
| 532 | * -- first 8 bits: 0x04; |
Gilles Peskine | 6c6a023 | 2018-11-15 17:44:43 +0100 | [diff] [blame] | 533 | * -- then x_P as a `ceiling(m/8)`-byte string, big endian; |
| 534 | * -- then y_P as a `ceiling(m/8)`-byte string, big endian; |
| 535 | * -- where `m` is the bit size associated with the curve, |
Gilles Peskine | 7b5b4a0 | 2018-11-14 21:05:10 +0100 | [diff] [blame] | 536 | * -- i.e. the bit size of `q` for a curve over `F_q`. |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 537 | * |
| 538 | * EcpkParameters ::= CHOICE { -- other choices are not allowed |
| 539 | * namedCurve OBJECT IDENTIFIER } |
| 540 | * ``` |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 541 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 542 | * \param handle Handle to the key to export. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 543 | * \param[out] data Buffer where the key data is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 544 | * \param data_size Size of the \p data buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 545 | * \param[out] data_length On success, the number of bytes |
| 546 | * that make up the key data. |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 547 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 548 | * \retval #PSA_SUCCESS |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 549 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 550 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 551 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 552 | * The key is neither a public key nor a key pair. |
| 553 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 554 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 555 | * The size of the \p data buffer is too small. You can determine a |
| 556 | * sufficient buffer size by calling |
| 557 | * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits) |
| 558 | * where \c type is the key type |
| 559 | * and \c bits is the key size in bits. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 560 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 561 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 562 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 563 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 564 | * The library has not been previously initialized by psa_crypto_init(). |
| 565 | * It is implementation-dependent whether a failure to initialize |
| 566 | * results in this error code. |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 567 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 568 | psa_status_t psa_export_public_key(psa_key_handle_t handle, |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 569 | uint8_t *data, |
| 570 | size_t data_size, |
| 571 | size_t *data_length); |
| 572 | |
| 573 | /**@}*/ |
| 574 | |
| 575 | /** \defgroup policy Key policies |
| 576 | * @{ |
| 577 | */ |
| 578 | |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 579 | /** The type of the key policy data structure. |
| 580 | * |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 581 | * Before calling any function on a key policy, the application must initialize |
| 582 | * it by any of the following means: |
| 583 | * - Set the structure to all-bits-zero, for example: |
| 584 | * \code |
| 585 | * psa_key_policy_t policy; |
| 586 | * memset(&policy, 0, sizeof(policy)); |
| 587 | * \endcode |
| 588 | * - Initialize the structure to logical zero values, for example: |
| 589 | * \code |
| 590 | * psa_key_policy_t policy = {0}; |
| 591 | * \endcode |
| 592 | * - Initialize the structure to the initializer #PSA_KEY_POLICY_INIT, |
| 593 | * for example: |
| 594 | * \code |
| 595 | * psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
| 596 | * \endcode |
| 597 | * - Assign the result of the function psa_key_policy_init() |
| 598 | * to the structure, for example: |
| 599 | * \code |
| 600 | * psa_key_policy_t policy; |
| 601 | * policy = psa_key_policy_init(); |
| 602 | * \endcode |
| 603 | * |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 604 | * This is an implementation-defined \c struct. Applications should not |
| 605 | * make any assumptions about the content of this structure except |
| 606 | * as directed by the documentation of a specific implementation. */ |
| 607 | typedef struct psa_key_policy_s psa_key_policy_t; |
| 608 | |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 609 | /** \def PSA_KEY_POLICY_INIT |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 610 | * |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 611 | * This macro returns a suitable initializer for a key policy object of type |
| 612 | * #psa_key_policy_t. |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 613 | */ |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 614 | #ifdef __DOXYGEN_ONLY__ |
| 615 | /* This is an example definition for documentation purposes. |
| 616 | * Implementations should define a suitable value in `crypto_struct.h`. |
| 617 | */ |
| 618 | #define PSA_KEY_POLICY_INIT {0} |
| 619 | #endif |
| 620 | |
| 621 | /** Return an initial value for a key policy that forbids all usage of the key. |
| 622 | */ |
| 623 | static psa_key_policy_t psa_key_policy_init(void); |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 624 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 625 | /** \brief Set the standard fields of a policy structure. |
| 626 | * |
| 627 | * Note that this function does not make any consistency check of the |
| 628 | * parameters. The values are only checked when applying the policy to |
| 629 | * a key slot with psa_set_key_policy(). |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 630 | * |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 631 | * \param[in,out] policy The key policy to modify. It must have been |
| 632 | * initialized as per the documentation for |
| 633 | * #psa_key_policy_t. |
| 634 | * \param usage The permitted uses for the key. |
| 635 | * \param alg The algorithm that the key may be used for. |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 636 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 637 | void psa_key_policy_set_usage(psa_key_policy_t *policy, |
| 638 | psa_key_usage_t usage, |
| 639 | psa_algorithm_t alg); |
| 640 | |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 641 | /** \brief Retrieve the usage field of a policy structure. |
| 642 | * |
| 643 | * \param[in] policy The policy object to query. |
| 644 | * |
| 645 | * \return The permitted uses for a key with this policy. |
| 646 | */ |
Gilles Peskine | aa7bc47 | 2018-07-12 00:54:56 +0200 | [diff] [blame] | 647 | psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy); |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 648 | |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 649 | /** \brief Retrieve the algorithm field of a policy structure. |
| 650 | * |
| 651 | * \param[in] policy The policy object to query. |
| 652 | * |
| 653 | * \return The permitted algorithm for a key with this policy. |
| 654 | */ |
Gilles Peskine | aa7bc47 | 2018-07-12 00:54:56 +0200 | [diff] [blame] | 655 | psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy); |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 656 | |
| 657 | /** \brief Set the usage policy on a key slot. |
| 658 | * |
| 659 | * This function must be called on an empty key slot, before importing, |
| 660 | * generating or creating a key in the slot. Changing the policy of an |
| 661 | * existing key is not permitted. |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 662 | * |
| 663 | * Implementations may set restrictions on supported key policies |
| 664 | * depending on the key type and the key slot. |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 665 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 666 | * \param handle Handle to the key whose policy is to be changed. |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 667 | * \param[in] policy The policy object to query. |
| 668 | * |
| 669 | * \retval #PSA_SUCCESS |
Gilles Peskine | 23fd2bd | 2018-12-11 15:51:32 +0100 | [diff] [blame] | 670 | * Success. |
| 671 | * If the key is persistent, it is implementation-defined whether |
| 672 | * the policy has been saved to persistent storage. Implementations |
| 673 | * may defer saving the policy until the key material is created. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 674 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 675 | * \retval #PSA_ERROR_OCCUPIED_SLOT |
| 676 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 677 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 678 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 679 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 680 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 681 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 682 | * The library has not been previously initialized by psa_crypto_init(). |
| 683 | * It is implementation-dependent whether a failure to initialize |
| 684 | * results in this error code. |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 685 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 686 | psa_status_t psa_set_key_policy(psa_key_handle_t handle, |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 687 | const psa_key_policy_t *policy); |
| 688 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 689 | /** \brief Get the usage policy for a key slot. |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 690 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 691 | * \param handle Handle to the key slot whose policy is being queried. |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 692 | * \param[out] policy On success, the key's policy. |
| 693 | * |
| 694 | * \retval #PSA_SUCCESS |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 695 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 696 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 697 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 698 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 699 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 700 | * The library has not been previously initialized by psa_crypto_init(). |
| 701 | * It is implementation-dependent whether a failure to initialize |
| 702 | * results in this error code. |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 703 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 704 | psa_status_t psa_get_key_policy(psa_key_handle_t handle, |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 705 | psa_key_policy_t *policy); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 706 | |
| 707 | /**@}*/ |
| 708 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 709 | /** \defgroup hash Message digests |
| 710 | * @{ |
| 711 | */ |
| 712 | |
Gilles Peskine | 69647a4 | 2019-01-14 20:18:12 +0100 | [diff] [blame] | 713 | /** Calculate the hash (digest) of a message. |
| 714 | * |
| 715 | * \note To verify the hash of a message against an |
| 716 | * expected value, use psa_hash_compare() instead. |
| 717 | * |
| 718 | * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value |
| 719 | * such that #PSA_ALG_IS_HASH(\p alg) is true). |
| 720 | * \param[in] input Buffer containing the message to hash. |
| 721 | * \param input_length Size of the \p input buffer in bytes. |
| 722 | * \param[out] hash Buffer where the hash is to be written. |
| 723 | * \param hash_size Size of the \p hash buffer in bytes. |
| 724 | * \param[out] hash_length On success, the number of bytes |
| 725 | * that make up the hash value. This is always |
| 726 | * #PSA_HASH_SIZE(\c alg) where \c alg is the |
| 727 | * hash algorithm that is calculated. |
| 728 | * |
| 729 | * \retval #PSA_SUCCESS |
| 730 | * Success. |
| 731 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 732 | * \p alg is not supported or is not a hash algorithm. |
| 733 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 734 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 735 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 736 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 737 | */ |
| 738 | psa_status_t psa_hash_compute(psa_algorithm_t alg, |
| 739 | const uint8_t *input, |
| 740 | size_t input_length, |
| 741 | uint8_t *hash, |
| 742 | size_t hash_size, |
| 743 | size_t *hash_length); |
| 744 | |
| 745 | /** Calculate the hash (digest) of a message and compare it with a |
| 746 | * reference value. |
| 747 | * |
| 748 | * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value |
| 749 | * such that #PSA_ALG_IS_HASH(\p alg) is true). |
| 750 | * \param[in] input Buffer containing the message to hash. |
| 751 | * \param input_length Size of the \p input buffer in bytes. |
| 752 | * \param[out] hash Buffer containing the expected hash value. |
Gilles Peskine | a05602d | 2019-01-17 15:25:52 +0100 | [diff] [blame^] | 753 | * \param hash_length Size of the \p hash buffer in bytes. |
Gilles Peskine | 69647a4 | 2019-01-14 20:18:12 +0100 | [diff] [blame] | 754 | * |
| 755 | * \retval #PSA_SUCCESS |
| 756 | * The expected hash is identical to the actual hash of the input. |
| 757 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
| 758 | * The hash of the message was calculated successfully, but it |
| 759 | * differs from the expected hash. |
| 760 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 761 | * \p alg is not supported or is not a hash algorithm. |
| 762 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 763 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 764 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 765 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 766 | */ |
| 767 | psa_status_t psa_hash_compare(psa_algorithm_t alg, |
| 768 | const uint8_t *input, |
| 769 | size_t input_length, |
| 770 | const uint8_t *hash, |
| 771 | const size_t hash_length); |
| 772 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 773 | /** The type of the state data structure for multipart hash operations. |
| 774 | * |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 775 | * Before calling any function on a hash operation object, the application must |
| 776 | * initialize it by any of the following means: |
| 777 | * - Set the structure to all-bits-zero, for example: |
| 778 | * \code |
| 779 | * psa_hash_operation_t operation; |
| 780 | * memset(&operation, 0, sizeof(operation)); |
| 781 | * \endcode |
| 782 | * - Initialize the structure to logical zero values, for example: |
| 783 | * \code |
| 784 | * psa_hash_operation_t operation = {0}; |
| 785 | * \endcode |
| 786 | * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT, |
| 787 | * for example: |
| 788 | * \code |
| 789 | * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 790 | * \endcode |
| 791 | * - Assign the result of the function psa_hash_operation_init() |
| 792 | * to the structure, for example: |
| 793 | * \code |
| 794 | * psa_hash_operation_t operation; |
| 795 | * operation = psa_hash_operation_init(); |
| 796 | * \endcode |
| 797 | * |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 798 | * This is an implementation-defined \c struct. Applications should not |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 799 | * make any assumptions about the content of this structure except |
| 800 | * as directed by the documentation of a specific implementation. */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 801 | typedef struct psa_hash_operation_s psa_hash_operation_t; |
| 802 | |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 803 | /** \def PSA_HASH_OPERATION_INIT |
| 804 | * |
| 805 | * This macro returns a suitable initializer for a hash operation object |
| 806 | * of type #psa_hash_operation_t. |
| 807 | */ |
| 808 | #ifdef __DOXYGEN_ONLY__ |
| 809 | /* This is an example definition for documentation purposes. |
| 810 | * Implementations should define a suitable value in `crypto_struct.h`. |
| 811 | */ |
| 812 | #define PSA_HASH_OPERATION_INIT {0} |
| 813 | #endif |
| 814 | |
| 815 | /** Return an initial value for a hash operation object. |
| 816 | */ |
| 817 | static psa_hash_operation_t psa_hash_operation_init(void); |
| 818 | |
Gilles Peskine | f45adda | 2019-01-14 18:29:18 +0100 | [diff] [blame] | 819 | /** Set up a multipart hash operation. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 820 | * |
| 821 | * The sequence of operations to calculate a hash (message digest) |
| 822 | * is as follows: |
| 823 | * -# Allocate an operation object which will be passed to all the functions |
| 824 | * listed here. |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 825 | * -# Initialize the operation object with one of the methods described in the |
| 826 | * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT. |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 827 | * -# Call psa_hash_setup() to specify the algorithm. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 828 | * -# Call psa_hash_update() zero, one or more times, passing a fragment |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 829 | * of the message each time. The hash that is calculated is the hash |
| 830 | * of the concatenation of these messages in order. |
| 831 | * -# To calculate the hash, call psa_hash_finish(). |
| 832 | * To compare the hash with an expected value, call psa_hash_verify(). |
| 833 | * |
| 834 | * The application may call psa_hash_abort() at any time after the operation |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 835 | * has been initialized. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 836 | * |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 837 | * After a successful call to psa_hash_setup(), the application must |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame] | 838 | * eventually terminate the operation. The following events terminate an |
| 839 | * operation: |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 840 | * - A failed call to psa_hash_update(). |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 841 | * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort(). |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 842 | * |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 843 | * \param[in,out] operation The operation object to set up. It must have |
| 844 | * been initialized as per the documentation for |
| 845 | * #psa_hash_operation_t and not yet in use. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 846 | * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value |
| 847 | * such that #PSA_ALG_IS_HASH(\p alg) is true). |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 848 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 849 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 850 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 851 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 852 | * \p alg is not supported or is not a hash algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 853 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 854 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 855 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 856 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 857 | */ |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 858 | psa_status_t psa_hash_setup(psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 859 | psa_algorithm_t alg); |
| 860 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 861 | /** Add a message fragment to a multipart hash operation. |
| 862 | * |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 863 | * The application must call psa_hash_setup() before calling this function. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 864 | * |
| 865 | * If this function returns an error status, the operation becomes inactive. |
| 866 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 867 | * \param[in,out] operation Active hash operation. |
| 868 | * \param[in] input Buffer containing the message fragment to hash. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 869 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 870 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 871 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 872 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 873 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | f45adda | 2019-01-14 18:29:18 +0100 | [diff] [blame] | 874 | * The operation state is not valid (not set up, or already completed). |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 875 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 876 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 877 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 878 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 879 | */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 880 | psa_status_t psa_hash_update(psa_hash_operation_t *operation, |
| 881 | const uint8_t *input, |
| 882 | size_t input_length); |
| 883 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 884 | /** Finish the calculation of the hash of a message. |
| 885 | * |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 886 | * The application must call psa_hash_setup() before calling this function. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 887 | * This function calculates the hash of the message formed by concatenating |
| 888 | * the inputs passed to preceding calls to psa_hash_update(). |
| 889 | * |
| 890 | * When this function returns, the operation becomes inactive. |
| 891 | * |
| 892 | * \warning Applications should not call this function if they expect |
| 893 | * a specific value for the hash. Call psa_hash_verify() instead. |
| 894 | * Beware that comparing integrity or authenticity data such as |
| 895 | * hash values with a function such as \c memcmp is risky |
| 896 | * because the time taken by the comparison may leak information |
| 897 | * about the hashed data which could allow an attacker to guess |
| 898 | * a valid hash and thereby bypass security controls. |
| 899 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 900 | * \param[in,out] operation Active hash operation. |
| 901 | * \param[out] hash Buffer where the hash is to be written. |
| 902 | * \param hash_size Size of the \p hash buffer in bytes. |
| 903 | * \param[out] hash_length On success, the number of bytes |
| 904 | * that make up the hash value. This is always |
Gilles Peskine | be42f31 | 2018-07-13 14:38:15 +0200 | [diff] [blame] | 905 | * #PSA_HASH_SIZE(\c alg) where \c alg is the |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 906 | * hash algorithm that is calculated. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 907 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 908 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 909 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 910 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | f45adda | 2019-01-14 18:29:18 +0100 | [diff] [blame] | 911 | * The operation state is not valid (not set up, or already completed). |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 912 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 913 | * The size of the \p hash buffer is too small. You can determine a |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 914 | * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg) |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 915 | * where \c alg is the hash algorithm that is calculated. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 916 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 917 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 918 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 919 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 920 | */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 921 | psa_status_t psa_hash_finish(psa_hash_operation_t *operation, |
| 922 | uint8_t *hash, |
| 923 | size_t hash_size, |
| 924 | size_t *hash_length); |
| 925 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 926 | /** Finish the calculation of the hash of a message and compare it with |
| 927 | * an expected value. |
| 928 | * |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 929 | * The application must call psa_hash_setup() before calling this function. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 930 | * This function calculates the hash of the message formed by concatenating |
| 931 | * the inputs passed to preceding calls to psa_hash_update(). It then |
| 932 | * compares the calculated hash with the expected hash passed as a |
| 933 | * parameter to this function. |
| 934 | * |
| 935 | * When this function returns, the operation becomes inactive. |
| 936 | * |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 937 | * \note Implementations shall make the best effort to ensure that the |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 938 | * comparison between the actual hash and the expected hash is performed |
| 939 | * in constant time. |
| 940 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 941 | * \param[in,out] operation Active hash operation. |
| 942 | * \param[in] hash Buffer containing the expected hash value. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 943 | * \param hash_length Size of the \p hash buffer in bytes. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 944 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 945 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 946 | * The expected hash is identical to the actual hash of the message. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 947 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 948 | * The hash of the message was calculated successfully, but it |
| 949 | * differs from the expected hash. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 950 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | f45adda | 2019-01-14 18:29:18 +0100 | [diff] [blame] | 951 | * The operation state is not valid (not set up, or already completed). |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 952 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 953 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 954 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 955 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 956 | */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 957 | psa_status_t psa_hash_verify(psa_hash_operation_t *operation, |
| 958 | const uint8_t *hash, |
| 959 | size_t hash_length); |
| 960 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 961 | /** Abort a hash operation. |
| 962 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 963 | * Aborting an operation frees all associated resources except for the |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 964 | * \p operation structure itself. Once aborted, the operation object |
| 965 | * can be reused for another operation by calling |
| 966 | * psa_hash_setup() again. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 967 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 968 | * You may call this function any time after the operation object has |
| 969 | * been initialized by any of the following methods: |
| 970 | * - A call to psa_hash_setup(), whether it succeeds or not. |
| 971 | * - Initializing the \c struct to all-bits-zero. |
| 972 | * - Initializing the \c struct to logical zeros, e.g. |
| 973 | * `psa_hash_operation_t operation = {0}`. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 974 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 975 | * In particular, calling psa_hash_abort() after the operation has been |
| 976 | * terminated by a call to psa_hash_abort(), psa_hash_finish() or |
| 977 | * psa_hash_verify() is safe and has no effect. |
| 978 | * |
| 979 | * \param[in,out] operation Initialized hash operation. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 980 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 981 | * \retval #PSA_SUCCESS |
| 982 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 983 | * \p operation is not an active hash operation. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 984 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 985 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 986 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 987 | */ |
| 988 | psa_status_t psa_hash_abort(psa_hash_operation_t *operation); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 989 | |
| 990 | /**@}*/ |
| 991 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 992 | /** \defgroup MAC Message authentication codes |
| 993 | * @{ |
| 994 | */ |
| 995 | |
Gilles Peskine | 69647a4 | 2019-01-14 20:18:12 +0100 | [diff] [blame] | 996 | /** Calculate the MAC (message authentication code) of a message. |
| 997 | * |
| 998 | * \note To verify the MAC of a message against an |
| 999 | * expected value, use psa_mac_verify() instead. |
| 1000 | * Beware that comparing integrity or authenticity data such as |
| 1001 | * MAC values with a function such as \c memcmp is risky |
| 1002 | * because the time taken by the comparison may leak information |
| 1003 | * about the MAC value which could allow an attacker to guess |
| 1004 | * a valid MAC and thereby bypass security controls. |
| 1005 | * |
| 1006 | * \param handle Handle to the key to use for the operation. |
| 1007 | * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value |
| 1008 | * such that #PSA_ALG_IS_MAC(alg) is true). |
| 1009 | * \param[in] input Buffer containing the input message. |
| 1010 | * \param input_length Size of the \p input buffer in bytes. |
| 1011 | * \param[out] mac Buffer where the MAC value is to be written. |
| 1012 | * \param mac_size Size of the \p mac buffer in bytes. |
| 1013 | * \param[out] mac_length On success, the number of bytes |
| 1014 | * that make up the mac value. This is always |
| 1015 | * #PSA_HASH_SIZE(\c alg) where \c alg is the |
| 1016 | * hash algorithm that is calculated. |
| 1017 | * |
| 1018 | * \retval #PSA_SUCCESS |
| 1019 | * Success. |
| 1020 | * \retval #PSA_ERROR_INVALID_HANDLE |
| 1021 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1022 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1023 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1024 | * \p key is not compatible with \p alg. |
| 1025 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1026 | * \p alg is not supported or is not a MAC algorithm. |
| 1027 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1028 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1029 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1030 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1031 | * \retval #PSA_ERROR_BAD_STATE |
| 1032 | * The library has not been previously initialized by psa_crypto_init(). |
| 1033 | * It is implementation-dependent whether a failure to initialize |
| 1034 | * results in this error code. |
| 1035 | */ |
| 1036 | psa_status_t psa_mac_compute(psa_key_handle_t handle, |
| 1037 | psa_algorithm_t alg, |
| 1038 | const uint8_t *input, |
| 1039 | size_t input_length, |
| 1040 | uint8_t *mac, |
| 1041 | size_t mac_size, |
| 1042 | size_t *mac_length); |
| 1043 | |
| 1044 | /** Calculate the MAC of a message and compare it with a reference value. |
| 1045 | * |
| 1046 | * \param handle Handle to the key to use for the operation. |
| 1047 | * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value |
| 1048 | * such that #PSA_ALG_IS_MAC(alg) is true). |
| 1049 | * \param[in] input Buffer containing the input message. |
| 1050 | * \param input_length Size of the \p input buffer in bytes. |
| 1051 | * \param[out] mac Buffer containing the expected MAC value. |
| 1052 | * \param mac_length Size of the \p mac buffer in bytes. |
| 1053 | * |
| 1054 | * \retval #PSA_SUCCESS |
| 1055 | * The expected MAC is identical to the actual MAC of the input. |
| 1056 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
| 1057 | * The MAC of the message was calculated successfully, but it |
| 1058 | * differs from the expected value. |
| 1059 | * \retval #PSA_ERROR_INVALID_HANDLE |
| 1060 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1061 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1062 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1063 | * \p key is not compatible with \p alg. |
| 1064 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1065 | * \p alg is not supported or is not a MAC algorithm. |
| 1066 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1067 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1068 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1069 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1070 | */ |
Gilles Peskine | a05602d | 2019-01-17 15:25:52 +0100 | [diff] [blame^] | 1071 | psa_status_t psa_mac_verify(psa_key_handle_t handle, |
| 1072 | psa_algorithm_t alg, |
Gilles Peskine | 69647a4 | 2019-01-14 20:18:12 +0100 | [diff] [blame] | 1073 | const uint8_t *input, |
| 1074 | size_t input_length, |
| 1075 | const uint8_t *mac, |
| 1076 | const size_t mac_length); |
| 1077 | |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1078 | /** The type of the state data structure for multipart MAC operations. |
| 1079 | * |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1080 | * Before calling any function on a MAC operation object, the application must |
| 1081 | * initialize it by any of the following means: |
| 1082 | * - Set the structure to all-bits-zero, for example: |
| 1083 | * \code |
| 1084 | * psa_mac_operation_t operation; |
| 1085 | * memset(&operation, 0, sizeof(operation)); |
| 1086 | * \endcode |
| 1087 | * - Initialize the structure to logical zero values, for example: |
| 1088 | * \code |
| 1089 | * psa_mac_operation_t operation = {0}; |
| 1090 | * \endcode |
| 1091 | * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT, |
| 1092 | * for example: |
| 1093 | * \code |
| 1094 | * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 1095 | * \endcode |
| 1096 | * - Assign the result of the function psa_mac_operation_init() |
| 1097 | * to the structure, for example: |
| 1098 | * \code |
| 1099 | * psa_mac_operation_t operation; |
| 1100 | * operation = psa_mac_operation_init(); |
| 1101 | * \endcode |
| 1102 | * |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 1103 | * This is an implementation-defined \c struct. Applications should not |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1104 | * make any assumptions about the content of this structure except |
| 1105 | * as directed by the documentation of a specific implementation. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1106 | typedef struct psa_mac_operation_s psa_mac_operation_t; |
| 1107 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1108 | /** \def PSA_MAC_OPERATION_INIT |
| 1109 | * |
| 1110 | * This macro returns a suitable initializer for a MAC operation object of type |
| 1111 | * #psa_mac_operation_t. |
| 1112 | */ |
| 1113 | #ifdef __DOXYGEN_ONLY__ |
| 1114 | /* This is an example definition for documentation purposes. |
| 1115 | * Implementations should define a suitable value in `crypto_struct.h`. |
| 1116 | */ |
| 1117 | #define PSA_MAC_OPERATION_INIT {0} |
| 1118 | #endif |
| 1119 | |
| 1120 | /** Return an initial value for a MAC operation object. |
| 1121 | */ |
| 1122 | static psa_mac_operation_t psa_mac_operation_init(void); |
| 1123 | |
Gilles Peskine | f45adda | 2019-01-14 18:29:18 +0100 | [diff] [blame] | 1124 | /** Set up a multipart MAC calculation operation. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1125 | * |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1126 | * This function sets up the calculation of the MAC |
| 1127 | * (message authentication code) of a byte string. |
| 1128 | * To verify the MAC of a message against an |
| 1129 | * expected value, use psa_mac_verify_setup() instead. |
| 1130 | * |
| 1131 | * The sequence of operations to calculate a MAC is as follows: |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1132 | * -# Allocate an operation object which will be passed to all the functions |
| 1133 | * listed here. |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1134 | * -# Initialize the operation object with one of the methods described in the |
| 1135 | * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT. |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1136 | * -# Call psa_mac_sign_setup() to specify the algorithm and key. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1137 | * -# Call psa_mac_update() zero, one or more times, passing a fragment |
| 1138 | * of the message each time. The MAC that is calculated is the MAC |
| 1139 | * of the concatenation of these messages in order. |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1140 | * -# At the end of the message, call psa_mac_sign_finish() to finish |
| 1141 | * calculating the MAC value and retrieve it. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1142 | * |
| 1143 | * The application may call psa_mac_abort() at any time after the operation |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1144 | * has been initialized. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1145 | * |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1146 | * After a successful call to psa_mac_sign_setup(), the application must |
| 1147 | * eventually terminate the operation through one of the following methods: |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1148 | * - A failed call to psa_mac_update(). |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1149 | * - A call to psa_mac_sign_finish() or psa_mac_abort(). |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1150 | * |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1151 | * \param[in,out] operation The operation object to set up. It must have |
| 1152 | * been initialized as per the documentation for |
| 1153 | * #psa_mac_operation_t and not yet in use. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1154 | * \param handle Handle to the key to use for the operation. |
Gilles Peskine | 5f25dd0 | 2019-01-14 18:24:53 +0100 | [diff] [blame] | 1155 | * It must remain valid until the operation |
| 1156 | * terminates. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1157 | * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value |
| 1158 | * such that #PSA_ALG_IS_MAC(alg) is true). |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1159 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1160 | * \retval #PSA_SUCCESS |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1161 | * Success. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1162 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1163 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1164 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1165 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1166 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1167 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1168 | * \p alg is not supported or is not a MAC algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1169 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1170 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1171 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1172 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1173 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1174 | * The library has not been previously initialized by psa_crypto_init(). |
| 1175 | * It is implementation-dependent whether a failure to initialize |
| 1176 | * results in this error code. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1177 | */ |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1178 | psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation, |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1179 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1180 | psa_algorithm_t alg); |
| 1181 | |
Gilles Peskine | f45adda | 2019-01-14 18:29:18 +0100 | [diff] [blame] | 1182 | /** Set up a multipart MAC verification operation. |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1183 | * |
| 1184 | * This function sets up the verification of the MAC |
| 1185 | * (message authentication code) of a byte string against an expected value. |
| 1186 | * |
| 1187 | * The sequence of operations to verify a MAC is as follows: |
| 1188 | * -# Allocate an operation object which will be passed to all the functions |
| 1189 | * listed here. |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1190 | * -# Initialize the operation object with one of the methods described in the |
| 1191 | * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT. |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1192 | * -# Call psa_mac_verify_setup() to specify the algorithm and key. |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1193 | * -# Call psa_mac_update() zero, one or more times, passing a fragment |
| 1194 | * of the message each time. The MAC that is calculated is the MAC |
| 1195 | * of the concatenation of these messages in order. |
| 1196 | * -# At the end of the message, call psa_mac_verify_finish() to finish |
| 1197 | * calculating the actual MAC of the message and verify it against |
| 1198 | * the expected value. |
| 1199 | * |
| 1200 | * The application may call psa_mac_abort() at any time after the operation |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1201 | * has been initialized. |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1202 | * |
| 1203 | * After a successful call to psa_mac_verify_setup(), the application must |
| 1204 | * eventually terminate the operation through one of the following methods: |
| 1205 | * - A failed call to psa_mac_update(). |
| 1206 | * - A call to psa_mac_verify_finish() or psa_mac_abort(). |
| 1207 | * |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1208 | * \param[in,out] operation The operation object to set up. It must have |
| 1209 | * been initialized as per the documentation for |
| 1210 | * #psa_mac_operation_t and not yet in use. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1211 | * \param handle Handle to the key to use for the operation. |
Gilles Peskine | 5f25dd0 | 2019-01-14 18:24:53 +0100 | [diff] [blame] | 1212 | * It must remain valid until the operation |
| 1213 | * terminates. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1214 | * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value |
| 1215 | * such that #PSA_ALG_IS_MAC(\p alg) is true). |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1216 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1217 | * \retval #PSA_SUCCESS |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1218 | * Success. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1219 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1220 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1221 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1222 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1223 | * \c key is not compatible with \c alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1224 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1225 | * \c alg is not supported or is not a MAC algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1226 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1227 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1228 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1229 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1230 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1231 | * The library has not been previously initialized by psa_crypto_init(). |
| 1232 | * It is implementation-dependent whether a failure to initialize |
| 1233 | * results in this error code. |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1234 | */ |
| 1235 | psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation, |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1236 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1237 | psa_algorithm_t alg); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1238 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1239 | /** Add a message fragment to a multipart MAC operation. |
| 1240 | * |
| 1241 | * The application must call psa_mac_sign_setup() or psa_mac_verify_setup() |
| 1242 | * before calling this function. |
| 1243 | * |
| 1244 | * If this function returns an error status, the operation becomes inactive. |
| 1245 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1246 | * \param[in,out] operation Active MAC operation. |
| 1247 | * \param[in] input Buffer containing the message fragment to add to |
| 1248 | * the MAC calculation. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1249 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1250 | * |
| 1251 | * \retval #PSA_SUCCESS |
| 1252 | * Success. |
| 1253 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | f45adda | 2019-01-14 18:29:18 +0100 | [diff] [blame] | 1254 | * The operation state is not valid (not set up, or already completed). |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1255 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1256 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1257 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1258 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1259 | */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1260 | psa_status_t psa_mac_update(psa_mac_operation_t *operation, |
| 1261 | const uint8_t *input, |
| 1262 | size_t input_length); |
| 1263 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1264 | /** Finish the calculation of the MAC of a message. |
| 1265 | * |
| 1266 | * The application must call psa_mac_sign_setup() before calling this function. |
| 1267 | * This function calculates the MAC of the message formed by concatenating |
| 1268 | * the inputs passed to preceding calls to psa_mac_update(). |
| 1269 | * |
| 1270 | * When this function returns, the operation becomes inactive. |
| 1271 | * |
| 1272 | * \warning Applications should not call this function if they expect |
| 1273 | * a specific value for the MAC. Call psa_mac_verify_finish() instead. |
| 1274 | * Beware that comparing integrity or authenticity data such as |
| 1275 | * MAC values with a function such as \c memcmp is risky |
| 1276 | * because the time taken by the comparison may leak information |
| 1277 | * about the MAC value which could allow an attacker to guess |
| 1278 | * a valid MAC and thereby bypass security controls. |
| 1279 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1280 | * \param[in,out] operation Active MAC operation. |
| 1281 | * \param[out] mac Buffer where the MAC value is to be written. |
| 1282 | * \param mac_size Size of the \p mac buffer in bytes. |
| 1283 | * \param[out] mac_length On success, the number of bytes |
| 1284 | * that make up the MAC value. This is always |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 1285 | * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg) |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1286 | * where \c key_type and \c key_bits are the type and |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 1287 | * bit-size respectively of the key and \c alg is the |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1288 | * MAC algorithm that is calculated. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1289 | * |
| 1290 | * \retval #PSA_SUCCESS |
| 1291 | * Success. |
| 1292 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | f45adda | 2019-01-14 18:29:18 +0100 | [diff] [blame] | 1293 | * The operation state is not valid (not set up, or already completed). |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1294 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1295 | * The size of the \p mac buffer is too small. You can determine a |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1296 | * sufficient buffer size by calling PSA_MAC_FINAL_SIZE(). |
| 1297 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1298 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1299 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1300 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1301 | */ |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 1302 | psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation, |
| 1303 | uint8_t *mac, |
| 1304 | size_t mac_size, |
| 1305 | size_t *mac_length); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1306 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1307 | /** Finish the calculation of the MAC of a message and compare it with |
| 1308 | * an expected value. |
| 1309 | * |
| 1310 | * The application must call psa_mac_verify_setup() before calling this function. |
| 1311 | * This function calculates the MAC of the message formed by concatenating |
| 1312 | * the inputs passed to preceding calls to psa_mac_update(). It then |
| 1313 | * compares the calculated MAC with the expected MAC passed as a |
| 1314 | * parameter to this function. |
| 1315 | * |
| 1316 | * When this function returns, the operation becomes inactive. |
| 1317 | * |
| 1318 | * \note Implementations shall make the best effort to ensure that the |
| 1319 | * comparison between the actual MAC and the expected MAC is performed |
| 1320 | * in constant time. |
| 1321 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1322 | * \param[in,out] operation Active MAC operation. |
| 1323 | * \param[in] mac Buffer containing the expected MAC value. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1324 | * \param mac_length Size of the \p mac buffer in bytes. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1325 | * |
| 1326 | * \retval #PSA_SUCCESS |
| 1327 | * The expected MAC is identical to the actual MAC of the message. |
| 1328 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
| 1329 | * The MAC of the message was calculated successfully, but it |
| 1330 | * differs from the expected MAC. |
| 1331 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | f45adda | 2019-01-14 18:29:18 +0100 | [diff] [blame] | 1332 | * The operation state is not valid (not set up, or already completed). |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1333 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1334 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1335 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1336 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1337 | */ |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 1338 | psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation, |
| 1339 | const uint8_t *mac, |
| 1340 | size_t mac_length); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1341 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1342 | /** Abort a MAC operation. |
| 1343 | * |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1344 | * Aborting an operation frees all associated resources except for the |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 1345 | * \p operation structure itself. Once aborted, the operation object |
| 1346 | * can be reused for another operation by calling |
| 1347 | * psa_mac_sign_setup() or psa_mac_verify_setup() again. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1348 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 1349 | * You may call this function any time after the operation object has |
| 1350 | * been initialized by any of the following methods: |
| 1351 | * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether |
| 1352 | * it succeeds or not. |
| 1353 | * - Initializing the \c struct to all-bits-zero. |
| 1354 | * - Initializing the \c struct to logical zeros, e.g. |
| 1355 | * `psa_mac_operation_t operation = {0}`. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1356 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 1357 | * In particular, calling psa_mac_abort() after the operation has been |
| 1358 | * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or |
| 1359 | * psa_mac_verify_finish() is safe and has no effect. |
| 1360 | * |
| 1361 | * \param[in,out] operation Initialized MAC operation. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1362 | * |
| 1363 | * \retval #PSA_SUCCESS |
| 1364 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1365 | * \p operation is not an active MAC operation. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1366 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1367 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1368 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1369 | */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1370 | psa_status_t psa_mac_abort(psa_mac_operation_t *operation); |
| 1371 | |
| 1372 | /**@}*/ |
| 1373 | |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1374 | /** \defgroup cipher Symmetric ciphers |
| 1375 | * @{ |
| 1376 | */ |
| 1377 | |
Gilles Peskine | 69647a4 | 2019-01-14 20:18:12 +0100 | [diff] [blame] | 1378 | /** Encrypt a message using a symmetric cipher. |
| 1379 | * |
| 1380 | * This function encrypts a message with a random IV (initialization |
| 1381 | * vector). |
| 1382 | * |
| 1383 | * \param handle Handle to the key to use for the operation. |
| 1384 | * It must remain valid until the operation |
| 1385 | * terminates. |
| 1386 | * \param alg The cipher algorithm to compute |
| 1387 | * (\c PSA_ALG_XXX value such that |
| 1388 | * #PSA_ALG_IS_CIPHER(\p alg) is true). |
| 1389 | * \param[in] input Buffer containing the message to encrypt. |
| 1390 | * \param input_length Size of the \p input buffer in bytes. |
| 1391 | * \param[out] output Buffer where the output is to be written. |
| 1392 | * The output contains the IV followed by |
| 1393 | * the ciphertext proper. |
| 1394 | * \param output_size Size of the \p output buffer in bytes. |
| 1395 | * \param[out] output_length On success, the number of bytes |
| 1396 | * that make up the output. |
| 1397 | * |
| 1398 | * \retval #PSA_SUCCESS |
| 1399 | * Success. |
| 1400 | * \retval #PSA_ERROR_INVALID_HANDLE |
| 1401 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1402 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1403 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1404 | * \p key is not compatible with \p alg. |
| 1405 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1406 | * \p alg is not supported or is not a cipher algorithm. |
| 1407 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 1408 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1409 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1410 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1411 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1412 | */ |
| 1413 | psa_status_t psa_cipher_encrypt(psa_key_handle_t handle, |
| 1414 | psa_algorithm_t alg, |
| 1415 | const uint8_t *input, |
| 1416 | size_t input_length, |
| 1417 | uint8_t *output, |
| 1418 | size_t output_size, |
| 1419 | size_t *output_length); |
| 1420 | |
| 1421 | /** Decrypt a message using a symmetric cipher. |
| 1422 | * |
| 1423 | * This function decrypts a message encrypted with a symmetric cipher. |
| 1424 | * |
| 1425 | * \param handle Handle to the key to use for the operation. |
| 1426 | * It must remain valid until the operation |
| 1427 | * terminates. |
| 1428 | * \param alg The cipher algorithm to compute |
| 1429 | * (\c PSA_ALG_XXX value such that |
| 1430 | * #PSA_ALG_IS_CIPHER(\p alg) is true). |
| 1431 | * \param[in] input Buffer containing the message to decrypt. |
| 1432 | * This consists of the IV followed by the |
| 1433 | * ciphertext proper. |
| 1434 | * \param input_length Size of the \p input buffer in bytes. |
| 1435 | * \param[out] output Buffer where the plaintext is to be written. |
| 1436 | * \param output_size Size of the \p output buffer in bytes. |
| 1437 | * \param[out] output_length On success, the number of bytes |
| 1438 | * that make up the output. |
| 1439 | * |
| 1440 | * \retval #PSA_SUCCESS |
| 1441 | * Success. |
| 1442 | * \retval #PSA_ERROR_INVALID_HANDLE |
| 1443 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1444 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1445 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1446 | * \p key is not compatible with \p alg. |
| 1447 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1448 | * \p alg is not supported or is not a cipher algorithm. |
| 1449 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 1450 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1451 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1452 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1453 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1454 | */ |
| 1455 | psa_status_t psa_cipher_decrypt(psa_key_handle_t handle, |
| 1456 | psa_algorithm_t alg, |
| 1457 | const uint8_t *input, |
| 1458 | size_t input_length, |
| 1459 | uint8_t *output, |
| 1460 | size_t output_size, |
| 1461 | size_t *output_length); |
| 1462 | |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1463 | /** The type of the state data structure for multipart cipher operations. |
| 1464 | * |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1465 | * Before calling any function on a cipher operation object, the application |
| 1466 | * must initialize it by any of the following means: |
| 1467 | * - Set the structure to all-bits-zero, for example: |
| 1468 | * \code |
| 1469 | * psa_cipher_operation_t operation; |
| 1470 | * memset(&operation, 0, sizeof(operation)); |
| 1471 | * \endcode |
| 1472 | * - Initialize the structure to logical zero values, for example: |
| 1473 | * \code |
| 1474 | * psa_cipher_operation_t operation = {0}; |
| 1475 | * \endcode |
| 1476 | * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT, |
| 1477 | * for example: |
| 1478 | * \code |
| 1479 | * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 1480 | * \endcode |
| 1481 | * - Assign the result of the function psa_cipher_operation_init() |
| 1482 | * to the structure, for example: |
| 1483 | * \code |
| 1484 | * psa_cipher_operation_t operation; |
| 1485 | * operation = psa_cipher_operation_init(); |
| 1486 | * \endcode |
| 1487 | * |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1488 | * This is an implementation-defined \c struct. Applications should not |
| 1489 | * make any assumptions about the content of this structure except |
| 1490 | * as directed by the documentation of a specific implementation. */ |
| 1491 | typedef struct psa_cipher_operation_s psa_cipher_operation_t; |
| 1492 | |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1493 | /** \def PSA_CIPHER_OPERATION_INIT |
| 1494 | * |
| 1495 | * This macro returns a suitable initializer for a cipher operation object of |
| 1496 | * type #psa_cipher_operation_t. |
| 1497 | */ |
| 1498 | #ifdef __DOXYGEN_ONLY__ |
| 1499 | /* This is an example definition for documentation purposes. |
| 1500 | * Implementations should define a suitable value in `crypto_struct.h`. |
| 1501 | */ |
| 1502 | #define PSA_CIPHER_OPERATION_INIT {0} |
| 1503 | #endif |
| 1504 | |
| 1505 | /** Return an initial value for a cipher operation object. |
| 1506 | */ |
| 1507 | static psa_cipher_operation_t psa_cipher_operation_init(void); |
| 1508 | |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1509 | /** Set the key for a multipart symmetric encryption operation. |
| 1510 | * |
| 1511 | * The sequence of operations to encrypt a message with a symmetric cipher |
| 1512 | * is as follows: |
| 1513 | * -# Allocate an operation object which will be passed to all the functions |
| 1514 | * listed here. |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1515 | * -# Initialize the operation object with one of the methods described in the |
| 1516 | * documentation for #psa_cipher_operation_t, e.g. |
| 1517 | * PSA_CIPHER_OPERATION_INIT. |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1518 | * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key. |
itayzafrir | ed7382f | 2018-08-02 14:19:33 +0300 | [diff] [blame] | 1519 | * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1520 | * generate or set the IV (initialization vector). You should use |
itayzafrir | ed7382f | 2018-08-02 14:19:33 +0300 | [diff] [blame] | 1521 | * psa_cipher_generate_iv() unless the protocol you are implementing |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1522 | * requires a specific IV value. |
| 1523 | * -# Call psa_cipher_update() zero, one or more times, passing a fragment |
| 1524 | * of the message each time. |
| 1525 | * -# Call psa_cipher_finish(). |
| 1526 | * |
| 1527 | * The application may call psa_cipher_abort() at any time after the operation |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1528 | * has been initialized. |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1529 | * |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1530 | * After a successful call to psa_cipher_encrypt_setup(), the application must |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame] | 1531 | * eventually terminate the operation. The following events terminate an |
| 1532 | * operation: |
Gilles Peskine | f45adda | 2019-01-14 18:29:18 +0100 | [diff] [blame] | 1533 | * - A failed call to any of the \c psa_cipher_xxx functions. |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 1534 | * - A call to psa_cipher_finish() or psa_cipher_abort(). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1535 | * |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1536 | * \param[in,out] operation The operation object to set up. It must have |
| 1537 | * been initialized as per the documentation for |
| 1538 | * #psa_cipher_operation_t and not yet in use. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1539 | * \param handle Handle to the key to use for the operation. |
Gilles Peskine | 5f25dd0 | 2019-01-14 18:24:53 +0100 | [diff] [blame] | 1540 | * It must remain valid until the operation |
| 1541 | * terminates. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1542 | * \param alg The cipher algorithm to compute |
| 1543 | * (\c PSA_ALG_XXX value such that |
| 1544 | * #PSA_ALG_IS_CIPHER(\p alg) is true). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1545 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1546 | * \retval #PSA_SUCCESS |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1547 | * Success. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1548 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1549 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1550 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1551 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1552 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1553 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1554 | * \p alg is not supported or is not a cipher algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1555 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1556 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1557 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1558 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1559 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1560 | * The library has not been previously initialized by psa_crypto_init(). |
| 1561 | * It is implementation-dependent whether a failure to initialize |
| 1562 | * results in this error code. |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1563 | */ |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1564 | psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation, |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1565 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1566 | psa_algorithm_t alg); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1567 | |
| 1568 | /** Set the key for a multipart symmetric decryption operation. |
| 1569 | * |
| 1570 | * The sequence of operations to decrypt a message with a symmetric cipher |
| 1571 | * is as follows: |
| 1572 | * -# Allocate an operation object which will be passed to all the functions |
| 1573 | * listed here. |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1574 | * -# Initialize the operation object with one of the methods described in the |
| 1575 | * documentation for #psa_cipher_operation_t, e.g. |
| 1576 | * PSA_CIPHER_OPERATION_INIT. |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1577 | * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key. |
Gilles Peskine | f45adda | 2019-01-14 18:29:18 +0100 | [diff] [blame] | 1578 | * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1579 | * decryption. If the IV is prepended to the ciphertext, you can call |
| 1580 | * psa_cipher_update() on a buffer containing the IV followed by the |
| 1581 | * beginning of the message. |
| 1582 | * -# Call psa_cipher_update() zero, one or more times, passing a fragment |
| 1583 | * of the message each time. |
| 1584 | * -# Call psa_cipher_finish(). |
| 1585 | * |
| 1586 | * The application may call psa_cipher_abort() at any time after the operation |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1587 | * has been initialized. |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1588 | * |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1589 | * After a successful call to psa_cipher_decrypt_setup(), the application must |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame] | 1590 | * eventually terminate the operation. The following events terminate an |
| 1591 | * operation: |
Gilles Peskine | f45adda | 2019-01-14 18:29:18 +0100 | [diff] [blame] | 1592 | * - A failed call to any of the \c psa_cipher_xxx functions. |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 1593 | * - A call to psa_cipher_finish() or psa_cipher_abort(). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1594 | * |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1595 | * \param[in,out] operation The operation object to set up. It must have |
| 1596 | * been initialized as per the documentation for |
| 1597 | * #psa_cipher_operation_t and not yet in use. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1598 | * \param handle Handle to the key to use for the operation. |
Gilles Peskine | 5f25dd0 | 2019-01-14 18:24:53 +0100 | [diff] [blame] | 1599 | * It must remain valid until the operation |
| 1600 | * terminates. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1601 | * \param alg The cipher algorithm to compute |
| 1602 | * (\c PSA_ALG_XXX value such that |
| 1603 | * #PSA_ALG_IS_CIPHER(\p alg) is true). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1604 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1605 | * \retval #PSA_SUCCESS |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1606 | * Success. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1607 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1608 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1609 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1610 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1611 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1612 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1613 | * \p alg is not supported or is not a cipher algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1614 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1615 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1616 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1617 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1618 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1619 | * The library has not been previously initialized by psa_crypto_init(). |
| 1620 | * It is implementation-dependent whether a failure to initialize |
| 1621 | * results in this error code. |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1622 | */ |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1623 | psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation, |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1624 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1625 | psa_algorithm_t alg); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1626 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1627 | /** Generate an IV for a symmetric encryption operation. |
| 1628 | * |
| 1629 | * This function generates a random IV (initialization vector), nonce |
| 1630 | * or initial counter value for the encryption operation as appropriate |
| 1631 | * for the chosen algorithm, key type and key size. |
| 1632 | * |
| 1633 | * The application must call psa_cipher_encrypt_setup() before |
| 1634 | * calling this function. |
| 1635 | * |
| 1636 | * If this function returns an error status, the operation becomes inactive. |
| 1637 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1638 | * \param[in,out] operation Active cipher operation. |
| 1639 | * \param[out] iv Buffer where the generated IV is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1640 | * \param iv_size Size of the \p iv buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1641 | * \param[out] iv_length On success, the number of bytes of the |
| 1642 | * generated IV. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1643 | * |
| 1644 | * \retval #PSA_SUCCESS |
| 1645 | * Success. |
| 1646 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | f45adda | 2019-01-14 18:29:18 +0100 | [diff] [blame] | 1647 | * The operation state is not valid (not set up, or IV already set). |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1648 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 1649 | * The size of the \p iv buffer is too small. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1650 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1651 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1652 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1653 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1654 | */ |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1655 | psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation, |
| 1656 | unsigned char *iv, |
| 1657 | size_t iv_size, |
| 1658 | size_t *iv_length); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1659 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1660 | /** Set the IV for a symmetric encryption or decryption operation. |
| 1661 | * |
Gilles Peskine | f45adda | 2019-01-14 18:29:18 +0100 | [diff] [blame] | 1662 | * This function sets the IV (initialization vector), nonce |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1663 | * or initial counter value for the encryption or decryption operation. |
| 1664 | * |
| 1665 | * The application must call psa_cipher_encrypt_setup() before |
| 1666 | * calling this function. |
| 1667 | * |
| 1668 | * If this function returns an error status, the operation becomes inactive. |
| 1669 | * |
| 1670 | * \note When encrypting, applications should use psa_cipher_generate_iv() |
| 1671 | * instead of this function, unless implementing a protocol that requires |
| 1672 | * a non-random IV. |
| 1673 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1674 | * \param[in,out] operation Active cipher operation. |
| 1675 | * \param[in] iv Buffer containing the IV to use. |
| 1676 | * \param iv_length Size of the IV in bytes. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1677 | * |
| 1678 | * \retval #PSA_SUCCESS |
| 1679 | * Success. |
| 1680 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | f45adda | 2019-01-14 18:29:18 +0100 | [diff] [blame] | 1681 | * The operation state is not valid (not set up, or IV already set). |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1682 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1683 | * The size of \p iv is not acceptable for the chosen algorithm, |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1684 | * or the chosen algorithm does not use an IV. |
| 1685 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1686 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1687 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1688 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1689 | */ |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1690 | psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation, |
| 1691 | const unsigned char *iv, |
| 1692 | size_t iv_length); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1693 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1694 | /** Encrypt or decrypt a message fragment in an active cipher operation. |
| 1695 | * |
Gilles Peskine | 9ac9426 | 2018-07-12 20:15:32 +0200 | [diff] [blame] | 1696 | * Before calling this function, you must: |
| 1697 | * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(). |
| 1698 | * The choice of setup function determines whether this function |
| 1699 | * encrypts or decrypts its input. |
| 1700 | * 2. If the algorithm requires an IV, call psa_cipher_generate_iv() |
| 1701 | * (recommended when encrypting) or psa_cipher_set_iv(). |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1702 | * |
| 1703 | * If this function returns an error status, the operation becomes inactive. |
| 1704 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1705 | * \param[in,out] operation Active cipher operation. |
| 1706 | * \param[in] input Buffer containing the message fragment to |
| 1707 | * encrypt or decrypt. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1708 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1709 | * \param[out] output Buffer where the output is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1710 | * \param output_size Size of the \p output buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1711 | * \param[out] output_length On success, the number of bytes |
| 1712 | * that make up the returned output. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1713 | * |
| 1714 | * \retval #PSA_SUCCESS |
| 1715 | * Success. |
| 1716 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | f45adda | 2019-01-14 18:29:18 +0100 | [diff] [blame] | 1717 | * The operation state is not valid (not set up, IV required but |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1718 | * not set, or already completed). |
| 1719 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 1720 | * The size of the \p output buffer is too small. |
| 1721 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1722 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1723 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1724 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1725 | */ |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1726 | psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, |
| 1727 | const uint8_t *input, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1728 | size_t input_length, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1729 | unsigned char *output, |
| 1730 | size_t output_size, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1731 | size_t *output_length); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1732 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1733 | /** Finish encrypting or decrypting a message in a cipher operation. |
| 1734 | * |
| 1735 | * The application must call psa_cipher_encrypt_setup() or |
| 1736 | * psa_cipher_decrypt_setup() before calling this function. The choice |
| 1737 | * of setup function determines whether this function encrypts or |
| 1738 | * decrypts its input. |
| 1739 | * |
| 1740 | * This function finishes the encryption or decryption of the message |
| 1741 | * formed by concatenating the inputs passed to preceding calls to |
| 1742 | * psa_cipher_update(). |
| 1743 | * |
| 1744 | * When this function returns, the operation becomes inactive. |
| 1745 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1746 | * \param[in,out] operation Active cipher operation. |
| 1747 | * \param[out] output Buffer where the output is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1748 | * \param output_size Size of the \p output buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1749 | * \param[out] output_length On success, the number of bytes |
| 1750 | * that make up the returned output. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1751 | * |
| 1752 | * \retval #PSA_SUCCESS |
| 1753 | * Success. |
| 1754 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | f45adda | 2019-01-14 18:29:18 +0100 | [diff] [blame] | 1755 | * The operation state is not valid (not set up, IV required but |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1756 | * not set, or already completed). |
| 1757 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 1758 | * The size of the \p output buffer is too small. |
| 1759 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1760 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1761 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1762 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1763 | */ |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1764 | psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1765 | uint8_t *output, |
Moran Peker | 0071b87 | 2018-04-22 20:16:58 +0300 | [diff] [blame] | 1766 | size_t output_size, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1767 | size_t *output_length); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1768 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1769 | /** Abort a cipher operation. |
| 1770 | * |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1771 | * Aborting an operation frees all associated resources except for the |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 1772 | * \p operation structure itself. Once aborted, the operation object |
| 1773 | * can be reused for another operation by calling |
| 1774 | * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1775 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 1776 | * You may call this function any time after the operation object has |
| 1777 | * been initialized by any of the following methods: |
| 1778 | * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(), |
| 1779 | * whether it succeeds or not. |
| 1780 | * - Initializing the \c struct to all-bits-zero. |
| 1781 | * - Initializing the \c struct to logical zeros, e.g. |
| 1782 | * `psa_cipher_operation_t operation = {0}`. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1783 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 1784 | * In particular, calling psa_cipher_abort() after the operation has been |
| 1785 | * terminated by a call to psa_cipher_abort() or psa_cipher_finish() |
| 1786 | * is safe and has no effect. |
| 1787 | * |
| 1788 | * \param[in,out] operation Initialized cipher operation. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1789 | * |
| 1790 | * \retval #PSA_SUCCESS |
| 1791 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1792 | * \p operation is not an active cipher operation. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1793 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1794 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1795 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1796 | */ |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1797 | psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation); |
| 1798 | |
| 1799 | /**@}*/ |
| 1800 | |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1801 | /** \defgroup aead Authenticated encryption with associated data (AEAD) |
| 1802 | * @{ |
| 1803 | */ |
| 1804 | |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1805 | /** Process an authenticated encryption operation. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1806 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1807 | * \param handle Handle to the key to use for the operation. |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1808 | * \param alg The AEAD algorithm to compute |
| 1809 | * (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 1810 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1811 | * \param[in] nonce Nonce or IV to use. |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1812 | * \param nonce_length Size of the \p nonce buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1813 | * \param[in] additional_data Additional data that will be authenticated |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1814 | * but not encrypted. |
| 1815 | * \param additional_data_length Size of \p additional_data in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1816 | * \param[in] plaintext Data that will be authenticated and |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1817 | * encrypted. |
| 1818 | * \param plaintext_length Size of \p plaintext in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1819 | * \param[out] ciphertext Output buffer for the authenticated and |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1820 | * encrypted data. The additional data is not |
| 1821 | * part of this output. For algorithms where the |
| 1822 | * encrypted data and the authentication tag |
| 1823 | * are defined as separate outputs, the |
| 1824 | * authentication tag is appended to the |
| 1825 | * encrypted data. |
| 1826 | * \param ciphertext_size Size of the \p ciphertext buffer in bytes. |
| 1827 | * This must be at least |
| 1828 | * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg, |
| 1829 | * \p plaintext_length). |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1830 | * \param[out] ciphertext_length On success, the size of the output |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1831 | * in the \b ciphertext buffer. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1832 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1833 | * \retval #PSA_SUCCESS |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1834 | * Success. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1835 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1836 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1837 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1838 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1839 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1840 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1841 | * \p alg is not supported or is not an AEAD algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1842 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1843 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1844 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1845 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1846 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1847 | * The library has not been previously initialized by psa_crypto_init(). |
| 1848 | * It is implementation-dependent whether a failure to initialize |
| 1849 | * results in this error code. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1850 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1851 | psa_status_t psa_aead_encrypt(psa_key_handle_t handle, |
Gilles Peskine | 9fb0e01 | 2018-07-19 15:51:49 +0200 | [diff] [blame] | 1852 | psa_algorithm_t alg, |
| 1853 | const uint8_t *nonce, |
| 1854 | size_t nonce_length, |
| 1855 | const uint8_t *additional_data, |
| 1856 | size_t additional_data_length, |
| 1857 | const uint8_t *plaintext, |
| 1858 | size_t plaintext_length, |
| 1859 | uint8_t *ciphertext, |
| 1860 | size_t ciphertext_size, |
| 1861 | size_t *ciphertext_length); |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1862 | |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1863 | /** Process an authenticated decryption operation. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1864 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1865 | * \param handle Handle to the key to use for the operation. |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1866 | * \param alg The AEAD algorithm to compute |
| 1867 | * (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 1868 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1869 | * \param[in] nonce Nonce or IV to use. |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1870 | * \param nonce_length Size of the \p nonce buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1871 | * \param[in] additional_data Additional data that has been authenticated |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1872 | * but not encrypted. |
| 1873 | * \param additional_data_length Size of \p additional_data in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1874 | * \param[in] ciphertext Data that has been authenticated and |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1875 | * encrypted. For algorithms where the |
| 1876 | * encrypted data and the authentication tag |
| 1877 | * are defined as separate inputs, the buffer |
| 1878 | * must contain the encrypted data followed |
| 1879 | * by the authentication tag. |
| 1880 | * \param ciphertext_length Size of \p ciphertext in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1881 | * \param[out] plaintext Output buffer for the decrypted data. |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1882 | * \param plaintext_size Size of the \p plaintext buffer in bytes. |
| 1883 | * This must be at least |
| 1884 | * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg, |
| 1885 | * \p ciphertext_length). |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1886 | * \param[out] plaintext_length On success, the size of the output |
mohammad1603 | fb5b9cb | 2018-06-06 13:44:27 +0300 | [diff] [blame] | 1887 | * in the \b plaintext buffer. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1888 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1889 | * \retval #PSA_SUCCESS |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1890 | * Success. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1891 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1892 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1893 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1894 | * The ciphertext is not authentic. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1895 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1896 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1897 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1898 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1899 | * \p alg is not supported or is not an AEAD algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1900 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1901 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1902 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1903 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1904 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1905 | * The library has not been previously initialized by psa_crypto_init(). |
| 1906 | * It is implementation-dependent whether a failure to initialize |
| 1907 | * results in this error code. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1908 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1909 | psa_status_t psa_aead_decrypt(psa_key_handle_t handle, |
Gilles Peskine | 9fb0e01 | 2018-07-19 15:51:49 +0200 | [diff] [blame] | 1910 | psa_algorithm_t alg, |
| 1911 | const uint8_t *nonce, |
| 1912 | size_t nonce_length, |
| 1913 | const uint8_t *additional_data, |
| 1914 | size_t additional_data_length, |
| 1915 | const uint8_t *ciphertext, |
| 1916 | size_t ciphertext_length, |
| 1917 | uint8_t *plaintext, |
| 1918 | size_t plaintext_size, |
| 1919 | size_t *plaintext_length); |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1920 | |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 1921 | /** The type of the state data structure for multipart AEAD operations. |
| 1922 | * |
| 1923 | * Before calling any function on an AEAD operation object, the application |
| 1924 | * must initialize it by any of the following means: |
| 1925 | * - Set the structure to all-bits-zero, for example: |
| 1926 | * \code |
| 1927 | * psa_aead_operation_t operation; |
| 1928 | * memset(&operation, 0, sizeof(operation)); |
| 1929 | * \endcode |
| 1930 | * - Initialize the structure to logical zero values, for example: |
| 1931 | * \code |
| 1932 | * psa_aead_operation_t operation = {0}; |
| 1933 | * \endcode |
| 1934 | * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT, |
| 1935 | * for example: |
| 1936 | * \code |
| 1937 | * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
| 1938 | * \endcode |
| 1939 | * - Assign the result of the function psa_aead_operation_init() |
| 1940 | * to the structure, for example: |
| 1941 | * \code |
| 1942 | * psa_aead_operation_t operation; |
| 1943 | * operation = psa_aead_operation_init(); |
| 1944 | * \endcode |
| 1945 | * |
| 1946 | * This is an implementation-defined \c struct. Applications should not |
| 1947 | * make any assumptions about the content of this structure except |
| 1948 | * as directed by the documentation of a specific implementation. */ |
| 1949 | typedef struct psa_aead_operation_s psa_aead_operation_t; |
| 1950 | |
| 1951 | /** \def PSA_AEAD_OPERATION_INIT |
| 1952 | * |
| 1953 | * This macro returns a suitable initializer for an AEAD operation object of |
| 1954 | * type #psa_aead_operation_t. |
| 1955 | */ |
| 1956 | #ifdef __DOXYGEN_ONLY__ |
| 1957 | /* This is an example definition for documentation purposes. |
| 1958 | * Implementations should define a suitable value in `crypto_struct.h`. |
| 1959 | */ |
| 1960 | #define PSA_AEAD_OPERATION_INIT {0} |
| 1961 | #endif |
| 1962 | |
| 1963 | /** Return an initial value for an AEAD operation object. |
| 1964 | */ |
| 1965 | static psa_aead_operation_t psa_aead_operation_init(void); |
| 1966 | |
| 1967 | /** Set the key for a multipart authenticated encryption operation. |
| 1968 | * |
| 1969 | * The sequence of operations to encrypt a message with authentication |
| 1970 | * is as follows: |
| 1971 | * -# Allocate an operation object which will be passed to all the functions |
| 1972 | * listed here. |
| 1973 | * -# Initialize the operation object with one of the methods described in the |
| 1974 | * documentation for #psa_aead_operation_t, e.g. |
| 1975 | * PSA_AEAD_OPERATION_INIT. |
| 1976 | * -# Call psa_aead_encrypt_setup() to specify the algorithm and key. |
| 1977 | * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to |
| 1978 | * generate or set the nonce. You should use |
| 1979 | * psa_aead_generate_nonce() unless the protocol you are implementing |
| 1980 | * requires a specific nonce value. |
| 1981 | * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment |
| 1982 | * of the non-encrypted additional authenticated data each time. |
| 1983 | * -# Call psa_aead_update() zero, one or more times, passing a fragment |
Gilles Peskine | a05602d | 2019-01-17 15:25:52 +0100 | [diff] [blame^] | 1984 | * of the message to encrypt each time. |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 1985 | * -# Call psa_aead_finish(). |
| 1986 | * |
| 1987 | * The application may call psa_aead_abort() at any time after the operation |
| 1988 | * has been initialized. |
| 1989 | * |
| 1990 | * After a successful call to psa_aead_encrypt_setup(), the application must |
| 1991 | * eventually terminate the operation. The following events terminate an |
| 1992 | * operation: |
| 1993 | * - A failed call to any of the \c psa_aead_xxx functions. |
| 1994 | * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort(). |
| 1995 | * |
| 1996 | * \param[in,out] operation The operation object to set up. It must have |
| 1997 | * been initialized as per the documentation for |
| 1998 | * #psa_aead_operation_t and not yet in use. |
| 1999 | * \param handle Handle to the key to use for the operation. |
| 2000 | * It must remain valid until the operation |
| 2001 | * terminates. |
| 2002 | * \param alg The AEAD algorithm to compute |
| 2003 | * (\c PSA_ALG_XXX value such that |
| 2004 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
| 2005 | * |
| 2006 | * \retval #PSA_SUCCESS |
| 2007 | * Success. |
| 2008 | * \retval #PSA_ERROR_INVALID_HANDLE |
| 2009 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 2010 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 2011 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2012 | * \p key is not compatible with \p alg. |
| 2013 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2014 | * \p alg is not supported or is not an AEAD algorithm. |
| 2015 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2016 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2017 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2018 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2019 | * \retval #PSA_ERROR_BAD_STATE |
| 2020 | * The library has not been previously initialized by psa_crypto_init(). |
| 2021 | * It is implementation-dependent whether a failure to initialize |
| 2022 | * results in this error code. |
| 2023 | */ |
| 2024 | psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation, |
| 2025 | psa_key_handle_t handle, |
| 2026 | psa_algorithm_t alg); |
| 2027 | |
| 2028 | /** Set the key for a multipart authenticated decryption operation. |
| 2029 | * |
| 2030 | * The sequence of operations to decrypt a message with authentication |
| 2031 | * is as follows: |
| 2032 | * -# Allocate an operation object which will be passed to all the functions |
| 2033 | * listed here. |
| 2034 | * -# Initialize the operation object with one of the methods described in the |
| 2035 | * documentation for #psa_aead_operation_t, e.g. |
| 2036 | * PSA_AEAD_OPERATION_INIT. |
| 2037 | * -# Call psa_aead_decrypt_setup() to specify the algorithm and key. |
| 2038 | * -# Call psa_aead_set_nonce() with the nonce for the decryption. |
| 2039 | * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment |
| 2040 | * of the non-encrypted additional authenticated data each time. |
| 2041 | * -# Call psa_aead_update() zero, one or more times, passing a fragment |
Gilles Peskine | a05602d | 2019-01-17 15:25:52 +0100 | [diff] [blame^] | 2042 | * of the ciphertext to decrypt each time. |
| 2043 | * -# Call psa_aead_verify(). |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 2044 | * |
| 2045 | * The application may call psa_aead_abort() at any time after the operation |
| 2046 | * has been initialized. |
| 2047 | * |
| 2048 | * After a successful call to psa_aead_decrypt_setup(), the application must |
| 2049 | * eventually terminate the operation. The following events terminate an |
| 2050 | * operation: |
| 2051 | * - A failed call to any of the \c psa_aead_xxx functions. |
| 2052 | * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort(). |
| 2053 | * |
| 2054 | * \param[in,out] operation The operation object to set up. It must have |
| 2055 | * been initialized as per the documentation for |
| 2056 | * #psa_aead_operation_t and not yet in use. |
| 2057 | * \param handle Handle to the key to use for the operation. |
| 2058 | * It must remain valid until the operation |
| 2059 | * terminates. |
| 2060 | * \param alg The AEAD algorithm to compute |
| 2061 | * (\c PSA_ALG_XXX value such that |
| 2062 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
| 2063 | * |
| 2064 | * \retval #PSA_SUCCESS |
| 2065 | * Success. |
| 2066 | * \retval #PSA_ERROR_INVALID_HANDLE |
| 2067 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 2068 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 2069 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2070 | * \p key is not compatible with \p alg. |
| 2071 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2072 | * \p alg is not supported or is not an AEAD algorithm. |
| 2073 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2074 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2075 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2076 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2077 | * \retval #PSA_ERROR_BAD_STATE |
| 2078 | * The library has not been previously initialized by psa_crypto_init(). |
| 2079 | * It is implementation-dependent whether a failure to initialize |
| 2080 | * results in this error code. |
| 2081 | */ |
| 2082 | psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation, |
| 2083 | psa_key_handle_t handle, |
| 2084 | psa_algorithm_t alg); |
| 2085 | |
| 2086 | /** Generate a random nonce for an authenticated encryption operation. |
| 2087 | * |
| 2088 | * This function generates a random nonce for the authenticated encryption |
| 2089 | * operation with an appropriate size for the chosen algorithm, key type |
| 2090 | * and key size. |
| 2091 | * |
| 2092 | * The application must call psa_aead_encrypt_setup() before |
| 2093 | * calling this function. |
| 2094 | * |
| 2095 | * If this function returns an error status, the operation becomes inactive. |
| 2096 | * |
| 2097 | * \param[in,out] operation Active AEAD operation. |
| 2098 | * \param[out] nonce Buffer where the generated nonce is to be |
| 2099 | * written. |
| 2100 | * \param nonce_size Size of the \p nonce buffer in bytes. |
| 2101 | * \param[out] nonce_length On success, the number of bytes of the |
| 2102 | * generated nonce. |
| 2103 | * |
| 2104 | * \retval #PSA_SUCCESS |
| 2105 | * Success. |
| 2106 | * \retval #PSA_ERROR_BAD_STATE |
| 2107 | * The operation state is not valid (not set up, or nonce already set). |
| 2108 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 2109 | * The size of the \p nonce buffer is too small. |
| 2110 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2111 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2112 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2113 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2114 | */ |
| 2115 | psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation, |
| 2116 | unsigned char *nonce, |
| 2117 | size_t nonce_size, |
| 2118 | size_t *nonce_length); |
| 2119 | |
| 2120 | /** Set the nonce for an authenticated encryption or decryption operation. |
| 2121 | * |
| 2122 | * This function sets the nonce for the authenticated |
| 2123 | * encryption or decryption operation. |
| 2124 | * |
| 2125 | * The application must call psa_aead_encrypt_setup() before |
| 2126 | * calling this function. |
| 2127 | * |
| 2128 | * If this function returns an error status, the operation becomes inactive. |
| 2129 | * |
Gilles Peskine | a05602d | 2019-01-17 15:25:52 +0100 | [diff] [blame^] | 2130 | * \note When encrypting, applications should use psa_aead_generate_nonce() |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 2131 | * instead of this function, unless implementing a protocol that requires |
| 2132 | * a non-random IV. |
| 2133 | * |
| 2134 | * \param[in,out] operation Active AEAD operation. |
Gilles Peskine | a05602d | 2019-01-17 15:25:52 +0100 | [diff] [blame^] | 2135 | * \param[in] nonce Buffer containing the nonce to use. |
| 2136 | * \param nonce_length Size of the nonce in bytes. |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 2137 | * |
| 2138 | * \retval #PSA_SUCCESS |
| 2139 | * Success. |
| 2140 | * \retval #PSA_ERROR_BAD_STATE |
| 2141 | * The operation state is not valid (not set up, or nonce already set). |
| 2142 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2143 | * The size of \p nonce is not acceptable for the chosen algorithm. |
| 2144 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2145 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2146 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2147 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2148 | */ |
| 2149 | psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation, |
| 2150 | const unsigned char *nonce, |
| 2151 | size_t nonce_length); |
| 2152 | |
| 2153 | /** Pass additional data to an active AEAD operation. |
| 2154 | * |
| 2155 | * Additional data is authenticated, but not encrypted. |
| 2156 | * |
| 2157 | * You may call this function multiple times to pass successive fragments |
| 2158 | * of the additional data. You may not call this function after passing |
| 2159 | * data to encrypt or decrypt with psa_aead_update(). |
| 2160 | * |
| 2161 | * Before calling this function, you must: |
| 2162 | * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup(). |
| 2163 | * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce(). |
| 2164 | * |
| 2165 | * If this function returns an error status, the operation becomes inactive. |
| 2166 | * |
| 2167 | * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS, |
| 2168 | * there is no guarantee that the input is valid. Therefore, until |
| 2169 | * you have called psa_aead_verify() and it has returned #PSA_SUCCESS, |
| 2170 | * treat the input as untrusted and prepare to undo any action that |
| 2171 | * depends on the input if psa_aead_verify() returns an error status. |
| 2172 | * |
| 2173 | * \param[in,out] operation Active AEAD operation. |
| 2174 | * \param[in] input Buffer containing the fragment of |
| 2175 | * additional data. |
| 2176 | * \param input_length Size of the \p input buffer in bytes. |
| 2177 | * |
| 2178 | * \retval #PSA_SUCCESS |
| 2179 | * Success. |
| 2180 | * \retval #PSA_ERROR_BAD_STATE |
| 2181 | * The operation state is not valid (not set up, nonce not set, |
| 2182 | * psa_aead_update() already called, or operation already completed). |
| 2183 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2184 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2185 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2186 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2187 | */ |
| 2188 | psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation, |
| 2189 | const uint8_t *input, |
| 2190 | size_t input_length); |
| 2191 | |
| 2192 | /** Encrypt or decrypt a message fragment in an active AEAD operation. |
| 2193 | * |
| 2194 | * Before calling this function, you must: |
| 2195 | * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup(). |
| 2196 | * The choice of setup function determines whether this function |
| 2197 | * encrypts or decrypts its input. |
| 2198 | * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce(). |
| 2199 | * 3. Call psa_aead_update_ad() to pass all the additional data. |
| 2200 | * |
| 2201 | * If this function returns an error status, the operation becomes inactive. |
| 2202 | * |
| 2203 | * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS, |
| 2204 | * there is no guarantee that the input is valid. Therefore, until |
| 2205 | * you have called psa_aead_verify() and it has returned #PSA_SUCCESS: |
| 2206 | * - Do not use the output in any way other than storing it in a |
| 2207 | * confidential location. If you take any action that depends |
| 2208 | * on the tentative decrypted data, this action will need to be |
| 2209 | * undone if the input turns out not to be valid. Furthermore, |
| 2210 | * if an adversary can observe that this action took place |
| 2211 | * (for example through timing), they may be able to use this |
| 2212 | * fact as an oracle to decrypt any message encrypted with the |
| 2213 | * same key. |
| 2214 | * - In particular, do not copy the output anywhere but to a |
| 2215 | * memory or storage space that you have exclusive access to. |
| 2216 | * |
| 2217 | * \param[in,out] operation Active AEAD operation. |
| 2218 | * \param[in] input Buffer containing the message fragment to |
| 2219 | * encrypt or decrypt. |
| 2220 | * \param input_length Size of the \p input buffer in bytes. |
| 2221 | * \param[out] output Buffer where the output is to be written. |
| 2222 | * \param output_size Size of the \p output buffer in bytes. |
| 2223 | * \param[out] output_length On success, the number of bytes |
| 2224 | * that make up the returned output. |
| 2225 | * |
| 2226 | * \retval #PSA_SUCCESS |
| 2227 | * Success. |
| 2228 | * \retval #PSA_ERROR_BAD_STATE |
| 2229 | * The operation state is not valid (not set up, nonce not set |
| 2230 | * or already completed). |
| 2231 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 2232 | * The size of the \p output buffer is too small. |
| 2233 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2234 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2235 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2236 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2237 | */ |
| 2238 | psa_status_t psa_aead_update(psa_aead_operation_t *operation, |
| 2239 | const uint8_t *input, |
| 2240 | size_t input_length, |
| 2241 | unsigned char *output, |
| 2242 | size_t output_size, |
| 2243 | size_t *output_length); |
| 2244 | |
| 2245 | /** Finish encrypting a message in an AEAD operation. |
| 2246 | * |
| 2247 | * The operation must have been set up with psa_aead_encrypt_setup(). |
| 2248 | * |
| 2249 | * This function finishes the authentication of the additional data |
| 2250 | * formed by concatenating the inputs passed to preceding calls to |
| 2251 | * psa_aead_update_ad() with the plaintext formed by concatenating the |
| 2252 | * inputs passed to preceding calls to psa_aead_update(). |
| 2253 | * |
| 2254 | * This function has two output buffers: |
| 2255 | * - \p ciphertext contains trailing ciphertext that was buffered from |
| 2256 | * preceding calls to psa_aead_update(). For all standard AEAD algorithms, |
| 2257 | * psa_aead_update() does not buffer any output and therefore \p ciphertext |
| 2258 | * will not contain any output and can be a 0-sized buffer. |
| 2259 | * - \p tag contains the authentication tag. Its length is always |
| 2260 | * #PSA_AEAD_TAG_LENGTH(\p alg) where \p alg is the AEAD algorithm |
| 2261 | * that the operation performs. |
| 2262 | * |
| 2263 | * When this function returns, the operation becomes inactive. |
| 2264 | * |
| 2265 | * \param[in,out] operation Active AEAD operation. |
| 2266 | * \param[out] ciphertext Buffer where the last part of the ciphertext |
| 2267 | * is to be written. |
| 2268 | * \param ciphertext_size Size of the \p ciphertext buffer in bytes. |
| 2269 | * \param[out] ciphertext_length On success, the number of bytes of |
| 2270 | * returned ciphertext. |
| 2271 | * \param[out] tag Buffer where the authentication tag is |
| 2272 | * to be written. |
| 2273 | * \param tag_size Size of the \p tag buffer in bytes. |
| 2274 | * \param[out] tag_length On success, the number of bytes |
| 2275 | * that make up the returned tag. |
| 2276 | * |
| 2277 | * \retval #PSA_SUCCESS |
| 2278 | * Success. |
| 2279 | * \retval #PSA_ERROR_BAD_STATE |
| 2280 | * The operation state is not valid (not set up, nonce not set, |
| 2281 | * decryption, or already completed). |
| 2282 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 2283 | * The size of the \p output buffer is too small. |
| 2284 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2285 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2286 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2287 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2288 | */ |
| 2289 | psa_status_t psa_aead_finish(psa_aead_operation_t *operation, |
Gilles Peskine | a05602d | 2019-01-17 15:25:52 +0100 | [diff] [blame^] | 2290 | uint8_t *ciphertext, |
| 2291 | size_t ciphertext_size, |
| 2292 | size_t *ciphertext_length, |
Gilles Peskine | 30a9e41 | 2019-01-14 18:36:12 +0100 | [diff] [blame] | 2293 | uint8_t *tag, |
| 2294 | size_t tag_size, |
| 2295 | size_t *tag_length); |
| 2296 | |
| 2297 | /** Finish authenticating and decrypting a message in an AEAD operation. |
| 2298 | * |
| 2299 | * The operation must have been set up with psa_aead_decrypt_setup(). |
| 2300 | * |
| 2301 | * This function finishes the authentication of the additional data |
| 2302 | * formed by concatenating the inputs passed to preceding calls to |
| 2303 | * psa_aead_update_ad() with the ciphertext formed by concatenating the |
| 2304 | * inputs passed to preceding calls to psa_aead_update(). |
| 2305 | * |
| 2306 | * When this function returns, the operation becomes inactive. |
| 2307 | * |
| 2308 | * \param[in,out] operation Active AEAD operation. |
| 2309 | * \param[in] tag Buffer containing the authentication tag. |
| 2310 | * \param tag_length Size of the \p tag buffer in bytes. |
| 2311 | * |
| 2312 | * \retval #PSA_SUCCESS |
| 2313 | * Success. |
| 2314 | * \retval #PSA_ERROR_BAD_STATE |
| 2315 | * The operation state is not valid (not set up, nonce not set, |
| 2316 | * encryption, or already completed). |
| 2317 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 2318 | * The size of the \p output buffer is too small. |
| 2319 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2320 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2321 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2322 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2323 | */ |
| 2324 | psa_status_t psa_aead_verify(psa_aead_operation_t *operation, |
| 2325 | const uint8_t *tag, |
| 2326 | size_t tag_length); |
| 2327 | |
| 2328 | /** Abort an AEAD operation. |
| 2329 | * |
| 2330 | * Aborting an operation frees all associated resources except for the |
| 2331 | * \p operation structure itself. Once aborted, the operation object |
| 2332 | * can be reused for another operation by calling |
| 2333 | * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again. |
| 2334 | * |
| 2335 | * You may call this function any time after the operation object has |
| 2336 | * been initialized by any of the following methods: |
| 2337 | * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(), |
| 2338 | * whether it succeeds or not. |
| 2339 | * - Initializing the \c struct to all-bits-zero. |
| 2340 | * - Initializing the \c struct to logical zeros, e.g. |
| 2341 | * `psa_aead_operation_t operation = {0}`. |
| 2342 | * |
| 2343 | * In particular, calling psa_aead_abort() after the operation has been |
| 2344 | * terminated by a call to psa_aead_abort() or psa_aead_finish() |
| 2345 | * is safe and has no effect. |
| 2346 | * |
| 2347 | * \param[in,out] operation Initialized AEAD operation. |
| 2348 | * |
| 2349 | * \retval #PSA_SUCCESS |
| 2350 | * \retval #PSA_ERROR_BAD_STATE |
| 2351 | * \p operation is not an active AEAD operation. |
| 2352 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2353 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2354 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2355 | */ |
| 2356 | psa_status_t psa_aead_abort(psa_aead_operation_t *operation); |
| 2357 | |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2358 | /**@}*/ |
| 2359 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2360 | /** \defgroup asymmetric Asymmetric cryptography |
| 2361 | * @{ |
| 2362 | */ |
| 2363 | |
| 2364 | /** |
| 2365 | * \brief Sign a hash or short message with a private key. |
| 2366 | * |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 2367 | * Note that to perform a hash-and-sign signature algorithm, you must |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2368 | * first calculate the hash by calling psa_hash_setup(), psa_hash_update() |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 2369 | * and psa_hash_finish(). Then pass the resulting hash as the \p hash |
| 2370 | * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg) |
| 2371 | * to determine the hash algorithm to use. |
| 2372 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2373 | * \param handle Handle to the key to use for the operation. |
| 2374 | * It must be an asymmetric key pair. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2375 | * \param alg A signature algorithm that is compatible with |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2376 | * the type of \p key. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2377 | * \param[in] hash The hash or message to sign. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2378 | * \param hash_length Size of the \p hash buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2379 | * \param[out] signature Buffer where the signature is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2380 | * \param signature_size Size of the \p signature buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2381 | * \param[out] signature_length On success, the number of bytes |
| 2382 | * that make up the returned signature value. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2383 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2384 | * \retval #PSA_SUCCESS |
| 2385 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2386 | * The size of the \p signature buffer is too small. You can |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2387 | * determine a sufficient buffer size by calling |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 2388 | * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg) |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2389 | * where \c key_type and \c key_bits are the type and bit-size |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2390 | * respectively of \p key. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2391 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2392 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2393 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2394 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2395 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2396 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2397 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 2398 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 2399 | * The library has not been previously initialized by psa_crypto_init(). |
| 2400 | * It is implementation-dependent whether a failure to initialize |
| 2401 | * results in this error code. |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2402 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2403 | psa_status_t psa_asymmetric_sign(psa_key_handle_t handle, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2404 | psa_algorithm_t alg, |
| 2405 | const uint8_t *hash, |
| 2406 | size_t hash_length, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2407 | uint8_t *signature, |
| 2408 | size_t signature_size, |
| 2409 | size_t *signature_length); |
| 2410 | |
| 2411 | /** |
| 2412 | * \brief Verify the signature a hash or short message using a public key. |
| 2413 | * |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 2414 | * Note that to perform a hash-and-sign signature algorithm, you must |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2415 | * first calculate the hash by calling psa_hash_setup(), psa_hash_update() |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 2416 | * and psa_hash_finish(). Then pass the resulting hash as the \p hash |
| 2417 | * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg) |
| 2418 | * to determine the hash algorithm to use. |
| 2419 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2420 | * \param handle Handle to the key to use for the operation. |
| 2421 | * It must be a public key or an asymmetric key pair. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2422 | * \param alg A signature algorithm that is compatible with |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2423 | * the type of \p key. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2424 | * \param[in] hash The hash or message whose signature is to be |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 2425 | * verified. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2426 | * \param hash_length Size of the \p hash buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2427 | * \param[in] signature Buffer containing the signature to verify. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2428 | * \param signature_length Size of the \p signature buffer in bytes. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2429 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2430 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2431 | * The signature is valid. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2432 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2433 | * The calculation was perfomed successfully, but the passed |
| 2434 | * signature is not a valid signature. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2435 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2436 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2437 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2438 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2439 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2440 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 2441 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 2442 | * The library has not been previously initialized by psa_crypto_init(). |
| 2443 | * It is implementation-dependent whether a failure to initialize |
| 2444 | * results in this error code. |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2445 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2446 | psa_status_t psa_asymmetric_verify(psa_key_handle_t handle, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2447 | psa_algorithm_t alg, |
| 2448 | const uint8_t *hash, |
| 2449 | size_t hash_length, |
Gilles Peskine | e9191ff | 2018-06-27 14:58:41 +0200 | [diff] [blame] | 2450 | const uint8_t *signature, |
Gilles Peskine | 526fab0 | 2018-06-27 18:19:40 +0200 | [diff] [blame] | 2451 | size_t signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2452 | |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2453 | /** |
| 2454 | * \brief Encrypt a short message with a public key. |
| 2455 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2456 | * \param handle Handle to the key to use for the operation. |
| 2457 | * It must be a public key or an asymmetric |
| 2458 | * key pair. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2459 | * \param alg An asymmetric encryption algorithm that is |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2460 | * compatible with the type of \p key. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2461 | * \param[in] input The message to encrypt. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2462 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2463 | * \param[in] salt A salt or label, if supported by the |
| 2464 | * encryption algorithm. |
| 2465 | * If the algorithm does not support a |
| 2466 | * salt, pass \c NULL. |
| 2467 | * If the algorithm supports an optional |
| 2468 | * salt and you do not want to pass a salt, |
| 2469 | * pass \c NULL. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2470 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2471 | * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is |
| 2472 | * supported. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2473 | * \param salt_length Size of the \p salt buffer in bytes. |
| 2474 | * If \p salt is \c NULL, pass 0. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2475 | * \param[out] output Buffer where the encrypted message is to |
| 2476 | * be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2477 | * \param output_size Size of the \p output buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2478 | * \param[out] output_length On success, the number of bytes |
| 2479 | * that make up the returned output. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2480 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2481 | * \retval #PSA_SUCCESS |
| 2482 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2483 | * The size of the \p output buffer is too small. You can |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2484 | * determine a sufficient buffer size by calling |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 2485 | * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg) |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2486 | * where \c key_type and \c key_bits are the type and bit-size |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2487 | * respectively of \p key. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2488 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2489 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2490 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2491 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2492 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2493 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2494 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 2495 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 2496 | * The library has not been previously initialized by psa_crypto_init(). |
| 2497 | * It is implementation-dependent whether a failure to initialize |
| 2498 | * results in this error code. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2499 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2500 | psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle, |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2501 | psa_algorithm_t alg, |
| 2502 | const uint8_t *input, |
| 2503 | size_t input_length, |
| 2504 | const uint8_t *salt, |
| 2505 | size_t salt_length, |
| 2506 | uint8_t *output, |
| 2507 | size_t output_size, |
| 2508 | size_t *output_length); |
| 2509 | |
| 2510 | /** |
| 2511 | * \brief Decrypt a short message with a private key. |
| 2512 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2513 | * \param handle Handle to the key to use for the operation. |
| 2514 | * It must be an asymmetric key pair. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2515 | * \param alg An asymmetric encryption algorithm that is |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2516 | * compatible with the type of \p key. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2517 | * \param[in] input The message to decrypt. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2518 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2519 | * \param[in] salt A salt or label, if supported by the |
| 2520 | * encryption algorithm. |
| 2521 | * If the algorithm does not support a |
| 2522 | * salt, pass \c NULL. |
| 2523 | * If the algorithm supports an optional |
| 2524 | * salt and you do not want to pass a salt, |
| 2525 | * pass \c NULL. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2526 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2527 | * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is |
| 2528 | * supported. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2529 | * \param salt_length Size of the \p salt buffer in bytes. |
| 2530 | * If \p salt is \c NULL, pass 0. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2531 | * \param[out] output Buffer where the decrypted message is to |
| 2532 | * be written. |
| 2533 | * \param output_size Size of the \c output buffer in bytes. |
| 2534 | * \param[out] output_length On success, the number of bytes |
| 2535 | * that make up the returned output. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2536 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2537 | * \retval #PSA_SUCCESS |
| 2538 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2539 | * The size of the \p output buffer is too small. You can |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2540 | * determine a sufficient buffer size by calling |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 2541 | * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg) |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2542 | * where \c key_type and \c key_bits are the type and bit-size |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2543 | * respectively of \p key. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2544 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2545 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2546 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2547 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2548 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2549 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2550 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
| 2551 | * \retval #PSA_ERROR_INVALID_PADDING |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 2552 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 2553 | * The library has not been previously initialized by psa_crypto_init(). |
| 2554 | * It is implementation-dependent whether a failure to initialize |
| 2555 | * results in this error code. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2556 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2557 | psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle, |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2558 | psa_algorithm_t alg, |
| 2559 | const uint8_t *input, |
| 2560 | size_t input_length, |
| 2561 | const uint8_t *salt, |
| 2562 | size_t salt_length, |
| 2563 | uint8_t *output, |
| 2564 | size_t output_size, |
| 2565 | size_t *output_length); |
| 2566 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2567 | /**@}*/ |
| 2568 | |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 2569 | /** \defgroup generators Generators |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2570 | * @{ |
| 2571 | */ |
| 2572 | |
| 2573 | /** The type of the state data structure for generators. |
| 2574 | * |
| 2575 | * Before calling any function on a generator, the application must |
| 2576 | * initialize it by any of the following means: |
| 2577 | * - Set the structure to all-bits-zero, for example: |
| 2578 | * \code |
| 2579 | * psa_crypto_generator_t generator; |
| 2580 | * memset(&generator, 0, sizeof(generator)); |
| 2581 | * \endcode |
| 2582 | * - Initialize the structure to logical zero values, for example: |
| 2583 | * \code |
| 2584 | * psa_crypto_generator_t generator = {0}; |
| 2585 | * \endcode |
| 2586 | * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT, |
| 2587 | * for example: |
| 2588 | * \code |
| 2589 | * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 2590 | * \endcode |
| 2591 | * - Assign the result of the function psa_crypto_generator_init() |
| 2592 | * to the structure, for example: |
| 2593 | * \code |
| 2594 | * psa_crypto_generator_t generator; |
| 2595 | * generator = psa_crypto_generator_init(); |
| 2596 | * \endcode |
| 2597 | * |
| 2598 | * This is an implementation-defined \c struct. Applications should not |
| 2599 | * make any assumptions about the content of this structure except |
| 2600 | * as directed by the documentation of a specific implementation. |
| 2601 | */ |
| 2602 | typedef struct psa_crypto_generator_s psa_crypto_generator_t; |
| 2603 | |
| 2604 | /** \def PSA_CRYPTO_GENERATOR_INIT |
| 2605 | * |
| 2606 | * This macro returns a suitable initializer for a generator object |
| 2607 | * of type #psa_crypto_generator_t. |
| 2608 | */ |
| 2609 | #ifdef __DOXYGEN_ONLY__ |
| 2610 | /* This is an example definition for documentation purposes. |
| 2611 | * Implementations should define a suitable value in `crypto_struct.h`. |
| 2612 | */ |
| 2613 | #define PSA_CRYPTO_GENERATOR_INIT {0} |
| 2614 | #endif |
| 2615 | |
| 2616 | /** Return an initial value for a generator object. |
| 2617 | */ |
| 2618 | static psa_crypto_generator_t psa_crypto_generator_init(void); |
| 2619 | |
| 2620 | /** Retrieve the current capacity of a generator. |
| 2621 | * |
| 2622 | * The capacity of a generator is the maximum number of bytes that it can |
| 2623 | * return. Reading *N* bytes from a generator reduces its capacity by *N*. |
| 2624 | * |
| 2625 | * \param[in] generator The generator to query. |
| 2626 | * \param[out] capacity On success, the capacity of the generator. |
| 2627 | * |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 2628 | * \retval #PSA_SUCCESS |
| 2629 | * \retval #PSA_ERROR_BAD_STATE |
| 2630 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2631 | */ |
| 2632 | psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator, |
| 2633 | size_t *capacity); |
| 2634 | |
| 2635 | /** Read some data from a generator. |
| 2636 | * |
| 2637 | * This function reads and returns a sequence of bytes from a generator. |
| 2638 | * The data that is read is discarded from the generator. The generator's |
| 2639 | * capacity is decreased by the number of bytes read. |
| 2640 | * |
| 2641 | * \param[in,out] generator The generator object to read from. |
| 2642 | * \param[out] output Buffer where the generator output will be |
| 2643 | * written. |
| 2644 | * \param output_length Number of bytes to output. |
| 2645 | * |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 2646 | * \retval #PSA_SUCCESS |
| 2647 | * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2648 | * There were fewer than \p output_length bytes |
| 2649 | * in the generator. Note that in this case, no |
| 2650 | * output is written to the output buffer. |
| 2651 | * The generator's capacity is set to 0, thus |
| 2652 | * subsequent calls to this function will not |
| 2653 | * succeed, even with a smaller output buffer. |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 2654 | * \retval #PSA_ERROR_BAD_STATE |
| 2655 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2656 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2657 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2658 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2659 | */ |
| 2660 | psa_status_t psa_generator_read(psa_crypto_generator_t *generator, |
| 2661 | uint8_t *output, |
| 2662 | size_t output_length); |
| 2663 | |
| 2664 | /** Create a symmetric key from data read from a generator. |
| 2665 | * |
| 2666 | * This function reads a sequence of bytes from a generator and imports |
| 2667 | * these bytes as a key. |
| 2668 | * The data that is read is discarded from the generator. The generator's |
| 2669 | * capacity is decreased by the number of bytes read. |
| 2670 | * |
| 2671 | * This function is equivalent to calling #psa_generator_read and |
| 2672 | * passing the resulting output to #psa_import_key, but |
| 2673 | * if the implementation provides an isolation boundary then |
| 2674 | * the key material is not exposed outside the isolation boundary. |
| 2675 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2676 | * \param handle Handle to the slot where the key will be stored. |
| 2677 | * This must be a valid slot for a key of the chosen |
| 2678 | * type: it must have been obtained by calling |
| 2679 | * psa_allocate_key() or psa_create_key() with the |
| 2680 | * correct \p type and with a maximum size that is |
| 2681 | * compatible with \p bits. |
| 2682 | * It must not contain any key material yet. |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2683 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). |
| 2684 | * This must be a symmetric key type. |
| 2685 | * \param bits Key size in bits. |
| 2686 | * \param[in,out] generator The generator object to read from. |
| 2687 | * |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 2688 | * \retval #PSA_SUCCESS |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2689 | * Success. |
Gilles Peskine | 23fd2bd | 2018-12-11 15:51:32 +0100 | [diff] [blame] | 2690 | * If the key is persistent, the key material and the key's metadata |
| 2691 | * have been saved to persistent storage. |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 2692 | * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2693 | * There were fewer than \p output_length bytes |
| 2694 | * in the generator. Note that in this case, no |
| 2695 | * output is written to the output buffer. |
| 2696 | * The generator's capacity is set to 0, thus |
| 2697 | * subsequent calls to this function will not |
| 2698 | * succeed, even with a smaller output buffer. |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 2699 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2700 | * The key type or key size is not supported, either by the |
| 2701 | * implementation in general or in this particular slot. |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 2702 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2703 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 2704 | * \retval #PSA_ERROR_OCCUPIED_SLOT |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2705 | * There is already a key in the specified slot. |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 2706 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2707 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 2708 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2709 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2710 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 2711 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 2712 | * The library has not been previously initialized by psa_crypto_init(). |
| 2713 | * It is implementation-dependent whether a failure to initialize |
| 2714 | * results in this error code. |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2715 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2716 | psa_status_t psa_generator_import_key(psa_key_handle_t handle, |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2717 | psa_key_type_t type, |
| 2718 | size_t bits, |
| 2719 | psa_crypto_generator_t *generator); |
| 2720 | |
| 2721 | /** Abort a generator. |
| 2722 | * |
| 2723 | * Once a generator has been aborted, its capacity is zero. |
| 2724 | * Aborting a generator frees all associated resources except for the |
| 2725 | * \c generator structure itself. |
| 2726 | * |
| 2727 | * This function may be called at any time as long as the generator |
| 2728 | * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to |
| 2729 | * psa_crypto_generator_init() or a zero value. In particular, it is valid |
| 2730 | * to call psa_generator_abort() twice, or to call psa_generator_abort() |
| 2731 | * on a generator that has not been set up. |
| 2732 | * |
| 2733 | * Once aborted, the generator object may be called. |
| 2734 | * |
| 2735 | * \param[in,out] generator The generator to abort. |
| 2736 | * |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 2737 | * \retval #PSA_SUCCESS |
| 2738 | * \retval #PSA_ERROR_BAD_STATE |
| 2739 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2740 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2741 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2742 | */ |
| 2743 | psa_status_t psa_generator_abort(psa_crypto_generator_t *generator); |
| 2744 | |
Gilles Peskine | 8feb3a8 | 2018-09-18 12:06:11 +0200 | [diff] [blame] | 2745 | /** Use the maximum possible capacity for a generator. |
| 2746 | * |
| 2747 | * Use this value as the capacity argument when setting up a generator |
| 2748 | * to indicate that the generator should have the maximum possible capacity. |
| 2749 | * The value of the maximum possible capacity depends on the generator |
| 2750 | * algorithm. |
| 2751 | */ |
| 2752 | #define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1)) |
| 2753 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2754 | /**@}*/ |
| 2755 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2756 | /** \defgroup derivation Key derivation |
| 2757 | * @{ |
| 2758 | */ |
| 2759 | |
| 2760 | /** Set up a key derivation operation. |
| 2761 | * |
| 2762 | * A key derivation algorithm takes three inputs: a secret input \p key and |
| 2763 | * two non-secret inputs \p label and p salt. |
| 2764 | * The result of this function is a byte generator which can |
| 2765 | * be used to produce keys and other cryptographic material. |
| 2766 | * |
| 2767 | * The role of \p label and \p salt is as follows: |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 2768 | * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step |
| 2769 | * and \p label is the info string used in the "expand" step. |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2770 | * |
Jaeden Amero | 9e919c6 | 2019-01-07 15:41:50 +0000 | [diff] [blame] | 2771 | * \param[in,out] generator The generator object to set up. It must have |
| 2772 | * been initialized as per the documentation for |
| 2773 | * #psa_crypto_generator_t and not yet in use. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2774 | * \param handle Handle to the secret key. |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2775 | * \param alg The key derivation algorithm to compute |
| 2776 | * (\c PSA_ALG_XXX value such that |
| 2777 | * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true). |
| 2778 | * \param[in] salt Salt to use. |
| 2779 | * \param salt_length Size of the \p salt buffer in bytes. |
| 2780 | * \param[in] label Label to use. |
| 2781 | * \param label_length Size of the \p label buffer in bytes. |
| 2782 | * \param capacity The maximum number of bytes that the |
| 2783 | * generator will be able to provide. |
| 2784 | * |
| 2785 | * \retval #PSA_SUCCESS |
| 2786 | * Success. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2787 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2788 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 2789 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 2790 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2791 | * \c key is not compatible with \c alg, |
| 2792 | * or \p capacity is too large for the specified algorithm and key. |
| 2793 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2794 | * \c alg is not supported or is not a key derivation algorithm. |
| 2795 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2796 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2797 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2798 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 2799 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 2800 | * The library has not been previously initialized by psa_crypto_init(). |
| 2801 | * It is implementation-dependent whether a failure to initialize |
| 2802 | * results in this error code. |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2803 | */ |
| 2804 | psa_status_t psa_key_derivation(psa_crypto_generator_t *generator, |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2805 | psa_key_handle_t handle, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2806 | psa_algorithm_t alg, |
| 2807 | const uint8_t *salt, |
| 2808 | size_t salt_length, |
| 2809 | const uint8_t *label, |
| 2810 | size_t label_length, |
| 2811 | size_t capacity); |
| 2812 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2813 | /** Set up a key agreement operation. |
| 2814 | * |
| 2815 | * A key agreement algorithm takes two inputs: a private key \p private_key |
| 2816 | * a public key \p peer_key. |
| 2817 | * The result of this function is a byte generator which can |
| 2818 | * be used to produce keys and other cryptographic material. |
| 2819 | * |
Gilles Peskine | 211a436 | 2018-10-25 22:22:31 +0200 | [diff] [blame] | 2820 | * The resulting generator always has the maximum capacity permitted by |
| 2821 | * the algorithm. |
| 2822 | * |
Jaeden Amero | 9e919c6 | 2019-01-07 15:41:50 +0000 | [diff] [blame] | 2823 | * \param[in,out] generator The generator object to set up. It must have |
| 2824 | * been initialized as per the documentation for |
| 2825 | * #psa_crypto_generator_t and not yet in use. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2826 | * \param private_key Handle to the private key to use. |
Gilles Peskine | d171e78 | 2018-11-15 17:46:21 +0100 | [diff] [blame] | 2827 | * \param[in] peer_key Public key of the peer. It must be |
| 2828 | * in the same format that psa_import_key() |
| 2829 | * accepts. The standard formats for public |
| 2830 | * keys are documented in the documentation |
| 2831 | * of psa_export_public_key(). |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2832 | * \param peer_key_length Size of \p peer_key in bytes. |
| 2833 | * \param alg The key agreement algorithm to compute |
| 2834 | * (\c PSA_ALG_XXX value such that |
| 2835 | * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true). |
| 2836 | * |
| 2837 | * \retval #PSA_SUCCESS |
| 2838 | * Success. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2839 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2840 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 2841 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 2842 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2843 | * \c private_key is not compatible with \c alg, |
| 2844 | * or \p peer_key is not valid for \c alg or not compatible with |
| 2845 | * \c private_key. |
| 2846 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2847 | * \c alg is not supported or is not a key derivation algorithm. |
| 2848 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2849 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2850 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2851 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2852 | */ |
| 2853 | psa_status_t psa_key_agreement(psa_crypto_generator_t *generator, |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2854 | psa_key_handle_t private_key, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2855 | const uint8_t *peer_key, |
| 2856 | size_t peer_key_length, |
| 2857 | psa_algorithm_t alg); |
| 2858 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2859 | /**@}*/ |
| 2860 | |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 2861 | /** \defgroup random Random generation |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2862 | * @{ |
| 2863 | */ |
| 2864 | |
| 2865 | /** |
| 2866 | * \brief Generate random bytes. |
| 2867 | * |
| 2868 | * \warning This function **can** fail! Callers MUST check the return status |
| 2869 | * and MUST NOT use the content of the output buffer if the return |
| 2870 | * status is not #PSA_SUCCESS. |
| 2871 | * |
| 2872 | * \note To generate a key, use psa_generate_key() instead. |
| 2873 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2874 | * \param[out] output Output buffer for the generated data. |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2875 | * \param output_size Number of bytes to generate and output. |
| 2876 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2877 | * \retval #PSA_SUCCESS |
| 2878 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2879 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
| 2880 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2881 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2882 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 2883 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 2884 | * The library has not been previously initialized by psa_crypto_init(). |
| 2885 | * It is implementation-dependent whether a failure to initialize |
| 2886 | * results in this error code. |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2887 | */ |
| 2888 | psa_status_t psa_generate_random(uint8_t *output, |
| 2889 | size_t output_size); |
| 2890 | |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 2891 | /** Extra parameters for RSA key generation. |
| 2892 | * |
Gilles Peskine | be42f31 | 2018-07-13 14:38:15 +0200 | [diff] [blame] | 2893 | * You may pass a pointer to a structure of this type as the \c extra |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 2894 | * parameter to psa_generate_key(). |
| 2895 | */ |
| 2896 | typedef struct { |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 2897 | uint32_t e; /**< Public exponent value. Default: 65537. */ |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 2898 | } psa_generate_key_extra_rsa; |
| 2899 | |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2900 | /** |
| 2901 | * \brief Generate a key or key pair. |
| 2902 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2903 | * \param handle Handle to the slot where the key will be stored. |
| 2904 | * This must be a valid slot for a key of the chosen |
| 2905 | * type: it must have been obtained by calling |
| 2906 | * psa_allocate_key() or psa_create_key() with the |
| 2907 | * correct \p type and with a maximum size that is |
| 2908 | * compatible with \p bits. |
| 2909 | * It must not contain any key material yet. |
Gilles Peskine | 4e69d7a | 2018-06-19 20:19:14 +0200 | [diff] [blame] | 2910 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). |
| 2911 | * \param bits Key size in bits. |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 2912 | * \param[in] extra Extra parameters for key generation. The |
Gilles Peskine | 4e69d7a | 2018-06-19 20:19:14 +0200 | [diff] [blame] | 2913 | * interpretation of this parameter depends on |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2914 | * \p type. All types support \c NULL to use |
Gilles Peskine | 3fa675c | 2018-07-12 01:31:03 +0200 | [diff] [blame] | 2915 | * default parameters. Implementation that support |
| 2916 | * the generation of vendor-specific key types |
| 2917 | * that allow extra parameters shall document |
| 2918 | * the format of these extra parameters and |
| 2919 | * the default values. For standard parameters, |
| 2920 | * the meaning of \p extra is as follows: |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2921 | * - For a symmetric key type (a type such |
Gilles Peskine | 3fa675c | 2018-07-12 01:31:03 +0200 | [diff] [blame] | 2922 | * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is |
| 2923 | * false), \p extra must be \c NULL. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2924 | * - For an elliptic curve key type (a type |
Gilles Peskine | 3fa675c | 2018-07-12 01:31:03 +0200 | [diff] [blame] | 2925 | * such that #PSA_KEY_TYPE_IS_ECC(\p type) is |
| 2926 | * false), \p extra must be \c NULL. |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 2927 | * - For an RSA key (\p type is |
| 2928 | * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an |
| 2929 | * optional #psa_generate_key_extra_rsa structure |
Gilles Peskine | 3fa675c | 2018-07-12 01:31:03 +0200 | [diff] [blame] | 2930 | * specifying the public exponent. The |
| 2931 | * default public exponent used when \p extra |
| 2932 | * is \c NULL is 65537. |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 2933 | * \param extra_size Size of the buffer that \p extra |
| 2934 | * points to, in bytes. Note that if \p extra is |
| 2935 | * \c NULL then \p extra_size must be zero. |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2936 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2937 | * \retval #PSA_SUCCESS |
Gilles Peskine | 23fd2bd | 2018-12-11 15:51:32 +0100 | [diff] [blame] | 2938 | * Success. |
| 2939 | * If the key is persistent, the key material and the key's metadata |
| 2940 | * have been saved to persistent storage. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2941 | * \retval #PSA_ERROR_INVALID_HANDLE |
| 2942 | * \retval #PSA_ERROR_OCCUPIED_SLOT |
| 2943 | * There is already a key in the specified slot. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2944 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2945 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2946 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2947 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
| 2948 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2949 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2950 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 2951 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 2952 | * The library has not been previously initialized by psa_crypto_init(). |
| 2953 | * It is implementation-dependent whether a failure to initialize |
| 2954 | * results in this error code. |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2955 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2956 | psa_status_t psa_generate_key(psa_key_handle_t handle, |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2957 | psa_key_type_t type, |
| 2958 | size_t bits, |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 2959 | const void *extra, |
| 2960 | size_t extra_size); |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2961 | |
| 2962 | /**@}*/ |
| 2963 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2964 | #ifdef __cplusplus |
| 2965 | } |
| 2966 | #endif |
| 2967 | |
Gilles Peskine | 0cad07c | 2018-06-27 19:49:02 +0200 | [diff] [blame] | 2968 | /* The file "crypto_sizes.h" contains definitions for size calculation |
| 2969 | * macros whose definitions are implementation-specific. */ |
| 2970 | #include "crypto_sizes.h" |
| 2971 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2972 | /* The file "crypto_struct.h" contains definitions for |
| 2973 | * implementation-specific structs that are declared above. */ |
| 2974 | #include "crypto_struct.h" |
| 2975 | |
| 2976 | /* The file "crypto_extra.h" contains vendor-specific definitions. This |
| 2977 | * can include vendor-defined algorithms, extra functions, etc. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2978 | #include "crypto_extra.h" |
| 2979 | |
| 2980 | #endif /* PSA_CRYPTO_H */ |