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 | */ |
| 5 | |
| 6 | #ifndef PSA_CRYPTO_H |
| 7 | #define PSA_CRYPTO_H |
| 8 | |
| 9 | #include "crypto_platform.h" |
| 10 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 11 | #include <stddef.h> |
| 12 | |
Gilles Peskine | 62a7e7e | 2018-02-07 21:54:47 +0100 | [diff] [blame] | 13 | #ifdef __DOXYGEN_ONLY__ |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 14 | /* This __DOXYGEN_ONLY__ block contains mock definitions for things that |
| 15 | * must be defined in the crypto_platform.h header. These mock definitions |
| 16 | * are present in this file as a convenience to generate pretty-printed |
| 17 | * documentation that includes those definitions. */ |
| 18 | |
Gilles Peskine | 62a7e7e | 2018-02-07 21:54:47 +0100 | [diff] [blame] | 19 | /** \defgroup platform Implementation-specific definitions |
| 20 | * @{ |
| 21 | */ |
| 22 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 23 | /** \brief Key slot number. |
| 24 | * |
| 25 | * This type represents key slots. It must be an unsigned integral |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 26 | * type. The choice of type is implementation-dependent. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 27 | * 0 is not a valid key slot number. The meaning of other values is |
| 28 | * implementation dependent. |
| 29 | * |
| 30 | * At any given point in time, each key slot either contains a |
| 31 | * cryptographic object, or is empty. Key slots are persistent: |
| 32 | * once set, the cryptographic object remains in the key slot until |
| 33 | * explicitly destroyed. |
| 34 | */ |
| 35 | typedef _unsigned_integral_type_ psa_key_slot_t; |
| 36 | |
Gilles Peskine | 62a7e7e | 2018-02-07 21:54:47 +0100 | [diff] [blame] | 37 | /**@}*/ |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 38 | #endif /* __DOXYGEN_ONLY__ */ |
Gilles Peskine | 62a7e7e | 2018-02-07 21:54:47 +0100 | [diff] [blame] | 39 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 40 | #ifdef __cplusplus |
| 41 | extern "C" { |
| 42 | #endif |
| 43 | |
| 44 | /** \defgroup basic Basic definitions |
| 45 | * @{ |
| 46 | */ |
| 47 | |
Gilles Peskine | e9a0a9d | 2018-06-20 13:59:04 +0200 | [diff] [blame] | 48 | #if defined(PSA_SUCCESS) |
| 49 | /* If PSA_SUCCESS is defined, assume that PSA crypto is being used |
| 50 | * together with PSA IPC, which also defines the identifier |
| 51 | * PSA_SUCCESS. We must not define PSA_SUCCESS ourselves in that case; |
| 52 | * the other error code names don't clash. Also define psa_status_t as |
| 53 | * an alias for the type used by PSA IPC. This is a temporary hack |
| 54 | * until we unify error reporting in PSA IPC and PSA crypo. |
| 55 | * |
| 56 | * Note that psa_defs.h must be included before this header! |
| 57 | */ |
| 58 | typedef psa_error_t psa_status_t; |
| 59 | |
| 60 | #else /* defined(PSA_SUCCESS) */ |
| 61 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 62 | /** |
| 63 | * \brief Function return status. |
| 64 | * |
Gilles Peskine | e9a0a9d | 2018-06-20 13:59:04 +0200 | [diff] [blame] | 65 | * This is either #PSA_SUCCESS (which is zero), indicating success, |
| 66 | * or a nonzero value indicating that an error occurred. Errors are |
| 67 | * encoded as one of the \c PSA_ERROR_xxx values defined here. |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 68 | */ |
itayzafrir | c2a7976 | 2018-06-18 16:20:16 +0300 | [diff] [blame] | 69 | typedef int32_t psa_status_t; |
Gilles Peskine | e9a0a9d | 2018-06-20 13:59:04 +0200 | [diff] [blame] | 70 | |
itayzafrir | c2a7976 | 2018-06-18 16:20:16 +0300 | [diff] [blame] | 71 | /** The action was completed successfully. */ |
| 72 | #define PSA_SUCCESS ((psa_status_t)0) |
Gilles Peskine | e9a0a9d | 2018-06-20 13:59:04 +0200 | [diff] [blame] | 73 | |
| 74 | #endif /* !defined(PSA_SUCCESS) */ |
itayzafrir | c2a7976 | 2018-06-18 16:20:16 +0300 | [diff] [blame] | 75 | |
| 76 | /** The requested operation or a parameter is not supported |
| 77 | * by this implementation. |
| 78 | * |
| 79 | * Implementations should return this error code when an enumeration |
| 80 | * parameter such as a key type, algorithm, etc. is not recognized. |
| 81 | * If a combination of parameters is recognized and identified as |
| 82 | * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */ |
| 83 | #define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)1) |
| 84 | |
| 85 | /** The requested action is denied by a policy. |
| 86 | * |
| 87 | * Implementations should return this error code when the parameters |
| 88 | * are recognized as valid and supported, and a policy explicitly |
| 89 | * denies the requested operation. |
| 90 | * |
| 91 | * If a subset of the parameters of a function call identify a |
| 92 | * forbidden operation, and another subset of the parameters are |
| 93 | * not valid or not supported, it is unspecified whether the function |
| 94 | * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or |
| 95 | * #PSA_ERROR_INVALID_ARGUMENT. */ |
| 96 | #define PSA_ERROR_NOT_PERMITTED ((psa_status_t)2) |
| 97 | |
| 98 | /** An output buffer is too small. |
| 99 | * |
Gilles Peskine | be42f31 | 2018-07-13 14:38:15 +0200 | [diff] [blame] | 100 | * Applications can call the \c PSA_xxx_SIZE macro listed in the function |
itayzafrir | c2a7976 | 2018-06-18 16:20:16 +0300 | [diff] [blame] | 101 | * description to determine a sufficient buffer size. |
| 102 | * |
| 103 | * Implementations should preferably return this error code only |
| 104 | * in cases when performing the operation with a larger output |
| 105 | * buffer would succeed. However implementations may return this |
| 106 | * error if a function has invalid or unsupported parameters in addition |
| 107 | * to the parameters that determine the necessary output buffer size. */ |
| 108 | #define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)3) |
| 109 | |
| 110 | /** A slot is occupied, but must be empty to carry out the |
| 111 | * requested action. |
| 112 | * |
| 113 | * If the slot number is invalid (i.e. the requested action could |
| 114 | * not be performed even after erasing the slot's content), |
| 115 | * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */ |
| 116 | #define PSA_ERROR_OCCUPIED_SLOT ((psa_status_t)4) |
| 117 | |
| 118 | /** A slot is empty, but must be occupied to carry out the |
| 119 | * requested action. |
| 120 | * |
| 121 | * If the slot number is invalid (i.e. the requested action could |
| 122 | * not be performed even after creating appropriate content in the slot), |
| 123 | * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */ |
| 124 | #define PSA_ERROR_EMPTY_SLOT ((psa_status_t)5) |
| 125 | |
| 126 | /** The requested action cannot be performed in the current state. |
| 127 | * |
| 128 | * Multipart operations return this error when one of the |
| 129 | * functions is called out of sequence. Refer to the function |
| 130 | * descriptions for permitted sequencing of functions. |
| 131 | * |
| 132 | * Implementations shall not return this error code to indicate |
| 133 | * that a key slot is occupied when it needs to be free or vice versa, |
| 134 | * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT |
| 135 | * as applicable. */ |
| 136 | #define PSA_ERROR_BAD_STATE ((psa_status_t)6) |
| 137 | |
| 138 | /** The parameters passed to the function are invalid. |
| 139 | * |
| 140 | * Implementations may return this error any time a parameter or |
| 141 | * combination of parameters are recognized as invalid. |
| 142 | * |
| 143 | * Implementations shall not return this error code to indicate |
| 144 | * that a key slot is occupied when it needs to be free or vice versa, |
| 145 | * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT |
| 146 | * as applicable. */ |
| 147 | #define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)7) |
| 148 | |
| 149 | /** There is not enough runtime memory. |
| 150 | * |
| 151 | * If the action is carried out across multiple security realms, this |
| 152 | * error can refer to available memory in any of the security realms. */ |
| 153 | #define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)8) |
| 154 | |
| 155 | /** There is not enough persistent storage. |
| 156 | * |
| 157 | * Functions that modify the key storage return this error code if |
| 158 | * there is insufficient storage space on the host media. In addition, |
| 159 | * many functions that do not otherwise access storage may return this |
| 160 | * error code if the implementation requires a mandatory log entry for |
| 161 | * the requested action and the log storage space is full. */ |
| 162 | #define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)9) |
| 163 | |
| 164 | /** There was a communication failure inside the implementation. |
| 165 | * |
| 166 | * This can indicate a communication failure between the application |
| 167 | * and an external cryptoprocessor or between the cryptoprocessor and |
| 168 | * an external volatile or persistent memory. A communication failure |
| 169 | * may be transient or permanent depending on the cause. |
| 170 | * |
| 171 | * \warning If a function returns this error, it is undetermined |
| 172 | * whether the requested action has completed or not. Implementations |
| 173 | * should return #PSA_SUCCESS on successful completion whenver |
| 174 | * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE |
| 175 | * if the requested action was completed successfully in an external |
| 176 | * cryptoprocessor but there was a breakdown of communication before |
| 177 | * the cryptoprocessor could report the status to the application. |
| 178 | */ |
| 179 | #define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)10) |
| 180 | |
| 181 | /** There was a storage failure that may have led to data loss. |
| 182 | * |
| 183 | * This error indicates that some persistent storage is corrupted. |
| 184 | * It should not be used for a corruption of volatile memory |
| 185 | * (use #PSA_ERROR_TAMPERING_DETECTED), for a communication error |
| 186 | * between the cryptoprocessor and its external storage (use |
| 187 | * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is |
| 188 | * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE). |
| 189 | * |
| 190 | * Note that a storage failure does not indicate that any data that was |
| 191 | * previously read is invalid. However this previously read data may no |
| 192 | * longer be readable from storage. |
| 193 | * |
| 194 | * When a storage failure occurs, it is no longer possible to ensure |
| 195 | * the global integrity of the keystore. Depending on the global |
| 196 | * integrity guarantees offered by the implementation, access to other |
| 197 | * data may or may not fail even if the data is still readable but |
| 198 | * its integrity canont be guaranteed. |
| 199 | * |
| 200 | * Implementations should only use this error code to report a |
| 201 | * permanent storage corruption. However application writers should |
| 202 | * keep in mind that transient errors while reading the storage may be |
| 203 | * reported using this error code. */ |
| 204 | #define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)11) |
| 205 | |
| 206 | /** A hardware failure was detected. |
| 207 | * |
| 208 | * A hardware failure may be transient or permanent depending on the |
| 209 | * cause. */ |
| 210 | #define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)12) |
| 211 | |
| 212 | /** A tampering attempt was detected. |
| 213 | * |
| 214 | * If an application receives this error code, there is no guarantee |
| 215 | * that previously accessed or computed data was correct and remains |
| 216 | * confidential. Applications should not perform any security function |
| 217 | * and should enter a safe failure state. |
| 218 | * |
| 219 | * Implementations may return this error code if they detect an invalid |
| 220 | * state that cannot happen during normal operation and that indicates |
| 221 | * that the implementation's security guarantees no longer hold. Depending |
| 222 | * on the implementation architecture and on its security and safety goals, |
| 223 | * the implementation may forcibly terminate the application. |
| 224 | * |
| 225 | * This error code is intended as a last resort when a security breach |
| 226 | * is detected and it is unsure whether the keystore data is still |
| 227 | * protected. Implementations shall only return this error code |
| 228 | * to report an alarm from a tampering detector, to indicate that |
| 229 | * the confidentiality of stored data can no longer be guaranteed, |
| 230 | * or to indicate that the integrity of previously returned data is now |
| 231 | * considered compromised. Implementations shall not use this error code |
| 232 | * to indicate a hardware failure that merely makes it impossible to |
| 233 | * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE, |
| 234 | * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE, |
| 235 | * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code |
| 236 | * instead). |
| 237 | * |
| 238 | * This error indicates an attack against the application. Implementations |
| 239 | * shall not return this error code as a consequence of the behavior of |
| 240 | * the application itself. */ |
| 241 | #define PSA_ERROR_TAMPERING_DETECTED ((psa_status_t)13) |
| 242 | |
| 243 | /** There is not enough entropy to generate random data needed |
| 244 | * for the requested action. |
| 245 | * |
| 246 | * This error indicates a failure of a hardware random generator. |
| 247 | * Application writers should note that this error can be returned not |
| 248 | * only by functions whose purpose is to generate random data, such |
| 249 | * as key, IV or nonce generation, but also by functions that execute |
| 250 | * an algorithm with a randomized result, as well as functions that |
| 251 | * use randomization of intermediate computations as a countermeasure |
| 252 | * to certain attacks. |
| 253 | * |
| 254 | * Implementations should avoid returning this error after psa_crypto_init() |
| 255 | * has succeeded. Implementations should generate sufficient |
| 256 | * entropy during initialization and subsequently use a cryptographically |
| 257 | * secure pseudorandom generator (PRNG). However implementations may return |
| 258 | * this error at any time if a policy requires the PRNG to be reseeded |
| 259 | * during normal operation. */ |
| 260 | #define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)14) |
| 261 | |
| 262 | /** The signature, MAC or hash is incorrect. |
| 263 | * |
| 264 | * Verification functions return this error if the verification |
| 265 | * calculations completed successfully, and the value to be verified |
| 266 | * was determined to be incorrect. |
| 267 | * |
| 268 | * If the value to verify has an invalid size, implementations may return |
| 269 | * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */ |
| 270 | #define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)15) |
| 271 | |
| 272 | /** The decrypted padding is incorrect. |
| 273 | * |
| 274 | * \warning In some protocols, when decrypting data, it is essential that |
| 275 | * the behavior of the application does not depend on whether the padding |
| 276 | * is correct, down to precise timing. Applications should prefer |
| 277 | * protocols that use authenticated encryption rather than plain |
| 278 | * encryption. If the application must perform a decryption of |
| 279 | * unauthenticated data, the application writer should take care not |
| 280 | * to reveal whether the padding is invalid. |
| 281 | * |
| 282 | * Implementations should strive to make valid and invalid padding |
| 283 | * as close as possible to indistinguishable to an external observer. |
| 284 | * In particular, the timing of a decryption operation should not |
| 285 | * depend on the validity of the padding. */ |
| 286 | #define PSA_ERROR_INVALID_PADDING ((psa_status_t)16) |
| 287 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 288 | /** The generator has insufficient capacity left. |
| 289 | * |
| 290 | * Once a function returns this error, attempts to read from the |
| 291 | * generator will always return this error. */ |
| 292 | #define PSA_ERROR_INSUFFICIENT_CAPACITY ((psa_status_t)17) |
| 293 | |
itayzafrir | c2a7976 | 2018-06-18 16:20:16 +0300 | [diff] [blame] | 294 | /** An error occurred that does not correspond to any defined |
| 295 | * failure cause. |
| 296 | * |
| 297 | * Implementations may use this error code if none of the other standard |
| 298 | * error codes are applicable. */ |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 299 | #define PSA_ERROR_UNKNOWN_ERROR ((psa_status_t)18) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 300 | |
| 301 | /** |
| 302 | * \brief Library initialization. |
| 303 | * |
| 304 | * Applications must call this function before calling any other |
| 305 | * function in this module. |
| 306 | * |
| 307 | * Applications may call this function more than once. Once a call |
| 308 | * succeeds, subsequent calls are guaranteed to succeed. |
| 309 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 310 | * \retval #PSA_SUCCESS |
| 311 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 312 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 313 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 314 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 315 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 316 | */ |
| 317 | psa_status_t psa_crypto_init(void); |
| 318 | |
Gilles Peskine | 2905a7a | 2018-03-07 16:39:31 +0100 | [diff] [blame] | 319 | #define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8) |
| 320 | #define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8) |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 321 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 322 | /**@}*/ |
| 323 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 324 | /** \defgroup crypto_types Key and algorithm types |
| 325 | * @{ |
| 326 | */ |
| 327 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 328 | /** \brief Encoding of a key type. |
| 329 | */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 330 | typedef uint32_t psa_key_type_t; |
| 331 | |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 332 | /** An invalid key type value. |
| 333 | * |
| 334 | * Zero is not the encoding of any key type. |
| 335 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 336 | #define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000) |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 337 | |
| 338 | /** Vendor-defined flag |
| 339 | * |
| 340 | * Key types defined by this standard will never have the |
| 341 | * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types |
| 342 | * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should |
| 343 | * respect the bitwise structure used by standard encodings whenever practical. |
| 344 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 345 | #define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 346 | |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 347 | #define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x7e000000) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 348 | |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 349 | /** Raw data. |
| 350 | * |
| 351 | * A "key" of this type cannot be used for any cryptographic operation. |
| 352 | * Applications may use this type to store arbitrary data in the keystore. */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 353 | #define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x02000000) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 354 | |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 355 | #define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x04000000) |
| 356 | #define PSA_KEY_TYPE_CATEGORY_ASYMMETRIC ((psa_key_type_t)0x06000000) |
| 357 | #define PSA_KEY_TYPE_PAIR_FLAG ((psa_key_type_t)0x01000000) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 358 | |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 359 | /** HMAC key. |
| 360 | * |
| 361 | * The key policy determines which underlying hash algorithm the key can be |
| 362 | * used for. |
| 363 | * |
| 364 | * HMAC keys should generally have the same size as the underlying hash. |
Gilles Peskine | be42f31 | 2018-07-13 14:38:15 +0200 | [diff] [blame] | 365 | * This size can be calculated with #PSA_HASH_SIZE(\c alg) where |
| 366 | * \c alg is the HMAC algorithm or the underlying hash algorithm. */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 367 | #define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x02000001) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 368 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 369 | /** A secret for key derivation. |
| 370 | * |
| 371 | * The key policy determines which key derivation algorithm the key |
| 372 | * can be used for. |
| 373 | */ |
| 374 | #define PSA_KEY_TYPE_DERIVE ((psa_key_type_t)0x02000101) |
| 375 | |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 376 | /** Key for an cipher, AEAD or MAC algorithm based on the AES block cipher. |
| 377 | * |
| 378 | * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or |
| 379 | * 32 bytes (AES-256). |
| 380 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 381 | #define PSA_KEY_TYPE_AES ((psa_key_type_t)0x04000001) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 382 | |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 383 | /** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES). |
| 384 | * |
| 385 | * The size of the key can be 8 bytes (single DES), 16 bytes (2-key 3DES) or |
| 386 | * 24 bytes (3-key 3DES). |
| 387 | * |
| 388 | * Note that single DES and 2-key 3DES are weak and strongly |
| 389 | * deprecated and should only be used to decrypt legacy data. 3-key 3DES |
| 390 | * is weak and deprecated and should only be used in legacy protocols. |
| 391 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 392 | #define PSA_KEY_TYPE_DES ((psa_key_type_t)0x04000002) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 393 | |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 394 | /** Key for an cipher, AEAD or MAC algorithm based on the |
| 395 | * Camellia block cipher. */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 396 | #define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x04000003) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 397 | |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 398 | /** Key for the RC4 stream cipher. |
| 399 | * |
| 400 | * Note that RC4 is weak and deprecated and should only be used in |
| 401 | * legacy protocols. */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 402 | #define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x04000004) |
| 403 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 404 | /** RSA public key. */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 405 | #define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x06010000) |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 406 | /** RSA key pair (private and public key). */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 407 | #define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x07010000) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 408 | |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 409 | /** DSA public key. */ |
| 410 | #define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x06020000) |
| 411 | /** DSA key pair (private and public key). */ |
| 412 | #define PSA_KEY_TYPE_DSA_KEYPAIR ((psa_key_type_t)0x07020000) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 413 | |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 414 | #define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x06030000) |
| 415 | #define PSA_KEY_TYPE_ECC_KEYPAIR_BASE ((psa_key_type_t)0x07030000) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 416 | #define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 417 | /** Elliptic curve key pair. */ |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 418 | #define PSA_KEY_TYPE_ECC_KEYPAIR(curve) \ |
| 419 | (PSA_KEY_TYPE_ECC_KEYPAIR_BASE | (curve)) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 420 | /** Elliptic curve public key. */ |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 421 | #define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \ |
| 422 | (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve)) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 423 | |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 424 | /** Whether a key type is vendor-defined. */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 425 | #define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 426 | (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0) |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 427 | |
| 428 | /** Whether a key type is asymmetric: either a key pair or a public key. */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 429 | #define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \ |
| 430 | (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_ASYMMETRIC) |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 431 | /** Whether a key type is the public part of a key pair. */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 432 | #define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \ |
Moran Peker | b4d0ddd | 2018-04-04 12:47:52 +0300 | [diff] [blame] | 433 | (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \ |
| 434 | PSA_KEY_TYPE_CATEGORY_ASYMMETRIC) |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 435 | /** Whether a key type is a key pair containing a private part and a public |
| 436 | * part. */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 437 | #define PSA_KEY_TYPE_IS_KEYPAIR(type) \ |
| 438 | (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \ |
| 439 | (PSA_KEY_TYPE_CATEGORY_ASYMMETRIC | PSA_KEY_TYPE_PAIR_FLAG)) |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 440 | /** The key pair type corresponding to a public key type. */ |
| 441 | #define PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY(type) \ |
| 442 | ((type) | PSA_KEY_TYPE_PAIR_FLAG) |
| 443 | /** The public key type corresponding to a key pair type. */ |
| 444 | #define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) \ |
| 445 | ((type) & ~PSA_KEY_TYPE_PAIR_FLAG) |
Gilles Peskine | 61a6037 | 2018-07-08 21:48:44 +0200 | [diff] [blame] | 446 | /** Whether a key type is an RSA key pair or public key. */ |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 447 | #define PSA_KEY_TYPE_IS_RSA(type) \ |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 448 | (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY) |
| 449 | /** Whether a key type is an elliptic curve key pair or public key. */ |
Gilles Peskine | c66ea6a | 2018-02-03 22:43:28 +0100 | [diff] [blame] | 450 | #define PSA_KEY_TYPE_IS_ECC(type) \ |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 451 | ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \ |
| 452 | ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 453 | |
Gilles Peskine | e1fed0d | 2018-06-18 20:45:45 +0200 | [diff] [blame] | 454 | /** The type of PSA elliptic curve identifiers. */ |
| 455 | typedef uint16_t psa_ecc_curve_t; |
| 456 | /** Extract the curve from an elliptic curve key type. */ |
| 457 | #define PSA_KEY_TYPE_GET_CURVE(type) \ |
| 458 | ((psa_ecc_curve_t) (PSA_KEY_TYPE_IS_ECC(type) ? \ |
| 459 | ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \ |
| 460 | 0)) |
| 461 | |
| 462 | /* The encoding of curve identifiers is currently aligned with the |
| 463 | * TLS Supported Groups Registry (formerly known as the |
| 464 | * TLS EC Named Curve Registry) |
| 465 | * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8 |
| 466 | * The values are defined by RFC 4492, RFC 7027 and RFC 7919. */ |
| 467 | #define PSA_ECC_CURVE_SECT163K1 ((psa_ecc_curve_t) 0x0001) |
| 468 | #define PSA_ECC_CURVE_SECT163R1 ((psa_ecc_curve_t) 0x0002) |
| 469 | #define PSA_ECC_CURVE_SECT163R2 ((psa_ecc_curve_t) 0x0003) |
| 470 | #define PSA_ECC_CURVE_SECT193R1 ((psa_ecc_curve_t) 0x0004) |
| 471 | #define PSA_ECC_CURVE_SECT193R2 ((psa_ecc_curve_t) 0x0005) |
| 472 | #define PSA_ECC_CURVE_SECT233K1 ((psa_ecc_curve_t) 0x0006) |
| 473 | #define PSA_ECC_CURVE_SECT233R1 ((psa_ecc_curve_t) 0x0007) |
| 474 | #define PSA_ECC_CURVE_SECT239K1 ((psa_ecc_curve_t) 0x0008) |
| 475 | #define PSA_ECC_CURVE_SECT283K1 ((psa_ecc_curve_t) 0x0009) |
| 476 | #define PSA_ECC_CURVE_SECT283R1 ((psa_ecc_curve_t) 0x000a) |
| 477 | #define PSA_ECC_CURVE_SECT409K1 ((psa_ecc_curve_t) 0x000b) |
| 478 | #define PSA_ECC_CURVE_SECT409R1 ((psa_ecc_curve_t) 0x000c) |
| 479 | #define PSA_ECC_CURVE_SECT571K1 ((psa_ecc_curve_t) 0x000d) |
| 480 | #define PSA_ECC_CURVE_SECT571R1 ((psa_ecc_curve_t) 0x000e) |
| 481 | #define PSA_ECC_CURVE_SECP160K1 ((psa_ecc_curve_t) 0x000f) |
| 482 | #define PSA_ECC_CURVE_SECP160R1 ((psa_ecc_curve_t) 0x0010) |
| 483 | #define PSA_ECC_CURVE_SECP160R2 ((psa_ecc_curve_t) 0x0011) |
| 484 | #define PSA_ECC_CURVE_SECP192K1 ((psa_ecc_curve_t) 0x0012) |
| 485 | #define PSA_ECC_CURVE_SECP192R1 ((psa_ecc_curve_t) 0x0013) |
| 486 | #define PSA_ECC_CURVE_SECP224K1 ((psa_ecc_curve_t) 0x0014) |
| 487 | #define PSA_ECC_CURVE_SECP224R1 ((psa_ecc_curve_t) 0x0015) |
| 488 | #define PSA_ECC_CURVE_SECP256K1 ((psa_ecc_curve_t) 0x0016) |
| 489 | #define PSA_ECC_CURVE_SECP256R1 ((psa_ecc_curve_t) 0x0017) |
| 490 | #define PSA_ECC_CURVE_SECP384R1 ((psa_ecc_curve_t) 0x0018) |
| 491 | #define PSA_ECC_CURVE_SECP521R1 ((psa_ecc_curve_t) 0x0019) |
| 492 | #define PSA_ECC_CURVE_BRAINPOOL_P256R1 ((psa_ecc_curve_t) 0x001a) |
| 493 | #define PSA_ECC_CURVE_BRAINPOOL_P384R1 ((psa_ecc_curve_t) 0x001b) |
| 494 | #define PSA_ECC_CURVE_BRAINPOOL_P512R1 ((psa_ecc_curve_t) 0x001c) |
| 495 | #define PSA_ECC_CURVE_CURVE25519 ((psa_ecc_curve_t) 0x001d) |
| 496 | #define PSA_ECC_CURVE_CURVE448 ((psa_ecc_curve_t) 0x001e) |
| 497 | #define PSA_ECC_CURVE_FFDHE_2048 ((psa_ecc_curve_t) 0x0100) |
| 498 | #define PSA_ECC_CURVE_FFDHE_3072 ((psa_ecc_curve_t) 0x0101) |
| 499 | #define PSA_ECC_CURVE_FFDHE_4096 ((psa_ecc_curve_t) 0x0102) |
| 500 | #define PSA_ECC_CURVE_FFDHE_6144 ((psa_ecc_curve_t) 0x0103) |
| 501 | #define PSA_ECC_CURVE_FFDHE_8192 ((psa_ecc_curve_t) 0x0104) |
| 502 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 503 | /** The block size of a block cipher. |
| 504 | * |
| 505 | * \param type A cipher key type (value of type #psa_key_type_t). |
| 506 | * |
| 507 | * \return The block size for a block cipher, or 1 for a stream cipher. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 508 | * The return value is undefined if \p type is not a supported |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 509 | * cipher key type. |
| 510 | * |
| 511 | * \note It is possible to build stream cipher algorithms on top of a block |
| 512 | * cipher, for example CTR mode (#PSA_ALG_CTR). |
| 513 | * This macro only takes the key type into account, so it cannot be |
| 514 | * used to determine the size of the data that #psa_cipher_update() |
| 515 | * might buffer for future processing in general. |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 516 | * |
| 517 | * \note This macro returns a compile-time constant if its argument is one. |
| 518 | * |
| 519 | * \warning This macro may evaluate its argument multiple times. |
| 520 | */ |
Gilles Peskine | 03182e9 | 2018-03-07 16:40:52 +0100 | [diff] [blame] | 521 | #define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 522 | ( \ |
| 523 | (type) == PSA_KEY_TYPE_AES ? 16 : \ |
| 524 | (type) == PSA_KEY_TYPE_DES ? 8 : \ |
| 525 | (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \ |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 526 | (type) == PSA_KEY_TYPE_ARC4 ? 1 : \ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 527 | 0) |
| 528 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 529 | /** \brief Encoding of a cryptographic algorithm. |
| 530 | * |
| 531 | * For algorithms that can be applied to multiple key types, this type |
| 532 | * does not encode the key type. For example, for symmetric ciphers |
| 533 | * based on a block cipher, #psa_algorithm_t encodes the block cipher |
| 534 | * mode and the padding mode while the block cipher itself is encoded |
| 535 | * via #psa_key_type_t. |
| 536 | */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 537 | typedef uint32_t psa_algorithm_t; |
| 538 | |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 539 | #define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000) |
| 540 | #define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000) |
| 541 | #define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000) |
| 542 | #define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000) |
| 543 | #define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000) |
| 544 | #define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000) |
| 545 | #define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000) |
| 546 | #define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000) |
| 547 | #define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000) |
| 548 | #define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 549 | |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 550 | #define PSA_ALG_IS_VENDOR_DEFINED(alg) \ |
| 551 | (((alg) & PSA_ALG_VENDOR_FLAG) != 0) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 552 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 553 | /** Whether the specified algorithm is a hash algorithm. |
| 554 | * |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 555 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 556 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 557 | * \return 1 if \p alg is a hash algorithm, 0 otherwise. |
| 558 | * This macro may return either 0 or 1 if \p alg is not a supported |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 559 | * algorithm identifier. |
| 560 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 561 | #define PSA_ALG_IS_HASH(alg) \ |
| 562 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 563 | |
| 564 | /** Whether the specified algorithm is a MAC algorithm. |
| 565 | * |
| 566 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 567 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 568 | * \return 1 if \p alg is a MAC algorithm, 0 otherwise. |
| 569 | * This macro may return either 0 or 1 if \p alg is not a supported |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 570 | * algorithm identifier. |
| 571 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 572 | #define PSA_ALG_IS_MAC(alg) \ |
| 573 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 574 | |
| 575 | /** Whether the specified algorithm is a symmetric cipher algorithm. |
| 576 | * |
| 577 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 578 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 579 | * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise. |
| 580 | * This macro may return either 0 or 1 if \p alg is not a supported |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 581 | * algorithm identifier. |
| 582 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 583 | #define PSA_ALG_IS_CIPHER(alg) \ |
| 584 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 585 | |
| 586 | /** Whether the specified algorithm is an authenticated encryption |
| 587 | * with associated data (AEAD) algorithm. |
| 588 | * |
| 589 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 590 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 591 | * \return 1 if \p alg is an AEAD algorithm, 0 otherwise. |
| 592 | * This macro may return either 0 or 1 if \p alg is not a supported |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 593 | * algorithm identifier. |
| 594 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 595 | #define PSA_ALG_IS_AEAD(alg) \ |
| 596 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 597 | |
| 598 | /** Whether the specified algorithm is a public-key signature algorithm. |
| 599 | * |
| 600 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 601 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 602 | * \return 1 if \p alg is a public-key signature algorithm, 0 otherwise. |
| 603 | * This macro may return either 0 or 1 if \p alg is not a supported |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 604 | * algorithm identifier. |
| 605 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 606 | #define PSA_ALG_IS_SIGN(alg) \ |
| 607 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 608 | |
| 609 | /** Whether the specified algorithm is a public-key encryption algorithm. |
| 610 | * |
| 611 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 612 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 613 | * \return 1 if \p alg is a public-key encryption algorithm, 0 otherwise. |
| 614 | * This macro may return either 0 or 1 if \p alg is not a supported |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 615 | * algorithm identifier. |
| 616 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 617 | #define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \ |
| 618 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 619 | |
| 620 | /** Whether the specified algorithm is a key agreement algorithm. |
| 621 | * |
| 622 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 623 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 624 | * \return 1 if \p alg is a key agreement algorithm, 0 otherwise. |
| 625 | * This macro may return either 0 or 1 if \p alg is not a supported |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 626 | * algorithm identifier. |
| 627 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 628 | #define PSA_ALG_IS_KEY_AGREEMENT(alg) \ |
| 629 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 630 | |
| 631 | /** Whether the specified algorithm is a key derivation algorithm. |
| 632 | * |
| 633 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 634 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 635 | * \return 1 if \p alg is a key derivation algorithm, 0 otherwise. |
| 636 | * This macro may return either 0 or 1 if \p alg is not a supported |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 637 | * algorithm identifier. |
| 638 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 639 | #define PSA_ALG_IS_KEY_DERIVATION(alg) \ |
| 640 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION) |
| 641 | |
| 642 | #define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff) |
| 643 | #define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001) |
| 644 | #define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002) |
| 645 | #define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003) |
Gilles Peskine | e3f694f | 2018-03-08 07:48:40 +0100 | [diff] [blame] | 646 | #define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004) |
| 647 | #define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 648 | #define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008) |
| 649 | #define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009) |
| 650 | #define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a) |
| 651 | #define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b) |
| 652 | #define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c) |
| 653 | #define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d) |
| 654 | #define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010) |
| 655 | #define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011) |
| 656 | #define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012) |
| 657 | #define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013) |
| 658 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 659 | #define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 660 | #define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000) |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 661 | /** Macro to build an HMAC algorithm. |
| 662 | * |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 663 | * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256. |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 664 | * |
Gilles Peskine | ea4469f | 2018-06-28 13:57:23 +0200 | [diff] [blame] | 665 | * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 666 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 667 | * |
Gilles Peskine | ea4469f | 2018-06-28 13:57:23 +0200 | [diff] [blame] | 668 | * \return The corresponding HMAC algorithm. |
| 669 | * \return Unspecified if \p alg is not a supported |
| 670 | * hash algorithm. |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 671 | */ |
| 672 | #define PSA_ALG_HMAC(hash_alg) \ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 673 | (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 674 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 675 | #define PSA_ALG_HMAC_HASH(hmac_alg) \ |
| 676 | (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK)) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 677 | |
| 678 | /** Whether the specified algorithm is an HMAC algorithm. |
| 679 | * |
| 680 | * HMAC is a family of MAC algorithms that are based on a hash function. |
| 681 | * |
| 682 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 683 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 684 | * \return 1 if \p alg is an HMAC algorithm, 0 otherwise. |
| 685 | * This macro may return either 0 or 1 if \p alg is not a supported |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 686 | * algorithm identifier. |
| 687 | */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 688 | #define PSA_ALG_IS_HMAC(alg) \ |
| 689 | (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \ |
| 690 | PSA_ALG_HMAC_BASE) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 691 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 692 | #define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000) |
| 693 | #define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001) |
| 694 | #define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002) |
| 695 | #define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 696 | |
| 697 | /** Whether the specified algorithm is a MAC algorithm based on a block cipher. |
| 698 | * |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 699 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 700 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 701 | * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise. |
| 702 | * This macro may return either 0 or 1 if \p alg is not a supported |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 703 | * algorithm identifier. |
| 704 | */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 705 | #define PSA_ALG_IS_CIPHER_MAC(alg) \ |
| 706 | (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \ |
| 707 | PSA_ALG_CIPHER_MAC_BASE) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 708 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 709 | #define PSA_ALG_CIPHER_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000) |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 710 | #define PSA_ALG_BLOCK_CIPHER_BASE ((psa_algorithm_t)0x04000000) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 711 | #define PSA_ALG_BLOCK_CIPHER_MODE_MASK ((psa_algorithm_t)0x000000ff) |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 712 | #define PSA_ALG_BLOCK_CIPHER_PADDING_MASK ((psa_algorithm_t)0x003f0000) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 713 | |
| 714 | /** Use a block cipher mode without padding. |
| 715 | * |
| 716 | * This padding mode may only be used with messages whose lengths are a |
| 717 | * whole number of blocks for the chosen block cipher. |
| 718 | */ |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 719 | #define PSA_ALG_BLOCK_CIPHER_PAD_NONE ((psa_algorithm_t)0x00000000) |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 720 | |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 721 | #define PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ((psa_algorithm_t)0x00010000) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 722 | |
| 723 | /** Whether the specified algorithm is a block cipher. |
| 724 | * |
| 725 | * A block cipher is a symmetric cipher that encrypts or decrypts messages |
| 726 | * by chopping them into fixed-size blocks. Processing a message requires |
| 727 | * applying a _padding mode_ to transform the message into one whose |
| 728 | * length is a whole number of blocks. To construct an algorithm |
| 729 | * identifier for a block cipher, apply a bitwise-or between the block |
| 730 | * cipher mode and the padding mode. For example, CBC with PKCS#7 padding |
| 731 | * is `PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7`. |
| 732 | * |
| 733 | * The transformation applied to each block is determined by the key type. |
| 734 | * For example, to use AES-128-CBC-PKCS7, use the algorithm above with |
| 735 | * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes). |
| 736 | * |
| 737 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 738 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 739 | * \return 1 if \p alg is a block cipher algorithm, 0 otherwise. |
| 740 | * This macro may return either 0 or 1 if \p alg is not a supported |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 741 | * algorithm identifier or if it is not a symmetric cipher algorithm. |
| 742 | */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 743 | #define PSA_ALG_IS_BLOCK_CIPHER(alg) \ |
| 744 | (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \ |
| 745 | PSA_ALG_BLOCK_CIPHER_BASE) |
| 746 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 747 | /** The CBC block cipher mode. |
| 748 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 749 | #define PSA_ALG_CBC_BASE ((psa_algorithm_t)0x04000001) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 750 | #define PSA_ALG_CFB_BASE ((psa_algorithm_t)0x04000002) |
| 751 | #define PSA_ALG_OFB_BASE ((psa_algorithm_t)0x04000003) |
| 752 | #define PSA_ALG_XTS_BASE ((psa_algorithm_t)0x04000004) |
Gilles Peskine | 5d1888e | 2018-07-12 00:32:42 +0200 | [diff] [blame] | 753 | |
| 754 | #define PSA_ALG_STREAM_CIPHER_BASE ((psa_algorithm_t)0x04800000) |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 755 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 756 | /** The CTR stream cipher mode. |
| 757 | * |
| 758 | * CTR is a stream cipher which is built from a block cipher. The |
| 759 | * underlying block cipher is determined by the key type. For example, |
| 760 | * to use AES-128-CTR, use this algorithm with |
| 761 | * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes). |
| 762 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 763 | #define PSA_ALG_CTR ((psa_algorithm_t)0x04800001) |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 764 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 765 | /** The ARC4 stream cipher algorithm. |
| 766 | */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 767 | #define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800002) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 768 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 769 | /** Whether the specified algorithm is a stream cipher. |
| 770 | * |
| 771 | * A stream cipher is a symmetric cipher that encrypts or decrypts messages |
| 772 | * by applying a bitwise-xor with a stream of bytes that is generated |
| 773 | * from a key. |
| 774 | * |
| 775 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 776 | * |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 777 | * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise. |
| 778 | * This macro may return either 0 or 1 if \p alg is not a supported |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 779 | * algorithm identifier or if it is not a symmetric cipher algorithm. |
| 780 | */ |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 781 | #define PSA_ALG_IS_STREAM_CIPHER(alg) \ |
| 782 | (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \ |
Gilles Peskine | 5d1888e | 2018-07-12 00:32:42 +0200 | [diff] [blame] | 783 | PSA_ALG_STREAM_CIPHER_BASE) |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 784 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 785 | #define PSA_ALG_CCM ((psa_algorithm_t)0x06000001) |
| 786 | #define PSA_ALG_GCM ((psa_algorithm_t)0x06000002) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 787 | |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 788 | #define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x10020000) |
| 789 | /** RSA PKCS#1 v1.5 signature with hashing. |
| 790 | * |
| 791 | * This is the signature scheme defined by RFC 8017 |
| 792 | * (PKCS#1: RSA Cryptography Specifications) under the name |
| 793 | * RSASSA-PKCS1-v1_5. |
| 794 | * |
| 795 | * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 796 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 797 | * |
| 798 | * \return The corresponding RSA PKCS#1 v1.5 signature algorithm. |
| 799 | * \return Unspecified if \p alg is not a supported |
| 800 | * hash algorithm. |
| 801 | */ |
Gilles Peskine | a592623 | 2018-03-28 14:16:50 +0200 | [diff] [blame] | 802 | #define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \ |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 803 | (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 804 | /** Raw PKCS#1 v1.5 signature. |
| 805 | * |
| 806 | * The input to this algorithm is the DigestInfo structure used by |
| 807 | * RFC 8017 (PKCS#1: RSA Cryptography Specifications), §9.2 |
| 808 | * steps 3–6. |
| 809 | */ |
| 810 | #define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE |
Gilles Peskine | a592623 | 2018-03-28 14:16:50 +0200 | [diff] [blame] | 811 | #define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \ |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 812 | (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 813 | |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 814 | #define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x10030000) |
| 815 | /** RSA PSS signature with hashing. |
| 816 | * |
| 817 | * This is the signature scheme defined by RFC 8017 |
| 818 | * (PKCS#1: RSA Cryptography Specifications) under the name |
| 819 | * RSASSA-PSS, with the message generation function MGF1. The specified |
| 820 | * hash algorithm is used to hash the input message, to create the |
| 821 | * salted hash, and for the mask generation. |
| 822 | * |
| 823 | * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 824 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 825 | * |
| 826 | * \return The corresponding RSA PSS signature algorithm. |
| 827 | * \return Unspecified if \p alg is not a supported |
| 828 | * hash algorithm. |
| 829 | */ |
| 830 | #define PSA_ALG_RSA_PSS(hash_alg) \ |
| 831 | (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 832 | #define PSA_ALG_IS_RSA_PSS(alg) \ |
| 833 | (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE) |
| 834 | |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 835 | #define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000) |
| 836 | /** DSA signature with hashing. |
| 837 | * |
| 838 | * This is the signature scheme defined by FIPS 186-4, |
| 839 | * with a random per-message secret number (*k*). |
| 840 | * |
| 841 | * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 842 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 843 | * |
| 844 | * \return The corresponding DSA signature algorithm. |
| 845 | * \return Unspecified if \p alg is not a supported |
| 846 | * hash algorithm. |
| 847 | */ |
| 848 | #define PSA_ALG_DSA(hash_alg) \ |
| 849 | (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 850 | #define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000) |
| 851 | #define PSA_ALG_DSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000) |
| 852 | #define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \ |
| 853 | (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 854 | #define PSA_ALG_IS_DSA(alg) \ |
| 855 | (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \ |
| 856 | PSA_ALG_DSA_BASE) |
| 857 | #define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \ |
| 858 | (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0) |
| 859 | |
| 860 | #define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x10060000) |
| 861 | /** ECDSA signature with hashing. |
| 862 | * |
| 863 | * This is the ECDSA signature scheme defined by ANSI X9.62, |
| 864 | * with a random per-message secret number (*k*). |
| 865 | * |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 866 | * The representation of the signature as a byte string consists of |
| 867 | * the concatentation of the signature values *r* and *s*. Each of |
| 868 | * *r* and *s* is encoded as an *N*-octet string, where *N* is the length |
| 869 | * of the base point of the curve in octets. Each value is represented |
| 870 | * in big-endian order (most significant octet first). |
| 871 | * |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 872 | * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 873 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 874 | * |
| 875 | * \return The corresponding ECDSA signature algorithm. |
| 876 | * \return Unspecified if \p alg is not a supported |
| 877 | * hash algorithm. |
| 878 | */ |
| 879 | #define PSA_ALG_ECDSA(hash_alg) \ |
| 880 | (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 881 | /** ECDSA signature without hashing. |
| 882 | * |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 883 | * This is the same signature scheme as #PSA_ALG_ECDSA(), but |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 884 | * without specifying a hash algorithm. This algorithm may only be |
| 885 | * used to sign or verify a sequence of bytes that should be an |
| 886 | * already-calculated hash. Note that the input is padded with |
| 887 | * zeros on the left or truncated on the left as required to fit |
| 888 | * the curve size. |
| 889 | */ |
| 890 | #define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE |
| 891 | #define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x10070000) |
| 892 | /** Deterministic ECDSA signature with hashing. |
| 893 | * |
| 894 | * This is the deterministic ECDSA signature scheme defined by RFC 6979. |
| 895 | * |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 896 | * The representation of a signature is the same as with #PSA_ALG_ECDSA(). |
| 897 | * |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 898 | * Note that when this algorithm is used for verification, signatures |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 899 | * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 900 | * same private key are accepted. In other words, |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 901 | * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from |
| 902 | * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification. |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 903 | * |
| 904 | * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 905 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 906 | * |
| 907 | * \return The corresponding deterministic ECDSA signature |
| 908 | * algorithm. |
| 909 | * \return Unspecified if \p alg is not a supported |
| 910 | * hash algorithm. |
| 911 | */ |
| 912 | #define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \ |
| 913 | (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 914 | #define PSA_ALG_IS_ECDSA(alg) \ |
| 915 | (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \ |
| 916 | PSA_ALG_ECDSA_BASE) |
| 917 | #define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \ |
| 918 | (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0) |
| 919 | |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 920 | /** Get the hash used by a hash-and-sign signature algorithm. |
| 921 | * |
| 922 | * A hash-and-sign algorithm is a signature algorithm which is |
| 923 | * composed of two phases: first a hashing phase which does not use |
| 924 | * the key and produces a hash of the input message, then a signing |
| 925 | * phase which only uses the hash and the key and not the message |
| 926 | * itself. |
| 927 | * |
| 928 | * \param alg A signature algorithm (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 929 | * #PSA_ALG_IS_SIGN(\p alg) is true). |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 930 | * |
| 931 | * \return The underlying hash algorithm if \p alg is a hash-and-sign |
| 932 | * algorithm. |
| 933 | * \return 0 if \p alg is a signature algorithm that does not |
| 934 | * follow the hash-and-sign structure. |
| 935 | * \return Unspecified if \p alg is not a signature algorithm or |
| 936 | * if it is not supported by the implementation. |
| 937 | */ |
| 938 | #define PSA_ALG_SIGN_GET_HASH(alg) \ |
Gilles Peskine | a81d85b | 2018-06-26 16:10:23 +0200 | [diff] [blame] | 939 | (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \ |
| 940 | PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg) ? \ |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 941 | ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \ |
| 942 | 0) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 943 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 944 | /** RSA PKCS#1 v1.5 encryption. |
| 945 | */ |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 946 | #define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12020000) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 947 | |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 948 | #define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x12030000) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 949 | /** RSA OAEP encryption. |
| 950 | * |
| 951 | * This is the encryption scheme defined by RFC 8017 |
| 952 | * (PKCS#1: RSA Cryptography Specifications) under the name |
| 953 | * RSAES-OAEP, with the message generation function MGF1. |
| 954 | * |
| 955 | * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that |
| 956 | * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use |
| 957 | * for MGF1. |
| 958 | * |
| 959 | * \return The corresponding RSA OAEP signature algorithm. |
| 960 | * \return Unspecified if \p alg is not a supported |
| 961 | * hash algorithm. |
| 962 | */ |
Gilles Peskine | 55bf3d1 | 2018-06-26 15:53:48 +0200 | [diff] [blame] | 963 | #define PSA_ALG_RSA_OAEP(hash_alg) \ |
| 964 | (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 965 | #define PSA_ALG_IS_RSA_OAEP(alg) \ |
| 966 | (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE) |
Gilles Peskine | d1e8e41 | 2018-06-07 09:49:39 +0200 | [diff] [blame] | 967 | |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame^] | 968 | #define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x30000100) |
| 969 | /** Macro to build an HKDF algorithm. |
| 970 | * |
| 971 | * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256. |
| 972 | * |
| 973 | * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that |
| 974 | * #PSA_ALG_IS_HASH(\p hash_alg) is true). |
| 975 | * |
| 976 | * \return The corresponding HKDF algorithm. |
| 977 | * \return Unspecified if \p alg is not a supported |
| 978 | * hash algorithm. |
| 979 | */ |
| 980 | #define PSA_ALG_HKDF(hash_alg) \ |
| 981 | (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 982 | /** Whether the specified algorithm is an HKDF algorithm. |
| 983 | * |
| 984 | * HKDF is a family of key derivation algorithms that are based on a hash |
| 985 | * function and the HMAC construction. |
| 986 | * |
| 987 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
| 988 | * |
| 989 | * \return 1 if \c alg is an HKDF algorithm, 0 otherwise. |
| 990 | * This macro may return either 0 or 1 if \c alg is not a supported |
| 991 | * key derivation algorithm identifier. |
| 992 | */ |
| 993 | #define PSA_ALG_IS_HKDF(alg) \ |
| 994 | (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE) |
| 995 | #define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \ |
| 996 | (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK)) |
| 997 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 998 | /**@}*/ |
| 999 | |
| 1000 | /** \defgroup key_management Key management |
| 1001 | * @{ |
| 1002 | */ |
| 1003 | |
| 1004 | /** |
| 1005 | * \brief Import a key in binary format. |
| 1006 | * |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 1007 | * This function supports any output from psa_export_key(). Refer to the |
| 1008 | * documentation of psa_export_key() for the format for each key type. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1009 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1010 | * \param key Slot where the key will be stored. This must be a |
| 1011 | * valid slot for a key of the chosen type. It must |
| 1012 | * be unoccupied. |
| 1013 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1014 | * \param[in] data Buffer containing the key data. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1015 | * \param data_length Size of the \p data buffer in bytes. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1016 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1017 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1018 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1019 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 1020 | * The key type or key size is not supported, either by the |
| 1021 | * implementation in general or in this particular slot. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1022 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1023 | * The key slot is invalid, |
| 1024 | * or the key data is not correctly formatted. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1025 | * \retval #PSA_ERROR_OCCUPIED_SLOT |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 1026 | * There is already a key in the specified slot. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1027 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1028 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE |
| 1029 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1030 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1031 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1032 | */ |
| 1033 | psa_status_t psa_import_key(psa_key_slot_t key, |
| 1034 | psa_key_type_t type, |
| 1035 | const uint8_t *data, |
| 1036 | size_t data_length); |
| 1037 | |
| 1038 | /** |
Gilles Peskine | 154bd95 | 2018-04-19 08:38:16 +0200 | [diff] [blame] | 1039 | * \brief Destroy a key and restore the slot to its default state. |
| 1040 | * |
| 1041 | * This function destroys the content of the key slot from both volatile |
| 1042 | * memory and, if applicable, non-volatile storage. Implementations shall |
| 1043 | * make a best effort to ensure that any previous content of the slot is |
| 1044 | * unrecoverable. |
| 1045 | * |
| 1046 | * This function also erases any metadata such as policies. It returns the |
| 1047 | * specified slot to its default state. |
| 1048 | * |
| 1049 | * \param key The key slot to erase. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1050 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1051 | * \retval #PSA_SUCCESS |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 1052 | * The slot's content, if any, has been erased. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1053 | * \retval #PSA_ERROR_NOT_PERMITTED |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 1054 | * The slot holds content and cannot be erased because it is |
| 1055 | * read-only, either due to a policy or due to physical restrictions. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1056 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 1057 | * The specified slot number does not designate a valid slot. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1058 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 1059 | * There was an failure in communication with the cryptoprocessor. |
| 1060 | * The key material may still be present in the cryptoprocessor. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1061 | * \retval #PSA_ERROR_STORAGE_FAILURE |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 1062 | * The storage is corrupted. Implementations shall make a best effort |
| 1063 | * to erase key material even in this stage, however applications |
| 1064 | * should be aware that it may be impossible to guarantee that the |
| 1065 | * key material is not recoverable in such cases. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1066 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 1067 | * An unexpected condition which is not a storage corruption or |
| 1068 | * a communication failure occurred. The cryptoprocessor may have |
| 1069 | * been compromised. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1070 | */ |
| 1071 | psa_status_t psa_destroy_key(psa_key_slot_t key); |
| 1072 | |
| 1073 | /** |
| 1074 | * \brief Get basic metadata about a key. |
| 1075 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1076 | * \param key Slot whose content is queried. This must |
| 1077 | * be an occupied key slot. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1078 | * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value). |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1079 | * This may be a null pointer, in which case the key type |
| 1080 | * is not written. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1081 | * \param[out] bits On success, the key size in bits. |
Gilles Peskine | 9a1ba0d | 2018-03-21 20:49:16 +0100 | [diff] [blame] | 1082 | * This may be a null pointer, in which case the key size |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1083 | * is not written. |
| 1084 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1085 | * \retval #PSA_SUCCESS |
| 1086 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1087 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1088 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1089 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1090 | */ |
| 1091 | psa_status_t psa_get_key_information(psa_key_slot_t key, |
| 1092 | psa_key_type_t *type, |
| 1093 | size_t *bits); |
| 1094 | |
| 1095 | /** |
| 1096 | * \brief Export a key in binary format. |
| 1097 | * |
| 1098 | * The output of this function can be passed to psa_import_key() to |
| 1099 | * create an equivalent object. |
| 1100 | * |
| 1101 | * If a key is created with psa_import_key() and then exported with |
| 1102 | * this function, it is not guaranteed that the resulting data is |
| 1103 | * identical: the implementation may choose a different representation |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 1104 | * of the same key if the format permits it. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1105 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1106 | * For standard key types, the output format is as follows: |
| 1107 | * |
| 1108 | * - For symmetric keys (including MAC keys), the format is the |
| 1109 | * raw bytes of the key. |
| 1110 | * - For DES, the key data consists of 8 bytes. The parity bits must be |
| 1111 | * correct. |
| 1112 | * - For Triple-DES, the format is the concatenation of the |
| 1113 | * two or three DES keys. |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 1114 | * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format |
Gilles Peskine | 2743e42 | 2018-06-27 22:57:11 +0200 | [diff] [blame] | 1115 | * is the non-encrypted DER representation defined by PKCS\#1 (RFC 8017) |
| 1116 | * as RSAPrivateKey. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1117 | * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format |
Gilles Peskine | 971f706 | 2018-03-20 17:52:58 +0100 | [diff] [blame] | 1118 | * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1119 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1120 | * \param key Slot whose content is to be exported. This must |
| 1121 | * be an occupied key slot. |
| 1122 | * \param[out] data Buffer where the key data is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1123 | * \param data_size Size of the \p data buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1124 | * \param[out] data_length On success, the number of bytes |
| 1125 | * that make up the key data. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1126 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1127 | * \retval #PSA_SUCCESS |
| 1128 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1129 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1130 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1131 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1132 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1133 | */ |
| 1134 | psa_status_t psa_export_key(psa_key_slot_t key, |
| 1135 | uint8_t *data, |
| 1136 | size_t data_size, |
| 1137 | size_t *data_length); |
| 1138 | |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1139 | /** |
| 1140 | * \brief Export a public key or the public part of a key pair in binary format. |
| 1141 | * |
| 1142 | * The output of this function can be passed to psa_import_key() to |
| 1143 | * create an object that is equivalent to the public key. |
| 1144 | * |
| 1145 | * For standard key types, the output format is as follows: |
| 1146 | * |
| 1147 | * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY), |
Moran Peker | dd4ea38 | 2018-04-03 15:30:03 +0300 | [diff] [blame] | 1148 | * the format is the DER representation of the public key defined by RFC 5280 |
Gilles Peskine | 971f706 | 2018-03-20 17:52:58 +0100 | [diff] [blame] | 1149 | * as SubjectPublicKeyInfo. |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1150 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1151 | * \param key Slot whose content is to be exported. This must |
| 1152 | * be an occupied key slot. |
| 1153 | * \param[out] data Buffer where the key data is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1154 | * \param data_size Size of the \p data buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1155 | * \param[out] data_length On success, the number of bytes |
| 1156 | * that make up the key data. |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1157 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1158 | * \retval #PSA_SUCCESS |
| 1159 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1160 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1161 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1162 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1163 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1164 | */ |
| 1165 | psa_status_t psa_export_public_key(psa_key_slot_t key, |
| 1166 | uint8_t *data, |
| 1167 | size_t data_size, |
| 1168 | size_t *data_length); |
| 1169 | |
| 1170 | /**@}*/ |
| 1171 | |
| 1172 | /** \defgroup policy Key policies |
| 1173 | * @{ |
| 1174 | */ |
| 1175 | |
| 1176 | /** \brief Encoding of permitted usage on a key. */ |
| 1177 | typedef uint32_t psa_key_usage_t; |
| 1178 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1179 | /** Whether the key may be exported. |
| 1180 | * |
| 1181 | * A public key or the public part of a key pair may always be exported |
| 1182 | * regardless of the value of this permission flag. |
| 1183 | * |
| 1184 | * If a key does not have export permission, implementations shall not |
| 1185 | * allow the key to be exported in plain form from the cryptoprocessor, |
| 1186 | * whether through psa_export_key() or through a proprietary interface. |
| 1187 | * The key may however be exportable in a wrapped form, i.e. in a form |
| 1188 | * where it is encrypted by another key. |
| 1189 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1190 | #define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001) |
| 1191 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1192 | /** Whether the key may be used to encrypt a message. |
| 1193 | * |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1194 | * This flag allows the key to be used for a symmetric encryption operation, |
| 1195 | * for an AEAD encryption-and-authentication operation, |
| 1196 | * or for an asymmetric encryption operation, |
| 1197 | * if otherwise permitted by the key's type and policy. |
| 1198 | * |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1199 | * For a key pair, this concerns the public key. |
| 1200 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1201 | #define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100) |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1202 | |
| 1203 | /** Whether the key may be used to decrypt a message. |
| 1204 | * |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1205 | * This flag allows the key to be used for a symmetric decryption operation, |
| 1206 | * for an AEAD decryption-and-verification operation, |
| 1207 | * or for an asymmetric decryption operation, |
| 1208 | * if otherwise permitted by the key's type and policy. |
| 1209 | * |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1210 | * For a key pair, this concerns the private key. |
| 1211 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1212 | #define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200) |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1213 | |
| 1214 | /** Whether the key may be used to sign a message. |
| 1215 | * |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1216 | * This flag allows the key to be used for a MAC calculation operation |
| 1217 | * or for an asymmetric signature operation, |
| 1218 | * if otherwise permitted by the key's type and policy. |
| 1219 | * |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1220 | * For a key pair, this concerns the private key. |
| 1221 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1222 | #define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400) |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1223 | |
| 1224 | /** Whether the key may be used to verify a message signature. |
| 1225 | * |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1226 | * This flag allows the key to be used for a MAC verification operation |
| 1227 | * or for an asymmetric signature verification operation, |
| 1228 | * if otherwise permitted by by the key's type and policy. |
| 1229 | * |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1230 | * For a key pair, this concerns the public key. |
| 1231 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1232 | #define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800) |
| 1233 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1234 | /** Whether the key may be used to derive other keys. |
| 1235 | */ |
| 1236 | #define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00001000) |
| 1237 | |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1238 | /** The type of the key policy data structure. |
| 1239 | * |
| 1240 | * This is an implementation-defined \c struct. Applications should not |
| 1241 | * make any assumptions about the content of this structure except |
| 1242 | * as directed by the documentation of a specific implementation. */ |
| 1243 | typedef struct psa_key_policy_s psa_key_policy_t; |
| 1244 | |
| 1245 | /** \brief Initialize a key policy structure to a default that forbids all |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 1246 | * usage of the key. |
| 1247 | * |
| 1248 | * \param[out] policy The policy object to initialize. |
| 1249 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1250 | void psa_key_policy_init(psa_key_policy_t *policy); |
| 1251 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1252 | /** \brief Set the standard fields of a policy structure. |
| 1253 | * |
| 1254 | * Note that this function does not make any consistency check of the |
| 1255 | * parameters. The values are only checked when applying the policy to |
| 1256 | * a key slot with psa_set_key_policy(). |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 1257 | * |
| 1258 | * \param[out] policy The policy object to modify. |
| 1259 | * \param usage The permitted uses for the key. |
| 1260 | * \param alg The algorithm that the key may be used for. |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1261 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1262 | void psa_key_policy_set_usage(psa_key_policy_t *policy, |
| 1263 | psa_key_usage_t usage, |
| 1264 | psa_algorithm_t alg); |
| 1265 | |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 1266 | /** \brief Retrieve the usage field of a policy structure. |
| 1267 | * |
| 1268 | * \param[in] policy The policy object to query. |
| 1269 | * |
| 1270 | * \return The permitted uses for a key with this policy. |
| 1271 | */ |
Gilles Peskine | aa7bc47 | 2018-07-12 00:54:56 +0200 | [diff] [blame] | 1272 | psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy); |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1273 | |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 1274 | /** \brief Retrieve the algorithm field of a policy structure. |
| 1275 | * |
| 1276 | * \param[in] policy The policy object to query. |
| 1277 | * |
| 1278 | * \return The permitted algorithm for a key with this policy. |
| 1279 | */ |
Gilles Peskine | aa7bc47 | 2018-07-12 00:54:56 +0200 | [diff] [blame] | 1280 | psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy); |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1281 | |
| 1282 | /** \brief Set the usage policy on a key slot. |
| 1283 | * |
| 1284 | * This function must be called on an empty key slot, before importing, |
| 1285 | * generating or creating a key in the slot. Changing the policy of an |
| 1286 | * existing key is not permitted. |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1287 | * |
| 1288 | * Implementations may set restrictions on supported key policies |
| 1289 | * depending on the key type and the key slot. |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 1290 | * |
| 1291 | * \param key The key slot whose policy is to be changed. |
| 1292 | * \param[in] policy The policy object to query. |
| 1293 | * |
| 1294 | * \retval #PSA_SUCCESS |
| 1295 | * \retval #PSA_ERROR_OCCUPIED_SLOT |
| 1296 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 1297 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1298 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1299 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1300 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1301 | */ |
| 1302 | psa_status_t psa_set_key_policy(psa_key_slot_t key, |
| 1303 | const psa_key_policy_t *policy); |
| 1304 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1305 | /** \brief Get the usage policy for a key slot. |
Gilles Peskine | 6ac73a9 | 2018-07-12 19:47:19 +0200 | [diff] [blame] | 1306 | * |
| 1307 | * \param key The key slot whose policy is being queried. |
| 1308 | * \param[out] policy On success, the key's policy. |
| 1309 | * |
| 1310 | * \retval #PSA_SUCCESS |
| 1311 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1312 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1313 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 1314 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 1315 | psa_status_t psa_get_key_policy(psa_key_slot_t key, |
| 1316 | psa_key_policy_t *policy); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1317 | |
| 1318 | /**@}*/ |
| 1319 | |
Gilles Peskine | 609b6a5 | 2018-03-03 21:31:50 +0100 | [diff] [blame] | 1320 | /** \defgroup persistence Key lifetime |
| 1321 | * @{ |
| 1322 | */ |
| 1323 | |
| 1324 | /** Encoding of key lifetimes. |
| 1325 | */ |
| 1326 | typedef uint32_t psa_key_lifetime_t; |
| 1327 | |
| 1328 | /** A volatile key slot retains its content as long as the application is |
| 1329 | * running. It is guaranteed to be erased on a power reset. |
| 1330 | */ |
| 1331 | #define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000) |
| 1332 | |
| 1333 | /** A persistent key slot retains its content as long as it is not explicitly |
| 1334 | * destroyed. |
| 1335 | */ |
| 1336 | #define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001) |
| 1337 | |
| 1338 | /** A write-once key slot may not be modified once a key has been set. |
| 1339 | * It will retain its content as long as the device remains operational. |
| 1340 | */ |
| 1341 | #define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff) |
| 1342 | |
Gilles Peskine | d393e18 | 2018-03-08 07:49:16 +0100 | [diff] [blame] | 1343 | /** \brief Retrieve the lifetime of a key slot. |
| 1344 | * |
| 1345 | * The assignment of lifetimes to slots is implementation-dependent. |
Gilles Peskine | 8ca5602 | 2018-04-17 14:07:59 +0200 | [diff] [blame] | 1346 | * |
Gilles Peskine | 9bb53d7 | 2018-04-17 14:09:24 +0200 | [diff] [blame] | 1347 | * \param key Slot to query. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1348 | * \param[out] lifetime On success, the lifetime value. |
Gilles Peskine | 8ca5602 | 2018-04-17 14:07:59 +0200 | [diff] [blame] | 1349 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1350 | * \retval #PSA_SUCCESS |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1351 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1352 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
mohammad1603 | a7d245a | 2018-04-17 00:40:08 -0700 | [diff] [blame] | 1353 | * The key slot is invalid. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1354 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1355 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1356 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | d393e18 | 2018-03-08 07:49:16 +0100 | [diff] [blame] | 1357 | */ |
Gilles Peskine | 609b6a5 | 2018-03-03 21:31:50 +0100 | [diff] [blame] | 1358 | psa_status_t psa_get_key_lifetime(psa_key_slot_t key, |
| 1359 | psa_key_lifetime_t *lifetime); |
| 1360 | |
Gilles Peskine | d393e18 | 2018-03-08 07:49:16 +0100 | [diff] [blame] | 1361 | /** \brief Change the lifetime of a key slot. |
| 1362 | * |
| 1363 | * Whether the lifetime of a key slot can be changed at all, and if so |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 1364 | * whether the lifetime of an occupied key slot can be changed, is |
Gilles Peskine | d393e18 | 2018-03-08 07:49:16 +0100 | [diff] [blame] | 1365 | * implementation-dependent. |
Gilles Peskine | 8ca5602 | 2018-04-17 14:07:59 +0200 | [diff] [blame] | 1366 | * |
Gilles Peskine | 9bb53d7 | 2018-04-17 14:09:24 +0200 | [diff] [blame] | 1367 | * \param key Slot whose lifetime is to be changed. |
| 1368 | * \param lifetime The lifetime value to set for the given key slot. |
Gilles Peskine | 8ca5602 | 2018-04-17 14:07:59 +0200 | [diff] [blame] | 1369 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1370 | * \retval #PSA_SUCCESS |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1371 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1372 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1373 | * The key slot is invalid, |
mohammad1603 | a7d245a | 2018-04-17 00:40:08 -0700 | [diff] [blame] | 1374 | * or the lifetime value is invalid. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1375 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | f0c9dd3 | 2018-04-17 14:11:07 +0200 | [diff] [blame] | 1376 | * The implementation does not support the specified lifetime value, |
| 1377 | * at least for the specified key slot. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1378 | * \retval #PSA_ERROR_OCCUPIED_SLOT |
Gilles Peskine | f0c9dd3 | 2018-04-17 14:11:07 +0200 | [diff] [blame] | 1379 | * The slot contains a key, and the implementation does not support |
| 1380 | * changing the lifetime of an occupied slot. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1381 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1382 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1383 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | d393e18 | 2018-03-08 07:49:16 +0100 | [diff] [blame] | 1384 | */ |
| 1385 | psa_status_t psa_set_key_lifetime(psa_key_slot_t key, |
mohammad1603 | ea05009 | 2018-04-17 00:31:34 -0700 | [diff] [blame] | 1386 | psa_key_lifetime_t lifetime); |
Gilles Peskine | d393e18 | 2018-03-08 07:49:16 +0100 | [diff] [blame] | 1387 | |
Gilles Peskine | 609b6a5 | 2018-03-03 21:31:50 +0100 | [diff] [blame] | 1388 | /**@}*/ |
| 1389 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1390 | /** \defgroup hash Message digests |
| 1391 | * @{ |
| 1392 | */ |
| 1393 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1394 | /** The type of the state data structure for multipart hash operations. |
| 1395 | * |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 1396 | * This is an implementation-defined \c struct. Applications should not |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1397 | * make any assumptions about the content of this structure except |
| 1398 | * as directed by the documentation of a specific implementation. */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1399 | typedef struct psa_hash_operation_s psa_hash_operation_t; |
| 1400 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1401 | /** The size of the output of psa_hash_finish(), in bytes. |
| 1402 | * |
| 1403 | * This is also the hash size that psa_hash_verify() expects. |
| 1404 | * |
| 1405 | * \param alg A hash algorithm (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 1406 | * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm |
Gilles Peskine | be42f31 | 2018-07-13 14:38:15 +0200 | [diff] [blame] | 1407 | * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 1408 | * hash algorithm). |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1409 | * |
| 1410 | * \return The hash size for the specified hash algorithm. |
| 1411 | * If the hash algorithm is not recognized, return 0. |
| 1412 | * An implementation may return either 0 or the correct size |
| 1413 | * for a hash algorithm that it recognizes, but does not support. |
| 1414 | */ |
Gilles Peskine | 7ed29c5 | 2018-06-26 15:50:08 +0200 | [diff] [blame] | 1415 | #define PSA_HASH_SIZE(alg) \ |
| 1416 | ( \ |
| 1417 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD2 ? 16 : \ |
| 1418 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD4 ? 16 : \ |
| 1419 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD5 ? 16 : \ |
| 1420 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \ |
| 1421 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \ |
| 1422 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \ |
| 1423 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \ |
| 1424 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \ |
| 1425 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \ |
| 1426 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \ |
| 1427 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \ |
| 1428 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \ |
| 1429 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \ |
| 1430 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \ |
| 1431 | PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1432 | 0) |
| 1433 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1434 | /** Start a multipart hash operation. |
| 1435 | * |
| 1436 | * The sequence of operations to calculate a hash (message digest) |
| 1437 | * is as follows: |
| 1438 | * -# Allocate an operation object which will be passed to all the functions |
| 1439 | * listed here. |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1440 | * -# Call psa_hash_setup() to specify the algorithm. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1441 | * -# Call psa_hash_update() zero, one or more times, passing a fragment |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1442 | * of the message each time. The hash that is calculated is the hash |
| 1443 | * of the concatenation of these messages in order. |
| 1444 | * -# To calculate the hash, call psa_hash_finish(). |
| 1445 | * To compare the hash with an expected value, call psa_hash_verify(). |
| 1446 | * |
| 1447 | * The application may call psa_hash_abort() at any time after the operation |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1448 | * has been initialized with psa_hash_setup(). |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1449 | * |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1450 | * After a successful call to psa_hash_setup(), the application must |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame] | 1451 | * eventually terminate the operation. The following events terminate an |
| 1452 | * operation: |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1453 | * - A failed call to psa_hash_update(). |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 1454 | * - 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] | 1455 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1456 | * \param[out] operation The operation object to use. |
| 1457 | * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value |
| 1458 | * such that #PSA_ALG_IS_HASH(\p alg) is true). |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1459 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1460 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1461 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1462 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1463 | * \p alg is not supported or is not a hash algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1464 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1465 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1466 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1467 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1468 | */ |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1469 | psa_status_t psa_hash_setup(psa_hash_operation_t *operation, |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1470 | psa_algorithm_t alg); |
| 1471 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1472 | /** Add a message fragment to a multipart hash operation. |
| 1473 | * |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1474 | * The application must call psa_hash_setup() before calling this function. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1475 | * |
| 1476 | * If this function returns an error status, the operation becomes inactive. |
| 1477 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1478 | * \param[in,out] operation Active hash operation. |
| 1479 | * \param[in] input Buffer containing the message fragment to hash. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1480 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1481 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1482 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1483 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1484 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1485 | * The operation state is not valid (not started, or already completed). |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1486 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1487 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1488 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1489 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1490 | */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1491 | psa_status_t psa_hash_update(psa_hash_operation_t *operation, |
| 1492 | const uint8_t *input, |
| 1493 | size_t input_length); |
| 1494 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1495 | /** Finish the calculation of the hash of a message. |
| 1496 | * |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1497 | * The application must call psa_hash_setup() before calling this function. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1498 | * This function calculates the hash of the message formed by concatenating |
| 1499 | * the inputs passed to preceding calls to psa_hash_update(). |
| 1500 | * |
| 1501 | * When this function returns, the operation becomes inactive. |
| 1502 | * |
| 1503 | * \warning Applications should not call this function if they expect |
| 1504 | * a specific value for the hash. Call psa_hash_verify() instead. |
| 1505 | * Beware that comparing integrity or authenticity data such as |
| 1506 | * hash values with a function such as \c memcmp is risky |
| 1507 | * because the time taken by the comparison may leak information |
| 1508 | * about the hashed data which could allow an attacker to guess |
| 1509 | * a valid hash and thereby bypass security controls. |
| 1510 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1511 | * \param[in,out] operation Active hash operation. |
| 1512 | * \param[out] hash Buffer where the hash is to be written. |
| 1513 | * \param hash_size Size of the \p hash buffer in bytes. |
| 1514 | * \param[out] hash_length On success, the number of bytes |
| 1515 | * that make up the hash value. This is always |
Gilles Peskine | be42f31 | 2018-07-13 14:38:15 +0200 | [diff] [blame] | 1516 | * #PSA_HASH_SIZE(\c alg) where \c alg is the |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1517 | * hash algorithm that is calculated. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1518 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1519 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1520 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1521 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1522 | * The operation state is not valid (not started, or already completed). |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1523 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1524 | * 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] | 1525 | * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg) |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1526 | * where \c alg is the hash algorithm that is calculated. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1527 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1528 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1529 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1530 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1531 | */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1532 | psa_status_t psa_hash_finish(psa_hash_operation_t *operation, |
| 1533 | uint8_t *hash, |
| 1534 | size_t hash_size, |
| 1535 | size_t *hash_length); |
| 1536 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1537 | /** Finish the calculation of the hash of a message and compare it with |
| 1538 | * an expected value. |
| 1539 | * |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1540 | * The application must call psa_hash_setup() before calling this function. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1541 | * This function calculates the hash of the message formed by concatenating |
| 1542 | * the inputs passed to preceding calls to psa_hash_update(). It then |
| 1543 | * compares the calculated hash with the expected hash passed as a |
| 1544 | * parameter to this function. |
| 1545 | * |
| 1546 | * When this function returns, the operation becomes inactive. |
| 1547 | * |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 1548 | * \note Implementations shall make the best effort to ensure that the |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1549 | * comparison between the actual hash and the expected hash is performed |
| 1550 | * in constant time. |
| 1551 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1552 | * \param[in,out] operation Active hash operation. |
| 1553 | * \param[in] hash Buffer containing the expected hash value. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1554 | * \param hash_length Size of the \p hash buffer in bytes. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1555 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1556 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1557 | * The expected hash is identical to the actual hash of the message. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1558 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1559 | * The hash of the message was calculated successfully, but it |
| 1560 | * differs from the expected hash. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1561 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1562 | * The operation state is not valid (not started, or already completed). |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1563 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1564 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1565 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1566 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1567 | */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1568 | psa_status_t psa_hash_verify(psa_hash_operation_t *operation, |
| 1569 | const uint8_t *hash, |
| 1570 | size_t hash_length); |
| 1571 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1572 | /** Abort a hash operation. |
| 1573 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1574 | * Aborting an operation frees all associated resources except for the |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 1575 | * \p operation structure itself. Once aborted, the operation object |
| 1576 | * can be reused for another operation by calling |
| 1577 | * psa_hash_setup() again. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1578 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 1579 | * You may call this function any time after the operation object has |
| 1580 | * been initialized by any of the following methods: |
| 1581 | * - A call to psa_hash_setup(), whether it succeeds or not. |
| 1582 | * - Initializing the \c struct to all-bits-zero. |
| 1583 | * - Initializing the \c struct to logical zeros, e.g. |
| 1584 | * `psa_hash_operation_t operation = {0}`. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1585 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 1586 | * In particular, calling psa_hash_abort() after the operation has been |
| 1587 | * terminated by a call to psa_hash_abort(), psa_hash_finish() or |
| 1588 | * psa_hash_verify() is safe and has no effect. |
| 1589 | * |
| 1590 | * \param[in,out] operation Initialized hash operation. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1591 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1592 | * \retval #PSA_SUCCESS |
| 1593 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1594 | * \p operation is not an active hash operation. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1595 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1596 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1597 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1598 | */ |
| 1599 | psa_status_t psa_hash_abort(psa_hash_operation_t *operation); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1600 | |
| 1601 | /**@}*/ |
| 1602 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1603 | /** \defgroup MAC Message authentication codes |
| 1604 | * @{ |
| 1605 | */ |
| 1606 | |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1607 | /** The type of the state data structure for multipart MAC operations. |
| 1608 | * |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 1609 | * This is an implementation-defined \c struct. Applications should not |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1610 | * make any assumptions about the content of this structure except |
| 1611 | * as directed by the documentation of a specific implementation. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1612 | typedef struct psa_mac_operation_s psa_mac_operation_t; |
| 1613 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1614 | /** Start a multipart MAC calculation operation. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1615 | * |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1616 | * This function sets up the calculation of the MAC |
| 1617 | * (message authentication code) of a byte string. |
| 1618 | * To verify the MAC of a message against an |
| 1619 | * expected value, use psa_mac_verify_setup() instead. |
| 1620 | * |
| 1621 | * The sequence of operations to calculate a MAC is as follows: |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1622 | * -# Allocate an operation object which will be passed to all the functions |
| 1623 | * listed here. |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1624 | * -# Call psa_mac_sign_setup() to specify the algorithm and key. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1625 | * The key remains associated with the operation even if the content |
| 1626 | * of the key slot changes. |
| 1627 | * -# Call psa_mac_update() zero, one or more times, passing a fragment |
| 1628 | * of the message each time. The MAC that is calculated is the MAC |
| 1629 | * of the concatenation of these messages in order. |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1630 | * -# At the end of the message, call psa_mac_sign_finish() to finish |
| 1631 | * calculating the MAC value and retrieve it. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1632 | * |
| 1633 | * The application may call psa_mac_abort() at any time after the operation |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1634 | * has been initialized with psa_mac_sign_setup(). |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1635 | * |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1636 | * After a successful call to psa_mac_sign_setup(), the application must |
| 1637 | * eventually terminate the operation through one of the following methods: |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1638 | * - A failed call to psa_mac_update(). |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1639 | * - A call to psa_mac_sign_finish() or psa_mac_abort(). |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1640 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1641 | * \param[out] operation The operation object to use. |
| 1642 | * \param key Slot containing the key to use for the operation. |
| 1643 | * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value |
| 1644 | * such that #PSA_ALG_IS_MAC(alg) is true). |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1645 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1646 | * \retval #PSA_SUCCESS |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1647 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1648 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1649 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1650 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1651 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1652 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1653 | * \p alg is not supported or is not a MAC algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1654 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1655 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1656 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1657 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1658 | */ |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1659 | psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation, |
| 1660 | psa_key_slot_t key, |
| 1661 | psa_algorithm_t alg); |
| 1662 | |
| 1663 | /** Start a multipart MAC verification operation. |
| 1664 | * |
| 1665 | * This function sets up the verification of the MAC |
| 1666 | * (message authentication code) of a byte string against an expected value. |
| 1667 | * |
| 1668 | * The sequence of operations to verify a MAC is as follows: |
| 1669 | * -# Allocate an operation object which will be passed to all the functions |
| 1670 | * listed here. |
| 1671 | * -# Call psa_mac_verify_setup() to specify the algorithm and key. |
| 1672 | * The key remains associated with the operation even if the content |
| 1673 | * of the key slot changes. |
| 1674 | * -# Call psa_mac_update() zero, one or more times, passing a fragment |
| 1675 | * of the message each time. The MAC that is calculated is the MAC |
| 1676 | * of the concatenation of these messages in order. |
| 1677 | * -# At the end of the message, call psa_mac_verify_finish() to finish |
| 1678 | * calculating the actual MAC of the message and verify it against |
| 1679 | * the expected value. |
| 1680 | * |
| 1681 | * The application may call psa_mac_abort() at any time after the operation |
| 1682 | * has been initialized with psa_mac_verify_setup(). |
| 1683 | * |
| 1684 | * After a successful call to psa_mac_verify_setup(), the application must |
| 1685 | * eventually terminate the operation through one of the following methods: |
| 1686 | * - A failed call to psa_mac_update(). |
| 1687 | * - A call to psa_mac_verify_finish() or psa_mac_abort(). |
| 1688 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1689 | * \param[out] operation The operation object to use. |
| 1690 | * \param key Slot containing the key to use for the operation. |
| 1691 | * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value |
| 1692 | * such that #PSA_ALG_IS_MAC(\p alg) is true). |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1693 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1694 | * \retval #PSA_SUCCESS |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1695 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1696 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1697 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1698 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1699 | * \c key is not compatible with \c alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1700 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1701 | * \c alg is not supported or is not a MAC algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1702 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1703 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1704 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1705 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1706 | */ |
| 1707 | psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation, |
| 1708 | psa_key_slot_t key, |
| 1709 | psa_algorithm_t alg); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1710 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1711 | /** Add a message fragment to a multipart MAC operation. |
| 1712 | * |
| 1713 | * The application must call psa_mac_sign_setup() or psa_mac_verify_setup() |
| 1714 | * before calling this function. |
| 1715 | * |
| 1716 | * If this function returns an error status, the operation becomes inactive. |
| 1717 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1718 | * \param[in,out] operation Active MAC operation. |
| 1719 | * \param[in] input Buffer containing the message fragment to add to |
| 1720 | * the MAC calculation. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1721 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1722 | * |
| 1723 | * \retval #PSA_SUCCESS |
| 1724 | * Success. |
| 1725 | * \retval #PSA_ERROR_BAD_STATE |
| 1726 | * The operation state is not valid (not started, or already completed). |
| 1727 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1728 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1729 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1730 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1731 | */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1732 | psa_status_t psa_mac_update(psa_mac_operation_t *operation, |
| 1733 | const uint8_t *input, |
| 1734 | size_t input_length); |
| 1735 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1736 | /** Finish the calculation of the MAC of a message. |
| 1737 | * |
| 1738 | * The application must call psa_mac_sign_setup() before calling this function. |
| 1739 | * This function calculates the MAC of the message formed by concatenating |
| 1740 | * the inputs passed to preceding calls to psa_mac_update(). |
| 1741 | * |
| 1742 | * When this function returns, the operation becomes inactive. |
| 1743 | * |
| 1744 | * \warning Applications should not call this function if they expect |
| 1745 | * a specific value for the MAC. Call psa_mac_verify_finish() instead. |
| 1746 | * Beware that comparing integrity or authenticity data such as |
| 1747 | * MAC values with a function such as \c memcmp is risky |
| 1748 | * because the time taken by the comparison may leak information |
| 1749 | * about the MAC value which could allow an attacker to guess |
| 1750 | * a valid MAC and thereby bypass security controls. |
| 1751 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1752 | * \param[in,out] operation Active MAC operation. |
| 1753 | * \param[out] mac Buffer where the MAC value is to be written. |
| 1754 | * \param mac_size Size of the \p mac buffer in bytes. |
| 1755 | * \param[out] mac_length On success, the number of bytes |
| 1756 | * that make up the MAC value. This is always |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 1757 | * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg) |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1758 | * where \c key_type and \c key_bits are the type and |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 1759 | * bit-size respectively of the key and \c alg is the |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1760 | * MAC algorithm that is calculated. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1761 | * |
| 1762 | * \retval #PSA_SUCCESS |
| 1763 | * Success. |
| 1764 | * \retval #PSA_ERROR_BAD_STATE |
| 1765 | * The operation state is not valid (not started, or already completed). |
| 1766 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1767 | * 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] | 1768 | * sufficient buffer size by calling PSA_MAC_FINAL_SIZE(). |
| 1769 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1770 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1771 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1772 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1773 | */ |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 1774 | psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation, |
| 1775 | uint8_t *mac, |
| 1776 | size_t mac_size, |
| 1777 | size_t *mac_length); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1778 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1779 | /** Finish the calculation of the MAC of a message and compare it with |
| 1780 | * an expected value. |
| 1781 | * |
| 1782 | * The application must call psa_mac_verify_setup() before calling this function. |
| 1783 | * This function calculates the MAC of the message formed by concatenating |
| 1784 | * the inputs passed to preceding calls to psa_mac_update(). It then |
| 1785 | * compares the calculated MAC with the expected MAC passed as a |
| 1786 | * parameter to this function. |
| 1787 | * |
| 1788 | * When this function returns, the operation becomes inactive. |
| 1789 | * |
| 1790 | * \note Implementations shall make the best effort to ensure that the |
| 1791 | * comparison between the actual MAC and the expected MAC is performed |
| 1792 | * in constant time. |
| 1793 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1794 | * \param[in,out] operation Active MAC operation. |
| 1795 | * \param[in] mac Buffer containing the expected MAC value. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1796 | * \param mac_length Size of the \p mac buffer in bytes. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1797 | * |
| 1798 | * \retval #PSA_SUCCESS |
| 1799 | * The expected MAC is identical to the actual MAC of the message. |
| 1800 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
| 1801 | * The MAC of the message was calculated successfully, but it |
| 1802 | * differs from the expected MAC. |
| 1803 | * \retval #PSA_ERROR_BAD_STATE |
| 1804 | * The operation state is not valid (not started, or already completed). |
| 1805 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1806 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1807 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1808 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1809 | */ |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 1810 | psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation, |
| 1811 | const uint8_t *mac, |
| 1812 | size_t mac_length); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1813 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1814 | /** Abort a MAC operation. |
| 1815 | * |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1816 | * Aborting an operation frees all associated resources except for the |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 1817 | * \p operation structure itself. Once aborted, the operation object |
| 1818 | * can be reused for another operation by calling |
| 1819 | * psa_mac_sign_setup() or psa_mac_verify_setup() again. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1820 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 1821 | * You may call this function any time after the operation object has |
| 1822 | * been initialized by any of the following methods: |
| 1823 | * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether |
| 1824 | * it succeeds or not. |
| 1825 | * - Initializing the \c struct to all-bits-zero. |
| 1826 | * - Initializing the \c struct to logical zeros, e.g. |
| 1827 | * `psa_mac_operation_t operation = {0}`. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1828 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 1829 | * In particular, calling psa_mac_abort() after the operation has been |
| 1830 | * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or |
| 1831 | * psa_mac_verify_finish() is safe and has no effect. |
| 1832 | * |
| 1833 | * \param[in,out] operation Initialized MAC operation. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1834 | * |
| 1835 | * \retval #PSA_SUCCESS |
| 1836 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1837 | * \p operation is not an active MAC operation. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1838 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1839 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1840 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1841 | */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1842 | psa_status_t psa_mac_abort(psa_mac_operation_t *operation); |
| 1843 | |
| 1844 | /**@}*/ |
| 1845 | |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1846 | /** \defgroup cipher Symmetric ciphers |
| 1847 | * @{ |
| 1848 | */ |
| 1849 | |
| 1850 | /** The type of the state data structure for multipart cipher operations. |
| 1851 | * |
| 1852 | * This is an implementation-defined \c struct. Applications should not |
| 1853 | * make any assumptions about the content of this structure except |
| 1854 | * as directed by the documentation of a specific implementation. */ |
| 1855 | typedef struct psa_cipher_operation_s psa_cipher_operation_t; |
| 1856 | |
| 1857 | /** Set the key for a multipart symmetric encryption operation. |
| 1858 | * |
| 1859 | * The sequence of operations to encrypt a message with a symmetric cipher |
| 1860 | * is as follows: |
| 1861 | * -# Allocate an operation object which will be passed to all the functions |
| 1862 | * listed here. |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1863 | * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key. |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1864 | * The key remains associated with the operation even if the content |
| 1865 | * of the key slot changes. |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1866 | * -# Call either psa_encrypt_generate_iv() or psa_cipher_set_iv() to |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1867 | * generate or set the IV (initialization vector). You should use |
| 1868 | * psa_encrypt_generate_iv() unless the protocol you are implementing |
| 1869 | * requires a specific IV value. |
| 1870 | * -# Call psa_cipher_update() zero, one or more times, passing a fragment |
| 1871 | * of the message each time. |
| 1872 | * -# Call psa_cipher_finish(). |
| 1873 | * |
| 1874 | * The application may call psa_cipher_abort() at any time after the operation |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1875 | * has been initialized with psa_cipher_encrypt_setup(). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1876 | * |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1877 | * After a successful call to psa_cipher_encrypt_setup(), the application must |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame] | 1878 | * eventually terminate the operation. The following events terminate an |
| 1879 | * operation: |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1880 | * - A failed call to psa_encrypt_generate_iv(), psa_cipher_set_iv() |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1881 | * or psa_cipher_update(). |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 1882 | * - A call to psa_cipher_finish() or psa_cipher_abort(). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1883 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1884 | * \param[out] operation The operation object to use. |
| 1885 | * \param key Slot containing the key to use for the operation. |
| 1886 | * \param alg The cipher algorithm to compute |
| 1887 | * (\c PSA_ALG_XXX value such that |
| 1888 | * #PSA_ALG_IS_CIPHER(\p alg) is true). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1889 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1890 | * \retval #PSA_SUCCESS |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1891 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1892 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1893 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1894 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1895 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1896 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1897 | * \p alg is not supported or is not a cipher algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1898 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1899 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1900 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1901 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1902 | */ |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1903 | psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation, |
| 1904 | psa_key_slot_t key, |
| 1905 | psa_algorithm_t alg); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1906 | |
| 1907 | /** Set the key for a multipart symmetric decryption operation. |
| 1908 | * |
| 1909 | * The sequence of operations to decrypt a message with a symmetric cipher |
| 1910 | * is as follows: |
| 1911 | * -# Allocate an operation object which will be passed to all the functions |
| 1912 | * listed here. |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1913 | * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key. |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1914 | * The key remains associated with the operation even if the content |
| 1915 | * of the key slot changes. |
| 1916 | * -# Call psa_cipher_update() with the IV (initialization vector) for the |
| 1917 | * decryption. If the IV is prepended to the ciphertext, you can call |
| 1918 | * psa_cipher_update() on a buffer containing the IV followed by the |
| 1919 | * beginning of the message. |
| 1920 | * -# Call psa_cipher_update() zero, one or more times, passing a fragment |
| 1921 | * of the message each time. |
| 1922 | * -# Call psa_cipher_finish(). |
| 1923 | * |
| 1924 | * The application may call psa_cipher_abort() at any time after the operation |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1925 | * has been initialized with psa_cipher_decrypt_setup(). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1926 | * |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1927 | * After a successful call to psa_cipher_decrypt_setup(), the application must |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame] | 1928 | * eventually terminate the operation. The following events terminate an |
| 1929 | * operation: |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1930 | * - A failed call to psa_cipher_update(). |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 1931 | * - A call to psa_cipher_finish() or psa_cipher_abort(). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1932 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1933 | * \param[out] operation The operation object to use. |
| 1934 | * \param key Slot containing the key to use for the operation. |
| 1935 | * \param alg The cipher algorithm to compute |
| 1936 | * (\c PSA_ALG_XXX value such that |
| 1937 | * #PSA_ALG_IS_CIPHER(\p alg) is true). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1938 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1939 | * \retval #PSA_SUCCESS |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1940 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1941 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 1942 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 1943 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1944 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1945 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1946 | * \p alg is not supported or is not a cipher algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 1947 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1948 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1949 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1950 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1951 | */ |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1952 | psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation, |
| 1953 | psa_key_slot_t key, |
| 1954 | psa_algorithm_t alg); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1955 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1956 | /** Generate an IV for a symmetric encryption operation. |
| 1957 | * |
| 1958 | * This function generates a random IV (initialization vector), nonce |
| 1959 | * or initial counter value for the encryption operation as appropriate |
| 1960 | * for the chosen algorithm, key type and key size. |
| 1961 | * |
| 1962 | * The application must call psa_cipher_encrypt_setup() before |
| 1963 | * calling this function. |
| 1964 | * |
| 1965 | * If this function returns an error status, the operation becomes inactive. |
| 1966 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1967 | * \param[in,out] operation Active cipher operation. |
| 1968 | * \param[out] iv Buffer where the generated IV is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 1969 | * \param iv_size Size of the \p iv buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 1970 | * \param[out] iv_length On success, the number of bytes of the |
| 1971 | * generated IV. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1972 | * |
| 1973 | * \retval #PSA_SUCCESS |
| 1974 | * Success. |
| 1975 | * \retval #PSA_ERROR_BAD_STATE |
| 1976 | * The operation state is not valid (not started, or IV already set). |
| 1977 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 1978 | * The size of the \p iv buffer is too small. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1979 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 1980 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 1981 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 1982 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 1983 | */ |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 1984 | psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation, |
| 1985 | unsigned char *iv, |
| 1986 | size_t iv_size, |
| 1987 | size_t *iv_length); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1988 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 1989 | /** Set the IV for a symmetric encryption or decryption operation. |
| 1990 | * |
| 1991 | * This function sets the random IV (initialization vector), nonce |
| 1992 | * or initial counter value for the encryption or decryption operation. |
| 1993 | * |
| 1994 | * The application must call psa_cipher_encrypt_setup() before |
| 1995 | * calling this function. |
| 1996 | * |
| 1997 | * If this function returns an error status, the operation becomes inactive. |
| 1998 | * |
| 1999 | * \note When encrypting, applications should use psa_cipher_generate_iv() |
| 2000 | * instead of this function, unless implementing a protocol that requires |
| 2001 | * a non-random IV. |
| 2002 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2003 | * \param[in,out] operation Active cipher operation. |
| 2004 | * \param[in] iv Buffer containing the IV to use. |
| 2005 | * \param iv_length Size of the IV in bytes. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2006 | * |
| 2007 | * \retval #PSA_SUCCESS |
| 2008 | * Success. |
| 2009 | * \retval #PSA_ERROR_BAD_STATE |
| 2010 | * The operation state is not valid (not started, or IV already set). |
| 2011 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2012 | * The size of \p iv is not acceptable for the chosen algorithm, |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2013 | * or the chosen algorithm does not use an IV. |
| 2014 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2015 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2016 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2017 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2018 | */ |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2019 | psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation, |
| 2020 | const unsigned char *iv, |
| 2021 | size_t iv_length); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2022 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2023 | /** Encrypt or decrypt a message fragment in an active cipher operation. |
| 2024 | * |
Gilles Peskine | 9ac9426 | 2018-07-12 20:15:32 +0200 | [diff] [blame] | 2025 | * Before calling this function, you must: |
| 2026 | * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(). |
| 2027 | * The choice of setup function determines whether this function |
| 2028 | * encrypts or decrypts its input. |
| 2029 | * 2. If the algorithm requires an IV, call psa_cipher_generate_iv() |
| 2030 | * (recommended when encrypting) or psa_cipher_set_iv(). |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2031 | * |
| 2032 | * If this function returns an error status, the operation becomes inactive. |
| 2033 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2034 | * \param[in,out] operation Active cipher operation. |
| 2035 | * \param[in] input Buffer containing the message fragment to |
| 2036 | * encrypt or decrypt. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2037 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2038 | * \param[out] output Buffer where the output is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2039 | * \param output_size Size of the \p output buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2040 | * \param[out] output_length On success, the number of bytes |
| 2041 | * that make up the returned output. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2042 | * |
| 2043 | * \retval #PSA_SUCCESS |
| 2044 | * Success. |
| 2045 | * \retval #PSA_ERROR_BAD_STATE |
| 2046 | * The operation state is not valid (not started, IV required but |
| 2047 | * not set, or already completed). |
| 2048 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 2049 | * The size of the \p output buffer is too small. |
| 2050 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2051 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2052 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2053 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2054 | */ |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2055 | psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, |
| 2056 | const uint8_t *input, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2057 | size_t input_length, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2058 | unsigned char *output, |
| 2059 | size_t output_size, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2060 | size_t *output_length); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2061 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2062 | /** Finish encrypting or decrypting a message in a cipher operation. |
| 2063 | * |
| 2064 | * The application must call psa_cipher_encrypt_setup() or |
| 2065 | * psa_cipher_decrypt_setup() before calling this function. The choice |
| 2066 | * of setup function determines whether this function encrypts or |
| 2067 | * decrypts its input. |
| 2068 | * |
| 2069 | * This function finishes the encryption or decryption of the message |
| 2070 | * formed by concatenating the inputs passed to preceding calls to |
| 2071 | * psa_cipher_update(). |
| 2072 | * |
| 2073 | * When this function returns, the operation becomes inactive. |
| 2074 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2075 | * \param[in,out] operation Active cipher operation. |
| 2076 | * \param[out] output Buffer where the output is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2077 | * \param output_size Size of the \p output buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2078 | * \param[out] output_length On success, the number of bytes |
| 2079 | * that make up the returned output. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2080 | * |
| 2081 | * \retval #PSA_SUCCESS |
| 2082 | * Success. |
| 2083 | * \retval #PSA_ERROR_BAD_STATE |
| 2084 | * The operation state is not valid (not started, IV required but |
| 2085 | * not set, or already completed). |
| 2086 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 2087 | * The size of the \p output buffer is too small. |
| 2088 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2089 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2090 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2091 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2092 | */ |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2093 | psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2094 | uint8_t *output, |
Moran Peker | 0071b87 | 2018-04-22 20:16:58 +0300 | [diff] [blame] | 2095 | size_t output_size, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 2096 | size_t *output_length); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2097 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2098 | /** Abort a cipher operation. |
| 2099 | * |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2100 | * Aborting an operation frees all associated resources except for the |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 2101 | * \p operation structure itself. Once aborted, the operation object |
| 2102 | * can be reused for another operation by calling |
| 2103 | * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2104 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 2105 | * You may call this function any time after the operation object has |
| 2106 | * been initialized by any of the following methods: |
| 2107 | * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(), |
| 2108 | * whether it succeeds or not. |
| 2109 | * - Initializing the \c struct to all-bits-zero. |
| 2110 | * - Initializing the \c struct to logical zeros, e.g. |
| 2111 | * `psa_cipher_operation_t operation = {0}`. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2112 | * |
Gilles Peskine | b82ab6f | 2018-07-13 15:33:43 +0200 | [diff] [blame] | 2113 | * In particular, calling psa_cipher_abort() after the operation has been |
| 2114 | * terminated by a call to psa_cipher_abort() or psa_cipher_finish() |
| 2115 | * is safe and has no effect. |
| 2116 | * |
| 2117 | * \param[in,out] operation Initialized cipher operation. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2118 | * |
| 2119 | * \retval #PSA_SUCCESS |
| 2120 | * \retval #PSA_ERROR_BAD_STATE |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2121 | * \p operation is not an active cipher operation. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 2122 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2123 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2124 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2125 | */ |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 2126 | psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation); |
| 2127 | |
| 2128 | /**@}*/ |
| 2129 | |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2130 | /** \defgroup aead Authenticated encryption with associated data (AEAD) |
| 2131 | * @{ |
| 2132 | */ |
| 2133 | |
Gilles Peskine | 5e39dc9 | 2018-06-08 11:41:57 +0200 | [diff] [blame] | 2134 | /** The tag size for an AEAD algorithm, in bytes. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2135 | * |
Gilles Peskine | 5e39dc9 | 2018-06-08 11:41:57 +0200 | [diff] [blame] | 2136 | * \param alg An AEAD algorithm |
| 2137 | * (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 2138 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
Gilles Peskine | 5e39dc9 | 2018-06-08 11:41:57 +0200 | [diff] [blame] | 2139 | * |
| 2140 | * \return The tag size for the specified algorithm. |
| 2141 | * If the AEAD algorithm does not have an identified |
| 2142 | * tag that can be distinguished from the rest of |
| 2143 | * the ciphertext, return 0. |
| 2144 | * If the AEAD algorithm is not recognized, return 0. |
| 2145 | * An implementation may return either 0 or a |
| 2146 | * correct size for an AEAD algorithm that it |
| 2147 | * recognizes, but does not support. |
| 2148 | */ |
| 2149 | #define PSA_AEAD_TAG_SIZE(alg) \ |
| 2150 | ((alg) == PSA_ALG_GCM ? 16 : \ |
| 2151 | (alg) == PSA_ALG_CCM ? 16 : \ |
| 2152 | 0) |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2153 | |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2154 | /** Process an authenticated encryption operation. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2155 | * |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2156 | * \param key Slot containing the key to use. |
| 2157 | * \param alg The AEAD algorithm to compute |
| 2158 | * (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 2159 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2160 | * \param[in] nonce Nonce or IV to use. |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2161 | * \param nonce_length Size of the \p nonce buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2162 | * \param[in] additional_data Additional data that will be authenticated |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2163 | * but not encrypted. |
| 2164 | * \param additional_data_length Size of \p additional_data in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2165 | * \param[in] plaintext Data that will be authenticated and |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2166 | * encrypted. |
| 2167 | * \param plaintext_length Size of \p plaintext in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2168 | * \param[out] ciphertext Output buffer for the authenticated and |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2169 | * encrypted data. The additional data is not |
| 2170 | * part of this output. For algorithms where the |
| 2171 | * encrypted data and the authentication tag |
| 2172 | * are defined as separate outputs, the |
| 2173 | * authentication tag is appended to the |
| 2174 | * encrypted data. |
| 2175 | * \param ciphertext_size Size of the \p ciphertext buffer in bytes. |
| 2176 | * This must be at least |
| 2177 | * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg, |
| 2178 | * \p plaintext_length). |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2179 | * \param[out] ciphertext_length On success, the size of the output |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2180 | * in the \b ciphertext buffer. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2181 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2182 | * \retval #PSA_SUCCESS |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2183 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2184 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 2185 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 2186 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2187 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2188 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2189 | * \p alg is not supported or is not an AEAD algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2190 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2191 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2192 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2193 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2194 | */ |
mohammad1603 | 39ee871 | 2018-04-26 00:51:02 +0300 | [diff] [blame] | 2195 | psa_status_t psa_aead_encrypt( psa_key_slot_t key, |
| 2196 | psa_algorithm_t alg, |
| 2197 | const uint8_t *nonce, |
| 2198 | size_t nonce_length, |
| 2199 | const uint8_t *additional_data, |
| 2200 | size_t additional_data_length, |
| 2201 | const uint8_t *plaintext, |
| 2202 | size_t plaintext_length, |
| 2203 | uint8_t *ciphertext, |
| 2204 | size_t ciphertext_size, |
| 2205 | size_t *ciphertext_length ); |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2206 | |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2207 | /** Process an authenticated decryption operation. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2208 | * |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2209 | * \param key Slot containing the key to use. |
| 2210 | * \param alg The AEAD algorithm to compute |
| 2211 | * (\c PSA_ALG_XXX value such that |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 2212 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2213 | * \param[in] nonce Nonce or IV to use. |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2214 | * \param nonce_length Size of the \p nonce buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2215 | * \param[in] additional_data Additional data that has been authenticated |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2216 | * but not encrypted. |
| 2217 | * \param additional_data_length Size of \p additional_data in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2218 | * \param[in] ciphertext Data that has been authenticated and |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2219 | * encrypted. For algorithms where the |
| 2220 | * encrypted data and the authentication tag |
| 2221 | * are defined as separate inputs, the buffer |
| 2222 | * must contain the encrypted data followed |
| 2223 | * by the authentication tag. |
| 2224 | * \param ciphertext_length Size of \p ciphertext in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2225 | * \param[out] plaintext Output buffer for the decrypted data. |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2226 | * \param plaintext_size Size of the \p plaintext buffer in bytes. |
| 2227 | * This must be at least |
| 2228 | * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg, |
| 2229 | * \p ciphertext_length). |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2230 | * \param[out] plaintext_length On success, the size of the output |
mohammad1603 | fb5b9cb | 2018-06-06 13:44:27 +0300 | [diff] [blame] | 2231 | * in the \b plaintext buffer. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2232 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2233 | * \retval #PSA_SUCCESS |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2234 | * Success. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2235 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 2236 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 2237 | * The ciphertext is not authentic. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2238 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 2239 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2240 | * \p key is not compatible with \p alg. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2241 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2242 | * \p alg is not supported or is not an AEAD algorithm. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2243 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2244 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2245 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2246 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2247 | */ |
mohammad1603 | 39ee871 | 2018-04-26 00:51:02 +0300 | [diff] [blame] | 2248 | psa_status_t psa_aead_decrypt( psa_key_slot_t key, |
| 2249 | psa_algorithm_t alg, |
| 2250 | const uint8_t *nonce, |
| 2251 | size_t nonce_length, |
| 2252 | const uint8_t *additional_data, |
| 2253 | size_t additional_data_length, |
| 2254 | const uint8_t *ciphertext, |
| 2255 | size_t ciphertext_length, |
| 2256 | uint8_t *plaintext, |
| 2257 | size_t plaintext_size, |
| 2258 | size_t *plaintext_length ); |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 2259 | |
| 2260 | /**@}*/ |
| 2261 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2262 | /** \defgroup asymmetric Asymmetric cryptography |
| 2263 | * @{ |
| 2264 | */ |
| 2265 | |
| 2266 | /** |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 2267 | * \brief ECDSA signature size for a given curve bit size |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 2268 | * |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 2269 | * \param curve_bits Curve size in bits. |
| 2270 | * \return Signature size in bytes. |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 2271 | * |
| 2272 | * \note This macro returns a compile-time constant if its argument is one. |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 2273 | */ |
Gilles Peskine | eae6eee | 2018-06-28 13:56:01 +0200 | [diff] [blame] | 2274 | #define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \ |
| 2275 | (PSA_BITS_TO_BYTES(curve_bits) * 2) |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 2276 | |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 2277 | /** |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2278 | * \brief Sign a hash or short message with a private key. |
| 2279 | * |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 2280 | * Note that to perform a hash-and-sign signature algorithm, you must |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2281 | * first calculate the hash by calling psa_hash_setup(), psa_hash_update() |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 2282 | * and psa_hash_finish(). Then pass the resulting hash as the \p hash |
| 2283 | * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg) |
| 2284 | * to determine the hash algorithm to use. |
| 2285 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2286 | * \param key Key slot containing an asymmetric key pair. |
| 2287 | * \param alg A signature algorithm that is compatible with |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2288 | * the type of \p key. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2289 | * \param[in] hash The hash or message to sign. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2290 | * \param hash_length Size of the \p hash buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2291 | * \param[in] salt A salt or label, if supported by the |
| 2292 | * signature algorithm. |
| 2293 | * If the signature algorithm does not support |
| 2294 | * a salt, pass \c NULL. |
| 2295 | * If the signature algorithm supports an |
| 2296 | * optional salt and you do not want to pass |
| 2297 | * a salt, pass \c NULL. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2298 | * \param salt_length Size of the \p salt buffer in bytes. |
| 2299 | * If \p salt is \c NULL, pass 0. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2300 | * \param[out] signature Buffer where the signature is to be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2301 | * \param signature_size Size of the \p signature buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2302 | * \param[out] signature_length On success, the number of bytes |
| 2303 | * that make up the returned signature value. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2304 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2305 | * \retval #PSA_SUCCESS |
| 2306 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2307 | * The size of the \p signature buffer is too small. You can |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2308 | * determine a sufficient buffer size by calling |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 2309 | * #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] | 2310 | * where \c key_type and \c key_bits are the type and bit-size |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2311 | * respectively of \p key. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2312 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2313 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2314 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2315 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2316 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2317 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2318 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2319 | */ |
| 2320 | psa_status_t psa_asymmetric_sign(psa_key_slot_t key, |
| 2321 | psa_algorithm_t alg, |
| 2322 | const uint8_t *hash, |
| 2323 | size_t hash_length, |
| 2324 | const uint8_t *salt, |
| 2325 | size_t salt_length, |
| 2326 | uint8_t *signature, |
| 2327 | size_t signature_size, |
| 2328 | size_t *signature_length); |
| 2329 | |
| 2330 | /** |
| 2331 | * \brief Verify the signature a hash or short message using a public key. |
| 2332 | * |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 2333 | * Note that to perform a hash-and-sign signature algorithm, you must |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2334 | * first calculate the hash by calling psa_hash_setup(), psa_hash_update() |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 2335 | * and psa_hash_finish(). Then pass the resulting hash as the \p hash |
| 2336 | * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg) |
| 2337 | * to determine the hash algorithm to use. |
| 2338 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2339 | * \param key Key slot containing a public key or an |
| 2340 | * asymmetric key pair. |
| 2341 | * \param alg A signature algorithm that is compatible with |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2342 | * the type of \p key. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2343 | * \param[in] hash The hash or message whose signature is to be |
Gilles Peskine | 08bac71 | 2018-06-26 16:14:46 +0200 | [diff] [blame] | 2344 | * verified. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2345 | * \param hash_length Size of the \p hash buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2346 | * \param[in] salt A salt or label, if supported by the signature |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2347 | * algorithm. |
| 2348 | * If the signature algorithm does not support a |
| 2349 | * salt, pass \c NULL. |
| 2350 | * If the signature algorithm supports an optional |
| 2351 | * salt and you do not want to pass a salt, |
| 2352 | * pass \c NULL. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2353 | * \param salt_length Size of the \p salt buffer in bytes. |
| 2354 | * If \p salt is \c NULL, pass 0. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2355 | * \param[in] signature Buffer containing the signature to verify. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2356 | * \param signature_length Size of the \p signature buffer in bytes. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2357 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2358 | * \retval #PSA_SUCCESS |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2359 | * The signature is valid. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2360 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 2361 | * The calculation was perfomed successfully, but the passed |
| 2362 | * signature is not a valid signature. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2363 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2364 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2365 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2366 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2367 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2368 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2369 | */ |
| 2370 | psa_status_t psa_asymmetric_verify(psa_key_slot_t key, |
| 2371 | psa_algorithm_t alg, |
| 2372 | const uint8_t *hash, |
| 2373 | size_t hash_length, |
| 2374 | const uint8_t *salt, |
| 2375 | size_t salt_length, |
Gilles Peskine | e9191ff | 2018-06-27 14:58:41 +0200 | [diff] [blame] | 2376 | const uint8_t *signature, |
Gilles Peskine | 526fab0 | 2018-06-27 18:19:40 +0200 | [diff] [blame] | 2377 | size_t signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2378 | |
Gilles Peskine | 723feff | 2018-05-31 20:08:13 +0200 | [diff] [blame] | 2379 | #define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \ |
| 2380 | (PSA_ALG_IS_RSA_OAEP_MGF1(alg) ? \ |
| 2381 | 2 * PSA_HASH_FINAL_SIZE(PSA_ALG_RSA_GET_HASH(alg)) + 1 : \ |
| 2382 | 11 /*PKCS#1v1.5*/) |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2383 | |
| 2384 | /** |
| 2385 | * \brief Encrypt a short message with a public key. |
| 2386 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2387 | * \param key Key slot containing a public key or an |
| 2388 | * asymmetric key pair. |
| 2389 | * \param alg An asymmetric encryption algorithm that is |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2390 | * compatible with the type of \p key. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2391 | * \param[in] input The message to encrypt. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2392 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2393 | * \param[in] salt A salt or label, if supported by the |
| 2394 | * encryption algorithm. |
| 2395 | * If the algorithm does not support a |
| 2396 | * salt, pass \c NULL. |
| 2397 | * If the algorithm supports an optional |
| 2398 | * salt and you do not want to pass a salt, |
| 2399 | * pass \c NULL. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2400 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2401 | * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is |
| 2402 | * supported. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2403 | * \param salt_length Size of the \p salt buffer in bytes. |
| 2404 | * If \p salt is \c NULL, pass 0. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2405 | * \param[out] output Buffer where the encrypted message is to |
| 2406 | * be written. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2407 | * \param output_size Size of the \p output buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2408 | * \param[out] output_length On success, the number of bytes |
| 2409 | * that make up the returned output. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2410 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2411 | * \retval #PSA_SUCCESS |
| 2412 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2413 | * The size of the \p output buffer is too small. You can |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2414 | * determine a sufficient buffer size by calling |
Gilles Peskine | 7256e6c | 2018-07-12 00:34:26 +0200 | [diff] [blame] | 2415 | * #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] | 2416 | * where \c key_type and \c key_bits are the type and bit-size |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2417 | * respectively of \p key. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2418 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2419 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2420 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2421 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2422 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2423 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2424 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2425 | */ |
| 2426 | psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key, |
| 2427 | psa_algorithm_t alg, |
| 2428 | const uint8_t *input, |
| 2429 | size_t input_length, |
| 2430 | const uint8_t *salt, |
| 2431 | size_t salt_length, |
| 2432 | uint8_t *output, |
| 2433 | size_t output_size, |
| 2434 | size_t *output_length); |
| 2435 | |
| 2436 | /** |
| 2437 | * \brief Decrypt a short message with a private key. |
| 2438 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2439 | * \param key Key slot containing an asymmetric key pair. |
| 2440 | * \param alg An asymmetric encryption algorithm that is |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2441 | * compatible with the type of \p key. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2442 | * \param[in] input The message to decrypt. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2443 | * \param input_length Size of the \p input buffer in bytes. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2444 | * \param[in] salt A salt or label, if supported by the |
| 2445 | * encryption algorithm. |
| 2446 | * If the algorithm does not support a |
| 2447 | * salt, pass \c NULL. |
| 2448 | * If the algorithm supports an optional |
| 2449 | * salt and you do not want to pass a salt, |
| 2450 | * pass \c NULL. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2451 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2452 | * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is |
| 2453 | * supported. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2454 | * \param salt_length Size of the \p salt buffer in bytes. |
| 2455 | * If \p salt is \c NULL, pass 0. |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2456 | * \param[out] output Buffer where the decrypted message is to |
| 2457 | * be written. |
| 2458 | * \param output_size Size of the \c output buffer in bytes. |
| 2459 | * \param[out] output_length On success, the number of bytes |
| 2460 | * that make up the returned output. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2461 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2462 | * \retval #PSA_SUCCESS |
| 2463 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2464 | * The size of the \p output buffer is too small. You can |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2465 | * determine a sufficient buffer size by calling |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 2466 | * #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] | 2467 | * where \c key_type and \c key_bits are the type and bit-size |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2468 | * respectively of \p key. |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2469 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2470 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2471 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2472 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2473 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2474 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2475 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
| 2476 | * \retval #PSA_ERROR_INVALID_PADDING |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 2477 | */ |
| 2478 | psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key, |
| 2479 | psa_algorithm_t alg, |
| 2480 | const uint8_t *input, |
| 2481 | size_t input_length, |
| 2482 | const uint8_t *salt, |
| 2483 | size_t salt_length, |
| 2484 | uint8_t *output, |
| 2485 | size_t output_size, |
| 2486 | size_t *output_length); |
| 2487 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 2488 | /**@}*/ |
| 2489 | |
Gilles Peskine | eab56e4 | 2018-07-12 17:12:33 +0200 | [diff] [blame] | 2490 | /** \defgroup generation Generators |
| 2491 | * @{ |
| 2492 | */ |
| 2493 | |
| 2494 | /** The type of the state data structure for generators. |
| 2495 | * |
| 2496 | * Before calling any function on a generator, the application must |
| 2497 | * initialize it by any of the following means: |
| 2498 | * - Set the structure to all-bits-zero, for example: |
| 2499 | * \code |
| 2500 | * psa_crypto_generator_t generator; |
| 2501 | * memset(&generator, 0, sizeof(generator)); |
| 2502 | * \endcode |
| 2503 | * - Initialize the structure to logical zero values, for example: |
| 2504 | * \code |
| 2505 | * psa_crypto_generator_t generator = {0}; |
| 2506 | * \endcode |
| 2507 | * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT, |
| 2508 | * for example: |
| 2509 | * \code |
| 2510 | * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 2511 | * \endcode |
| 2512 | * - Assign the result of the function psa_crypto_generator_init() |
| 2513 | * to the structure, for example: |
| 2514 | * \code |
| 2515 | * psa_crypto_generator_t generator; |
| 2516 | * generator = psa_crypto_generator_init(); |
| 2517 | * \endcode |
| 2518 | * |
| 2519 | * This is an implementation-defined \c struct. Applications should not |
| 2520 | * make any assumptions about the content of this structure except |
| 2521 | * as directed by the documentation of a specific implementation. |
| 2522 | */ |
| 2523 | typedef struct psa_crypto_generator_s psa_crypto_generator_t; |
| 2524 | |
| 2525 | /** \def PSA_CRYPTO_GENERATOR_INIT |
| 2526 | * |
| 2527 | * This macro returns a suitable initializer for a generator object |
| 2528 | * of type #psa_crypto_generator_t. |
| 2529 | */ |
| 2530 | #ifdef __DOXYGEN_ONLY__ |
| 2531 | /* This is an example definition for documentation purposes. |
| 2532 | * Implementations should define a suitable value in `crypto_struct.h`. |
| 2533 | */ |
| 2534 | #define PSA_CRYPTO_GENERATOR_INIT {0} |
| 2535 | #endif |
| 2536 | |
| 2537 | /** Return an initial value for a generator object. |
| 2538 | */ |
| 2539 | static psa_crypto_generator_t psa_crypto_generator_init(void); |
| 2540 | |
| 2541 | /** Retrieve the current capacity of a generator. |
| 2542 | * |
| 2543 | * The capacity of a generator is the maximum number of bytes that it can |
| 2544 | * return. Reading *N* bytes from a generator reduces its capacity by *N*. |
| 2545 | * |
| 2546 | * \param[in] generator The generator to query. |
| 2547 | * \param[out] capacity On success, the capacity of the generator. |
| 2548 | * |
| 2549 | * \retval PSA_SUCCESS |
| 2550 | * \retval PSA_ERROR_BAD_STATE |
| 2551 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 2552 | */ |
| 2553 | psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator, |
| 2554 | size_t *capacity); |
| 2555 | |
| 2556 | /** Read some data from a generator. |
| 2557 | * |
| 2558 | * This function reads and returns a sequence of bytes from a generator. |
| 2559 | * The data that is read is discarded from the generator. The generator's |
| 2560 | * capacity is decreased by the number of bytes read. |
| 2561 | * |
| 2562 | * \param[in,out] generator The generator object to read from. |
| 2563 | * \param[out] output Buffer where the generator output will be |
| 2564 | * written. |
| 2565 | * \param output_length Number of bytes to output. |
| 2566 | * |
| 2567 | * \retval PSA_SUCCESS |
| 2568 | * \retval PSA_ERROR_INSUFFICIENT_CAPACITY |
| 2569 | * There were fewer than \p output_length bytes |
| 2570 | * in the generator. Note that in this case, no |
| 2571 | * output is written to the output buffer. |
| 2572 | * The generator's capacity is set to 0, thus |
| 2573 | * subsequent calls to this function will not |
| 2574 | * succeed, even with a smaller output buffer. |
| 2575 | * \retval PSA_ERROR_BAD_STATE |
| 2576 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 2577 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 2578 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 2579 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 2580 | */ |
| 2581 | psa_status_t psa_generator_read(psa_crypto_generator_t *generator, |
| 2582 | uint8_t *output, |
| 2583 | size_t output_length); |
| 2584 | |
| 2585 | /** Create a symmetric key from data read from a generator. |
| 2586 | * |
| 2587 | * This function reads a sequence of bytes from a generator and imports |
| 2588 | * these bytes as a key. |
| 2589 | * The data that is read is discarded from the generator. The generator's |
| 2590 | * capacity is decreased by the number of bytes read. |
| 2591 | * |
| 2592 | * This function is equivalent to calling #psa_generator_read and |
| 2593 | * passing the resulting output to #psa_import_key, but |
| 2594 | * if the implementation provides an isolation boundary then |
| 2595 | * the key material is not exposed outside the isolation boundary. |
| 2596 | * |
| 2597 | * \param key Slot where the key will be stored. This must be a |
| 2598 | * valid slot for a key of the chosen type. It must |
| 2599 | * be unoccupied. |
| 2600 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). |
| 2601 | * This must be a symmetric key type. |
| 2602 | * \param bits Key size in bits. |
| 2603 | * \param[in,out] generator The generator object to read from. |
| 2604 | * |
| 2605 | * \retval PSA_SUCCESS |
| 2606 | * Success. |
| 2607 | * \retval PSA_ERROR_INSUFFICIENT_CAPACITY |
| 2608 | * There were fewer than \p output_length bytes |
| 2609 | * in the generator. Note that in this case, no |
| 2610 | * output is written to the output buffer. |
| 2611 | * The generator's capacity is set to 0, thus |
| 2612 | * subsequent calls to this function will not |
| 2613 | * succeed, even with a smaller output buffer. |
| 2614 | * \retval PSA_ERROR_NOT_SUPPORTED |
| 2615 | * The key type or key size is not supported, either by the |
| 2616 | * implementation in general or in this particular slot. |
| 2617 | * \retval PSA_ERROR_BAD_STATE |
| 2618 | * \retval PSA_ERROR_INVALID_ARGUMENT |
| 2619 | * The key slot is invalid. |
| 2620 | * \retval PSA_ERROR_OCCUPIED_SLOT |
| 2621 | * There is already a key in the specified slot. |
| 2622 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 2623 | * \retval PSA_ERROR_INSUFFICIENT_STORAGE |
| 2624 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 2625 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 2626 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 2627 | */ |
| 2628 | psa_status_t psa_generator_import_key(psa_key_slot_t key, |
| 2629 | psa_key_type_t type, |
| 2630 | size_t bits, |
| 2631 | psa_crypto_generator_t *generator); |
| 2632 | |
| 2633 | /** Abort a generator. |
| 2634 | * |
| 2635 | * Once a generator has been aborted, its capacity is zero. |
| 2636 | * Aborting a generator frees all associated resources except for the |
| 2637 | * \c generator structure itself. |
| 2638 | * |
| 2639 | * This function may be called at any time as long as the generator |
| 2640 | * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to |
| 2641 | * psa_crypto_generator_init() or a zero value. In particular, it is valid |
| 2642 | * to call psa_generator_abort() twice, or to call psa_generator_abort() |
| 2643 | * on a generator that has not been set up. |
| 2644 | * |
| 2645 | * Once aborted, the generator object may be called. |
| 2646 | * |
| 2647 | * \param[in,out] generator The generator to abort. |
| 2648 | * |
| 2649 | * \retval PSA_SUCCESS |
| 2650 | * \retval PSA_ERROR_BAD_STATE |
| 2651 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 2652 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 2653 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 2654 | */ |
| 2655 | psa_status_t psa_generator_abort(psa_crypto_generator_t *generator); |
| 2656 | |
| 2657 | /**@}*/ |
| 2658 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2659 | /** \defgroup derivation Key derivation |
| 2660 | * @{ |
| 2661 | */ |
| 2662 | |
| 2663 | /** Set up a key derivation operation. |
| 2664 | * |
| 2665 | * A key derivation algorithm takes three inputs: a secret input \p key and |
| 2666 | * two non-secret inputs \p label and p salt. |
| 2667 | * The result of this function is a byte generator which can |
| 2668 | * be used to produce keys and other cryptographic material. |
| 2669 | * |
| 2670 | * The role of \p label and \p salt is as follows: |
Gilles Peskine | bef7f14 | 2018-07-12 17:22:21 +0200 | [diff] [blame^] | 2671 | * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step |
| 2672 | * and \p label is the info string used in the "expand" step. |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2673 | * |
| 2674 | * \param[in,out] generator The generator object to set up. It must |
| 2675 | * have been initialized to . |
| 2676 | * \param key Slot containing the secret key to use. |
| 2677 | * \param alg The key derivation algorithm to compute |
| 2678 | * (\c PSA_ALG_XXX value such that |
| 2679 | * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true). |
| 2680 | * \param[in] salt Salt to use. |
| 2681 | * \param salt_length Size of the \p salt buffer in bytes. |
| 2682 | * \param[in] label Label to use. |
| 2683 | * \param label_length Size of the \p label buffer in bytes. |
| 2684 | * \param capacity The maximum number of bytes that the |
| 2685 | * generator will be able to provide. |
| 2686 | * |
| 2687 | * \retval #PSA_SUCCESS |
| 2688 | * Success. |
| 2689 | * \retval #PSA_ERROR_EMPTY_SLOT |
| 2690 | * \retval #PSA_ERROR_NOT_PERMITTED |
| 2691 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2692 | * \c key is not compatible with \c alg, |
| 2693 | * or \p capacity is too large for the specified algorithm and key. |
| 2694 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2695 | * \c alg is not supported or is not a key derivation algorithm. |
| 2696 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2697 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2698 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2699 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 2700 | */ |
| 2701 | psa_status_t psa_key_derivation(psa_crypto_generator_t *generator, |
| 2702 | psa_key_type_t key, |
| 2703 | psa_algorithm_t alg, |
| 2704 | const uint8_t *salt, |
| 2705 | size_t salt_length, |
| 2706 | const uint8_t *label, |
| 2707 | size_t label_length, |
| 2708 | size_t capacity); |
| 2709 | |
| 2710 | /**@}*/ |
| 2711 | |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2712 | /** \defgroup generation Key generation |
| 2713 | * @{ |
| 2714 | */ |
| 2715 | |
| 2716 | /** |
| 2717 | * \brief Generate random bytes. |
| 2718 | * |
| 2719 | * \warning This function **can** fail! Callers MUST check the return status |
| 2720 | * and MUST NOT use the content of the output buffer if the return |
| 2721 | * status is not #PSA_SUCCESS. |
| 2722 | * |
| 2723 | * \note To generate a key, use psa_generate_key() instead. |
| 2724 | * |
Gilles Peskine | edd11a1 | 2018-07-12 01:08:58 +0200 | [diff] [blame] | 2725 | * \param[out] output Output buffer for the generated data. |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2726 | * \param output_size Number of bytes to generate and output. |
| 2727 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2728 | * \retval #PSA_SUCCESS |
| 2729 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2730 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
| 2731 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2732 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2733 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2734 | */ |
| 2735 | psa_status_t psa_generate_random(uint8_t *output, |
| 2736 | size_t output_size); |
| 2737 | |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 2738 | /** Extra parameters for RSA key generation. |
| 2739 | * |
Gilles Peskine | be42f31 | 2018-07-13 14:38:15 +0200 | [diff] [blame] | 2740 | * You may pass a pointer to a structure of this type as the \c extra |
Gilles Peskine | 4c317f4 | 2018-07-12 01:24:09 +0200 | [diff] [blame] | 2741 | * parameter to psa_generate_key(). |
| 2742 | */ |
| 2743 | typedef struct { |
| 2744 | uint32_t e; /**! Public exponent value. Default: 65537. */ |
| 2745 | } psa_generate_key_extra_rsa; |
| 2746 | |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2747 | /** |
| 2748 | * \brief Generate a key or key pair. |
| 2749 | * |
Gilles Peskine | 4e69d7a | 2018-06-19 20:19:14 +0200 | [diff] [blame] | 2750 | * \param key Slot where the key will be stored. This must be a |
| 2751 | * valid slot for a key of the chosen type. It must |
| 2752 | * be unoccupied. |
| 2753 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). |
| 2754 | * \param bits Key size in bits. |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 2755 | * \param[in] extra Extra parameters for key generation. The |
Gilles Peskine | 4e69d7a | 2018-06-19 20:19:14 +0200 | [diff] [blame] | 2756 | * interpretation of this parameter depends on |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2757 | * \p type. All types support \c NULL to use |
Gilles Peskine | 3fa675c | 2018-07-12 01:31:03 +0200 | [diff] [blame] | 2758 | * default parameters. Implementation that support |
| 2759 | * the generation of vendor-specific key types |
| 2760 | * that allow extra parameters shall document |
| 2761 | * the format of these extra parameters and |
| 2762 | * the default values. For standard parameters, |
| 2763 | * the meaning of \p extra is as follows: |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2764 | * - For a symmetric key type (a type such |
Gilles Peskine | 3fa675c | 2018-07-12 01:31:03 +0200 | [diff] [blame] | 2765 | * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is |
| 2766 | * false), \p extra must be \c NULL. |
Gilles Peskine | fa4070c | 2018-07-12 19:23:03 +0200 | [diff] [blame] | 2767 | * - For an elliptic curve key type (a type |
Gilles Peskine | 3fa675c | 2018-07-12 01:31:03 +0200 | [diff] [blame] | 2768 | * such that #PSA_KEY_TYPE_IS_ECC(\p type) is |
| 2769 | * false), \p extra must be \c NULL. |
Gilles Peskine | dda3bd3 | 2018-07-12 19:40:46 +0200 | [diff] [blame] | 2770 | * - For an RSA key (\p type is |
| 2771 | * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an |
| 2772 | * optional #psa_generate_key_extra_rsa structure |
Gilles Peskine | 3fa675c | 2018-07-12 01:31:03 +0200 | [diff] [blame] | 2773 | * specifying the public exponent. The |
| 2774 | * default public exponent used when \p extra |
| 2775 | * is \c NULL is 65537. |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 2776 | * \param extra_size Size of the buffer that \p extra |
| 2777 | * points to, in bytes. Note that if \p extra is |
| 2778 | * \c NULL then \p extra_size must be zero. |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2779 | * |
Gilles Peskine | 2853849 | 2018-07-11 17:34:00 +0200 | [diff] [blame] | 2780 | * \retval #PSA_SUCCESS |
| 2781 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 2782 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 2783 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 2784 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
| 2785 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 2786 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 2787 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2788 | */ |
| 2789 | psa_status_t psa_generate_key(psa_key_slot_t key, |
| 2790 | psa_key_type_t type, |
| 2791 | size_t bits, |
Gilles Peskine | 53d991e | 2018-07-12 01:14:59 +0200 | [diff] [blame] | 2792 | const void *extra, |
| 2793 | size_t extra_size); |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 2794 | |
| 2795 | /**@}*/ |
| 2796 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2797 | #ifdef __cplusplus |
| 2798 | } |
| 2799 | #endif |
| 2800 | |
Gilles Peskine | 0cad07c | 2018-06-27 19:49:02 +0200 | [diff] [blame] | 2801 | /* The file "crypto_sizes.h" contains definitions for size calculation |
| 2802 | * macros whose definitions are implementation-specific. */ |
| 2803 | #include "crypto_sizes.h" |
| 2804 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 2805 | /* The file "crypto_struct.h" contains definitions for |
| 2806 | * implementation-specific structs that are declared above. */ |
| 2807 | #include "crypto_struct.h" |
| 2808 | |
| 2809 | /* The file "crypto_extra.h" contains vendor-specific definitions. This |
| 2810 | * can include vendor-defined algorithms, extra functions, etc. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2811 | #include "crypto_extra.h" |
| 2812 | |
| 2813 | #endif /* PSA_CRYPTO_H */ |