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 | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 713 | /** The type of the state data structure for multipart hash operations. |
| 714 | * |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 715 | * Before calling any function on a hash operation object, the application must |
| 716 | * initialize it by any of the following means: |
| 717 | * - Set the structure to all-bits-zero, for example: |
| 718 | * \code |
| 719 | * psa_hash_operation_t operation; |
| 720 | * memset(&operation, 0, sizeof(operation)); |
| 721 | * \endcode |
| 722 | * - Initialize the structure to logical zero values, for example: |
| 723 | * \code |
| 724 | * psa_hash_operation_t operation = {0}; |
| 725 | * \endcode |
| 726 | * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT, |
| 727 | * for example: |
| 728 | * \code |
| 729 | * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 730 | * \endcode |
| 731 | * - Assign the result of the function psa_hash_operation_init() |
| 732 | * to the structure, for example: |
| 733 | * \code |
| 734 | * psa_hash_operation_t operation; |
| 735 | * operation = psa_hash_operation_init(); |
| 736 | * \endcode |
| 737 | * |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 738 | * This is an implementation-defined \c struct. Applications should not |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 739 | * make any assumptions about the content of this structure except |
| 740 | * as directed by the documentation of a specific implementation. */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 741 | typedef struct psa_hash_operation_s psa_hash_operation_t; |
| 742 | |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 743 | /** \def PSA_HASH_OPERATION_INIT |
| 744 | * |
| 745 | * This macro returns a suitable initializer for a hash operation object |
| 746 | * of type #psa_hash_operation_t. |
| 747 | */ |
| 748 | #ifdef __DOXYGEN_ONLY__ |
| 749 | /* This is an example definition for documentation purposes. |
| 750 | * Implementations should define a suitable value in `crypto_struct.h`. |
| 751 | */ |
| 752 | #define PSA_HASH_OPERATION_INIT {0} |
| 753 | #endif |
| 754 | |
| 755 | /** Return an initial value for a hash operation object. |
| 756 | */ |
| 757 | static psa_hash_operation_t psa_hash_operation_init(void); |
| 758 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 759 | /** Start a multipart hash operation. |
| 760 | * |
| 761 | * The sequence of operations to calculate a hash (message digest) |
| 762 | * is as follows: |
| 763 | * -# Allocate an operation object which will be passed to all the functions |
| 764 | * listed here. |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 765 | * -# Initialize the operation object with one of the methods described in the |
| 766 | * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT. |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 767 | * -# Call psa_hash_setup() to specify the algorithm. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 768 | * -# Call psa_hash_update() zero, one or more times, passing a fragment |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 769 | * of the message each time. The hash that is calculated is the hash |
| 770 | * of the concatenation of these messages in order. |
| 771 | * -# To calculate the hash, call psa_hash_finish(). |
| 772 | * To compare the hash with an expected value, call psa_hash_verify(). |
| 773 | * |
| 774 | * 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] | 775 | * has been initialized. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 776 | * |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 777 | * After a successful call to psa_hash_setup(), the application must |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame] | 778 | * eventually terminate the operation. The following events terminate an |
| 779 | * operation: |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 780 | * - A failed call to psa_hash_update(). |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 781 | * - 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] | 782 | * |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 783 | * \param[in,out] operation The operation object to set up. It must have |
| 784 | * been initialized as per the documentation for |
| 785 | * #psa_hash_operation_t and not yet in use. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 786 | * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value |
| 787 | * such that #PSA_ALG_IS_HASH(\p alg) is true). |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 788 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 789 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 790 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 791 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 792 | * \p alg is not supported or is not a hash algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 793 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 794 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 795 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 796 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 797 | */ |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 798 | psa_status_t psa_hash_setup(psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 799 | psa_algorithm_t alg); |
| 800 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 801 | /** Add a message fragment to a multipart hash operation. |
| 802 | * |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 803 | * The application must call psa_hash_setup() before calling this function. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 804 | * |
| 805 | * If this function returns an error status, the operation becomes inactive. |
| 806 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 807 | * \param[in,out] operation Active hash operation. |
| 808 | * \param[in] input Buffer containing the message fragment to hash. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 809 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 810 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 811 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 812 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 813 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 814 | * The operation state is not valid (not started, or already completed). |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 815 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 816 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 817 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 818 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 819 | */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 820 | psa_status_t psa_hash_update(psa_hash_operation_t *operation, |
| 821 | const uint8_t *input, |
| 822 | size_t input_length); |
| 823 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 824 | /** Finish the calculation of the hash of a message. |
| 825 | * |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 826 | * The application must call psa_hash_setup() before calling this function. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 827 | * This function calculates the hash of the message formed by concatenating |
| 828 | * the inputs passed to preceding calls to psa_hash_update(). |
| 829 | * |
| 830 | * When this function returns, the operation becomes inactive. |
| 831 | * |
| 832 | * \warning Applications should not call this function if they expect |
| 833 | * a specific value for the hash. Call psa_hash_verify() instead. |
| 834 | * Beware that comparing integrity or authenticity data such as |
| 835 | * hash values with a function such as \c memcmp is risky |
| 836 | * because the time taken by the comparison may leak information |
| 837 | * about the hashed data which could allow an attacker to guess |
| 838 | * a valid hash and thereby bypass security controls. |
| 839 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 840 | * \param[in,out] operation Active hash operation. |
| 841 | * \param[out] hash Buffer where the hash is to be written. |
| 842 | * \param hash_size Size of the \p hash buffer in bytes. |
| 843 | * \param[out] hash_length On success, the number of bytes |
| 844 | * that make up the hash value. This is always |
Gilles Peskine | be42f31 | 2018-07-13 14:38:15 +0200 | [diff] [blame] | 845 | * #PSA_HASH_SIZE(\c alg) where \c alg is the |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 846 | * hash algorithm that is calculated. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 847 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 848 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 849 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 850 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 851 | * The operation state is not valid (not started, or already completed). |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 852 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 853 | * 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] | 854 | * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg) |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 855 | * where \c alg is the hash algorithm that is calculated. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 856 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 857 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 858 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 859 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 860 | */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 861 | psa_status_t psa_hash_finish(psa_hash_operation_t *operation, |
| 862 | uint8_t *hash, |
| 863 | size_t hash_size, |
| 864 | size_t *hash_length); |
| 865 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 866 | /** Finish the calculation of the hash of a message and compare it with |
| 867 | * an expected value. |
| 868 | * |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 869 | * The application must call psa_hash_setup() before calling this function. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 870 | * This function calculates the hash of the message formed by concatenating |
| 871 | * the inputs passed to preceding calls to psa_hash_update(). It then |
| 872 | * compares the calculated hash with the expected hash passed as a |
| 873 | * parameter to this function. |
| 874 | * |
| 875 | * When this function returns, the operation becomes inactive. |
| 876 | * |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 877 | * \note Implementations shall make the best effort to ensure that the |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 878 | * comparison between the actual hash and the expected hash is performed |
| 879 | * in constant time. |
| 880 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 881 | * \param[in,out] operation Active hash operation. |
| 882 | * \param[in] hash Buffer containing the expected hash value. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 883 | * \param hash_length Size of the \p hash buffer in bytes. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 884 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 885 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 886 | * The expected hash is identical to the actual hash of the message. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 887 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 888 | * The hash of the message was calculated successfully, but it |
| 889 | * differs from the expected hash. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 890 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 891 | * The operation state is not valid (not started, or already completed). |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 892 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 893 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 894 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 895 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 896 | */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 897 | psa_status_t psa_hash_verify(psa_hash_operation_t *operation, |
| 898 | const uint8_t *hash, |
| 899 | size_t hash_length); |
| 900 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 901 | /** Abort a hash operation. |
| 902 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 903 | * Aborting an operation frees all associated resources except for the |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 904 | * \p operation structure itself. Once aborted, the operation object |
| 905 | * can be reused for another operation by calling |
| 906 | * psa_hash_setup() again. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 907 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 908 | * You may call this function any time after the operation object has |
| 909 | * been initialized by any of the following methods: |
| 910 | * - A call to psa_hash_setup(), whether it succeeds or not. |
| 911 | * - Initializing the \c struct to all-bits-zero. |
| 912 | * - Initializing the \c struct to logical zeros, e.g. |
| 913 | * `psa_hash_operation_t operation = {0}`. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 914 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 915 | * In particular, calling psa_hash_abort() after the operation has been |
| 916 | * terminated by a call to psa_hash_abort(), psa_hash_finish() or |
| 917 | * psa_hash_verify() is safe and has no effect. |
| 918 | * |
| 919 | * \param[in,out] operation Initialized hash operation. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 920 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 921 | * \retval #PSA_SUCCESS |
| 922 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 923 | * \p operation is not an active hash operation. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 924 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 925 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 926 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 927 | */ |
| 928 | psa_status_t psa_hash_abort(psa_hash_operation_t *operation); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 929 | |
| 930 | /**@}*/ |
| 931 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 932 | /** \defgroup MAC Message authentication codes |
| 933 | * @{ |
| 934 | */ |
| 935 | |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 936 | /** The type of the state data structure for multipart MAC operations. |
| 937 | * |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 938 | * Before calling any function on a MAC operation object, the application must |
| 939 | * initialize it by any of the following means: |
| 940 | * - Set the structure to all-bits-zero, for example: |
| 941 | * \code |
| 942 | * psa_mac_operation_t operation; |
| 943 | * memset(&operation, 0, sizeof(operation)); |
| 944 | * \endcode |
| 945 | * - Initialize the structure to logical zero values, for example: |
| 946 | * \code |
| 947 | * psa_mac_operation_t operation = {0}; |
| 948 | * \endcode |
| 949 | * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT, |
| 950 | * for example: |
| 951 | * \code |
| 952 | * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 953 | * \endcode |
| 954 | * - Assign the result of the function psa_mac_operation_init() |
| 955 | * to the structure, for example: |
| 956 | * \code |
| 957 | * psa_mac_operation_t operation; |
| 958 | * operation = psa_mac_operation_init(); |
| 959 | * \endcode |
| 960 | * |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 961 | * This is an implementation-defined \c struct. Applications should not |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 962 | * make any assumptions about the content of this structure except |
| 963 | * as directed by the documentation of a specific implementation. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 964 | typedef struct psa_mac_operation_s psa_mac_operation_t; |
| 965 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 966 | /** \def PSA_MAC_OPERATION_INIT |
| 967 | * |
| 968 | * This macro returns a suitable initializer for a MAC operation object of type |
| 969 | * #psa_mac_operation_t. |
| 970 | */ |
| 971 | #ifdef __DOXYGEN_ONLY__ |
| 972 | /* This is an example definition for documentation purposes. |
| 973 | * Implementations should define a suitable value in `crypto_struct.h`. |
| 974 | */ |
| 975 | #define PSA_MAC_OPERATION_INIT {0} |
| 976 | #endif |
| 977 | |
| 978 | /** Return an initial value for a MAC operation object. |
| 979 | */ |
| 980 | static psa_mac_operation_t psa_mac_operation_init(void); |
| 981 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 982 | /** Start a multipart MAC calculation operation. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 983 | * |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 984 | * This function sets up the calculation of the MAC |
| 985 | * (message authentication code) of a byte string. |
| 986 | * To verify the MAC of a message against an |
| 987 | * expected value, use psa_mac_verify_setup() instead. |
| 988 | * |
| 989 | * The sequence of operations to calculate a MAC is as follows: |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 990 | * -# Allocate an operation object which will be passed to all the functions |
| 991 | * listed here. |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 992 | * -# Initialize the operation object with one of the methods described in the |
| 993 | * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT. |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 994 | * -# Call psa_mac_sign_setup() to specify the algorithm and key. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 995 | * -# Call psa_mac_update() zero, one or more times, passing a fragment |
| 996 | * of the message each time. The MAC that is calculated is the MAC |
| 997 | * of the concatenation of these messages in order. |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 998 | * -# At the end of the message, call psa_mac_sign_finish() to finish |
| 999 | * calculating the MAC value and retrieve it. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1000 | * |
| 1001 | * 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] | 1002 | * has been initialized. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1003 | * |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1004 | * After a successful call to psa_mac_sign_setup(), the application must |
| 1005 | * eventually terminate the operation through one of the following methods: |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1006 | * - A failed call to psa_mac_update(). |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1007 | * - A call to psa_mac_sign_finish() or psa_mac_abort(). |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1008 | * |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1009 | * \param[in,out] operation The operation object to set up. It must have |
| 1010 | * been initialized as per the documentation for |
| 1011 | * #psa_mac_operation_t and not yet in use. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1012 | * \param handle Handle to the key to use for the operation. |
Gilles Peskine | 5f25dd0 | 2019-01-14 18:24:53 +0100 | [diff] [blame^] | 1013 | * It must remain valid until the operation |
| 1014 | * terminates. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1015 | * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value |
| 1016 | * such that #PSA_ALG_IS_MAC(alg) is true). |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1017 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1018 | * \retval #PSA_SUCCESS |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1019 | * Success. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1020 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1021 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1022 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1023 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1024 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1025 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1026 | * \p alg is not supported or is not a MAC algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1027 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1028 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1029 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1030 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1031 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 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. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1035 | */ |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1036 | psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation, |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1037 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1038 | psa_algorithm_t alg); |
| 1039 | |
| 1040 | /** Start a multipart MAC verification operation. |
| 1041 | * |
| 1042 | * This function sets up the verification of the MAC |
| 1043 | * (message authentication code) of a byte string against an expected value. |
| 1044 | * |
| 1045 | * The sequence of operations to verify a MAC is as follows: |
| 1046 | * -# Allocate an operation object which will be passed to all the functions |
| 1047 | * listed here. |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1048 | * -# Initialize the operation object with one of the methods described in the |
| 1049 | * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT. |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1050 | * -# Call psa_mac_verify_setup() to specify the algorithm and key. |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1051 | * -# Call psa_mac_update() zero, one or more times, passing a fragment |
| 1052 | * of the message each time. The MAC that is calculated is the MAC |
| 1053 | * of the concatenation of these messages in order. |
| 1054 | * -# At the end of the message, call psa_mac_verify_finish() to finish |
| 1055 | * calculating the actual MAC of the message and verify it against |
| 1056 | * the expected value. |
| 1057 | * |
| 1058 | * 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] | 1059 | * has been initialized. |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1060 | * |
| 1061 | * After a successful call to psa_mac_verify_setup(), the application must |
| 1062 | * eventually terminate the operation through one of the following methods: |
| 1063 | * - A failed call to psa_mac_update(). |
| 1064 | * - A call to psa_mac_verify_finish() or psa_mac_abort(). |
| 1065 | * |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1066 | * \param[in,out] operation The operation object to set up. It must have |
| 1067 | * been initialized as per the documentation for |
| 1068 | * #psa_mac_operation_t and not yet in use. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1069 | * \param handle Handle to the key to use for the operation. |
Gilles Peskine | 5f25dd0 | 2019-01-14 18:24:53 +0100 | [diff] [blame^] | 1070 | * It must remain valid until the operation |
| 1071 | * terminates. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1072 | * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value |
| 1073 | * such that #PSA_ALG_IS_MAC(\p alg) is true). |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1074 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1075 | * \retval #PSA_SUCCESS |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1076 | * Success. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1077 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1078 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1079 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1080 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1081 | * \c key is not compatible with \c alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1082 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1083 | * \c alg is not supported or is not a MAC algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1084 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1085 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1086 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1087 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1088 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1089 | * The library has not been previously initialized by psa_crypto_init(). |
| 1090 | * It is implementation-dependent whether a failure to initialize |
| 1091 | * results in this error code. |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1092 | */ |
| 1093 | psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation, |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1094 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1095 | psa_algorithm_t alg); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1096 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1097 | /** Add a message fragment to a multipart MAC operation. |
| 1098 | * |
| 1099 | * The application must call psa_mac_sign_setup() or psa_mac_verify_setup() |
| 1100 | * before calling this function. |
| 1101 | * |
| 1102 | * If this function returns an error status, the operation becomes inactive. |
| 1103 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1104 | * \param[in,out] operation Active MAC operation. |
| 1105 | * \param[in] input Buffer containing the message fragment to add to |
| 1106 | * the MAC calculation. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1107 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1108 | * |
| 1109 | * \retval #PSA_SUCCESS |
| 1110 | * Success. |
| 1111 | * \retval #PSA_ERROR_BAD_STATE |
| 1112 | * The operation state is not valid (not started, or already completed). |
| 1113 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1114 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1115 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1116 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1117 | */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1118 | psa_status_t psa_mac_update(psa_mac_operation_t *operation, |
| 1119 | const uint8_t *input, |
| 1120 | size_t input_length); |
| 1121 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1122 | /** Finish the calculation of the MAC of a message. |
| 1123 | * |
| 1124 | * The application must call psa_mac_sign_setup() before calling this function. |
| 1125 | * This function calculates the MAC of the message formed by concatenating |
| 1126 | * the inputs passed to preceding calls to psa_mac_update(). |
| 1127 | * |
| 1128 | * When this function returns, the operation becomes inactive. |
| 1129 | * |
| 1130 | * \warning Applications should not call this function if they expect |
| 1131 | * a specific value for the MAC. Call psa_mac_verify_finish() instead. |
| 1132 | * Beware that comparing integrity or authenticity data such as |
| 1133 | * MAC values with a function such as \c memcmp is risky |
| 1134 | * because the time taken by the comparison may leak information |
| 1135 | * about the MAC value which could allow an attacker to guess |
| 1136 | * a valid MAC and thereby bypass security controls. |
| 1137 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1138 | * \param[in,out] operation Active MAC operation. |
| 1139 | * \param[out] mac Buffer where the MAC value is to be written. |
| 1140 | * \param mac_size Size of the \p mac buffer in bytes. |
| 1141 | * \param[out] mac_length On success, the number of bytes |
| 1142 | * that make up the MAC value. This is always |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 1143 | * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg) |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1144 | * where \c key_type and \c key_bits are the type and |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 1145 | * bit-size respectively of the key and \c alg is the |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1146 | * MAC algorithm that is calculated. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1147 | * |
| 1148 | * \retval #PSA_SUCCESS |
| 1149 | * Success. |
| 1150 | * \retval #PSA_ERROR_BAD_STATE |
| 1151 | * The operation state is not valid (not started, or already completed). |
| 1152 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1153 | * 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] | 1154 | * sufficient buffer size by calling PSA_MAC_FINAL_SIZE(). |
| 1155 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1156 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1157 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1158 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1159 | */ |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 1160 | psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation, |
| 1161 | uint8_t *mac, |
| 1162 | size_t mac_size, |
| 1163 | size_t *mac_length); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1164 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1165 | /** Finish the calculation of the MAC of a message and compare it with |
| 1166 | * an expected value. |
| 1167 | * |
| 1168 | * The application must call psa_mac_verify_setup() before calling this function. |
| 1169 | * This function calculates the MAC of the message formed by concatenating |
| 1170 | * the inputs passed to preceding calls to psa_mac_update(). It then |
| 1171 | * compares the calculated MAC with the expected MAC passed as a |
| 1172 | * parameter to this function. |
| 1173 | * |
| 1174 | * When this function returns, the operation becomes inactive. |
| 1175 | * |
| 1176 | * \note Implementations shall make the best effort to ensure that the |
| 1177 | * comparison between the actual MAC and the expected MAC is performed |
| 1178 | * in constant time. |
| 1179 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1180 | * \param[in,out] operation Active MAC operation. |
| 1181 | * \param[in] mac Buffer containing the expected MAC value. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1182 | * \param mac_length Size of the \p mac buffer in bytes. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1183 | * |
| 1184 | * \retval #PSA_SUCCESS |
| 1185 | * The expected MAC is identical to the actual MAC of the message. |
| 1186 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
| 1187 | * The MAC of the message was calculated successfully, but it |
| 1188 | * differs from the expected MAC. |
| 1189 | * \retval #PSA_ERROR_BAD_STATE |
| 1190 | * The operation state is not valid (not started, or already completed). |
| 1191 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1192 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1193 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1194 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1195 | */ |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 1196 | psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation, |
| 1197 | const uint8_t *mac, |
| 1198 | size_t mac_length); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1199 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1200 | /** Abort a MAC operation. |
| 1201 | * |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1202 | * Aborting an operation frees all associated resources except for the |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 1203 | * \p operation structure itself. Once aborted, the operation object |
| 1204 | * can be reused for another operation by calling |
| 1205 | * psa_mac_sign_setup() or psa_mac_verify_setup() again. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1206 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 1207 | * You may call this function any time after the operation object has |
| 1208 | * been initialized by any of the following methods: |
| 1209 | * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether |
| 1210 | * it succeeds or not. |
| 1211 | * - Initializing the \c struct to all-bits-zero. |
| 1212 | * - Initializing the \c struct to logical zeros, e.g. |
| 1213 | * `psa_mac_operation_t operation = {0}`. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1214 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 1215 | * In particular, calling psa_mac_abort() after the operation has been |
| 1216 | * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or |
| 1217 | * psa_mac_verify_finish() is safe and has no effect. |
| 1218 | * |
| 1219 | * \param[in,out] operation Initialized MAC operation. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1220 | * |
| 1221 | * \retval #PSA_SUCCESS |
| 1222 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1223 | * \p operation is not an active MAC operation. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1224 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1225 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1226 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1227 | */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1228 | psa_status_t psa_mac_abort(psa_mac_operation_t *operation); |
| 1229 | |
| 1230 | /**@}*/ |
| 1231 | |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1232 | /** \defgroup cipher Symmetric ciphers |
| 1233 | * @{ |
| 1234 | */ |
| 1235 | |
| 1236 | /** The type of the state data structure for multipart cipher operations. |
| 1237 | * |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1238 | * Before calling any function on a cipher operation object, the application |
| 1239 | * must initialize it by any of the following means: |
| 1240 | * - Set the structure to all-bits-zero, for example: |
| 1241 | * \code |
| 1242 | * psa_cipher_operation_t operation; |
| 1243 | * memset(&operation, 0, sizeof(operation)); |
| 1244 | * \endcode |
| 1245 | * - Initialize the structure to logical zero values, for example: |
| 1246 | * \code |
| 1247 | * psa_cipher_operation_t operation = {0}; |
| 1248 | * \endcode |
| 1249 | * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT, |
| 1250 | * for example: |
| 1251 | * \code |
| 1252 | * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 1253 | * \endcode |
| 1254 | * - Assign the result of the function psa_cipher_operation_init() |
| 1255 | * to the structure, for example: |
| 1256 | * \code |
| 1257 | * psa_cipher_operation_t operation; |
| 1258 | * operation = psa_cipher_operation_init(); |
| 1259 | * \endcode |
| 1260 | * |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1261 | * This is an implementation-defined \c struct. Applications should not |
| 1262 | * make any assumptions about the content of this structure except |
| 1263 | * as directed by the documentation of a specific implementation. */ |
| 1264 | typedef struct psa_cipher_operation_s psa_cipher_operation_t; |
| 1265 | |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1266 | /** \def PSA_CIPHER_OPERATION_INIT |
| 1267 | * |
| 1268 | * This macro returns a suitable initializer for a cipher operation object of |
| 1269 | * type #psa_cipher_operation_t. |
| 1270 | */ |
| 1271 | #ifdef __DOXYGEN_ONLY__ |
| 1272 | /* This is an example definition for documentation purposes. |
| 1273 | * Implementations should define a suitable value in `crypto_struct.h`. |
| 1274 | */ |
| 1275 | #define PSA_CIPHER_OPERATION_INIT {0} |
| 1276 | #endif |
| 1277 | |
| 1278 | /** Return an initial value for a cipher operation object. |
| 1279 | */ |
| 1280 | static psa_cipher_operation_t psa_cipher_operation_init(void); |
| 1281 | |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1282 | /** Set the key for a multipart symmetric encryption operation. |
| 1283 | * |
| 1284 | * The sequence of operations to encrypt a message with a symmetric cipher |
| 1285 | * is as follows: |
| 1286 | * -# Allocate an operation object which will be passed to all the functions |
| 1287 | * listed here. |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1288 | * -# Initialize the operation object with one of the methods described in the |
| 1289 | * documentation for #psa_cipher_operation_t, e.g. |
| 1290 | * PSA_CIPHER_OPERATION_INIT. |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1291 | * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key. |
itayzafrir | ed7382f | 2018-08-02 14:19:33 +0300 | [diff] [blame] | 1292 | * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1293 | * generate or set the IV (initialization vector). You should use |
itayzafrir | ed7382f | 2018-08-02 14:19:33 +0300 | [diff] [blame] | 1294 | * psa_cipher_generate_iv() unless the protocol you are implementing |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1295 | * requires a specific IV value. |
| 1296 | * -# Call psa_cipher_update() zero, one or more times, passing a fragment |
| 1297 | * of the message each time. |
| 1298 | * -# Call psa_cipher_finish(). |
| 1299 | * |
| 1300 | * 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] | 1301 | * has been initialized. |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1302 | * |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1303 | * After a successful call to psa_cipher_encrypt_setup(), the application must |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame] | 1304 | * eventually terminate the operation. The following events terminate an |
| 1305 | * operation: |
itayzafrir | ed7382f | 2018-08-02 14:19:33 +0300 | [diff] [blame] | 1306 | * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv() |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1307 | * or psa_cipher_update(). |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 1308 | * - A call to psa_cipher_finish() or psa_cipher_abort(). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1309 | * |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1310 | * \param[in,out] operation The operation object to set up. It must have |
| 1311 | * been initialized as per the documentation for |
| 1312 | * #psa_cipher_operation_t and not yet in use. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1313 | * \param handle Handle to the key to use for the operation. |
Gilles Peskine | 5f25dd0 | 2019-01-14 18:24:53 +0100 | [diff] [blame^] | 1314 | * It must remain valid until the operation |
| 1315 | * terminates. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1316 | * \param alg The cipher algorithm to compute |
| 1317 | * (\c PSA_ALG_XXX value such that |
| 1318 | * #PSA_ALG_IS_CIPHER(\p alg) is true). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1319 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1320 | * \retval #PSA_SUCCESS |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1321 | * Success. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1322 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1323 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1324 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1325 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1326 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1327 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1328 | * \p alg is not supported or is not a cipher algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1329 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1330 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1331 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1332 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1333 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1334 | * The library has not been previously initialized by psa_crypto_init(). |
| 1335 | * It is implementation-dependent whether a failure to initialize |
| 1336 | * results in this error code. |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1337 | */ |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1338 | psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation, |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1339 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1340 | psa_algorithm_t alg); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1341 | |
| 1342 | /** Set the key for a multipart symmetric decryption operation. |
| 1343 | * |
| 1344 | * The sequence of operations to decrypt a message with a symmetric cipher |
| 1345 | * is as follows: |
| 1346 | * -# Allocate an operation object which will be passed to all the functions |
| 1347 | * listed here. |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1348 | * -# Initialize the operation object with one of the methods described in the |
| 1349 | * documentation for #psa_cipher_operation_t, e.g. |
| 1350 | * PSA_CIPHER_OPERATION_INIT. |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1351 | * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key. |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1352 | * -# Call psa_cipher_update() with the IV (initialization vector) for the |
| 1353 | * decryption. If the IV is prepended to the ciphertext, you can call |
| 1354 | * psa_cipher_update() on a buffer containing the IV followed by the |
| 1355 | * beginning of the message. |
| 1356 | * -# Call psa_cipher_update() zero, one or more times, passing a fragment |
| 1357 | * of the message each time. |
| 1358 | * -# Call psa_cipher_finish(). |
| 1359 | * |
| 1360 | * 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] | 1361 | * has been initialized. |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1362 | * |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1363 | * After a successful call to psa_cipher_decrypt_setup(), the application must |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame] | 1364 | * eventually terminate the operation. The following events terminate an |
| 1365 | * operation: |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1366 | * - A failed call to psa_cipher_update(). |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 1367 | * - A call to psa_cipher_finish() or psa_cipher_abort(). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1368 | * |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1369 | * \param[in,out] operation The operation object to set up. It must have |
| 1370 | * been initialized as per the documentation for |
| 1371 | * #psa_cipher_operation_t and not yet in use. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1372 | * \param handle Handle to the key to use for the operation. |
Gilles Peskine | 5f25dd0 | 2019-01-14 18:24:53 +0100 | [diff] [blame^] | 1373 | * It must remain valid until the operation |
| 1374 | * terminates. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1375 | * \param alg The cipher algorithm to compute |
| 1376 | * (\c PSA_ALG_XXX value such that |
| 1377 | * #PSA_ALG_IS_CIPHER(\p alg) is true). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1378 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1379 | * \retval #PSA_SUCCESS |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1380 | * Success. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1381 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1382 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1383 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1384 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1385 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1386 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1387 | * \p alg is not supported or is not a cipher algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1388 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1389 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1390 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1391 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1392 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1393 | * The library has not been previously initialized by psa_crypto_init(). |
| 1394 | * It is implementation-dependent whether a failure to initialize |
| 1395 | * results in this error code. |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1396 | */ |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1397 | psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation, |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1398 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1399 | psa_algorithm_t alg); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1400 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1401 | /** Generate an IV for a symmetric encryption operation. |
| 1402 | * |
| 1403 | * This function generates a random IV (initialization vector), nonce |
| 1404 | * or initial counter value for the encryption operation as appropriate |
| 1405 | * for the chosen algorithm, key type and key size. |
| 1406 | * |
| 1407 | * The application must call psa_cipher_encrypt_setup() before |
| 1408 | * calling this function. |
| 1409 | * |
| 1410 | * If this function returns an error status, the operation becomes inactive. |
| 1411 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1412 | * \param[in,out] operation Active cipher operation. |
| 1413 | * \param[out] iv Buffer where the generated IV is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1414 | * \param iv_size Size of the \p iv buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1415 | * \param[out] iv_length On success, the number of bytes of the |
| 1416 | * generated IV. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1417 | * |
| 1418 | * \retval #PSA_SUCCESS |
| 1419 | * Success. |
| 1420 | * \retval #PSA_ERROR_BAD_STATE |
| 1421 | * The operation state is not valid (not started, or IV already set). |
| 1422 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 1423 | * The size of the \p iv buffer is too small. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1424 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1425 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1426 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1427 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1428 | */ |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1429 | psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation, |
| 1430 | unsigned char *iv, |
| 1431 | size_t iv_size, |
| 1432 | size_t *iv_length); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1433 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1434 | /** Set the IV for a symmetric encryption or decryption operation. |
| 1435 | * |
| 1436 | * This function sets the random IV (initialization vector), nonce |
| 1437 | * or initial counter value for the encryption or decryption operation. |
| 1438 | * |
| 1439 | * The application must call psa_cipher_encrypt_setup() before |
| 1440 | * calling this function. |
| 1441 | * |
| 1442 | * If this function returns an error status, the operation becomes inactive. |
| 1443 | * |
| 1444 | * \note When encrypting, applications should use psa_cipher_generate_iv() |
| 1445 | * instead of this function, unless implementing a protocol that requires |
| 1446 | * a non-random IV. |
| 1447 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1448 | * \param[in,out] operation Active cipher operation. |
| 1449 | * \param[in] iv Buffer containing the IV to use. |
| 1450 | * \param iv_length Size of the IV in bytes. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1451 | * |
| 1452 | * \retval #PSA_SUCCESS |
| 1453 | * Success. |
| 1454 | * \retval #PSA_ERROR_BAD_STATE |
| 1455 | * The operation state is not valid (not started, or IV already set). |
| 1456 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1457 | * The size of \p iv is not acceptable for the chosen algorithm, |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1458 | * or the chosen algorithm does not use an IV. |
| 1459 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1460 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1461 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1462 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1463 | */ |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1464 | psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation, |
| 1465 | const unsigned char *iv, |
| 1466 | size_t iv_length); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1467 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1468 | /** Encrypt or decrypt a message fragment in an active cipher operation. |
| 1469 | * |
Gilles Peskine | 9ac9426 | 2018-07-12 20:15:32 +0200 | [diff] [blame] | 1470 | * Before calling this function, you must: |
| 1471 | * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(). |
| 1472 | * The choice of setup function determines whether this function |
| 1473 | * encrypts or decrypts its input. |
| 1474 | * 2. If the algorithm requires an IV, call psa_cipher_generate_iv() |
| 1475 | * (recommended when encrypting) or psa_cipher_set_iv(). |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1476 | * |
| 1477 | * If this function returns an error status, the operation becomes inactive. |
| 1478 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1479 | * \param[in,out] operation Active cipher operation. |
| 1480 | * \param[in] input Buffer containing the message fragment to |
| 1481 | * encrypt or decrypt. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1482 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1483 | * \param[out] output Buffer where the output is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1484 | * \param output_size Size of the \p output buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1485 | * \param[out] output_length On success, the number of bytes |
| 1486 | * that make up the returned output. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1487 | * |
| 1488 | * \retval #PSA_SUCCESS |
| 1489 | * Success. |
| 1490 | * \retval #PSA_ERROR_BAD_STATE |
| 1491 | * The operation state is not valid (not started, IV required but |
| 1492 | * not set, or already completed). |
| 1493 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 1494 | * The size of the \p output buffer is too small. |
| 1495 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1496 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1497 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1498 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1499 | */ |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1500 | psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, |
| 1501 | const uint8_t *input, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1502 | size_t input_length, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1503 | unsigned char *output, |
| 1504 | size_t output_size, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1505 | size_t *output_length); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1506 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1507 | /** Finish encrypting or decrypting a message in a cipher operation. |
| 1508 | * |
| 1509 | * The application must call psa_cipher_encrypt_setup() or |
| 1510 | * psa_cipher_decrypt_setup() before calling this function. The choice |
| 1511 | * of setup function determines whether this function encrypts or |
| 1512 | * decrypts its input. |
| 1513 | * |
| 1514 | * This function finishes the encryption or decryption of the message |
| 1515 | * formed by concatenating the inputs passed to preceding calls to |
| 1516 | * psa_cipher_update(). |
| 1517 | * |
| 1518 | * When this function returns, the operation becomes inactive. |
| 1519 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1520 | * \param[in,out] operation Active cipher operation. |
| 1521 | * \param[out] output Buffer where the output is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1522 | * \param output_size Size of the \p output buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1523 | * \param[out] output_length On success, the number of bytes |
| 1524 | * that make up the returned output. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1525 | * |
| 1526 | * \retval #PSA_SUCCESS |
| 1527 | * Success. |
| 1528 | * \retval #PSA_ERROR_BAD_STATE |
| 1529 | * The operation state is not valid (not started, IV required but |
| 1530 | * not set, or already completed). |
| 1531 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 1532 | * The size of the \p output buffer is too small. |
| 1533 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1534 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1535 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1536 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1537 | */ |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1538 | psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1539 | uint8_t *output, |
Moran Peker | 0071b87 | 2018-04-22 20:16:58 +0300 | [diff] [blame] | 1540 | size_t output_size, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1541 | size_t *output_length); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1542 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1543 | /** Abort a cipher operation. |
| 1544 | * |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1545 | * Aborting an operation frees all associated resources except for the |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 1546 | * \p operation structure itself. Once aborted, the operation object |
| 1547 | * can be reused for another operation by calling |
| 1548 | * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1549 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 1550 | * You may call this function any time after the operation object has |
| 1551 | * been initialized by any of the following methods: |
| 1552 | * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(), |
| 1553 | * whether it succeeds or not. |
| 1554 | * - Initializing the \c struct to all-bits-zero. |
| 1555 | * - Initializing the \c struct to logical zeros, e.g. |
| 1556 | * `psa_cipher_operation_t operation = {0}`. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1557 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 1558 | * In particular, calling psa_cipher_abort() after the operation has been |
| 1559 | * terminated by a call to psa_cipher_abort() or psa_cipher_finish() |
| 1560 | * is safe and has no effect. |
| 1561 | * |
| 1562 | * \param[in,out] operation Initialized cipher operation. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1563 | * |
| 1564 | * \retval #PSA_SUCCESS |
| 1565 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1566 | * \p operation is not an active cipher operation. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1567 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1568 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1569 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1570 | */ |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1571 | psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation); |
| 1572 | |
| 1573 | /**@}*/ |
| 1574 | |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1575 | /** \defgroup aead Authenticated encryption with associated data (AEAD) |
| 1576 | * @{ |
| 1577 | */ |
| 1578 | |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1579 | /** Process an authenticated encryption operation. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1580 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1581 | * \param handle Handle to the key to use for the operation. |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1582 | * \param alg The AEAD algorithm to compute |
| 1583 | * (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 1584 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1585 | * \param[in] nonce Nonce or IV to use. |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1586 | * \param nonce_length Size of the \p nonce buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1587 | * \param[in] additional_data Additional data that will be authenticated |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1588 | * but not encrypted. |
| 1589 | * \param additional_data_length Size of \p additional_data in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1590 | * \param[in] plaintext Data that will be authenticated and |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1591 | * encrypted. |
| 1592 | * \param plaintext_length Size of \p plaintext in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1593 | * \param[out] ciphertext Output buffer for the authenticated and |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1594 | * encrypted data. The additional data is not |
| 1595 | * part of this output. For algorithms where the |
| 1596 | * encrypted data and the authentication tag |
| 1597 | * are defined as separate outputs, the |
| 1598 | * authentication tag is appended to the |
| 1599 | * encrypted data. |
| 1600 | * \param ciphertext_size Size of the \p ciphertext buffer in bytes. |
| 1601 | * This must be at least |
| 1602 | * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg, |
| 1603 | * \p plaintext_length). |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1604 | * \param[out] ciphertext_length On success, the size of the output |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1605 | * in the \b ciphertext buffer. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1606 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1607 | * \retval #PSA_SUCCESS |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1608 | * Success. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1609 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1610 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1611 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1612 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1613 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1614 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1615 | * \p alg is not supported or is not an AEAD algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1616 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1617 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1618 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1619 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1620 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1621 | * The library has not been previously initialized by psa_crypto_init(). |
| 1622 | * It is implementation-dependent whether a failure to initialize |
| 1623 | * results in this error code. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1624 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1625 | psa_status_t psa_aead_encrypt(psa_key_handle_t handle, |
Gilles Peskine | 9fb0e01 | 2018-07-19 15:51:49 +0200 | [diff] [blame] | 1626 | psa_algorithm_t alg, |
| 1627 | const uint8_t *nonce, |
| 1628 | size_t nonce_length, |
| 1629 | const uint8_t *additional_data, |
| 1630 | size_t additional_data_length, |
| 1631 | const uint8_t *plaintext, |
| 1632 | size_t plaintext_length, |
| 1633 | uint8_t *ciphertext, |
| 1634 | size_t ciphertext_size, |
| 1635 | size_t *ciphertext_length); |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1636 | |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1637 | /** Process an authenticated decryption operation. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1638 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1639 | * \param handle Handle to the key to use for the operation. |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1640 | * \param alg The AEAD algorithm to compute |
| 1641 | * (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 1642 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1643 | * \param[in] nonce Nonce or IV to use. |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1644 | * \param nonce_length Size of the \p nonce buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1645 | * \param[in] additional_data Additional data that has been authenticated |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1646 | * but not encrypted. |
| 1647 | * \param additional_data_length Size of \p additional_data in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1648 | * \param[in] ciphertext Data that has been authenticated and |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1649 | * encrypted. For algorithms where the |
| 1650 | * encrypted data and the authentication tag |
| 1651 | * are defined as separate inputs, the buffer |
| 1652 | * must contain the encrypted data followed |
| 1653 | * by the authentication tag. |
| 1654 | * \param ciphertext_length Size of \p ciphertext in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1655 | * \param[out] plaintext Output buffer for the decrypted data. |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1656 | * \param plaintext_size Size of the \p plaintext buffer in bytes. |
| 1657 | * This must be at least |
| 1658 | * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg, |
| 1659 | * \p ciphertext_length). |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1660 | * \param[out] plaintext_length On success, the size of the output |
mohammad1603 | fb5b9cb | 2018-06-06 13:44:27 +0300 | [diff] [blame] | 1661 | * in the \b plaintext buffer. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1662 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1663 | * \retval #PSA_SUCCESS |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1664 | * Success. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1665 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1666 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1667 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1668 | * The ciphertext is not authentic. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1669 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1670 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1671 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1672 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1673 | * \p alg is not supported or is not an AEAD algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1674 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1675 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1676 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1677 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1678 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1679 | * The library has not been previously initialized by psa_crypto_init(). |
| 1680 | * It is implementation-dependent whether a failure to initialize |
| 1681 | * results in this error code. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1682 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1683 | psa_status_t psa_aead_decrypt(psa_key_handle_t handle, |
Gilles Peskine | 9fb0e01 | 2018-07-19 15:51:49 +0200 | [diff] [blame] | 1684 | psa_algorithm_t alg, |
| 1685 | const uint8_t *nonce, |
| 1686 | size_t nonce_length, |
| 1687 | const uint8_t *additional_data, |
| 1688 | size_t additional_data_length, |
| 1689 | const uint8_t *ciphertext, |
| 1690 | size_t ciphertext_length, |
| 1691 | uint8_t *plaintext, |
| 1692 | size_t plaintext_size, |
| 1693 | size_t *plaintext_length); |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1694 | |
| 1695 | /**@}*/ |
| 1696 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1697 | /** \defgroup asymmetric Asymmetric cryptography |
| 1698 | * @{ |
| 1699 | */ |
| 1700 | |
| 1701 | /** |
| 1702 | * \brief Sign a hash or short message with a private key. |
| 1703 | * |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 1704 | * Note that to perform a hash-and-sign signature algorithm, you must |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1705 | * first calculate the hash by calling psa_hash_setup(), psa_hash_update() |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 1706 | * and psa_hash_finish(). Then pass the resulting hash as the \p hash |
| 1707 | * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg) |
| 1708 | * to determine the hash algorithm to use. |
| 1709 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1710 | * \param handle Handle to the key to use for the operation. |
| 1711 | * It must be an asymmetric key pair. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1712 | * \param alg A signature algorithm that is compatible with |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1713 | * the type of \p key. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1714 | * \param[in] hash The hash or message to sign. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1715 | * \param hash_length Size of the \p hash buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1716 | * \param[out] signature Buffer where the signature is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1717 | * \param signature_size Size of the \p signature buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1718 | * \param[out] signature_length On success, the number of bytes |
| 1719 | * that make up the returned signature value. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1720 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1721 | * \retval #PSA_SUCCESS |
| 1722 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1723 | * The size of the \p signature buffer is too small. You can |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1724 | * determine a sufficient buffer size by calling |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 1725 | * #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] | 1726 | * 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] | 1727 | * respectively of \p key. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1728 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1729 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1730 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1731 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1732 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1733 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1734 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1735 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1736 | * The library has not been previously initialized by psa_crypto_init(). |
| 1737 | * It is implementation-dependent whether a failure to initialize |
| 1738 | * results in this error code. |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1739 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1740 | psa_status_t psa_asymmetric_sign(psa_key_handle_t handle, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1741 | psa_algorithm_t alg, |
| 1742 | const uint8_t *hash, |
| 1743 | size_t hash_length, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1744 | uint8_t *signature, |
| 1745 | size_t signature_size, |
| 1746 | size_t *signature_length); |
| 1747 | |
| 1748 | /** |
| 1749 | * \brief Verify the signature a hash or short message using a public key. |
| 1750 | * |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 1751 | * Note that to perform a hash-and-sign signature algorithm, you must |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1752 | * first calculate the hash by calling psa_hash_setup(), psa_hash_update() |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 1753 | * and psa_hash_finish(). Then pass the resulting hash as the \p hash |
| 1754 | * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg) |
| 1755 | * to determine the hash algorithm to use. |
| 1756 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1757 | * \param handle Handle to the key to use for the operation. |
| 1758 | * It must be a public key or an asymmetric key pair. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1759 | * \param alg A signature algorithm that is compatible with |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1760 | * the type of \p key. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1761 | * \param[in] hash The hash or message whose signature is to be |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 1762 | * verified. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1763 | * \param hash_length Size of the \p hash buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1764 | * \param[in] signature Buffer containing the signature to verify. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1765 | * \param signature_length Size of the \p signature buffer in bytes. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1766 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1767 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1768 | * The signature is valid. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1769 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1770 | * The calculation was perfomed successfully, but the passed |
| 1771 | * signature is not a valid signature. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1772 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1773 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1774 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1775 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1776 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1777 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1778 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1779 | * The library has not been previously initialized by psa_crypto_init(). |
| 1780 | * It is implementation-dependent whether a failure to initialize |
| 1781 | * results in this error code. |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1782 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1783 | psa_status_t psa_asymmetric_verify(psa_key_handle_t handle, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1784 | psa_algorithm_t alg, |
| 1785 | const uint8_t *hash, |
| 1786 | size_t hash_length, |
Gilles Peskine | e9191ff | 2018-06-27 14:58:41 +0200 | [diff] [blame] | 1787 | const uint8_t *signature, |
Gilles Peskine | 526fab0 | 2018-06-27 18:19:40 +0200 | [diff] [blame] | 1788 | size_t signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1789 | |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 1790 | /** |
| 1791 | * \brief Encrypt a short message with a public key. |
| 1792 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1793 | * \param handle Handle to the key to use for the operation. |
| 1794 | * It must be a public key or an asymmetric |
| 1795 | * key pair. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1796 | * \param alg An asymmetric encryption algorithm that is |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1797 | * compatible with the type of \p key. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1798 | * \param[in] input The message to encrypt. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1799 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1800 | * \param[in] salt A salt or label, if supported by the |
| 1801 | * encryption algorithm. |
| 1802 | * If the algorithm does not support a |
| 1803 | * salt, pass \c NULL. |
| 1804 | * If the algorithm supports an optional |
| 1805 | * salt and you do not want to pass a salt, |
| 1806 | * pass \c NULL. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 1807 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1808 | * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is |
| 1809 | * supported. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1810 | * \param salt_length Size of the \p salt buffer in bytes. |
| 1811 | * If \p salt is \c NULL, pass 0. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1812 | * \param[out] output Buffer where the encrypted message is to |
| 1813 | * be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1814 | * \param output_size Size of the \p output buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1815 | * \param[out] output_length On success, the number of bytes |
| 1816 | * that make up the returned output. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 1817 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1818 | * \retval #PSA_SUCCESS |
| 1819 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1820 | * The size of the \p output buffer is too small. You can |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 1821 | * determine a sufficient buffer size by calling |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 1822 | * #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] | 1823 | * 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] | 1824 | * respectively of \p key. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1825 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1826 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1827 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1828 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1829 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1830 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1831 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1832 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1833 | * The library has not been previously initialized by psa_crypto_init(). |
| 1834 | * It is implementation-dependent whether a failure to initialize |
| 1835 | * results in this error code. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 1836 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1837 | psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle, |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 1838 | psa_algorithm_t alg, |
| 1839 | const uint8_t *input, |
| 1840 | size_t input_length, |
| 1841 | const uint8_t *salt, |
| 1842 | size_t salt_length, |
| 1843 | uint8_t *output, |
| 1844 | size_t output_size, |
| 1845 | size_t *output_length); |
| 1846 | |
| 1847 | /** |
| 1848 | * \brief Decrypt a short message with a private key. |
| 1849 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1850 | * \param handle Handle to the key to use for the operation. |
| 1851 | * It must be an asymmetric key pair. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1852 | * \param alg An asymmetric encryption algorithm that is |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1853 | * compatible with the type of \p key. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1854 | * \param[in] input The message to decrypt. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1855 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1856 | * \param[in] salt A salt or label, if supported by the |
| 1857 | * encryption algorithm. |
| 1858 | * If the algorithm does not support a |
| 1859 | * salt, pass \c NULL. |
| 1860 | * If the algorithm supports an optional |
| 1861 | * salt and you do not want to pass a salt, |
| 1862 | * pass \c NULL. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 1863 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1864 | * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is |
| 1865 | * supported. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1866 | * \param salt_length Size of the \p salt buffer in bytes. |
| 1867 | * If \p salt is \c NULL, pass 0. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1868 | * \param[out] output Buffer where the decrypted message is to |
| 1869 | * be written. |
| 1870 | * \param output_size Size of the \c output buffer in bytes. |
| 1871 | * \param[out] output_length On success, the number of bytes |
| 1872 | * that make up the returned output. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 1873 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1874 | * \retval #PSA_SUCCESS |
| 1875 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1876 | * The size of the \p output buffer is too small. You can |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 1877 | * determine a sufficient buffer size by calling |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 1878 | * #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] | 1879 | * 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] | 1880 | * respectively of \p key. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1881 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1882 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1883 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1884 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1885 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1886 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1887 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
| 1888 | * \retval #PSA_ERROR_INVALID_PADDING |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1889 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1890 | * The library has not been previously initialized by psa_crypto_init(). |
| 1891 | * It is implementation-dependent whether a failure to initialize |
| 1892 | * results in this error code. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 1893 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1894 | psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle, |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 1895 | psa_algorithm_t alg, |
| 1896 | const uint8_t *input, |
| 1897 | size_t input_length, |
| 1898 | const uint8_t *salt, |
| 1899 | size_t salt_length, |
| 1900 | uint8_t *output, |
| 1901 | size_t output_size, |
| 1902 | size_t *output_length); |
| 1903 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1904 | /**@}*/ |
| 1905 | |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 1906 | /** \defgroup generators Generators |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 1907 | * @{ |
| 1908 | */ |
| 1909 | |
| 1910 | /** The type of the state data structure for generators. |
| 1911 | * |
| 1912 | * Before calling any function on a generator, the application must |
| 1913 | * initialize it by any of the following means: |
| 1914 | * - Set the structure to all-bits-zero, for example: |
| 1915 | * \code |
| 1916 | * psa_crypto_generator_t generator; |
| 1917 | * memset(&generator, 0, sizeof(generator)); |
| 1918 | * \endcode |
| 1919 | * - Initialize the structure to logical zero values, for example: |
| 1920 | * \code |
| 1921 | * psa_crypto_generator_t generator = {0}; |
| 1922 | * \endcode |
| 1923 | * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT, |
| 1924 | * for example: |
| 1925 | * \code |
| 1926 | * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 1927 | * \endcode |
| 1928 | * - Assign the result of the function psa_crypto_generator_init() |
| 1929 | * to the structure, for example: |
| 1930 | * \code |
| 1931 | * psa_crypto_generator_t generator; |
| 1932 | * generator = psa_crypto_generator_init(); |
| 1933 | * \endcode |
| 1934 | * |
| 1935 | * This is an implementation-defined \c struct. Applications should not |
| 1936 | * make any assumptions about the content of this structure except |
| 1937 | * as directed by the documentation of a specific implementation. |
| 1938 | */ |
| 1939 | typedef struct psa_crypto_generator_s psa_crypto_generator_t; |
| 1940 | |
| 1941 | /** \def PSA_CRYPTO_GENERATOR_INIT |
| 1942 | * |
| 1943 | * This macro returns a suitable initializer for a generator object |
| 1944 | * of type #psa_crypto_generator_t. |
| 1945 | */ |
| 1946 | #ifdef __DOXYGEN_ONLY__ |
| 1947 | /* This is an example definition for documentation purposes. |
| 1948 | * Implementations should define a suitable value in `crypto_struct.h`. |
| 1949 | */ |
| 1950 | #define PSA_CRYPTO_GENERATOR_INIT {0} |
| 1951 | #endif |
| 1952 | |
| 1953 | /** Return an initial value for a generator object. |
| 1954 | */ |
| 1955 | static psa_crypto_generator_t psa_crypto_generator_init(void); |
| 1956 | |
| 1957 | /** Retrieve the current capacity of a generator. |
| 1958 | * |
| 1959 | * The capacity of a generator is the maximum number of bytes that it can |
| 1960 | * return. Reading *N* bytes from a generator reduces its capacity by *N*. |
| 1961 | * |
| 1962 | * \param[in] generator The generator to query. |
| 1963 | * \param[out] capacity On success, the capacity of the generator. |
| 1964 | * |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 1965 | * \retval #PSA_SUCCESS |
| 1966 | * \retval #PSA_ERROR_BAD_STATE |
| 1967 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 1968 | */ |
| 1969 | psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator, |
| 1970 | size_t *capacity); |
| 1971 | |
| 1972 | /** Read some data from a generator. |
| 1973 | * |
| 1974 | * This function reads and returns a sequence of bytes from a generator. |
| 1975 | * The data that is read is discarded from the generator. The generator's |
| 1976 | * capacity is decreased by the number of bytes read. |
| 1977 | * |
| 1978 | * \param[in,out] generator The generator object to read from. |
| 1979 | * \param[out] output Buffer where the generator output will be |
| 1980 | * written. |
| 1981 | * \param output_length Number of bytes to output. |
| 1982 | * |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 1983 | * \retval #PSA_SUCCESS |
| 1984 | * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 1985 | * There were fewer than \p output_length bytes |
| 1986 | * in the generator. Note that in this case, no |
| 1987 | * output is written to the output buffer. |
| 1988 | * The generator's capacity is set to 0, thus |
| 1989 | * subsequent calls to this function will not |
| 1990 | * succeed, even with a smaller output buffer. |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 1991 | * \retval #PSA_ERROR_BAD_STATE |
| 1992 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1993 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1994 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1995 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 1996 | */ |
| 1997 | psa_status_t psa_generator_read(psa_crypto_generator_t *generator, |
| 1998 | uint8_t *output, |
| 1999 | size_t output_length); |
| 2000 | |
| 2001 | /** Create a symmetric key from data read from a generator. |
| 2002 | * |
| 2003 | * This function reads a sequence of bytes from a generator and imports |
| 2004 | * these bytes as a key. |
| 2005 | * The data that is read is discarded from the generator. The generator's |
| 2006 | * capacity is decreased by the number of bytes read. |
| 2007 | * |
| 2008 | * This function is equivalent to calling #psa_generator_read and |
| 2009 | * passing the resulting output to #psa_import_key, but |
| 2010 | * if the implementation provides an isolation boundary then |
| 2011 | * the key material is not exposed outside the isolation boundary. |
| 2012 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2013 | * \param handle Handle to the slot where the key will be stored. |
| 2014 | * This must be a valid slot for a key of the chosen |
| 2015 | * type: it must have been obtained by calling |
| 2016 | * psa_allocate_key() or psa_create_key() with the |
| 2017 | * correct \p type and with a maximum size that is |
| 2018 | * compatible with \p bits. |
| 2019 | * It must not contain any key material yet. |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2020 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). |
| 2021 | * This must be a symmetric key type. |
| 2022 | * \param bits Key size in bits. |
| 2023 | * \param[in,out] generator The generator object to read from. |
| 2024 | * |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 2025 | * \retval #PSA_SUCCESS |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2026 | * Success. |
Gilles Peskine | 23fd2bd | 2018-12-11 15:51:32 +0100 | [diff] [blame] | 2027 | * If the key is persistent, the key material and the key's metadata |
| 2028 | * have been saved to persistent storage. |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 2029 | * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2030 | * There were fewer than \p output_length bytes |
| 2031 | * in the generator. Note that in this case, no |
| 2032 | * output is written to the output buffer. |
| 2033 | * The generator's capacity is set to 0, thus |
| 2034 | * subsequent calls to this function will not |
| 2035 | * succeed, even with a smaller output buffer. |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 2036 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2037 | * The key type or key size is not supported, either by the |
| 2038 | * implementation in general or in this particular slot. |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 2039 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2040 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 2041 | * \retval #PSA_ERROR_OCCUPIED_SLOT |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2042 | * There is already a key in the specified slot. |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 2043 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2044 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 2045 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2046 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2047 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 2048 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 2049 | * The library has not been previously initialized by psa_crypto_init(). |
| 2050 | * It is implementation-dependent whether a failure to initialize |
| 2051 | * results in this error code. |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2052 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2053 | psa_status_t psa_generator_import_key(psa_key_handle_t handle, |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2054 | psa_key_type_t type, |
| 2055 | size_t bits, |
| 2056 | psa_crypto_generator_t *generator); |
| 2057 | |
| 2058 | /** Abort a generator. |
| 2059 | * |
| 2060 | * Once a generator has been aborted, its capacity is zero. |
| 2061 | * Aborting a generator frees all associated resources except for the |
| 2062 | * \c generator structure itself. |
| 2063 | * |
| 2064 | * This function may be called at any time as long as the generator |
| 2065 | * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to |
| 2066 | * psa_crypto_generator_init() or a zero value. In particular, it is valid |
| 2067 | * to call psa_generator_abort() twice, or to call psa_generator_abort() |
| 2068 | * on a generator that has not been set up. |
| 2069 | * |
| 2070 | * Once aborted, the generator object may be called. |
| 2071 | * |
| 2072 | * \param[in,out] generator The generator to abort. |
| 2073 | * |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 2074 | * \retval #PSA_SUCCESS |
| 2075 | * \retval #PSA_ERROR_BAD_STATE |
| 2076 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2077 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2078 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2079 | */ |
| 2080 | psa_status_t psa_generator_abort(psa_crypto_generator_t *generator); |
| 2081 | |
Gilles Peskine | 8feb3a8 | 2018-09-18 12:06:11 +0200 | [diff] [blame] | 2082 | /** Use the maximum possible capacity for a generator. |
| 2083 | * |
| 2084 | * Use this value as the capacity argument when setting up a generator |
| 2085 | * to indicate that the generator should have the maximum possible capacity. |
| 2086 | * The value of the maximum possible capacity depends on the generator |
| 2087 | * algorithm. |
| 2088 | */ |
| 2089 | #define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1)) |
| 2090 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2091 | /**@}*/ |
| 2092 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2093 | /** \defgroup derivation Key derivation |
| 2094 | * @{ |
| 2095 | */ |
| 2096 | |
| 2097 | /** Set up a key derivation operation. |
| 2098 | * |
| 2099 | * A key derivation algorithm takes three inputs: a secret input \p key and |
| 2100 | * two non-secret inputs \p label and p salt. |
| 2101 | * The result of this function is a byte generator which can |
| 2102 | * be used to produce keys and other cryptographic material. |
| 2103 | * |
| 2104 | * The role of \p label and \p salt is as follows: |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 2105 | * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step |
| 2106 | * and \p label is the info string used in the "expand" step. |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2107 | * |
Jaeden Amero | 9e919c6 | 2019-01-07 15:41:50 +0000 | [diff] [blame] | 2108 | * \param[in,out] generator The generator object to set up. It must have |
| 2109 | * been initialized as per the documentation for |
| 2110 | * #psa_crypto_generator_t and not yet in use. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2111 | * \param handle Handle to the secret key. |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2112 | * \param alg The key derivation algorithm to compute |
| 2113 | * (\c PSA_ALG_XXX value such that |
| 2114 | * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true). |
| 2115 | * \param[in] salt Salt to use. |
| 2116 | * \param salt_length Size of the \p salt buffer in bytes. |
| 2117 | * \param[in] label Label to use. |
| 2118 | * \param label_length Size of the \p label buffer in bytes. |
| 2119 | * \param capacity The maximum number of bytes that the |
| 2120 | * generator will be able to provide. |
| 2121 | * |
| 2122 | * \retval #PSA_SUCCESS |
| 2123 | * Success. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2124 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2125 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 2126 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 2127 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2128 | * \c key is not compatible with \c alg, |
| 2129 | * or \p capacity is too large for the specified algorithm and key. |
| 2130 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2131 | * \c alg is not supported or is not a key derivation algorithm. |
| 2132 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2133 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2134 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2135 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 2136 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 2137 | * The library has not been previously initialized by psa_crypto_init(). |
| 2138 | * It is implementation-dependent whether a failure to initialize |
| 2139 | * results in this error code. |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2140 | */ |
| 2141 | psa_status_t psa_key_derivation(psa_crypto_generator_t *generator, |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2142 | psa_key_handle_t handle, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2143 | psa_algorithm_t alg, |
| 2144 | const uint8_t *salt, |
| 2145 | size_t salt_length, |
| 2146 | const uint8_t *label, |
| 2147 | size_t label_length, |
| 2148 | size_t capacity); |
| 2149 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2150 | /** Set up a key agreement operation. |
| 2151 | * |
| 2152 | * A key agreement algorithm takes two inputs: a private key \p private_key |
| 2153 | * a public key \p peer_key. |
| 2154 | * The result of this function is a byte generator which can |
| 2155 | * be used to produce keys and other cryptographic material. |
| 2156 | * |
Gilles Peskine | 211a436 | 2018-10-25 22:22:31 +0200 | [diff] [blame] | 2157 | * The resulting generator always has the maximum capacity permitted by |
| 2158 | * the algorithm. |
| 2159 | * |
Jaeden Amero | 9e919c6 | 2019-01-07 15:41:50 +0000 | [diff] [blame] | 2160 | * \param[in,out] generator The generator object to set up. It must have |
| 2161 | * been initialized as per the documentation for |
| 2162 | * #psa_crypto_generator_t and not yet in use. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2163 | * \param private_key Handle to the private key to use. |
Gilles Peskine | d171e78 | 2018-11-15 17:46:21 +0100 | [diff] [blame] | 2164 | * \param[in] peer_key Public key of the peer. It must be |
| 2165 | * in the same format that psa_import_key() |
| 2166 | * accepts. The standard formats for public |
| 2167 | * keys are documented in the documentation |
| 2168 | * of psa_export_public_key(). |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2169 | * \param peer_key_length Size of \p peer_key in bytes. |
| 2170 | * \param alg The key agreement algorithm to compute |
| 2171 | * (\c PSA_ALG_XXX value such that |
| 2172 | * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true). |
| 2173 | * |
| 2174 | * \retval #PSA_SUCCESS |
| 2175 | * Success. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2176 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2177 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 2178 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 2179 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2180 | * \c private_key is not compatible with \c alg, |
| 2181 | * or \p peer_key is not valid for \c alg or not compatible with |
| 2182 | * \c private_key. |
| 2183 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2184 | * \c alg is not supported or is not a key derivation algorithm. |
| 2185 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2186 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2187 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2188 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2189 | */ |
| 2190 | psa_status_t psa_key_agreement(psa_crypto_generator_t *generator, |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2191 | psa_key_handle_t private_key, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2192 | const uint8_t *peer_key, |
| 2193 | size_t peer_key_length, |
| 2194 | psa_algorithm_t alg); |
| 2195 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2196 | /**@}*/ |
| 2197 | |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 2198 | /** \defgroup random Random generation |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2199 | * @{ |
| 2200 | */ |
| 2201 | |
| 2202 | /** |
| 2203 | * \brief Generate random bytes. |
| 2204 | * |
| 2205 | * \warning This function **can** fail! Callers MUST check the return status |
| 2206 | * and MUST NOT use the content of the output buffer if the return |
| 2207 | * status is not #PSA_SUCCESS. |
| 2208 | * |
| 2209 | * \note To generate a key, use psa_generate_key() instead. |
| 2210 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2211 | * \param[out] output Output buffer for the generated data. |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2212 | * \param output_size Number of bytes to generate and output. |
| 2213 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2214 | * \retval #PSA_SUCCESS |
| 2215 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2216 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
| 2217 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2218 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2219 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 2220 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 2221 | * The library has not been previously initialized by psa_crypto_init(). |
| 2222 | * It is implementation-dependent whether a failure to initialize |
| 2223 | * results in this error code. |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2224 | */ |
| 2225 | psa_status_t psa_generate_random(uint8_t *output, |
| 2226 | size_t output_size); |
| 2227 | |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 2228 | /** Extra parameters for RSA key generation. |
| 2229 | * |
Gilles Peskine | be42f31 | 2018-07-13 14:38:15 +0200 | [diff] [blame] | 2230 | * 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] | 2231 | * parameter to psa_generate_key(). |
| 2232 | */ |
| 2233 | typedef struct { |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 2234 | uint32_t e; /**< Public exponent value. Default: 65537. */ |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 2235 | } psa_generate_key_extra_rsa; |
| 2236 | |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2237 | /** |
| 2238 | * \brief Generate a key or key pair. |
| 2239 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2240 | * \param handle Handle to the slot where the key will be stored. |
| 2241 | * This must be a valid slot for a key of the chosen |
| 2242 | * type: it must have been obtained by calling |
| 2243 | * psa_allocate_key() or psa_create_key() with the |
| 2244 | * correct \p type and with a maximum size that is |
| 2245 | * compatible with \p bits. |
| 2246 | * It must not contain any key material yet. |
Gilles Peskine | 4e69d7a | 2018-06-19 20:19:14 +0200 | [diff] [blame] | 2247 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). |
| 2248 | * \param bits Key size in bits. |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 2249 | * \param[in] extra Extra parameters for key generation. The |
Gilles Peskine | 4e69d7a | 2018-06-19 20:19:14 +0200 | [diff] [blame] | 2250 | * interpretation of this parameter depends on |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2251 | * \p type. All types support \c NULL to use |
Gilles Peskine | 3fa675c | 2018-07-12 01:31:03 +0200 | [diff] [blame] | 2252 | * default parameters. Implementation that support |
| 2253 | * the generation of vendor-specific key types |
| 2254 | * that allow extra parameters shall document |
| 2255 | * the format of these extra parameters and |
| 2256 | * the default values. For standard parameters, |
| 2257 | * the meaning of \p extra is as follows: |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2258 | * - For a symmetric key type (a type such |
Gilles Peskine | 3fa675c | 2018-07-12 01:31:03 +0200 | [diff] [blame] | 2259 | * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is |
| 2260 | * false), \p extra must be \c NULL. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2261 | * - For an elliptic curve key type (a type |
Gilles Peskine | 3fa675c | 2018-07-12 01:31:03 +0200 | [diff] [blame] | 2262 | * such that #PSA_KEY_TYPE_IS_ECC(\p type) is |
| 2263 | * false), \p extra must be \c NULL. |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 2264 | * - For an RSA key (\p type is |
| 2265 | * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an |
| 2266 | * optional #psa_generate_key_extra_rsa structure |
Gilles Peskine | 3fa675c | 2018-07-12 01:31:03 +0200 | [diff] [blame] | 2267 | * specifying the public exponent. The |
| 2268 | * default public exponent used when \p extra |
| 2269 | * is \c NULL is 65537. |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 2270 | * \param extra_size Size of the buffer that \p extra |
| 2271 | * points to, in bytes. Note that if \p extra is |
| 2272 | * \c NULL then \p extra_size must be zero. |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2273 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2274 | * \retval #PSA_SUCCESS |
Gilles Peskine | 23fd2bd | 2018-12-11 15:51:32 +0100 | [diff] [blame] | 2275 | * Success. |
| 2276 | * If the key is persistent, the key material and the key's metadata |
| 2277 | * have been saved to persistent storage. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2278 | * \retval #PSA_ERROR_INVALID_HANDLE |
| 2279 | * \retval #PSA_ERROR_OCCUPIED_SLOT |
| 2280 | * There is already a key in the specified slot. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2281 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2282 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2283 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2284 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
| 2285 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2286 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2287 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 2288 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 2289 | * The library has not been previously initialized by psa_crypto_init(). |
| 2290 | * It is implementation-dependent whether a failure to initialize |
| 2291 | * results in this error code. |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2292 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2293 | psa_status_t psa_generate_key(psa_key_handle_t handle, |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2294 | psa_key_type_t type, |
| 2295 | size_t bits, |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 2296 | const void *extra, |
| 2297 | size_t extra_size); |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2298 | |
| 2299 | /**@}*/ |
| 2300 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2301 | #ifdef __cplusplus |
| 2302 | } |
| 2303 | #endif |
| 2304 | |
Gilles Peskine | 0cad07c | 2018-06-27 19:49:02 +0200 | [diff] [blame] | 2305 | /* The file "crypto_sizes.h" contains definitions for size calculation |
| 2306 | * macros whose definitions are implementation-specific. */ |
| 2307 | #include "crypto_sizes.h" |
| 2308 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2309 | /* The file "crypto_struct.h" contains definitions for |
| 2310 | * implementation-specific structs that are declared above. */ |
| 2311 | #include "crypto_struct.h" |
| 2312 | |
| 2313 | /* The file "crypto_extra.h" contains vendor-specific definitions. This |
| 2314 | * can include vendor-defined algorithms, extra functions, etc. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2315 | #include "crypto_extra.h" |
| 2316 | |
| 2317 | #endif /* PSA_CRYPTO_H */ |