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