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