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