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