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 | * |
| 234 | * \param handle The key handle to close. |
| 235 | * |
| 236 | * \retval #PSA_SUCCESS |
| 237 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 238 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
Gilles Peskine | f535eb2 | 2018-11-30 14:08:36 +0100 | [diff] [blame] | 239 | */ |
| 240 | psa_status_t psa_close_key(psa_key_handle_t handle); |
| 241 | |
Gilles Peskine | 3cac8c4 | 2018-11-30 14:07:45 +0100 | [diff] [blame] | 242 | /**@}*/ |
| 243 | |
| 244 | /** \defgroup import_export Key import and export |
| 245 | * @{ |
| 246 | */ |
| 247 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 248 | /** |
| 249 | * \brief Import a key in binary format. |
| 250 | * |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 251 | * This function supports any output from psa_export_key(). Refer to the |
Gilles Peskine | f793393 | 2018-10-31 14:07:52 +0100 | [diff] [blame] | 252 | * documentation of psa_export_public_key() for the format of public keys |
| 253 | * and to the documentation of psa_export_key() for the format for |
| 254 | * other key types. |
| 255 | * |
| 256 | * This specification supports a single format for each key type. |
| 257 | * Implementations may support other formats as long as the standard |
| 258 | * format is supported. Implementations that support other formats |
| 259 | * should ensure that the formats are clearly unambiguous so as to |
| 260 | * minimize the risk that an invalid input is accidentally interpreted |
| 261 | * according to a different format. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 262 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 263 | * \param handle Handle to the slot where the key will be stored. |
| 264 | * This must be a valid slot for a key of the chosen |
| 265 | * type: it must have been obtained by calling |
| 266 | * psa_allocate_key() or psa_create_key() with the |
| 267 | * correct \p type and with a maximum size that is |
| 268 | * compatible with \p data. |
Gilles Peskine | f793393 | 2018-10-31 14:07:52 +0100 | [diff] [blame] | 269 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). On a successful |
| 270 | * import, the key slot will contain a key of this type. |
| 271 | * \param[in] data Buffer containing the key data. The content of this |
| 272 | * buffer is interpreted according to \p type. It must |
| 273 | * contain the format described in the documentation |
| 274 | * of psa_export_key() or psa_export_public_key() for |
| 275 | * the chosen type. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 276 | * \param data_length Size of the \p data buffer in bytes. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 277 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 278 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 279 | * Success. |
Gilles Peskine | 23fd2bd | 2018-12-11 15:51:32 +0100 | [diff] [blame] | 280 | * If the key is persistent, the key material and the key's metadata |
| 281 | * have been saved to persistent storage. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 282 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 283 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 284 | * The key type or key size is not supported, either by the |
| 285 | * implementation in general or in this particular slot. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 286 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 287 | * The key slot is invalid, |
| 288 | * or the key data is not correctly formatted. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 289 | * \retval #PSA_ERROR_OCCUPIED_SLOT |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 290 | * There is already a key in the specified slot. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 291 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 292 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 293 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 294 | * \retval #PSA_ERROR_STORAGE_FAILURE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 295 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 296 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 297 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 298 | * The library has not been previously initialized by psa_crypto_init(). |
| 299 | * It is implementation-dependent whether a failure to initialize |
| 300 | * results in this error code. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 301 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 302 | psa_status_t psa_import_key(psa_key_handle_t handle, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 303 | psa_key_type_t type, |
| 304 | const uint8_t *data, |
| 305 | size_t data_length); |
| 306 | |
| 307 | /** |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 308 | * \brief Destroy a key. |
Gilles Peskine | 154bd95 | 2018-04-19 08:38:16 +0200 | [diff] [blame] | 309 | * |
| 310 | * This function destroys the content of the key slot from both volatile |
| 311 | * memory and, if applicable, non-volatile storage. Implementations shall |
| 312 | * make a best effort to ensure that any previous content of the slot is |
| 313 | * unrecoverable. |
| 314 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 315 | * This function also erases any metadata such as policies and frees all |
| 316 | * resources associated with the key. |
Gilles Peskine | 154bd95 | 2018-04-19 08:38:16 +0200 | [diff] [blame] | 317 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 318 | * \param handle Handle to the key slot to erase. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 319 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 320 | * \retval #PSA_SUCCESS |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 321 | * The slot's content, if any, has been erased. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 322 | * \retval #PSA_ERROR_NOT_PERMITTED |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 323 | * The slot holds content and cannot be erased because it is |
| 324 | * read-only, either due to a policy or due to physical restrictions. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 325 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 326 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 327 | * There was an failure in communication with the cryptoprocessor. |
| 328 | * The key material may still be present in the cryptoprocessor. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 329 | * \retval #PSA_ERROR_STORAGE_FAILURE |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 330 | * The storage is corrupted. Implementations shall make a best effort |
| 331 | * to erase key material even in this stage, however applications |
| 332 | * should be aware that it may be impossible to guarantee that the |
| 333 | * key material is not recoverable in such cases. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 334 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 335 | * An unexpected condition which is not a storage corruption or |
| 336 | * a communication failure occurred. The cryptoprocessor may have |
| 337 | * been compromised. |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 338 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 339 | * The library has not been previously initialized by psa_crypto_init(). |
| 340 | * It is implementation-dependent whether a failure to initialize |
| 341 | * results in this error code. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 342 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 343 | psa_status_t psa_destroy_key(psa_key_handle_t handle); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 344 | |
| 345 | /** |
| 346 | * \brief Get basic metadata about a key. |
| 347 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 348 | * \param handle Handle to the key slot to query. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 349 | * \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] | 350 | * This may be a null pointer, in which case the key type |
| 351 | * is not written. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 352 | * \param[out] bits On success, the key size in bits. |
Gilles Peskine | 9a1ba0d | 2018-03-21 20:49:16 +0100 | [diff] [blame] | 353 | * This may be a null pointer, in which case the key size |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 354 | * is not written. |
| 355 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 356 | * \retval #PSA_SUCCESS |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 357 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 358 | * \retval #PSA_ERROR_EMPTY_SLOT |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 359 | * 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] | 360 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 361 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 362 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 363 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 364 | * The library has not been previously initialized by psa_crypto_init(). |
| 365 | * It is implementation-dependent whether a failure to initialize |
| 366 | * results in this error code. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 367 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 368 | psa_status_t psa_get_key_information(psa_key_handle_t handle, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 369 | psa_key_type_t *type, |
| 370 | size_t *bits); |
| 371 | |
| 372 | /** |
Jaeden Amero | 283dfd1 | 2019-01-11 12:06:22 +0000 | [diff] [blame] | 373 | * \brief Set domain parameters for a key. |
| 374 | * |
| 375 | * Some key types require additional domain parameters to be set before import |
| 376 | * or generation of the key. The domain parameters can be set with this |
| 377 | * function or, for key generation, through the \c extra parameter of |
| 378 | * psa_generate_key(). |
| 379 | * |
| 380 | * The format for the required domain parameters varies by the key type. |
Jaeden Amero | 1308fb5 | 2019-01-11 13:50:43 +0000 | [diff] [blame] | 381 | * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), |
| 382 | * the `Dss-Parms` format as defined by RFC 3279 §2.3.2. |
| 383 | * ``` |
| 384 | * Dss-Parms ::= SEQUENCE { |
| 385 | * p INTEGER, |
| 386 | * q INTEGER, |
| 387 | * g INTEGER |
| 388 | * } |
| 389 | * ``` |
Jaeden Amero | 8851c40 | 2019-01-11 14:20:03 +0000 | [diff] [blame] | 390 | * - For Diffie-Hellman key exchange keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY), the |
| 391 | * `DomainParameters` format as defined by RFC 3279 §2.3.3. |
| 392 | * ``` |
| 393 | * DomainParameters ::= SEQUENCE { |
| 394 | * p INTEGER, -- odd prime, p=jq +1 |
| 395 | * g INTEGER, -- generator, g |
| 396 | * q INTEGER, -- factor of p-1 |
| 397 | * j INTEGER OPTIONAL, -- subgroup factor |
| 398 | * validationParms ValidationParms OPTIONAL |
| 399 | * } |
| 400 | * ValidationParms ::= SEQUENCE { |
| 401 | * seed BIT STRING, |
| 402 | * pgenCounter INTEGER |
| 403 | * } |
| 404 | * ``` |
Jaeden Amero | 283dfd1 | 2019-01-11 12:06:22 +0000 | [diff] [blame] | 405 | * |
Gilles Peskine | 3a74e00 | 2019-01-18 17:11:25 +0100 | [diff] [blame^] | 406 | * \param handle Handle to the slot where the key will be stored. |
| 407 | * This must be a valid slot for a key of the chosen |
| 408 | * type: it must have been obtained by calling |
| 409 | * psa_allocate_key() or psa_create_key() with the |
| 410 | * correct \p type and with a maximum size that is |
| 411 | * compatible with \p data. It must not contain |
| 412 | * key material yet. |
| 413 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). When |
| 414 | * subsequently creating key material into \p handle, |
| 415 | * the type must be compatible. |
Jaeden Amero | 283dfd1 | 2019-01-11 12:06:22 +0000 | [diff] [blame] | 416 | * \param[in] data Buffer containing the key domain parameters. The content |
| 417 | * of this buffer is interpreted according to \p type. of |
| 418 | * psa_export_key() or psa_export_public_key() for the |
| 419 | * chosen type. |
| 420 | * \param data_length Size of the \p data buffer in bytes. |
| 421 | * |
| 422 | * \retval #PSA_SUCCESS |
| 423 | * \retval #PSA_ERROR_INVALID_HANDLE |
| 424 | * \retval #PSA_ERROR_OCCUPIED_SLOT |
| 425 | * There is already a key in the specified slot. |
| 426 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 427 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 428 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 429 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 430 | * \retval #PSA_ERROR_BAD_STATE |
| 431 | * The library has not been previously initialized by psa_crypto_init(). |
| 432 | * It is implementation-dependent whether a failure to initialize |
| 433 | * results in this error code. |
| 434 | */ |
| 435 | psa_status_t psa_set_key_domain_parameters(psa_key_handle_t handle, |
Gilles Peskine | 3a74e00 | 2019-01-18 17:11:25 +0100 | [diff] [blame^] | 436 | psa_key_type_t type, |
Jaeden Amero | 283dfd1 | 2019-01-11 12:06:22 +0000 | [diff] [blame] | 437 | const uint8_t *data, |
| 438 | size_t data_length); |
| 439 | |
| 440 | /** |
| 441 | * \brief Get domain parameters for a key. |
| 442 | * |
| 443 | * Get the domain parameters for a key with this function, if any. The format |
| 444 | * of the domain parameters written to \p data is specified in the |
| 445 | * documentation for psa_set_key_domain_parameters(). |
| 446 | * |
| 447 | * \param handle Handle to the key to get domain parameters from. |
| 448 | * \param[out] data On success, the key domain parameters. |
| 449 | * \param data_size Size of the \p data buffer in bytes. |
| 450 | * \param[out] data_length On success, the number of bytes |
| 451 | * that make up the key domain parameters data. |
| 452 | * |
| 453 | * \retval #PSA_SUCCESS |
| 454 | * \retval #PSA_ERROR_INVALID_HANDLE |
| 455 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 456 | * There is no key in the specified slot. |
| 457 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 458 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 459 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 460 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 461 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 462 | * \retval #PSA_ERROR_BAD_STATE |
| 463 | * The library has not been previously initialized by psa_crypto_init(). |
| 464 | * It is implementation-dependent whether a failure to initialize |
| 465 | * results in this error code. |
| 466 | */ |
| 467 | psa_status_t psa_get_key_domain_parameters(psa_key_handle_t handle, |
| 468 | uint8_t *data, |
| 469 | size_t data_size, |
| 470 | size_t *data_length); |
| 471 | |
| 472 | /** |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 473 | * \brief Export a key in binary format. |
| 474 | * |
| 475 | * The output of this function can be passed to psa_import_key() to |
| 476 | * create an equivalent object. |
| 477 | * |
Gilles Peskine | f793393 | 2018-10-31 14:07:52 +0100 | [diff] [blame] | 478 | * If the implementation of psa_import_key() supports other formats |
| 479 | * beyond the format specified here, the output from psa_export_key() |
| 480 | * must use the representation specified here, not the original |
| 481 | * representation. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 482 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 483 | * For standard key types, the output format is as follows: |
| 484 | * |
| 485 | * - For symmetric keys (including MAC keys), the format is the |
| 486 | * raw bytes of the key. |
| 487 | * - For DES, the key data consists of 8 bytes. The parity bits must be |
| 488 | * correct. |
| 489 | * - For Triple-DES, the format is the concatenation of the |
| 490 | * two or three DES keys. |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 491 | * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 492 | * is the non-encrypted DER encoding of the representation defined by |
| 493 | * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0. |
| 494 | * ``` |
| 495 | * RSAPrivateKey ::= SEQUENCE { |
Gilles Peskine | 4f6c77b | 2018-08-11 01:17:53 +0200 | [diff] [blame] | 496 | * version INTEGER, -- must be 0 |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 497 | * modulus INTEGER, -- n |
| 498 | * publicExponent INTEGER, -- e |
| 499 | * privateExponent INTEGER, -- d |
| 500 | * prime1 INTEGER, -- p |
| 501 | * prime2 INTEGER, -- q |
| 502 | * exponent1 INTEGER, -- d mod (p-1) |
| 503 | * exponent2 INTEGER, -- d mod (q-1) |
| 504 | * coefficient INTEGER, -- (inverse of q) mod p |
| 505 | * } |
| 506 | * ``` |
Jaeden Amero | 1308fb5 | 2019-01-11 13:50:43 +0000 | [diff] [blame] | 507 | * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the |
| 508 | * representation of the private key `x` as a big-endian byte string. The |
| 509 | * length of the byte string is the private key size in bytes (leading zeroes |
| 510 | * are not stripped). |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 511 | * - For elliptic curve key pairs (key types for which |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 512 | * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is |
Gilles Peskine | 6c6a023 | 2018-11-15 17:44:43 +0100 | [diff] [blame] | 513 | * a representation of the private value as a `ceiling(m/8)`-byte string |
| 514 | * where `m` is the bit size associated with the curve, i.e. the bit size |
| 515 | * of the order of the curve's coordinate field. This byte string is |
| 516 | * in little-endian order for Montgomery curves (curve types |
| 517 | * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass |
| 518 | * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX` |
| 519 | * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`). |
Gilles Peskine | f76aa77 | 2018-10-29 19:24:33 +0100 | [diff] [blame] | 520 | * This is the content of the `privateKey` field of the `ECPrivateKey` |
| 521 | * format defined by RFC 5915. |
Jaeden Amero | 8851c40 | 2019-01-11 14:20:03 +0000 | [diff] [blame] | 522 | * - For Diffie-Hellman key exchange key pairs (#PSA_KEY_TYPE_DH_KEYPAIR), the |
| 523 | * format is the representation of the private key `x` as a big-endian byte |
| 524 | * string. The length of the byte string is the private key size in bytes |
| 525 | * (leading zeroes are not stripped). |
Gilles Peskine | 4e1e9be | 2018-08-10 18:57:40 +0200 | [diff] [blame] | 526 | * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is |
| 527 | * true), the format is the same as for psa_export_public_key(). |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 528 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 529 | * \param handle Handle to the key to export. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 530 | * \param[out] data Buffer where the key data is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 531 | * \param data_size Size of the \p data buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 532 | * \param[out] data_length On success, the number of bytes |
| 533 | * that make up the key data. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 534 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 535 | * \retval #PSA_SUCCESS |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 536 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 537 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 538 | * \retval #PSA_ERROR_NOT_PERMITTED |
Darryl Green | 9e2d7a0 | 2018-07-24 16:33:30 +0100 | [diff] [blame] | 539 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 540 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 541 | * The size of the \p data buffer is too small. You can determine a |
| 542 | * sufficient buffer size by calling |
| 543 | * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits) |
| 544 | * where \c type is the key type |
| 545 | * and \c bits is the key size in bits. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 546 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 547 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 548 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 549 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 550 | * The library has not been previously initialized by psa_crypto_init(). |
| 551 | * It is implementation-dependent whether a failure to initialize |
| 552 | * results in this error code. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 553 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 554 | psa_status_t psa_export_key(psa_key_handle_t handle, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 555 | uint8_t *data, |
| 556 | size_t data_size, |
| 557 | size_t *data_length); |
| 558 | |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 559 | /** |
| 560 | * \brief Export a public key or the public part of a key pair in binary format. |
| 561 | * |
| 562 | * The output of this function can be passed to psa_import_key() to |
| 563 | * create an object that is equivalent to the public key. |
| 564 | * |
Jaeden Amero | d3a0c2c | 2019-01-11 17:15:56 +0000 | [diff] [blame] | 565 | * This specification supports a single format for each key type. |
| 566 | * Implementations may support other formats as long as the standard |
| 567 | * format is supported. Implementations that support other formats |
| 568 | * should ensure that the formats are clearly unambiguous so as to |
| 569 | * minimize the risk that an invalid input is accidentally interpreted |
| 570 | * according to a different format. |
| 571 | * |
Jaeden Amero | 6b19600 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 572 | * For standard key types, the output format is as follows: |
| 573 | * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of |
| 574 | * the representation defined by RFC 3279 §2.3.1 as `RSAPublicKey`. |
| 575 | * ``` |
| 576 | * RSAPublicKey ::= SEQUENCE { |
| 577 | * modulus INTEGER, -- n |
| 578 | * publicExponent INTEGER } -- e |
| 579 | * ``` |
Jaeden Amero | 0ae445f | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 580 | * - For elliptic curve public keys (key types for which |
| 581 | * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed |
| 582 | * representation defined by SEC1 §2.3.3 as the content of an ECPoint. |
| 583 | * Let `m` be the bit size associated with the curve, i.e. the bit size of |
| 584 | * `q` for a curve over `F_q`. The representation consists of: |
| 585 | * - The byte 0x04; |
| 586 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 587 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian. |
Jaeden Amero | 1308fb5 | 2019-01-11 13:50:43 +0000 | [diff] [blame] | 588 | * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), the format is the |
| 589 | * representation of the public key `y = g^x mod p` as a big-endian byte |
| 590 | * string. The length of the byte string is the length of the base prime `p` |
| 591 | * in bytes. |
Jaeden Amero | 8851c40 | 2019-01-11 14:20:03 +0000 | [diff] [blame] | 592 | * - For Diffie-Hellman key exchange public keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY), |
| 593 | * the format is the representation of the public key `y = g^x mod p` as a |
| 594 | * big-endian byte string. The length of the byte string is the length of the |
| 595 | * base prime `p` in bytes. |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 596 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 597 | * \param handle Handle to the key to export. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 598 | * \param[out] data Buffer where the key data is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 599 | * \param data_size Size of the \p data buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 600 | * \param[out] data_length On success, the number of bytes |
| 601 | * that make up the key data. |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 602 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 603 | * \retval #PSA_SUCCESS |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 604 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 605 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 606 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 607 | * The key is neither a public key nor a key pair. |
| 608 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 609 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 610 | * The size of the \p data buffer is too small. You can determine a |
| 611 | * sufficient buffer size by calling |
| 612 | * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits) |
| 613 | * where \c type is the key type |
| 614 | * and \c bits is the key size in bits. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 615 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 616 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 617 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 618 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 619 | * The library has not been previously initialized by psa_crypto_init(). |
| 620 | * It is implementation-dependent whether a failure to initialize |
| 621 | * results in this error code. |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 622 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 623 | psa_status_t psa_export_public_key(psa_key_handle_t handle, |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 624 | uint8_t *data, |
| 625 | size_t data_size, |
| 626 | size_t *data_length); |
| 627 | |
| 628 | /**@}*/ |
| 629 | |
| 630 | /** \defgroup policy Key policies |
| 631 | * @{ |
| 632 | */ |
| 633 | |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 634 | /** The type of the key policy data structure. |
| 635 | * |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 636 | * Before calling any function on a key policy, the application must initialize |
| 637 | * it by any of the following means: |
| 638 | * - Set the structure to all-bits-zero, for example: |
| 639 | * \code |
| 640 | * psa_key_policy_t policy; |
| 641 | * memset(&policy, 0, sizeof(policy)); |
| 642 | * \endcode |
| 643 | * - Initialize the structure to logical zero values, for example: |
| 644 | * \code |
| 645 | * psa_key_policy_t policy = {0}; |
| 646 | * \endcode |
| 647 | * - Initialize the structure to the initializer #PSA_KEY_POLICY_INIT, |
| 648 | * for example: |
| 649 | * \code |
| 650 | * psa_key_policy_t policy = PSA_KEY_POLICY_INIT; |
| 651 | * \endcode |
| 652 | * - Assign the result of the function psa_key_policy_init() |
| 653 | * to the structure, for example: |
| 654 | * \code |
| 655 | * psa_key_policy_t policy; |
| 656 | * policy = psa_key_policy_init(); |
| 657 | * \endcode |
| 658 | * |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 659 | * This is an implementation-defined \c struct. Applications should not |
| 660 | * make any assumptions about the content of this structure except |
| 661 | * as directed by the documentation of a specific implementation. */ |
| 662 | typedef struct psa_key_policy_s psa_key_policy_t; |
| 663 | |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 664 | /** \def PSA_KEY_POLICY_INIT |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 665 | * |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 666 | * This macro returns a suitable initializer for a key policy object of type |
| 667 | * #psa_key_policy_t. |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 668 | */ |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 669 | #ifdef __DOXYGEN_ONLY__ |
| 670 | /* This is an example definition for documentation purposes. |
| 671 | * Implementations should define a suitable value in `crypto_struct.h`. |
| 672 | */ |
| 673 | #define PSA_KEY_POLICY_INIT {0} |
| 674 | #endif |
| 675 | |
| 676 | /** Return an initial value for a key policy that forbids all usage of the key. |
| 677 | */ |
| 678 | static psa_key_policy_t psa_key_policy_init(void); |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 679 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 680 | /** \brief Set the standard fields of a policy structure. |
| 681 | * |
| 682 | * Note that this function does not make any consistency check of the |
| 683 | * parameters. The values are only checked when applying the policy to |
| 684 | * a key slot with psa_set_key_policy(). |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 685 | * |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 686 | * \param[in,out] policy The key policy to modify. It must have been |
| 687 | * initialized as per the documentation for |
| 688 | * #psa_key_policy_t. |
| 689 | * \param usage The permitted uses for the key. |
| 690 | * \param alg The algorithm that the key may be used for. |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 691 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 692 | void psa_key_policy_set_usage(psa_key_policy_t *policy, |
| 693 | psa_key_usage_t usage, |
| 694 | psa_algorithm_t alg); |
| 695 | |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 696 | /** \brief Retrieve the usage field of a policy structure. |
| 697 | * |
| 698 | * \param[in] policy The policy object to query. |
| 699 | * |
| 700 | * \return The permitted uses for a key with this policy. |
| 701 | */ |
Gilles Peskine | aa7bc47 | 2018-07-12 00:54:56 +0200 | [diff] [blame] | 702 | 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] | 703 | |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 704 | /** \brief Retrieve the algorithm field of a policy structure. |
| 705 | * |
| 706 | * \param[in] policy The policy object to query. |
| 707 | * |
| 708 | * \return The permitted algorithm for a key with this policy. |
| 709 | */ |
Gilles Peskine | aa7bc47 | 2018-07-12 00:54:56 +0200 | [diff] [blame] | 710 | 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] | 711 | |
| 712 | /** \brief Set the usage policy on a key slot. |
| 713 | * |
| 714 | * This function must be called on an empty key slot, before importing, |
| 715 | * generating or creating a key in the slot. Changing the policy of an |
| 716 | * existing key is not permitted. |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 717 | * |
| 718 | * Implementations may set restrictions on supported key policies |
| 719 | * depending on the key type and the key slot. |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 720 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 721 | * \param handle Handle to the key whose policy is to be changed. |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 722 | * \param[in] policy The policy object to query. |
| 723 | * |
| 724 | * \retval #PSA_SUCCESS |
Gilles Peskine | 23fd2bd | 2018-12-11 15:51:32 +0100 | [diff] [blame] | 725 | * Success. |
| 726 | * If the key is persistent, it is implementation-defined whether |
| 727 | * the policy has been saved to persistent storage. Implementations |
| 728 | * may defer saving the policy until the key material is created. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 729 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 730 | * \retval #PSA_ERROR_OCCUPIED_SLOT |
| 731 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 732 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 733 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 734 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 735 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 736 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 737 | * The library has not been previously initialized by psa_crypto_init(). |
| 738 | * It is implementation-dependent whether a failure to initialize |
| 739 | * results in this error code. |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 740 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 741 | psa_status_t psa_set_key_policy(psa_key_handle_t handle, |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 742 | const psa_key_policy_t *policy); |
| 743 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 744 | /** \brief Get the usage policy for a key slot. |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 745 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 746 | * \param handle Handle to the key slot whose policy is being queried. |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 747 | * \param[out] policy On success, the key's policy. |
| 748 | * |
| 749 | * \retval #PSA_SUCCESS |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 750 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 751 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 752 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 753 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 754 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 755 | * The library has not been previously initialized by psa_crypto_init(). |
| 756 | * It is implementation-dependent whether a failure to initialize |
| 757 | * results in this error code. |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 758 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 759 | psa_status_t psa_get_key_policy(psa_key_handle_t handle, |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 760 | psa_key_policy_t *policy); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 761 | |
| 762 | /**@}*/ |
| 763 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 764 | /** \defgroup hash Message digests |
| 765 | * @{ |
| 766 | */ |
| 767 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 768 | /** The type of the state data structure for multipart hash operations. |
| 769 | * |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 770 | * Before calling any function on a hash operation object, the application must |
| 771 | * initialize it by any of the following means: |
| 772 | * - Set the structure to all-bits-zero, for example: |
| 773 | * \code |
| 774 | * psa_hash_operation_t operation; |
| 775 | * memset(&operation, 0, sizeof(operation)); |
| 776 | * \endcode |
| 777 | * - Initialize the structure to logical zero values, for example: |
| 778 | * \code |
| 779 | * psa_hash_operation_t operation = {0}; |
| 780 | * \endcode |
| 781 | * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT, |
| 782 | * for example: |
| 783 | * \code |
| 784 | * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 785 | * \endcode |
| 786 | * - Assign the result of the function psa_hash_operation_init() |
| 787 | * to the structure, for example: |
| 788 | * \code |
| 789 | * psa_hash_operation_t operation; |
| 790 | * operation = psa_hash_operation_init(); |
| 791 | * \endcode |
| 792 | * |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 793 | * This is an implementation-defined \c struct. Applications should not |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 794 | * make any assumptions about the content of this structure except |
| 795 | * as directed by the documentation of a specific implementation. */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 796 | typedef struct psa_hash_operation_s psa_hash_operation_t; |
| 797 | |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 798 | /** \def PSA_HASH_OPERATION_INIT |
| 799 | * |
| 800 | * This macro returns a suitable initializer for a hash operation object |
| 801 | * of type #psa_hash_operation_t. |
| 802 | */ |
| 803 | #ifdef __DOXYGEN_ONLY__ |
| 804 | /* This is an example definition for documentation purposes. |
| 805 | * Implementations should define a suitable value in `crypto_struct.h`. |
| 806 | */ |
| 807 | #define PSA_HASH_OPERATION_INIT {0} |
| 808 | #endif |
| 809 | |
| 810 | /** Return an initial value for a hash operation object. |
| 811 | */ |
| 812 | static psa_hash_operation_t psa_hash_operation_init(void); |
| 813 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 814 | /** Start a multipart hash operation. |
| 815 | * |
| 816 | * The sequence of operations to calculate a hash (message digest) |
| 817 | * is as follows: |
| 818 | * -# Allocate an operation object which will be passed to all the functions |
| 819 | * listed here. |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 820 | * -# Initialize the operation object with one of the methods described in the |
| 821 | * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT. |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 822 | * -# Call psa_hash_setup() to specify the algorithm. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 823 | * -# Call psa_hash_update() zero, one or more times, passing a fragment |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 824 | * of the message each time. The hash that is calculated is the hash |
| 825 | * of the concatenation of these messages in order. |
| 826 | * -# To calculate the hash, call psa_hash_finish(). |
| 827 | * To compare the hash with an expected value, call psa_hash_verify(). |
| 828 | * |
| 829 | * 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] | 830 | * has been initialized. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 831 | * |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 832 | * After a successful call to psa_hash_setup(), the application must |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame] | 833 | * eventually terminate the operation. The following events terminate an |
| 834 | * operation: |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 835 | * - A failed call to psa_hash_update(). |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 836 | * - 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] | 837 | * |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 838 | * \param[in,out] operation The operation object to set up. It must have |
| 839 | * been initialized as per the documentation for |
| 840 | * #psa_hash_operation_t and not yet in use. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 841 | * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value |
| 842 | * such that #PSA_ALG_IS_HASH(\p alg) is true). |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 843 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 844 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 845 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 846 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 847 | * \p alg is not supported or is not a hash algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 848 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 849 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 850 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 851 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 852 | */ |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 853 | psa_status_t psa_hash_setup(psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 854 | psa_algorithm_t alg); |
| 855 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 856 | /** Add a message fragment to a multipart hash operation. |
| 857 | * |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 858 | * The application must call psa_hash_setup() before calling this function. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 859 | * |
| 860 | * If this function returns an error status, the operation becomes inactive. |
| 861 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 862 | * \param[in,out] operation Active hash operation. |
| 863 | * \param[in] input Buffer containing the message fragment to hash. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 864 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 865 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 866 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 867 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 868 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 869 | * The operation state is not valid (not started, or already completed). |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 870 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 871 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 872 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 873 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 874 | */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 875 | psa_status_t psa_hash_update(psa_hash_operation_t *operation, |
| 876 | const uint8_t *input, |
| 877 | size_t input_length); |
| 878 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 879 | /** Finish the calculation of the hash of a message. |
| 880 | * |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 881 | * The application must call psa_hash_setup() before calling this function. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 882 | * This function calculates the hash of the message formed by concatenating |
| 883 | * the inputs passed to preceding calls to psa_hash_update(). |
| 884 | * |
| 885 | * When this function returns, the operation becomes inactive. |
| 886 | * |
| 887 | * \warning Applications should not call this function if they expect |
| 888 | * a specific value for the hash. Call psa_hash_verify() instead. |
| 889 | * Beware that comparing integrity or authenticity data such as |
| 890 | * hash values with a function such as \c memcmp is risky |
| 891 | * because the time taken by the comparison may leak information |
| 892 | * about the hashed data which could allow an attacker to guess |
| 893 | * a valid hash and thereby bypass security controls. |
| 894 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 895 | * \param[in,out] operation Active hash operation. |
| 896 | * \param[out] hash Buffer where the hash is to be written. |
| 897 | * \param hash_size Size of the \p hash buffer in bytes. |
| 898 | * \param[out] hash_length On success, the number of bytes |
| 899 | * that make up the hash value. This is always |
Gilles Peskine | be42f31 | 2018-07-13 14:38:15 +0200 | [diff] [blame] | 900 | * #PSA_HASH_SIZE(\c alg) where \c alg is the |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 901 | * hash algorithm that is calculated. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 902 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 903 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 904 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 905 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 906 | * The operation state is not valid (not started, or already completed). |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 907 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 908 | * 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] | 909 | * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg) |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 910 | * where \c alg is the hash algorithm that is calculated. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 911 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 912 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 913 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 914 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 915 | */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 916 | psa_status_t psa_hash_finish(psa_hash_operation_t *operation, |
| 917 | uint8_t *hash, |
| 918 | size_t hash_size, |
| 919 | size_t *hash_length); |
| 920 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 921 | /** Finish the calculation of the hash of a message and compare it with |
| 922 | * an expected value. |
| 923 | * |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 924 | * The application must call psa_hash_setup() before calling this function. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 925 | * This function calculates the hash of the message formed by concatenating |
| 926 | * the inputs passed to preceding calls to psa_hash_update(). It then |
| 927 | * compares the calculated hash with the expected hash passed as a |
| 928 | * parameter to this function. |
| 929 | * |
| 930 | * When this function returns, the operation becomes inactive. |
| 931 | * |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 932 | * \note Implementations shall make the best effort to ensure that the |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 933 | * comparison between the actual hash and the expected hash is performed |
| 934 | * in constant time. |
| 935 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 936 | * \param[in,out] operation Active hash operation. |
| 937 | * \param[in] hash Buffer containing the expected hash value. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 938 | * \param hash_length Size of the \p hash buffer in bytes. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 939 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 940 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 941 | * The expected hash is identical to the actual hash of the message. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 942 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 943 | * The hash of the message was calculated successfully, but it |
| 944 | * differs from the expected hash. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 945 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 946 | * The operation state is not valid (not started, or already completed). |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 947 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 948 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 949 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 950 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 951 | */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 952 | psa_status_t psa_hash_verify(psa_hash_operation_t *operation, |
| 953 | const uint8_t *hash, |
| 954 | size_t hash_length); |
| 955 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 956 | /** Abort a hash operation. |
| 957 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 958 | * Aborting an operation frees all associated resources except for the |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 959 | * \p operation structure itself. Once aborted, the operation object |
| 960 | * can be reused for another operation by calling |
| 961 | * psa_hash_setup() again. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 962 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 963 | * You may call this function any time after the operation object has |
| 964 | * been initialized by any of the following methods: |
| 965 | * - A call to psa_hash_setup(), whether it succeeds or not. |
| 966 | * - Initializing the \c struct to all-bits-zero. |
| 967 | * - Initializing the \c struct to logical zeros, e.g. |
| 968 | * `psa_hash_operation_t operation = {0}`. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 969 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 970 | * In particular, calling psa_hash_abort() after the operation has been |
| 971 | * terminated by a call to psa_hash_abort(), psa_hash_finish() or |
| 972 | * psa_hash_verify() is safe and has no effect. |
| 973 | * |
| 974 | * \param[in,out] operation Initialized hash operation. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 975 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 976 | * \retval #PSA_SUCCESS |
| 977 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 978 | * \p operation is not an active hash operation. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 979 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 980 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 981 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 982 | */ |
| 983 | psa_status_t psa_hash_abort(psa_hash_operation_t *operation); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 984 | |
| 985 | /**@}*/ |
| 986 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 987 | /** \defgroup MAC Message authentication codes |
| 988 | * @{ |
| 989 | */ |
| 990 | |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 991 | /** The type of the state data structure for multipart MAC operations. |
| 992 | * |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 993 | * Before calling any function on a MAC operation object, the application must |
| 994 | * initialize it by any of the following means: |
| 995 | * - Set the structure to all-bits-zero, for example: |
| 996 | * \code |
| 997 | * psa_mac_operation_t operation; |
| 998 | * memset(&operation, 0, sizeof(operation)); |
| 999 | * \endcode |
| 1000 | * - Initialize the structure to logical zero values, for example: |
| 1001 | * \code |
| 1002 | * psa_mac_operation_t operation = {0}; |
| 1003 | * \endcode |
| 1004 | * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT, |
| 1005 | * for example: |
| 1006 | * \code |
| 1007 | * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 1008 | * \endcode |
| 1009 | * - Assign the result of the function psa_mac_operation_init() |
| 1010 | * to the structure, for example: |
| 1011 | * \code |
| 1012 | * psa_mac_operation_t operation; |
| 1013 | * operation = psa_mac_operation_init(); |
| 1014 | * \endcode |
| 1015 | * |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 1016 | * This is an implementation-defined \c struct. Applications should not |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1017 | * make any assumptions about the content of this structure except |
| 1018 | * as directed by the documentation of a specific implementation. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1019 | typedef struct psa_mac_operation_s psa_mac_operation_t; |
| 1020 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1021 | /** \def PSA_MAC_OPERATION_INIT |
| 1022 | * |
| 1023 | * This macro returns a suitable initializer for a MAC operation object of type |
| 1024 | * #psa_mac_operation_t. |
| 1025 | */ |
| 1026 | #ifdef __DOXYGEN_ONLY__ |
| 1027 | /* This is an example definition for documentation purposes. |
| 1028 | * Implementations should define a suitable value in `crypto_struct.h`. |
| 1029 | */ |
| 1030 | #define PSA_MAC_OPERATION_INIT {0} |
| 1031 | #endif |
| 1032 | |
| 1033 | /** Return an initial value for a MAC operation object. |
| 1034 | */ |
| 1035 | static psa_mac_operation_t psa_mac_operation_init(void); |
| 1036 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1037 | /** Start a multipart MAC calculation operation. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1038 | * |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1039 | * This function sets up the calculation of the MAC |
| 1040 | * (message authentication code) of a byte string. |
| 1041 | * To verify the MAC of a message against an |
| 1042 | * expected value, use psa_mac_verify_setup() instead. |
| 1043 | * |
| 1044 | * The sequence of operations to calculate a MAC is as follows: |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1045 | * -# Allocate an operation object which will be passed to all the functions |
| 1046 | * listed here. |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1047 | * -# Initialize the operation object with one of the methods described in the |
| 1048 | * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT. |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1049 | * -# Call psa_mac_sign_setup() to specify the algorithm and key. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1050 | * The key remains associated with the operation even if the content |
| 1051 | * of the key slot changes. |
| 1052 | * -# Call psa_mac_update() zero, one or more times, passing a fragment |
| 1053 | * of the message each time. The MAC that is calculated is the MAC |
| 1054 | * of the concatenation of these messages in order. |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1055 | * -# At the end of the message, call psa_mac_sign_finish() to finish |
| 1056 | * calculating the MAC value and retrieve it. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 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 | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1060 | * |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1061 | * After a successful call to psa_mac_sign_setup(), the application must |
| 1062 | * eventually terminate the operation through one of the following methods: |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1063 | * - A failed call to psa_mac_update(). |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1064 | * - A call to psa_mac_sign_finish() or psa_mac_abort(). |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 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 | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1070 | * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value |
| 1071 | * such that #PSA_ALG_IS_MAC(alg) is true). |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1072 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1073 | * \retval #PSA_SUCCESS |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1074 | * Success. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1075 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1076 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1077 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1078 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1079 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1080 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1081 | * \p alg is not supported or is not a MAC algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1082 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1083 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1084 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1085 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1086 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1087 | * The library has not been previously initialized by psa_crypto_init(). |
| 1088 | * It is implementation-dependent whether a failure to initialize |
| 1089 | * results in this error code. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1090 | */ |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1091 | psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation, |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1092 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1093 | psa_algorithm_t alg); |
| 1094 | |
| 1095 | /** Start a multipart MAC verification operation. |
| 1096 | * |
| 1097 | * This function sets up the verification of the MAC |
| 1098 | * (message authentication code) of a byte string against an expected value. |
| 1099 | * |
| 1100 | * The sequence of operations to verify a MAC is as follows: |
| 1101 | * -# Allocate an operation object which will be passed to all the functions |
| 1102 | * listed here. |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1103 | * -# Initialize the operation object with one of the methods described in the |
| 1104 | * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT. |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1105 | * -# Call psa_mac_verify_setup() to specify the algorithm and key. |
| 1106 | * The key remains associated with the operation even if the content |
| 1107 | * of the key slot changes. |
| 1108 | * -# Call psa_mac_update() zero, one or more times, passing a fragment |
| 1109 | * of the message each time. The MAC that is calculated is the MAC |
| 1110 | * of the concatenation of these messages in order. |
| 1111 | * -# At the end of the message, call psa_mac_verify_finish() to finish |
| 1112 | * calculating the actual MAC of the message and verify it against |
| 1113 | * the expected value. |
| 1114 | * |
| 1115 | * 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] | 1116 | * has been initialized. |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1117 | * |
| 1118 | * After a successful call to psa_mac_verify_setup(), the application must |
| 1119 | * eventually terminate the operation through one of the following methods: |
| 1120 | * - A failed call to psa_mac_update(). |
| 1121 | * - A call to psa_mac_verify_finish() or psa_mac_abort(). |
| 1122 | * |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1123 | * \param[in,out] operation The operation object to set up. It must have |
| 1124 | * been initialized as per the documentation for |
| 1125 | * #psa_mac_operation_t and not yet in use. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1126 | * \param handle Handle to the key to use for the operation. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1127 | * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value |
| 1128 | * such that #PSA_ALG_IS_MAC(\p alg) is true). |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1129 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1130 | * \retval #PSA_SUCCESS |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1131 | * Success. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1132 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1133 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1134 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1135 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1136 | * \c key is not compatible with \c alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1137 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1138 | * \c alg is not supported or is not a MAC algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1139 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1140 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1141 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1142 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1143 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1144 | * The library has not been previously initialized by psa_crypto_init(). |
| 1145 | * It is implementation-dependent whether a failure to initialize |
| 1146 | * results in this error code. |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1147 | */ |
| 1148 | psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation, |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1149 | psa_key_handle_t handle, |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1150 | psa_algorithm_t alg); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1151 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1152 | /** Add a message fragment to a multipart MAC operation. |
| 1153 | * |
| 1154 | * The application must call psa_mac_sign_setup() or psa_mac_verify_setup() |
| 1155 | * before calling this function. |
| 1156 | * |
| 1157 | * If this function returns an error status, the operation becomes inactive. |
| 1158 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1159 | * \param[in,out] operation Active MAC operation. |
| 1160 | * \param[in] input Buffer containing the message fragment to add to |
| 1161 | * the MAC calculation. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1162 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1163 | * |
| 1164 | * \retval #PSA_SUCCESS |
| 1165 | * Success. |
| 1166 | * \retval #PSA_ERROR_BAD_STATE |
| 1167 | * The operation state is not valid (not started, or already completed). |
| 1168 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1169 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1170 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1171 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1172 | */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1173 | psa_status_t psa_mac_update(psa_mac_operation_t *operation, |
| 1174 | const uint8_t *input, |
| 1175 | size_t input_length); |
| 1176 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1177 | /** Finish the calculation of the MAC of a message. |
| 1178 | * |
| 1179 | * The application must call psa_mac_sign_setup() before calling this function. |
| 1180 | * This function calculates the MAC of the message formed by concatenating |
| 1181 | * the inputs passed to preceding calls to psa_mac_update(). |
| 1182 | * |
| 1183 | * When this function returns, the operation becomes inactive. |
| 1184 | * |
| 1185 | * \warning Applications should not call this function if they expect |
| 1186 | * a specific value for the MAC. Call psa_mac_verify_finish() instead. |
| 1187 | * Beware that comparing integrity or authenticity data such as |
| 1188 | * MAC values with a function such as \c memcmp is risky |
| 1189 | * because the time taken by the comparison may leak information |
| 1190 | * about the MAC value which could allow an attacker to guess |
| 1191 | * a valid MAC and thereby bypass security controls. |
| 1192 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1193 | * \param[in,out] operation Active MAC operation. |
| 1194 | * \param[out] mac Buffer where the MAC value is to be written. |
| 1195 | * \param mac_size Size of the \p mac buffer in bytes. |
| 1196 | * \param[out] mac_length On success, the number of bytes |
| 1197 | * that make up the MAC value. This is always |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 1198 | * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg) |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1199 | * where \c key_type and \c key_bits are the type and |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 1200 | * bit-size respectively of the key and \c alg is the |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1201 | * MAC algorithm that is calculated. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1202 | * |
| 1203 | * \retval #PSA_SUCCESS |
| 1204 | * Success. |
| 1205 | * \retval #PSA_ERROR_BAD_STATE |
| 1206 | * The operation state is not valid (not started, or already completed). |
| 1207 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1208 | * 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] | 1209 | * sufficient buffer size by calling PSA_MAC_FINAL_SIZE(). |
| 1210 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1211 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1212 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1213 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1214 | */ |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 1215 | psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation, |
| 1216 | uint8_t *mac, |
| 1217 | size_t mac_size, |
| 1218 | size_t *mac_length); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1219 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1220 | /** Finish the calculation of the MAC of a message and compare it with |
| 1221 | * an expected value. |
| 1222 | * |
| 1223 | * The application must call psa_mac_verify_setup() before calling this function. |
| 1224 | * This function calculates the MAC of the message formed by concatenating |
| 1225 | * the inputs passed to preceding calls to psa_mac_update(). It then |
| 1226 | * compares the calculated MAC with the expected MAC passed as a |
| 1227 | * parameter to this function. |
| 1228 | * |
| 1229 | * When this function returns, the operation becomes inactive. |
| 1230 | * |
| 1231 | * \note Implementations shall make the best effort to ensure that the |
| 1232 | * comparison between the actual MAC and the expected MAC is performed |
| 1233 | * in constant time. |
| 1234 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1235 | * \param[in,out] operation Active MAC operation. |
| 1236 | * \param[in] mac Buffer containing the expected MAC value. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1237 | * \param mac_length Size of the \p mac buffer in bytes. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1238 | * |
| 1239 | * \retval #PSA_SUCCESS |
| 1240 | * The expected MAC is identical to the actual MAC of the message. |
| 1241 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
| 1242 | * The MAC of the message was calculated successfully, but it |
| 1243 | * differs from the expected MAC. |
| 1244 | * \retval #PSA_ERROR_BAD_STATE |
| 1245 | * The operation state is not valid (not started, or already completed). |
| 1246 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1247 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1248 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1249 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1250 | */ |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 1251 | psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation, |
| 1252 | const uint8_t *mac, |
| 1253 | size_t mac_length); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1254 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1255 | /** Abort a MAC operation. |
| 1256 | * |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1257 | * Aborting an operation frees all associated resources except for the |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 1258 | * \p operation structure itself. Once aborted, the operation object |
| 1259 | * can be reused for another operation by calling |
| 1260 | * psa_mac_sign_setup() or psa_mac_verify_setup() again. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1261 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 1262 | * You may call this function any time after the operation object has |
| 1263 | * been initialized by any of the following methods: |
| 1264 | * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether |
| 1265 | * it succeeds or not. |
| 1266 | * - Initializing the \c struct to all-bits-zero. |
| 1267 | * - Initializing the \c struct to logical zeros, e.g. |
| 1268 | * `psa_mac_operation_t operation = {0}`. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1269 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 1270 | * In particular, calling psa_mac_abort() after the operation has been |
| 1271 | * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or |
| 1272 | * psa_mac_verify_finish() is safe and has no effect. |
| 1273 | * |
| 1274 | * \param[in,out] operation Initialized MAC operation. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1275 | * |
| 1276 | * \retval #PSA_SUCCESS |
| 1277 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1278 | * \p operation is not an active MAC operation. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1279 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1280 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1281 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1282 | */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1283 | psa_status_t psa_mac_abort(psa_mac_operation_t *operation); |
| 1284 | |
| 1285 | /**@}*/ |
| 1286 | |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1287 | /** \defgroup cipher Symmetric ciphers |
| 1288 | * @{ |
| 1289 | */ |
| 1290 | |
| 1291 | /** The type of the state data structure for multipart cipher operations. |
| 1292 | * |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1293 | * Before calling any function on a cipher operation object, the application |
| 1294 | * must initialize it by any of the following means: |
| 1295 | * - Set the structure to all-bits-zero, for example: |
| 1296 | * \code |
| 1297 | * psa_cipher_operation_t operation; |
| 1298 | * memset(&operation, 0, sizeof(operation)); |
| 1299 | * \endcode |
| 1300 | * - Initialize the structure to logical zero values, for example: |
| 1301 | * \code |
| 1302 | * psa_cipher_operation_t operation = {0}; |
| 1303 | * \endcode |
| 1304 | * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT, |
| 1305 | * for example: |
| 1306 | * \code |
| 1307 | * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 1308 | * \endcode |
| 1309 | * - Assign the result of the function psa_cipher_operation_init() |
| 1310 | * to the structure, for example: |
| 1311 | * \code |
| 1312 | * psa_cipher_operation_t operation; |
| 1313 | * operation = psa_cipher_operation_init(); |
| 1314 | * \endcode |
| 1315 | * |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1316 | * This is an implementation-defined \c struct. Applications should not |
| 1317 | * make any assumptions about the content of this structure except |
| 1318 | * as directed by the documentation of a specific implementation. */ |
| 1319 | typedef struct psa_cipher_operation_s psa_cipher_operation_t; |
| 1320 | |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1321 | /** \def PSA_CIPHER_OPERATION_INIT |
| 1322 | * |
| 1323 | * This macro returns a suitable initializer for a cipher operation object of |
| 1324 | * type #psa_cipher_operation_t. |
| 1325 | */ |
| 1326 | #ifdef __DOXYGEN_ONLY__ |
| 1327 | /* This is an example definition for documentation purposes. |
| 1328 | * Implementations should define a suitable value in `crypto_struct.h`. |
| 1329 | */ |
| 1330 | #define PSA_CIPHER_OPERATION_INIT {0} |
| 1331 | #endif |
| 1332 | |
| 1333 | /** Return an initial value for a cipher operation object. |
| 1334 | */ |
| 1335 | static psa_cipher_operation_t psa_cipher_operation_init(void); |
| 1336 | |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1337 | /** Set the key for a multipart symmetric encryption operation. |
| 1338 | * |
| 1339 | * The sequence of operations to encrypt a message with a symmetric cipher |
| 1340 | * is as follows: |
| 1341 | * -# Allocate an operation object which will be passed to all the functions |
| 1342 | * listed here. |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1343 | * -# Initialize the operation object with one of the methods described in the |
| 1344 | * documentation for #psa_cipher_operation_t, e.g. |
| 1345 | * PSA_CIPHER_OPERATION_INIT. |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1346 | * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key. |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1347 | * The key remains associated with the operation even if the content |
| 1348 | * of the key slot changes. |
itayzafrir | ed7382f | 2018-08-02 14:19:33 +0300 | [diff] [blame] | 1349 | * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1350 | * generate or set the IV (initialization vector). You should use |
itayzafrir | ed7382f | 2018-08-02 14:19:33 +0300 | [diff] [blame] | 1351 | * psa_cipher_generate_iv() unless the protocol you are implementing |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1352 | * requires a specific IV value. |
| 1353 | * -# Call psa_cipher_update() zero, one or more times, passing a fragment |
| 1354 | * of the message each time. |
| 1355 | * -# Call psa_cipher_finish(). |
| 1356 | * |
| 1357 | * 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] | 1358 | * has been initialized. |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1359 | * |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1360 | * After a successful call to psa_cipher_encrypt_setup(), the application must |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame] | 1361 | * eventually terminate the operation. The following events terminate an |
| 1362 | * operation: |
itayzafrir | ed7382f | 2018-08-02 14:19:33 +0300 | [diff] [blame] | 1363 | * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv() |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1364 | * or psa_cipher_update(). |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 1365 | * - A call to psa_cipher_finish() or psa_cipher_abort(). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1366 | * |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1367 | * \param[in,out] operation The operation object to set up. It must have |
| 1368 | * been initialized as per the documentation for |
| 1369 | * #psa_cipher_operation_t and not yet in use. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1370 | * \param handle Handle to the key to use for the operation. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1371 | * \param alg The cipher algorithm to compute |
| 1372 | * (\c PSA_ALG_XXX value such that |
| 1373 | * #PSA_ALG_IS_CIPHER(\p alg) is true). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1374 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1375 | * \retval #PSA_SUCCESS |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1376 | * Success. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1377 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1378 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1379 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1380 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1381 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1382 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1383 | * \p alg is not supported or is not a cipher algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1384 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1385 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1386 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1387 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1388 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1389 | * The library has not been previously initialized by psa_crypto_init(). |
| 1390 | * It is implementation-dependent whether a failure to initialize |
| 1391 | * results in this error code. |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1392 | */ |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1393 | psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation, |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1394 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1395 | psa_algorithm_t alg); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1396 | |
| 1397 | /** Set the key for a multipart symmetric decryption operation. |
| 1398 | * |
| 1399 | * The sequence of operations to decrypt a message with a symmetric cipher |
| 1400 | * is as follows: |
| 1401 | * -# Allocate an operation object which will be passed to all the functions |
| 1402 | * listed here. |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1403 | * -# Initialize the operation object with one of the methods described in the |
| 1404 | * documentation for #psa_cipher_operation_t, e.g. |
| 1405 | * PSA_CIPHER_OPERATION_INIT. |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1406 | * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key. |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1407 | * The key remains associated with the operation even if the content |
| 1408 | * of the key slot changes. |
| 1409 | * -# Call psa_cipher_update() with the IV (initialization vector) for the |
| 1410 | * decryption. If the IV is prepended to the ciphertext, you can call |
| 1411 | * psa_cipher_update() on a buffer containing the IV followed by the |
| 1412 | * beginning of the message. |
| 1413 | * -# Call psa_cipher_update() zero, one or more times, passing a fragment |
| 1414 | * of the message each time. |
| 1415 | * -# Call psa_cipher_finish(). |
| 1416 | * |
| 1417 | * 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] | 1418 | * has been initialized. |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1419 | * |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1420 | * After a successful call to psa_cipher_decrypt_setup(), the application must |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame] | 1421 | * eventually terminate the operation. The following events terminate an |
| 1422 | * operation: |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1423 | * - A failed call to psa_cipher_update(). |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 1424 | * - A call to psa_cipher_finish() or psa_cipher_abort(). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1425 | * |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1426 | * \param[in,out] operation The operation object to set up. It must have |
| 1427 | * been initialized as per the documentation for |
| 1428 | * #psa_cipher_operation_t and not yet in use. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1429 | * \param handle Handle to the key to use for the operation. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1430 | * \param alg The cipher algorithm to compute |
| 1431 | * (\c PSA_ALG_XXX value such that |
| 1432 | * #PSA_ALG_IS_CIPHER(\p alg) is true). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1433 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1434 | * \retval #PSA_SUCCESS |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1435 | * Success. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1436 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1437 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1438 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1439 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1440 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1441 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1442 | * \p alg is not supported or is not a cipher algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1443 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1444 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1445 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1446 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1447 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1448 | * The library has not been previously initialized by psa_crypto_init(). |
| 1449 | * It is implementation-dependent whether a failure to initialize |
| 1450 | * results in this error code. |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1451 | */ |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1452 | psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation, |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1453 | psa_key_handle_t handle, |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1454 | psa_algorithm_t alg); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1455 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1456 | /** Generate an IV for a symmetric encryption operation. |
| 1457 | * |
| 1458 | * This function generates a random IV (initialization vector), nonce |
| 1459 | * or initial counter value for the encryption operation as appropriate |
| 1460 | * for the chosen algorithm, key type and key size. |
| 1461 | * |
| 1462 | * The application must call psa_cipher_encrypt_setup() before |
| 1463 | * calling this function. |
| 1464 | * |
| 1465 | * If this function returns an error status, the operation becomes inactive. |
| 1466 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1467 | * \param[in,out] operation Active cipher operation. |
| 1468 | * \param[out] iv Buffer where the generated IV is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1469 | * \param iv_size Size of the \p iv buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1470 | * \param[out] iv_length On success, the number of bytes of the |
| 1471 | * generated IV. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1472 | * |
| 1473 | * \retval #PSA_SUCCESS |
| 1474 | * Success. |
| 1475 | * \retval #PSA_ERROR_BAD_STATE |
| 1476 | * The operation state is not valid (not started, or IV already set). |
| 1477 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 1478 | * The size of the \p iv buffer is too small. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1479 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1480 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1481 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1482 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1483 | */ |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1484 | psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation, |
| 1485 | unsigned char *iv, |
| 1486 | size_t iv_size, |
| 1487 | size_t *iv_length); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1488 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1489 | /** Set the IV for a symmetric encryption or decryption operation. |
| 1490 | * |
| 1491 | * This function sets the random IV (initialization vector), nonce |
| 1492 | * or initial counter value for the encryption or decryption operation. |
| 1493 | * |
| 1494 | * The application must call psa_cipher_encrypt_setup() before |
| 1495 | * calling this function. |
| 1496 | * |
| 1497 | * If this function returns an error status, the operation becomes inactive. |
| 1498 | * |
| 1499 | * \note When encrypting, applications should use psa_cipher_generate_iv() |
| 1500 | * instead of this function, unless implementing a protocol that requires |
| 1501 | * a non-random IV. |
| 1502 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1503 | * \param[in,out] operation Active cipher operation. |
| 1504 | * \param[in] iv Buffer containing the IV to use. |
| 1505 | * \param iv_length Size of the IV in bytes. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1506 | * |
| 1507 | * \retval #PSA_SUCCESS |
| 1508 | * Success. |
| 1509 | * \retval #PSA_ERROR_BAD_STATE |
| 1510 | * The operation state is not valid (not started, or IV already set). |
| 1511 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1512 | * The size of \p iv is not acceptable for the chosen algorithm, |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1513 | * or the chosen algorithm does not use an IV. |
| 1514 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1515 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1516 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1517 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1518 | */ |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1519 | psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation, |
| 1520 | const unsigned char *iv, |
| 1521 | size_t iv_length); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1522 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1523 | /** Encrypt or decrypt a message fragment in an active cipher operation. |
| 1524 | * |
Gilles Peskine | 9ac9426 | 2018-07-12 20:15:32 +0200 | [diff] [blame] | 1525 | * Before calling this function, you must: |
| 1526 | * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(). |
| 1527 | * The choice of setup function determines whether this function |
| 1528 | * encrypts or decrypts its input. |
| 1529 | * 2. If the algorithm requires an IV, call psa_cipher_generate_iv() |
| 1530 | * (recommended when encrypting) or psa_cipher_set_iv(). |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1531 | * |
| 1532 | * If this function returns an error status, the operation becomes inactive. |
| 1533 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1534 | * \param[in,out] operation Active cipher operation. |
| 1535 | * \param[in] input Buffer containing the message fragment to |
| 1536 | * encrypt or decrypt. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1537 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1538 | * \param[out] output Buffer where the output is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1539 | * \param output_size Size of the \p output buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1540 | * \param[out] output_length On success, the number of bytes |
| 1541 | * that make up the returned output. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1542 | * |
| 1543 | * \retval #PSA_SUCCESS |
| 1544 | * Success. |
| 1545 | * \retval #PSA_ERROR_BAD_STATE |
| 1546 | * The operation state is not valid (not started, IV required but |
| 1547 | * not set, or already completed). |
| 1548 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 1549 | * The size of the \p output buffer is too small. |
| 1550 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1551 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1552 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1553 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1554 | */ |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1555 | psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, |
| 1556 | const uint8_t *input, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1557 | size_t input_length, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1558 | unsigned char *output, |
| 1559 | size_t output_size, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1560 | size_t *output_length); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1561 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1562 | /** Finish encrypting or decrypting a message in a cipher operation. |
| 1563 | * |
| 1564 | * The application must call psa_cipher_encrypt_setup() or |
| 1565 | * psa_cipher_decrypt_setup() before calling this function. The choice |
| 1566 | * of setup function determines whether this function encrypts or |
| 1567 | * decrypts its input. |
| 1568 | * |
| 1569 | * This function finishes the encryption or decryption of the message |
| 1570 | * formed by concatenating the inputs passed to preceding calls to |
| 1571 | * psa_cipher_update(). |
| 1572 | * |
| 1573 | * When this function returns, the operation becomes inactive. |
| 1574 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1575 | * \param[in,out] operation Active cipher operation. |
| 1576 | * \param[out] output Buffer where the output is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1577 | * \param output_size Size of the \p output buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1578 | * \param[out] output_length On success, the number of bytes |
| 1579 | * that make up the returned output. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1580 | * |
| 1581 | * \retval #PSA_SUCCESS |
| 1582 | * Success. |
| 1583 | * \retval #PSA_ERROR_BAD_STATE |
| 1584 | * The operation state is not valid (not started, IV required but |
| 1585 | * not set, or already completed). |
| 1586 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 1587 | * The size of the \p output buffer is too small. |
| 1588 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1589 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1590 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1591 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1592 | */ |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1593 | psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1594 | uint8_t *output, |
Moran Peker | 0071b87 | 2018-04-22 20:16:58 +0300 | [diff] [blame] | 1595 | size_t output_size, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1596 | size_t *output_length); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1597 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1598 | /** Abort a cipher operation. |
| 1599 | * |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1600 | * Aborting an operation frees all associated resources except for the |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 1601 | * \p operation structure itself. Once aborted, the operation object |
| 1602 | * can be reused for another operation by calling |
| 1603 | * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1604 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 1605 | * You may call this function any time after the operation object has |
| 1606 | * been initialized by any of the following methods: |
| 1607 | * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(), |
| 1608 | * whether it succeeds or not. |
| 1609 | * - Initializing the \c struct to all-bits-zero. |
| 1610 | * - Initializing the \c struct to logical zeros, e.g. |
| 1611 | * `psa_cipher_operation_t operation = {0}`. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1612 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 1613 | * In particular, calling psa_cipher_abort() after the operation has been |
| 1614 | * terminated by a call to psa_cipher_abort() or psa_cipher_finish() |
| 1615 | * is safe and has no effect. |
| 1616 | * |
| 1617 | * \param[in,out] operation Initialized cipher operation. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1618 | * |
| 1619 | * \retval #PSA_SUCCESS |
| 1620 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1621 | * \p operation is not an active cipher operation. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1622 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1623 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1624 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1625 | */ |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1626 | psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation); |
| 1627 | |
| 1628 | /**@}*/ |
| 1629 | |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1630 | /** \defgroup aead Authenticated encryption with associated data (AEAD) |
| 1631 | * @{ |
| 1632 | */ |
| 1633 | |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1634 | /** Process an authenticated encryption operation. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1635 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1636 | * \param handle Handle to the key to use for the operation. |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1637 | * \param alg The AEAD algorithm to compute |
| 1638 | * (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 1639 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1640 | * \param[in] nonce Nonce or IV to use. |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1641 | * \param nonce_length Size of the \p nonce buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1642 | * \param[in] additional_data Additional data that will be authenticated |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1643 | * but not encrypted. |
| 1644 | * \param additional_data_length Size of \p additional_data in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1645 | * \param[in] plaintext Data that will be authenticated and |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1646 | * encrypted. |
| 1647 | * \param plaintext_length Size of \p plaintext in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1648 | * \param[out] ciphertext Output buffer for the authenticated and |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1649 | * encrypted data. The additional data is not |
| 1650 | * part of this output. For algorithms where the |
| 1651 | * encrypted data and the authentication tag |
| 1652 | * are defined as separate outputs, the |
| 1653 | * authentication tag is appended to the |
| 1654 | * encrypted data. |
| 1655 | * \param ciphertext_size Size of the \p ciphertext buffer in bytes. |
| 1656 | * This must be at least |
| 1657 | * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg, |
| 1658 | * \p plaintext_length). |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1659 | * \param[out] ciphertext_length On success, the size of the output |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1660 | * in the \b ciphertext buffer. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1661 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1662 | * \retval #PSA_SUCCESS |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1663 | * Success. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1664 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1665 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1666 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1667 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1668 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1669 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1670 | * \p alg is not supported or is not an AEAD algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1671 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1672 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1673 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1674 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1675 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1676 | * The library has not been previously initialized by psa_crypto_init(). |
| 1677 | * It is implementation-dependent whether a failure to initialize |
| 1678 | * results in this error code. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1679 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1680 | psa_status_t psa_aead_encrypt(psa_key_handle_t handle, |
Gilles Peskine | 9fb0e01 | 2018-07-19 15:51:49 +0200 | [diff] [blame] | 1681 | psa_algorithm_t alg, |
| 1682 | const uint8_t *nonce, |
| 1683 | size_t nonce_length, |
| 1684 | const uint8_t *additional_data, |
| 1685 | size_t additional_data_length, |
| 1686 | const uint8_t *plaintext, |
| 1687 | size_t plaintext_length, |
| 1688 | uint8_t *ciphertext, |
| 1689 | size_t ciphertext_size, |
| 1690 | size_t *ciphertext_length); |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1691 | |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1692 | /** Process an authenticated decryption operation. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1693 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1694 | * \param handle Handle to the key to use for the operation. |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1695 | * \param alg The AEAD algorithm to compute |
| 1696 | * (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 1697 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1698 | * \param[in] nonce Nonce or IV to use. |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1699 | * \param nonce_length Size of the \p nonce buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1700 | * \param[in] additional_data Additional data that has been authenticated |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1701 | * but not encrypted. |
| 1702 | * \param additional_data_length Size of \p additional_data in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1703 | * \param[in] ciphertext Data that has been authenticated and |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1704 | * encrypted. For algorithms where the |
| 1705 | * encrypted data and the authentication tag |
| 1706 | * are defined as separate inputs, the buffer |
| 1707 | * must contain the encrypted data followed |
| 1708 | * by the authentication tag. |
| 1709 | * \param ciphertext_length Size of \p ciphertext in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1710 | * \param[out] plaintext Output buffer for the decrypted data. |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1711 | * \param plaintext_size Size of the \p plaintext buffer in bytes. |
| 1712 | * This must be at least |
| 1713 | * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg, |
| 1714 | * \p ciphertext_length). |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1715 | * \param[out] plaintext_length On success, the size of the output |
mohammad1603 | fb5b9cb | 2018-06-06 13:44:27 +0300 | [diff] [blame] | 1716 | * in the \b plaintext buffer. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1717 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1718 | * \retval #PSA_SUCCESS |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1719 | * Success. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1720 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1721 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1722 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1723 | * The ciphertext is not authentic. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1724 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1725 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1726 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1727 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1728 | * \p alg is not supported or is not an AEAD algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1729 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1730 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1731 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1732 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1733 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1734 | * The library has not been previously initialized by psa_crypto_init(). |
| 1735 | * It is implementation-dependent whether a failure to initialize |
| 1736 | * results in this error code. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1737 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1738 | psa_status_t psa_aead_decrypt(psa_key_handle_t handle, |
Gilles Peskine | 9fb0e01 | 2018-07-19 15:51:49 +0200 | [diff] [blame] | 1739 | psa_algorithm_t alg, |
| 1740 | const uint8_t *nonce, |
| 1741 | size_t nonce_length, |
| 1742 | const uint8_t *additional_data, |
| 1743 | size_t additional_data_length, |
| 1744 | const uint8_t *ciphertext, |
| 1745 | size_t ciphertext_length, |
| 1746 | uint8_t *plaintext, |
| 1747 | size_t plaintext_size, |
| 1748 | size_t *plaintext_length); |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1749 | |
| 1750 | /**@}*/ |
| 1751 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1752 | /** \defgroup asymmetric Asymmetric cryptography |
| 1753 | * @{ |
| 1754 | */ |
| 1755 | |
| 1756 | /** |
| 1757 | * \brief Sign a hash or short message with a private key. |
| 1758 | * |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 1759 | * Note that to perform a hash-and-sign signature algorithm, you must |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1760 | * first calculate the hash by calling psa_hash_setup(), psa_hash_update() |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 1761 | * and psa_hash_finish(). Then pass the resulting hash as the \p hash |
| 1762 | * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg) |
| 1763 | * to determine the hash algorithm to use. |
| 1764 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1765 | * \param handle Handle to the key to use for the operation. |
| 1766 | * It must be an asymmetric key pair. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1767 | * \param alg A signature algorithm that is compatible with |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1768 | * the type of \p key. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1769 | * \param[in] hash The hash or message to sign. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1770 | * \param hash_length Size of the \p hash buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1771 | * \param[out] signature Buffer where the signature is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1772 | * \param signature_size Size of the \p signature buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1773 | * \param[out] signature_length On success, the number of bytes |
| 1774 | * that make up the returned signature value. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1775 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1776 | * \retval #PSA_SUCCESS |
| 1777 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1778 | * The size of the \p signature buffer is too small. You can |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1779 | * determine a sufficient buffer size by calling |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 1780 | * #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] | 1781 | * 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] | 1782 | * respectively of \p key. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1783 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1784 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1785 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1786 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1787 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1788 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1789 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1790 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1791 | * The library has not been previously initialized by psa_crypto_init(). |
| 1792 | * It is implementation-dependent whether a failure to initialize |
| 1793 | * results in this error code. |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1794 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1795 | psa_status_t psa_asymmetric_sign(psa_key_handle_t handle, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1796 | psa_algorithm_t alg, |
| 1797 | const uint8_t *hash, |
| 1798 | size_t hash_length, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1799 | uint8_t *signature, |
| 1800 | size_t signature_size, |
| 1801 | size_t *signature_length); |
| 1802 | |
| 1803 | /** |
| 1804 | * \brief Verify the signature a hash or short message using a public key. |
| 1805 | * |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 1806 | * Note that to perform a hash-and-sign signature algorithm, you must |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1807 | * first calculate the hash by calling psa_hash_setup(), psa_hash_update() |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 1808 | * and psa_hash_finish(). Then pass the resulting hash as the \p hash |
| 1809 | * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg) |
| 1810 | * to determine the hash algorithm to use. |
| 1811 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1812 | * \param handle Handle to the key to use for the operation. |
| 1813 | * It must be a public key or an asymmetric key pair. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1814 | * \param alg A signature algorithm that is compatible with |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1815 | * the type of \p key. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1816 | * \param[in] hash The hash or message whose signature is to be |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 1817 | * verified. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1818 | * \param hash_length Size of the \p hash buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1819 | * \param[in] signature Buffer containing the signature to verify. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1820 | * \param signature_length Size of the \p signature buffer in bytes. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1821 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1822 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1823 | * The signature is valid. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1824 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1825 | * The calculation was perfomed successfully, but the passed |
| 1826 | * signature is not a valid signature. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1827 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1828 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1829 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1830 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1831 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1832 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1833 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1834 | * The library has not been previously initialized by psa_crypto_init(). |
| 1835 | * It is implementation-dependent whether a failure to initialize |
| 1836 | * results in this error code. |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1837 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1838 | psa_status_t psa_asymmetric_verify(psa_key_handle_t handle, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1839 | psa_algorithm_t alg, |
| 1840 | const uint8_t *hash, |
| 1841 | size_t hash_length, |
Gilles Peskine | e9191ff | 2018-06-27 14:58:41 +0200 | [diff] [blame] | 1842 | const uint8_t *signature, |
Gilles Peskine | 526fab0 | 2018-06-27 18:19:40 +0200 | [diff] [blame] | 1843 | size_t signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1844 | |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 1845 | /** |
| 1846 | * \brief Encrypt a short message with a public key. |
| 1847 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1848 | * \param handle Handle to the key to use for the operation. |
| 1849 | * It must be a public key or an asymmetric |
| 1850 | * key pair. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1851 | * \param alg An asymmetric encryption algorithm that is |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1852 | * compatible with the type of \p key. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1853 | * \param[in] input The message to encrypt. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1854 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1855 | * \param[in] salt A salt or label, if supported by the |
| 1856 | * encryption algorithm. |
| 1857 | * If the algorithm does not support a |
| 1858 | * salt, pass \c NULL. |
| 1859 | * If the algorithm supports an optional |
| 1860 | * salt and you do not want to pass a salt, |
| 1861 | * pass \c NULL. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 1862 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1863 | * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is |
| 1864 | * supported. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1865 | * \param salt_length Size of the \p salt buffer in bytes. |
| 1866 | * If \p salt is \c NULL, pass 0. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1867 | * \param[out] output Buffer where the encrypted message is to |
| 1868 | * be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1869 | * \param output_size Size of the \p output buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1870 | * \param[out] output_length On success, the number of bytes |
| 1871 | * that make up the returned output. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 1872 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1873 | * \retval #PSA_SUCCESS |
| 1874 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1875 | * The size of the \p output buffer is too small. You can |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 1876 | * determine a sufficient buffer size by calling |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 1877 | * #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] | 1878 | * 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] | 1879 | * respectively of \p key. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1880 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1881 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1882 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1883 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1884 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1885 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1886 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1887 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1888 | * The library has not been previously initialized by psa_crypto_init(). |
| 1889 | * It is implementation-dependent whether a failure to initialize |
| 1890 | * results in this error code. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 1891 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1892 | psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle, |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 1893 | psa_algorithm_t alg, |
| 1894 | const uint8_t *input, |
| 1895 | size_t input_length, |
| 1896 | const uint8_t *salt, |
| 1897 | size_t salt_length, |
| 1898 | uint8_t *output, |
| 1899 | size_t output_size, |
| 1900 | size_t *output_length); |
| 1901 | |
| 1902 | /** |
| 1903 | * \brief Decrypt a short message with a private key. |
| 1904 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1905 | * \param handle Handle to the key to use for the operation. |
| 1906 | * It must be an asymmetric key pair. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1907 | * \param alg An asymmetric encryption algorithm that is |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1908 | * compatible with the type of \p key. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1909 | * \param[in] input The message to decrypt. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1910 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1911 | * \param[in] salt A salt or label, if supported by the |
| 1912 | * encryption algorithm. |
| 1913 | * If the algorithm does not support a |
| 1914 | * salt, pass \c NULL. |
| 1915 | * If the algorithm supports an optional |
| 1916 | * salt and you do not want to pass a salt, |
| 1917 | * pass \c NULL. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 1918 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1919 | * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is |
| 1920 | * supported. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1921 | * \param salt_length Size of the \p salt buffer in bytes. |
| 1922 | * If \p salt is \c NULL, pass 0. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1923 | * \param[out] output Buffer where the decrypted message is to |
| 1924 | * be written. |
| 1925 | * \param output_size Size of the \c output buffer in bytes. |
| 1926 | * \param[out] output_length On success, the number of bytes |
| 1927 | * that make up the returned output. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 1928 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1929 | * \retval #PSA_SUCCESS |
| 1930 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1931 | * The size of the \p output buffer is too small. You can |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 1932 | * determine a sufficient buffer size by calling |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 1933 | * #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] | 1934 | * 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] | 1935 | * respectively of \p key. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1936 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1937 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1938 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1939 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1940 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1941 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1942 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
| 1943 | * \retval #PSA_ERROR_INVALID_PADDING |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 1944 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 1945 | * The library has not been previously initialized by psa_crypto_init(). |
| 1946 | * It is implementation-dependent whether a failure to initialize |
| 1947 | * results in this error code. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 1948 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 1949 | psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle, |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 1950 | psa_algorithm_t alg, |
| 1951 | const uint8_t *input, |
| 1952 | size_t input_length, |
| 1953 | const uint8_t *salt, |
| 1954 | size_t salt_length, |
| 1955 | uint8_t *output, |
| 1956 | size_t output_size, |
| 1957 | size_t *output_length); |
| 1958 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1959 | /**@}*/ |
| 1960 | |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 1961 | /** \defgroup generators Generators |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 1962 | * @{ |
| 1963 | */ |
| 1964 | |
| 1965 | /** The type of the state data structure for generators. |
| 1966 | * |
| 1967 | * Before calling any function on a generator, the application must |
| 1968 | * initialize it by any of the following means: |
| 1969 | * - Set the structure to all-bits-zero, for example: |
| 1970 | * \code |
| 1971 | * psa_crypto_generator_t generator; |
| 1972 | * memset(&generator, 0, sizeof(generator)); |
| 1973 | * \endcode |
| 1974 | * - Initialize the structure to logical zero values, for example: |
| 1975 | * \code |
| 1976 | * psa_crypto_generator_t generator = {0}; |
| 1977 | * \endcode |
| 1978 | * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT, |
| 1979 | * for example: |
| 1980 | * \code |
| 1981 | * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 1982 | * \endcode |
| 1983 | * - Assign the result of the function psa_crypto_generator_init() |
| 1984 | * to the structure, for example: |
| 1985 | * \code |
| 1986 | * psa_crypto_generator_t generator; |
| 1987 | * generator = psa_crypto_generator_init(); |
| 1988 | * \endcode |
| 1989 | * |
| 1990 | * This is an implementation-defined \c struct. Applications should not |
| 1991 | * make any assumptions about the content of this structure except |
| 1992 | * as directed by the documentation of a specific implementation. |
| 1993 | */ |
| 1994 | typedef struct psa_crypto_generator_s psa_crypto_generator_t; |
| 1995 | |
| 1996 | /** \def PSA_CRYPTO_GENERATOR_INIT |
| 1997 | * |
| 1998 | * This macro returns a suitable initializer for a generator object |
| 1999 | * of type #psa_crypto_generator_t. |
| 2000 | */ |
| 2001 | #ifdef __DOXYGEN_ONLY__ |
| 2002 | /* This is an example definition for documentation purposes. |
| 2003 | * Implementations should define a suitable value in `crypto_struct.h`. |
| 2004 | */ |
| 2005 | #define PSA_CRYPTO_GENERATOR_INIT {0} |
| 2006 | #endif |
| 2007 | |
| 2008 | /** Return an initial value for a generator object. |
| 2009 | */ |
| 2010 | static psa_crypto_generator_t psa_crypto_generator_init(void); |
| 2011 | |
| 2012 | /** Retrieve the current capacity of a generator. |
| 2013 | * |
| 2014 | * The capacity of a generator is the maximum number of bytes that it can |
| 2015 | * return. Reading *N* bytes from a generator reduces its capacity by *N*. |
| 2016 | * |
| 2017 | * \param[in] generator The generator to query. |
| 2018 | * \param[out] capacity On success, the capacity of the generator. |
| 2019 | * |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 2020 | * \retval #PSA_SUCCESS |
| 2021 | * \retval #PSA_ERROR_BAD_STATE |
| 2022 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2023 | */ |
| 2024 | psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator, |
| 2025 | size_t *capacity); |
| 2026 | |
| 2027 | /** Read some data from a generator. |
| 2028 | * |
| 2029 | * This function reads and returns a sequence of bytes from a generator. |
| 2030 | * The data that is read is discarded from the generator. The generator's |
| 2031 | * capacity is decreased by the number of bytes read. |
| 2032 | * |
| 2033 | * \param[in,out] generator The generator object to read from. |
| 2034 | * \param[out] output Buffer where the generator output will be |
| 2035 | * written. |
| 2036 | * \param output_length Number of bytes to output. |
| 2037 | * |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 2038 | * \retval #PSA_SUCCESS |
| 2039 | * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2040 | * There were fewer than \p output_length bytes |
| 2041 | * in the generator. Note that in this case, no |
| 2042 | * output is written to the output buffer. |
| 2043 | * The generator's capacity is set to 0, thus |
| 2044 | * subsequent calls to this function will not |
| 2045 | * succeed, even with a smaller output buffer. |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 2046 | * \retval #PSA_ERROR_BAD_STATE |
| 2047 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2048 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2049 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2050 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2051 | */ |
| 2052 | psa_status_t psa_generator_read(psa_crypto_generator_t *generator, |
| 2053 | uint8_t *output, |
| 2054 | size_t output_length); |
| 2055 | |
| 2056 | /** Create a symmetric key from data read from a generator. |
| 2057 | * |
| 2058 | * This function reads a sequence of bytes from a generator and imports |
| 2059 | * these bytes as a key. |
| 2060 | * The data that is read is discarded from the generator. The generator's |
| 2061 | * capacity is decreased by the number of bytes read. |
| 2062 | * |
| 2063 | * This function is equivalent to calling #psa_generator_read and |
| 2064 | * passing the resulting output to #psa_import_key, but |
| 2065 | * if the implementation provides an isolation boundary then |
| 2066 | * the key material is not exposed outside the isolation boundary. |
| 2067 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2068 | * \param handle Handle to the slot where the key will be stored. |
| 2069 | * This must be a valid slot for a key of the chosen |
| 2070 | * type: it must have been obtained by calling |
| 2071 | * psa_allocate_key() or psa_create_key() with the |
| 2072 | * correct \p type and with a maximum size that is |
| 2073 | * compatible with \p bits. |
| 2074 | * It must not contain any key material yet. |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2075 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). |
| 2076 | * This must be a symmetric key type. |
| 2077 | * \param bits Key size in bits. |
| 2078 | * \param[in,out] generator The generator object to read from. |
| 2079 | * |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 2080 | * \retval #PSA_SUCCESS |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2081 | * Success. |
Gilles Peskine | 23fd2bd | 2018-12-11 15:51:32 +0100 | [diff] [blame] | 2082 | * If the key is persistent, the key material and the key's metadata |
| 2083 | * have been saved to persistent storage. |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 2084 | * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2085 | * There were fewer than \p output_length bytes |
| 2086 | * in the generator. Note that in this case, no |
| 2087 | * output is written to the output buffer. |
| 2088 | * The generator's capacity is set to 0, thus |
| 2089 | * subsequent calls to this function will not |
| 2090 | * succeed, even with a smaller output buffer. |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 2091 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2092 | * The key type or key size is not supported, either by the |
| 2093 | * implementation in general or in this particular slot. |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 2094 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2095 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 2096 | * \retval #PSA_ERROR_OCCUPIED_SLOT |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2097 | * There is already a key in the specified slot. |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 2098 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2099 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 2100 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2101 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2102 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 2103 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 2104 | * The library has not been previously initialized by psa_crypto_init(). |
| 2105 | * It is implementation-dependent whether a failure to initialize |
| 2106 | * results in this error code. |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2107 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2108 | psa_status_t psa_generator_import_key(psa_key_handle_t handle, |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2109 | psa_key_type_t type, |
| 2110 | size_t bits, |
| 2111 | psa_crypto_generator_t *generator); |
| 2112 | |
| 2113 | /** Abort a generator. |
| 2114 | * |
| 2115 | * Once a generator has been aborted, its capacity is zero. |
| 2116 | * Aborting a generator frees all associated resources except for the |
| 2117 | * \c generator structure itself. |
| 2118 | * |
| 2119 | * This function may be called at any time as long as the generator |
| 2120 | * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to |
| 2121 | * psa_crypto_generator_init() or a zero value. In particular, it is valid |
| 2122 | * to call psa_generator_abort() twice, or to call psa_generator_abort() |
| 2123 | * on a generator that has not been set up. |
| 2124 | * |
| 2125 | * Once aborted, the generator object may be called. |
| 2126 | * |
| 2127 | * \param[in,out] generator The generator to abort. |
| 2128 | * |
Gilles Peskine | 644cd5f | 2018-12-11 16:47:35 +0100 | [diff] [blame] | 2129 | * \retval #PSA_SUCCESS |
| 2130 | * \retval #PSA_ERROR_BAD_STATE |
| 2131 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2132 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2133 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2134 | */ |
| 2135 | psa_status_t psa_generator_abort(psa_crypto_generator_t *generator); |
| 2136 | |
Gilles Peskine | 8feb3a8 | 2018-09-18 12:06:11 +0200 | [diff] [blame] | 2137 | /** Use the maximum possible capacity for a generator. |
| 2138 | * |
| 2139 | * Use this value as the capacity argument when setting up a generator |
| 2140 | * to indicate that the generator should have the maximum possible capacity. |
| 2141 | * The value of the maximum possible capacity depends on the generator |
| 2142 | * algorithm. |
| 2143 | */ |
| 2144 | #define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1)) |
| 2145 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2146 | /**@}*/ |
| 2147 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2148 | /** \defgroup derivation Key derivation |
| 2149 | * @{ |
| 2150 | */ |
| 2151 | |
| 2152 | /** Set up a key derivation operation. |
| 2153 | * |
| 2154 | * A key derivation algorithm takes three inputs: a secret input \p key and |
| 2155 | * two non-secret inputs \p label and p salt. |
| 2156 | * The result of this function is a byte generator which can |
| 2157 | * be used to produce keys and other cryptographic material. |
| 2158 | * |
| 2159 | * The role of \p label and \p salt is as follows: |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame] | 2160 | * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step |
| 2161 | * and \p label is the info string used in the "expand" step. |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2162 | * |
Jaeden Amero | 9e919c6 | 2019-01-07 15:41:50 +0000 | [diff] [blame] | 2163 | * \param[in,out] generator The generator object to set up. It must have |
| 2164 | * been initialized as per the documentation for |
| 2165 | * #psa_crypto_generator_t and not yet in use. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2166 | * \param handle Handle to the secret key. |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2167 | * \param alg The key derivation algorithm to compute |
| 2168 | * (\c PSA_ALG_XXX value such that |
| 2169 | * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true). |
| 2170 | * \param[in] salt Salt to use. |
| 2171 | * \param salt_length Size of the \p salt buffer in bytes. |
| 2172 | * \param[in] label Label to use. |
| 2173 | * \param label_length Size of the \p label buffer in bytes. |
| 2174 | * \param capacity The maximum number of bytes that the |
| 2175 | * generator will be able to provide. |
| 2176 | * |
| 2177 | * \retval #PSA_SUCCESS |
| 2178 | * Success. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2179 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2180 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 2181 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 2182 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2183 | * \c key is not compatible with \c alg, |
| 2184 | * or \p capacity is too large for the specified algorithm and key. |
| 2185 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2186 | * \c alg is not supported or is not a key derivation algorithm. |
| 2187 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2188 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2189 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2190 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 2191 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 2192 | * The library has not been previously initialized by psa_crypto_init(). |
| 2193 | * It is implementation-dependent whether a failure to initialize |
| 2194 | * results in this error code. |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2195 | */ |
| 2196 | psa_status_t psa_key_derivation(psa_crypto_generator_t *generator, |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2197 | psa_key_handle_t handle, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2198 | psa_algorithm_t alg, |
| 2199 | const uint8_t *salt, |
| 2200 | size_t salt_length, |
| 2201 | const uint8_t *label, |
| 2202 | size_t label_length, |
| 2203 | size_t capacity); |
| 2204 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2205 | /** Set up a key agreement operation. |
| 2206 | * |
| 2207 | * A key agreement algorithm takes two inputs: a private key \p private_key |
| 2208 | * a public key \p peer_key. |
| 2209 | * The result of this function is a byte generator which can |
| 2210 | * be used to produce keys and other cryptographic material. |
| 2211 | * |
Gilles Peskine | 211a436 | 2018-10-25 22:22:31 +0200 | [diff] [blame] | 2212 | * The resulting generator always has the maximum capacity permitted by |
| 2213 | * the algorithm. |
| 2214 | * |
Jaeden Amero | 8afbff8 | 2019-01-14 16:56:20 +0000 | [diff] [blame] | 2215 | * \param[in,out] generator The generator object to set up. It must have been |
| 2216 | * initialized as per the documentation for |
| 2217 | * #psa_crypto_generator_t and not yet in use. |
| 2218 | * \param private_key Handle to the private key to use. |
| 2219 | * \param[in] peer_key Public key of the peer. The peer key must be in the |
| 2220 | * same format that psa_import_key() accepts for the |
| 2221 | * public key type corresponding to the type of |
| 2222 | * private_key. That is, this function performs the |
| 2223 | * equivalent of |
| 2224 | * `psa_import_key(internal_public_key_handle, |
| 2225 | * PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(private_key_type), |
| 2226 | * peer_key, peer_key_length)` where |
| 2227 | * `private_key_type` is the type of `private_key`. |
| 2228 | * For example, for EC keys, this means that peer_key |
| 2229 | * is interpreted as a point on the curve that the |
| 2230 | * private key is on. The standard formats for public |
| 2231 | * keys are documented in the documentation of |
| 2232 | * psa_export_public_key(). |
| 2233 | * \param peer_key_length Size of \p peer_key in bytes. |
| 2234 | * \param alg The key agreement algorithm to compute |
| 2235 | * (\c PSA_ALG_XXX value such that |
| 2236 | * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true). |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2237 | * |
| 2238 | * \retval #PSA_SUCCESS |
| 2239 | * Success. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2240 | * \retval #PSA_ERROR_INVALID_HANDLE |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2241 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 2242 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 2243 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2244 | * \c private_key is not compatible with \c alg, |
| 2245 | * or \p peer_key is not valid for \c alg or not compatible with |
| 2246 | * \c private_key. |
| 2247 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2248 | * \c alg is not supported or is not a key derivation algorithm. |
| 2249 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2250 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2251 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2252 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2253 | */ |
| 2254 | psa_status_t psa_key_agreement(psa_crypto_generator_t *generator, |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2255 | psa_key_handle_t private_key, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2256 | const uint8_t *peer_key, |
| 2257 | size_t peer_key_length, |
| 2258 | psa_algorithm_t alg); |
| 2259 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2260 | /**@}*/ |
| 2261 | |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 2262 | /** \defgroup random Random generation |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2263 | * @{ |
| 2264 | */ |
| 2265 | |
| 2266 | /** |
| 2267 | * \brief Generate random bytes. |
| 2268 | * |
| 2269 | * \warning This function **can** fail! Callers MUST check the return status |
| 2270 | * and MUST NOT use the content of the output buffer if the return |
| 2271 | * status is not #PSA_SUCCESS. |
| 2272 | * |
| 2273 | * \note To generate a key, use psa_generate_key() instead. |
| 2274 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2275 | * \param[out] output Output buffer for the generated data. |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2276 | * \param output_size Number of bytes to generate and output. |
| 2277 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2278 | * \retval #PSA_SUCCESS |
| 2279 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2280 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
| 2281 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2282 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2283 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 2284 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 2285 | * The library has not been previously initialized by psa_crypto_init(). |
| 2286 | * It is implementation-dependent whether a failure to initialize |
| 2287 | * results in this error code. |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2288 | */ |
| 2289 | psa_status_t psa_generate_random(uint8_t *output, |
| 2290 | size_t output_size); |
| 2291 | |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 2292 | /** Extra parameters for RSA key generation. |
| 2293 | * |
Gilles Peskine | be42f31 | 2018-07-13 14:38:15 +0200 | [diff] [blame] | 2294 | * 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] | 2295 | * parameter to psa_generate_key(). |
| 2296 | */ |
| 2297 | typedef struct { |
Gilles Peskine | edd7687 | 2018-07-20 17:42:05 +0200 | [diff] [blame] | 2298 | uint32_t e; /**< Public exponent value. Default: 65537. */ |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 2299 | } psa_generate_key_extra_rsa; |
| 2300 | |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2301 | /** |
| 2302 | * \brief Generate a key or key pair. |
| 2303 | * |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2304 | * \param handle Handle to the slot where the key will be stored. |
| 2305 | * This must be a valid slot for a key of the chosen |
| 2306 | * type: it must have been obtained by calling |
| 2307 | * psa_allocate_key() or psa_create_key() with the |
| 2308 | * correct \p type and with a maximum size that is |
| 2309 | * compatible with \p bits. |
| 2310 | * It must not contain any key material yet. |
Gilles Peskine | 4e69d7a | 2018-06-19 20:19:14 +0200 | [diff] [blame] | 2311 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). |
| 2312 | * \param bits Key size in bits. |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 2313 | * \param[in] extra Extra parameters for key generation. The |
Gilles Peskine | 4e69d7a | 2018-06-19 20:19:14 +0200 | [diff] [blame] | 2314 | * interpretation of this parameter depends on |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2315 | * \p type. All types support \c NULL to use |
Gilles Peskine | 3fa675c | 2018-07-12 01:31:03 +0200 | [diff] [blame] | 2316 | * default parameters. Implementation that support |
| 2317 | * the generation of vendor-specific key types |
| 2318 | * that allow extra parameters shall document |
| 2319 | * the format of these extra parameters and |
| 2320 | * the default values. For standard parameters, |
| 2321 | * the meaning of \p extra is as follows: |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2322 | * - For a symmetric key type (a type such |
Gilles Peskine | 3fa675c | 2018-07-12 01:31:03 +0200 | [diff] [blame] | 2323 | * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is |
| 2324 | * false), \p extra must be \c NULL. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2325 | * - For an elliptic curve key type (a type |
Gilles Peskine | 3fa675c | 2018-07-12 01:31:03 +0200 | [diff] [blame] | 2326 | * such that #PSA_KEY_TYPE_IS_ECC(\p type) is |
| 2327 | * false), \p extra must be \c NULL. |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 2328 | * - For an RSA key (\p type is |
| 2329 | * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an |
| 2330 | * optional #psa_generate_key_extra_rsa structure |
Gilles Peskine | 3fa675c | 2018-07-12 01:31:03 +0200 | [diff] [blame] | 2331 | * specifying the public exponent. The |
| 2332 | * default public exponent used when \p extra |
| 2333 | * is \c NULL is 65537. |
Jaeden Amero | 1308fb5 | 2019-01-11 13:50:43 +0000 | [diff] [blame] | 2334 | * - For an DSA key (\p type is |
| 2335 | * #PSA_KEY_TYPE_DSA_KEYPAIR), \p extra is an |
| 2336 | * optional structure specifying the key domain |
| 2337 | * parameters. The key domain parameters can also be |
| 2338 | * provided by psa_set_key_domain_parameters(), |
| 2339 | * which documents the format of the structure. |
Jaeden Amero | 8851c40 | 2019-01-11 14:20:03 +0000 | [diff] [blame] | 2340 | * - For a DH key (\p type is |
| 2341 | * #PSA_KEY_TYPE_DH_KEYPAIR), the \p extra is an |
| 2342 | * optional structure specifying the key domain |
| 2343 | * parameters. The key domain parameters can also be |
| 2344 | * provided by psa_set_key_domain_parameters(), |
| 2345 | * which documents the format of the structure. |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 2346 | * \param extra_size Size of the buffer that \p extra |
| 2347 | * points to, in bytes. Note that if \p extra is |
| 2348 | * \c NULL then \p extra_size must be zero. |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2349 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2350 | * \retval #PSA_SUCCESS |
Gilles Peskine | 23fd2bd | 2018-12-11 15:51:32 +0100 | [diff] [blame] | 2351 | * Success. |
| 2352 | * If the key is persistent, the key material and the key's metadata |
| 2353 | * have been saved to persistent storage. |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2354 | * \retval #PSA_ERROR_INVALID_HANDLE |
| 2355 | * \retval #PSA_ERROR_OCCUPIED_SLOT |
| 2356 | * There is already a key in the specified slot. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2357 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2358 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2359 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2360 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
| 2361 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2362 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2363 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
itayzafrir | 90d8c7a | 2018-09-12 11:44:52 +0300 | [diff] [blame] | 2364 | * \retval #PSA_ERROR_BAD_STATE |
itayzafrir | 1861709 | 2018-09-16 12:22:41 +0300 | [diff] [blame] | 2365 | * The library has not been previously initialized by psa_crypto_init(). |
| 2366 | * It is implementation-dependent whether a failure to initialize |
| 2367 | * results in this error code. |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2368 | */ |
Gilles Peskine | ae32aac | 2018-11-30 14:39:32 +0100 | [diff] [blame] | 2369 | psa_status_t psa_generate_key(psa_key_handle_t handle, |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2370 | psa_key_type_t type, |
| 2371 | size_t bits, |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 2372 | const void *extra, |
| 2373 | size_t extra_size); |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2374 | |
| 2375 | /**@}*/ |
| 2376 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2377 | #ifdef __cplusplus |
| 2378 | } |
| 2379 | #endif |
| 2380 | |
Gilles Peskine | 0cad07c | 2018-06-27 19:49:02 +0200 | [diff] [blame] | 2381 | /* The file "crypto_sizes.h" contains definitions for size calculation |
| 2382 | * macros whose definitions are implementation-specific. */ |
| 2383 | #include "crypto_sizes.h" |
| 2384 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2385 | /* The file "crypto_struct.h" contains definitions for |
| 2386 | * implementation-specific structs that are declared above. */ |
| 2387 | #include "crypto_struct.h" |
| 2388 | |
| 2389 | /* The file "crypto_extra.h" contains vendor-specific definitions. This |
| 2390 | * can include vendor-defined algorithms, extra functions, etc. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2391 | #include "crypto_extra.h" |
| 2392 | |
| 2393 | #endif /* PSA_CRYPTO_H */ |