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