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