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