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