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