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