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