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