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