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