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