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