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