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