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 | * |
| 100 | * Applications can call the `PSA_xxx_SIZE` macro listed in the function |
| 101 | * description to determine a sufficient buffer size. |
| 102 | * |
| 103 | * Implementations should preferably return this error code only |
| 104 | * in cases when performing the operation with a larger output |
| 105 | * buffer would succeed. However implementations may return this |
| 106 | * error if a function has invalid or unsupported parameters in addition |
| 107 | * to the parameters that determine the necessary output buffer size. */ |
| 108 | #define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)3) |
| 109 | |
| 110 | /** A slot is occupied, but must be empty to carry out the |
| 111 | * requested action. |
| 112 | * |
| 113 | * If the slot number is invalid (i.e. the requested action could |
| 114 | * not be performed even after erasing the slot's content), |
| 115 | * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */ |
| 116 | #define PSA_ERROR_OCCUPIED_SLOT ((psa_status_t)4) |
| 117 | |
| 118 | /** A slot is empty, but must be occupied to carry out the |
| 119 | * requested action. |
| 120 | * |
| 121 | * If the slot number is invalid (i.e. the requested action could |
| 122 | * not be performed even after creating appropriate content in the slot), |
| 123 | * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */ |
| 124 | #define PSA_ERROR_EMPTY_SLOT ((psa_status_t)5) |
| 125 | |
| 126 | /** The requested action cannot be performed in the current state. |
| 127 | * |
| 128 | * Multipart operations return this error when one of the |
| 129 | * functions is called out of sequence. Refer to the function |
| 130 | * descriptions for permitted sequencing of functions. |
| 131 | * |
| 132 | * Implementations shall not return this error code to indicate |
| 133 | * that a key slot is occupied when it needs to be free or vice versa, |
| 134 | * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT |
| 135 | * as applicable. */ |
| 136 | #define PSA_ERROR_BAD_STATE ((psa_status_t)6) |
| 137 | |
| 138 | /** The parameters passed to the function are invalid. |
| 139 | * |
| 140 | * Implementations may return this error any time a parameter or |
| 141 | * combination of parameters are recognized as invalid. |
| 142 | * |
| 143 | * Implementations shall not return this error code to indicate |
| 144 | * that a key slot is occupied when it needs to be free or vice versa, |
| 145 | * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT |
| 146 | * as applicable. */ |
| 147 | #define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)7) |
| 148 | |
| 149 | /** There is not enough runtime memory. |
| 150 | * |
| 151 | * If the action is carried out across multiple security realms, this |
| 152 | * error can refer to available memory in any of the security realms. */ |
| 153 | #define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)8) |
| 154 | |
| 155 | /** There is not enough persistent storage. |
| 156 | * |
| 157 | * Functions that modify the key storage return this error code if |
| 158 | * there is insufficient storage space on the host media. In addition, |
| 159 | * many functions that do not otherwise access storage may return this |
| 160 | * error code if the implementation requires a mandatory log entry for |
| 161 | * the requested action and the log storage space is full. */ |
| 162 | #define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)9) |
| 163 | |
| 164 | /** There was a communication failure inside the implementation. |
| 165 | * |
| 166 | * This can indicate a communication failure between the application |
| 167 | * and an external cryptoprocessor or between the cryptoprocessor and |
| 168 | * an external volatile or persistent memory. A communication failure |
| 169 | * may be transient or permanent depending on the cause. |
| 170 | * |
| 171 | * \warning If a function returns this error, it is undetermined |
| 172 | * whether the requested action has completed or not. Implementations |
| 173 | * should return #PSA_SUCCESS on successful completion whenver |
| 174 | * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE |
| 175 | * if the requested action was completed successfully in an external |
| 176 | * cryptoprocessor but there was a breakdown of communication before |
| 177 | * the cryptoprocessor could report the status to the application. |
| 178 | */ |
| 179 | #define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)10) |
| 180 | |
| 181 | /** There was a storage failure that may have led to data loss. |
| 182 | * |
| 183 | * This error indicates that some persistent storage is corrupted. |
| 184 | * It should not be used for a corruption of volatile memory |
| 185 | * (use #PSA_ERROR_TAMPERING_DETECTED), for a communication error |
| 186 | * between the cryptoprocessor and its external storage (use |
| 187 | * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is |
| 188 | * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE). |
| 189 | * |
| 190 | * Note that a storage failure does not indicate that any data that was |
| 191 | * previously read is invalid. However this previously read data may no |
| 192 | * longer be readable from storage. |
| 193 | * |
| 194 | * When a storage failure occurs, it is no longer possible to ensure |
| 195 | * the global integrity of the keystore. Depending on the global |
| 196 | * integrity guarantees offered by the implementation, access to other |
| 197 | * data may or may not fail even if the data is still readable but |
| 198 | * its integrity canont be guaranteed. |
| 199 | * |
| 200 | * Implementations should only use this error code to report a |
| 201 | * permanent storage corruption. However application writers should |
| 202 | * keep in mind that transient errors while reading the storage may be |
| 203 | * reported using this error code. */ |
| 204 | #define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)11) |
| 205 | |
| 206 | /** A hardware failure was detected. |
| 207 | * |
| 208 | * A hardware failure may be transient or permanent depending on the |
| 209 | * cause. */ |
| 210 | #define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)12) |
| 211 | |
| 212 | /** A tampering attempt was detected. |
| 213 | * |
| 214 | * If an application receives this error code, there is no guarantee |
| 215 | * that previously accessed or computed data was correct and remains |
| 216 | * confidential. Applications should not perform any security function |
| 217 | * and should enter a safe failure state. |
| 218 | * |
| 219 | * Implementations may return this error code if they detect an invalid |
| 220 | * state that cannot happen during normal operation and that indicates |
| 221 | * that the implementation's security guarantees no longer hold. Depending |
| 222 | * on the implementation architecture and on its security and safety goals, |
| 223 | * the implementation may forcibly terminate the application. |
| 224 | * |
| 225 | * This error code is intended as a last resort when a security breach |
| 226 | * is detected and it is unsure whether the keystore data is still |
| 227 | * protected. Implementations shall only return this error code |
| 228 | * to report an alarm from a tampering detector, to indicate that |
| 229 | * the confidentiality of stored data can no longer be guaranteed, |
| 230 | * or to indicate that the integrity of previously returned data is now |
| 231 | * considered compromised. Implementations shall not use this error code |
| 232 | * to indicate a hardware failure that merely makes it impossible to |
| 233 | * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE, |
| 234 | * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE, |
| 235 | * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code |
| 236 | * instead). |
| 237 | * |
| 238 | * This error indicates an attack against the application. Implementations |
| 239 | * shall not return this error code as a consequence of the behavior of |
| 240 | * the application itself. */ |
| 241 | #define PSA_ERROR_TAMPERING_DETECTED ((psa_status_t)13) |
| 242 | |
| 243 | /** There is not enough entropy to generate random data needed |
| 244 | * for the requested action. |
| 245 | * |
| 246 | * This error indicates a failure of a hardware random generator. |
| 247 | * Application writers should note that this error can be returned not |
| 248 | * only by functions whose purpose is to generate random data, such |
| 249 | * as key, IV or nonce generation, but also by functions that execute |
| 250 | * an algorithm with a randomized result, as well as functions that |
| 251 | * use randomization of intermediate computations as a countermeasure |
| 252 | * to certain attacks. |
| 253 | * |
| 254 | * Implementations should avoid returning this error after psa_crypto_init() |
| 255 | * has succeeded. Implementations should generate sufficient |
| 256 | * entropy during initialization and subsequently use a cryptographically |
| 257 | * secure pseudorandom generator (PRNG). However implementations may return |
| 258 | * this error at any time if a policy requires the PRNG to be reseeded |
| 259 | * during normal operation. */ |
| 260 | #define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)14) |
| 261 | |
| 262 | /** The signature, MAC or hash is incorrect. |
| 263 | * |
| 264 | * Verification functions return this error if the verification |
| 265 | * calculations completed successfully, and the value to be verified |
| 266 | * was determined to be incorrect. |
| 267 | * |
| 268 | * If the value to verify has an invalid size, implementations may return |
| 269 | * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */ |
| 270 | #define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)15) |
| 271 | |
| 272 | /** The decrypted padding is incorrect. |
| 273 | * |
| 274 | * \warning In some protocols, when decrypting data, it is essential that |
| 275 | * the behavior of the application does not depend on whether the padding |
| 276 | * is correct, down to precise timing. Applications should prefer |
| 277 | * protocols that use authenticated encryption rather than plain |
| 278 | * encryption. If the application must perform a decryption of |
| 279 | * unauthenticated data, the application writer should take care not |
| 280 | * to reveal whether the padding is invalid. |
| 281 | * |
| 282 | * Implementations should strive to make valid and invalid padding |
| 283 | * as close as possible to indistinguishable to an external observer. |
| 284 | * In particular, the timing of a decryption operation should not |
| 285 | * depend on the validity of the padding. */ |
| 286 | #define PSA_ERROR_INVALID_PADDING ((psa_status_t)16) |
| 287 | |
| 288 | /** An error occurred that does not correspond to any defined |
| 289 | * failure cause. |
| 290 | * |
| 291 | * Implementations may use this error code if none of the other standard |
| 292 | * error codes are applicable. */ |
| 293 | #define PSA_ERROR_UNKNOWN_ERROR ((psa_status_t)17) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 294 | |
| 295 | /** |
| 296 | * \brief Library initialization. |
| 297 | * |
| 298 | * Applications must call this function before calling any other |
| 299 | * function in this module. |
| 300 | * |
| 301 | * Applications may call this function more than once. Once a call |
| 302 | * succeeds, subsequent calls are guaranteed to succeed. |
| 303 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 304 | * \retval PSA_SUCCESS |
| 305 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 306 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 307 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 308 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 309 | * \retval PSA_ERROR_INSUFFICIENT_ENTROPY |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 310 | */ |
| 311 | psa_status_t psa_crypto_init(void); |
| 312 | |
Gilles Peskine | 2905a7a | 2018-03-07 16:39:31 +0100 | [diff] [blame] | 313 | #define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8) |
| 314 | #define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8) |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 315 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 316 | /**@}*/ |
| 317 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 318 | /** \defgroup crypto_types Key and algorithm types |
| 319 | * @{ |
| 320 | */ |
| 321 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 322 | /** \brief Encoding of a key type. |
| 323 | */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 324 | typedef uint32_t psa_key_type_t; |
| 325 | |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 326 | /** An invalid key type value. |
| 327 | * |
| 328 | * Zero is not the encoding of any key type. |
| 329 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 330 | #define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000) |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 331 | |
| 332 | /** Vendor-defined flag |
| 333 | * |
| 334 | * Key types defined by this standard will never have the |
| 335 | * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types |
| 336 | * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should |
| 337 | * respect the bitwise structure used by standard encodings whenever practical. |
| 338 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 339 | #define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 340 | |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 341 | #define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x7e000000) |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 342 | /** Raw data. |
| 343 | * |
| 344 | * A "key" of this type cannot be used for any cryptographic operation. |
| 345 | * Applications may use this type to store arbitrary data in the keystore. */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 346 | #define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x02000000) |
| 347 | #define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x04000000) |
| 348 | #define PSA_KEY_TYPE_CATEGORY_ASYMMETRIC ((psa_key_type_t)0x06000000) |
| 349 | #define PSA_KEY_TYPE_PAIR_FLAG ((psa_key_type_t)0x01000000) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 350 | |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 351 | /** HMAC key. |
| 352 | * |
| 353 | * The key policy determines which underlying hash algorithm the key can be |
| 354 | * used for. |
| 355 | * |
| 356 | * HMAC keys should generally have the same size as the underlying hash. |
| 357 | * This size can be calculated with `PSA_HASH_SIZE(alg)` where |
| 358 | * `alg` is the HMAC algorithm or the underlying hash algorithm. */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 359 | #define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x02000001) |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 360 | /** Key for an cipher, AEAD or MAC algorithm based on the AES block cipher. |
| 361 | * |
| 362 | * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or |
| 363 | * 32 bytes (AES-256). |
| 364 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 365 | #define PSA_KEY_TYPE_AES ((psa_key_type_t)0x04000001) |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 366 | /** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES). |
| 367 | * |
| 368 | * The size of the key can be 8 bytes (single DES), 16 bytes (2-key 3DES) or |
| 369 | * 24 bytes (3-key 3DES). |
| 370 | * |
| 371 | * Note that single DES and 2-key 3DES are weak and strongly |
| 372 | * deprecated and should only be used to decrypt legacy data. 3-key 3DES |
| 373 | * is weak and deprecated and should only be used in legacy protocols. |
| 374 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 375 | #define PSA_KEY_TYPE_DES ((psa_key_type_t)0x04000002) |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 376 | /** Key for an cipher, AEAD or MAC algorithm based on the |
| 377 | * Camellia block cipher. */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 378 | #define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x04000003) |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 379 | /** Key for the RC4 stream cipher. |
| 380 | * |
| 381 | * Note that RC4 is weak and deprecated and should only be used in |
| 382 | * legacy protocols. */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 383 | #define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x04000004) |
| 384 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 385 | /** RSA public key. */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 386 | #define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x06010000) |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 387 | /** RSA key pair (private and public key). */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 388 | #define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x07010000) |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 389 | /** DSA public key. */ |
| 390 | #define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x06020000) |
| 391 | /** DSA key pair (private and public key). */ |
| 392 | #define PSA_KEY_TYPE_DSA_KEYPAIR ((psa_key_type_t)0x07020000) |
| 393 | #define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x06030000) |
| 394 | #define PSA_KEY_TYPE_ECC_KEYPAIR_BASE ((psa_key_type_t)0x07030000) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 395 | #define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff) |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 396 | #define PSA_KEY_TYPE_ECC_KEYPAIR(curve) \ |
| 397 | (PSA_KEY_TYPE_ECC_KEYPAIR_BASE | (curve)) |
| 398 | #define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \ |
| 399 | (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve)) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 400 | |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 401 | /** Whether a key type is vendor-defined. */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 402 | #define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 403 | (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0) |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 404 | |
| 405 | /** 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] | 406 | #define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \ |
| 407 | (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_ASYMMETRIC) |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 408 | /** Whether a key type is the public part of a key pair. */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 409 | #define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \ |
Moran Peker | b4d0ddd | 2018-04-04 12:47:52 +0300 | [diff] [blame] | 410 | (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \ |
| 411 | PSA_KEY_TYPE_CATEGORY_ASYMMETRIC) |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 412 | /** Whether a key type is a key pair containing a private part and a public |
| 413 | * part. */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 414 | #define PSA_KEY_TYPE_IS_KEYPAIR(type) \ |
| 415 | (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \ |
| 416 | (PSA_KEY_TYPE_CATEGORY_ASYMMETRIC | PSA_KEY_TYPE_PAIR_FLAG)) |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 417 | /** Whether a key type is an RSA key pair or public key. */ |
| 418 | /** The key pair type corresponding to a public key type. */ |
| 419 | #define PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY(type) \ |
| 420 | ((type) | PSA_KEY_TYPE_PAIR_FLAG) |
| 421 | /** The public key type corresponding to a key pair type. */ |
| 422 | #define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) \ |
| 423 | ((type) & ~PSA_KEY_TYPE_PAIR_FLAG) |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 424 | #define PSA_KEY_TYPE_IS_RSA(type) \ |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 425 | (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY) |
| 426 | /** 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] | 427 | #define PSA_KEY_TYPE_IS_ECC(type) \ |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 428 | ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \ |
| 429 | ~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] | 430 | |
Gilles Peskine | e1fed0d | 2018-06-18 20:45:45 +0200 | [diff] [blame] | 431 | /** The type of PSA elliptic curve identifiers. */ |
| 432 | typedef uint16_t psa_ecc_curve_t; |
| 433 | /** Extract the curve from an elliptic curve key type. */ |
| 434 | #define PSA_KEY_TYPE_GET_CURVE(type) \ |
| 435 | ((psa_ecc_curve_t) (PSA_KEY_TYPE_IS_ECC(type) ? \ |
| 436 | ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \ |
| 437 | 0)) |
| 438 | |
| 439 | /* The encoding of curve identifiers is currently aligned with the |
| 440 | * TLS Supported Groups Registry (formerly known as the |
| 441 | * TLS EC Named Curve Registry) |
| 442 | * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8 |
| 443 | * The values are defined by RFC 4492, RFC 7027 and RFC 7919. */ |
| 444 | #define PSA_ECC_CURVE_SECT163K1 ((psa_ecc_curve_t) 0x0001) |
| 445 | #define PSA_ECC_CURVE_SECT163R1 ((psa_ecc_curve_t) 0x0002) |
| 446 | #define PSA_ECC_CURVE_SECT163R2 ((psa_ecc_curve_t) 0x0003) |
| 447 | #define PSA_ECC_CURVE_SECT193R1 ((psa_ecc_curve_t) 0x0004) |
| 448 | #define PSA_ECC_CURVE_SECT193R2 ((psa_ecc_curve_t) 0x0005) |
| 449 | #define PSA_ECC_CURVE_SECT233K1 ((psa_ecc_curve_t) 0x0006) |
| 450 | #define PSA_ECC_CURVE_SECT233R1 ((psa_ecc_curve_t) 0x0007) |
| 451 | #define PSA_ECC_CURVE_SECT239K1 ((psa_ecc_curve_t) 0x0008) |
| 452 | #define PSA_ECC_CURVE_SECT283K1 ((psa_ecc_curve_t) 0x0009) |
| 453 | #define PSA_ECC_CURVE_SECT283R1 ((psa_ecc_curve_t) 0x000a) |
| 454 | #define PSA_ECC_CURVE_SECT409K1 ((psa_ecc_curve_t) 0x000b) |
| 455 | #define PSA_ECC_CURVE_SECT409R1 ((psa_ecc_curve_t) 0x000c) |
| 456 | #define PSA_ECC_CURVE_SECT571K1 ((psa_ecc_curve_t) 0x000d) |
| 457 | #define PSA_ECC_CURVE_SECT571R1 ((psa_ecc_curve_t) 0x000e) |
| 458 | #define PSA_ECC_CURVE_SECP160K1 ((psa_ecc_curve_t) 0x000f) |
| 459 | #define PSA_ECC_CURVE_SECP160R1 ((psa_ecc_curve_t) 0x0010) |
| 460 | #define PSA_ECC_CURVE_SECP160R2 ((psa_ecc_curve_t) 0x0011) |
| 461 | #define PSA_ECC_CURVE_SECP192K1 ((psa_ecc_curve_t) 0x0012) |
| 462 | #define PSA_ECC_CURVE_SECP192R1 ((psa_ecc_curve_t) 0x0013) |
| 463 | #define PSA_ECC_CURVE_SECP224K1 ((psa_ecc_curve_t) 0x0014) |
| 464 | #define PSA_ECC_CURVE_SECP224R1 ((psa_ecc_curve_t) 0x0015) |
| 465 | #define PSA_ECC_CURVE_SECP256K1 ((psa_ecc_curve_t) 0x0016) |
| 466 | #define PSA_ECC_CURVE_SECP256R1 ((psa_ecc_curve_t) 0x0017) |
| 467 | #define PSA_ECC_CURVE_SECP384R1 ((psa_ecc_curve_t) 0x0018) |
| 468 | #define PSA_ECC_CURVE_SECP521R1 ((psa_ecc_curve_t) 0x0019) |
| 469 | #define PSA_ECC_CURVE_BRAINPOOL_P256R1 ((psa_ecc_curve_t) 0x001a) |
| 470 | #define PSA_ECC_CURVE_BRAINPOOL_P384R1 ((psa_ecc_curve_t) 0x001b) |
| 471 | #define PSA_ECC_CURVE_BRAINPOOL_P512R1 ((psa_ecc_curve_t) 0x001c) |
| 472 | #define PSA_ECC_CURVE_CURVE25519 ((psa_ecc_curve_t) 0x001d) |
| 473 | #define PSA_ECC_CURVE_CURVE448 ((psa_ecc_curve_t) 0x001e) |
| 474 | #define PSA_ECC_CURVE_FFDHE_2048 ((psa_ecc_curve_t) 0x0100) |
| 475 | #define PSA_ECC_CURVE_FFDHE_3072 ((psa_ecc_curve_t) 0x0101) |
| 476 | #define PSA_ECC_CURVE_FFDHE_4096 ((psa_ecc_curve_t) 0x0102) |
| 477 | #define PSA_ECC_CURVE_FFDHE_6144 ((psa_ecc_curve_t) 0x0103) |
| 478 | #define PSA_ECC_CURVE_FFDHE_8192 ((psa_ecc_curve_t) 0x0104) |
| 479 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 480 | /** The block size of a block cipher. |
| 481 | * |
| 482 | * \param type A cipher key type (value of type #psa_key_type_t). |
| 483 | * |
| 484 | * \return The block size for a block cipher, or 1 for a stream cipher. |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 485 | * The return value is undefined if \c type is not a supported |
| 486 | * cipher key type. |
| 487 | * |
| 488 | * \note It is possible to build stream cipher algorithms on top of a block |
| 489 | * cipher, for example CTR mode (#PSA_ALG_CTR). |
| 490 | * This macro only takes the key type into account, so it cannot be |
| 491 | * used to determine the size of the data that #psa_cipher_update() |
| 492 | * might buffer for future processing in general. |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 493 | * |
| 494 | * \note This macro returns a compile-time constant if its argument is one. |
| 495 | * |
| 496 | * \warning This macro may evaluate its argument multiple times. |
| 497 | */ |
Gilles Peskine | 03182e9 | 2018-03-07 16:40:52 +0100 | [diff] [blame] | 498 | #define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 499 | ( \ |
| 500 | (type) == PSA_KEY_TYPE_AES ? 16 : \ |
| 501 | (type) == PSA_KEY_TYPE_DES ? 8 : \ |
| 502 | (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \ |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 503 | (type) == PSA_KEY_TYPE_ARC4 ? 1 : \ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 504 | 0) |
| 505 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 506 | /** \brief Encoding of a cryptographic algorithm. |
| 507 | * |
| 508 | * For algorithms that can be applied to multiple key types, this type |
| 509 | * does not encode the key type. For example, for symmetric ciphers |
| 510 | * based on a block cipher, #psa_algorithm_t encodes the block cipher |
| 511 | * mode and the padding mode while the block cipher itself is encoded |
| 512 | * via #psa_key_type_t. |
| 513 | */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 514 | typedef uint32_t psa_algorithm_t; |
| 515 | |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 516 | #define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000) |
| 517 | #define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000) |
| 518 | #define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000) |
| 519 | #define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000) |
| 520 | #define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000) |
| 521 | #define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000) |
| 522 | #define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000) |
| 523 | #define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000) |
| 524 | #define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000) |
| 525 | #define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 526 | |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 527 | #define PSA_ALG_IS_VENDOR_DEFINED(alg) \ |
| 528 | (((alg) & PSA_ALG_VENDOR_FLAG) != 0) |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 529 | /** Whether the specified algorithm is a hash algorithm. |
| 530 | * |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 531 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 532 | * |
| 533 | * \return 1 if \c alg is a hash algorithm, 0 otherwise. |
| 534 | * This macro may return either 0 or 1 if \c alg is not a valid |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 535 | * algorithm identifier. |
| 536 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 537 | #define PSA_ALG_IS_HASH(alg) \ |
| 538 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH) |
| 539 | #define PSA_ALG_IS_MAC(alg) \ |
| 540 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC) |
| 541 | #define PSA_ALG_IS_CIPHER(alg) \ |
| 542 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER) |
| 543 | #define PSA_ALG_IS_AEAD(alg) \ |
| 544 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD) |
| 545 | #define PSA_ALG_IS_SIGN(alg) \ |
| 546 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN) |
| 547 | #define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \ |
| 548 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION) |
| 549 | #define PSA_ALG_IS_KEY_AGREEMENT(alg) \ |
| 550 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT) |
| 551 | #define PSA_ALG_IS_KEY_DERIVATION(alg) \ |
| 552 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION) |
| 553 | |
| 554 | #define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff) |
| 555 | #define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001) |
| 556 | #define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002) |
| 557 | #define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003) |
Gilles Peskine | e3f694f | 2018-03-08 07:48:40 +0100 | [diff] [blame] | 558 | #define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004) |
| 559 | #define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 560 | #define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008) |
| 561 | #define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009) |
| 562 | #define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a) |
| 563 | #define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b) |
| 564 | #define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c) |
| 565 | #define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d) |
| 566 | #define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010) |
| 567 | #define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011) |
| 568 | #define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012) |
| 569 | #define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013) |
| 570 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 571 | #define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 572 | #define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000) |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 573 | /** Macro to build an HMAC algorithm. |
| 574 | * |
| 575 | * For example, `PSA_ALG_HMAC(PSA_ALG_SHA256)` is HMAC-SHA-256. |
| 576 | * |
| 577 | * \param alg A hash algorithm (\c PSA_ALG_XXX value such that |
| 578 | * #PSA_ALG_IS_HASH(alg) is true). |
| 579 | * |
| 580 | * \return The corresponding HMAC algorithm. |
| 581 | * \return Unspecified if \p alg is not a hash algorithm. |
| 582 | */ |
| 583 | #define PSA_ALG_HMAC(hash_alg) \ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 584 | (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 585 | #define PSA_ALG_HMAC_HASH(hmac_alg) \ |
| 586 | (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK)) |
| 587 | #define PSA_ALG_IS_HMAC(alg) \ |
| 588 | (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \ |
| 589 | PSA_ALG_HMAC_BASE) |
| 590 | #define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000) |
| 591 | #define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001) |
| 592 | #define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002) |
| 593 | #define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003) |
| 594 | #define PSA_ALG_IS_CIPHER_MAC(alg) \ |
| 595 | (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \ |
| 596 | PSA_ALG_CIPHER_MAC_BASE) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 597 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 598 | #define PSA_ALG_CIPHER_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000) |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 599 | #define PSA_ALG_BLOCK_CIPHER_BASE ((psa_algorithm_t)0x04000000) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 600 | #define PSA_ALG_BLOCK_CIPHER_MODE_MASK ((psa_algorithm_t)0x000000ff) |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 601 | #define PSA_ALG_BLOCK_CIPHER_PADDING_MASK ((psa_algorithm_t)0x003f0000) |
| 602 | #define PSA_ALG_BLOCK_CIPHER_PAD_NONE ((psa_algorithm_t)0x00000000) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 603 | #define PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ((psa_algorithm_t)0x00010000) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 604 | #define PSA_ALG_IS_BLOCK_CIPHER(alg) \ |
| 605 | (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \ |
| 606 | PSA_ALG_BLOCK_CIPHER_BASE) |
| 607 | |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 608 | #define PSA_ALG_CBC_BASE ((psa_algorithm_t)0x04000001) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 609 | #define PSA_ALG_CFB_BASE ((psa_algorithm_t)0x04000002) |
| 610 | #define PSA_ALG_OFB_BASE ((psa_algorithm_t)0x04000003) |
| 611 | #define PSA_ALG_XTS_BASE ((psa_algorithm_t)0x04000004) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 612 | #define PSA_ALG_STREAM_CIPHER ((psa_algorithm_t)0x04800000) |
| 613 | #define PSA_ALG_CTR ((psa_algorithm_t)0x04800001) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 614 | #define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800002) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 615 | |
Moran Peker | bed71a2 | 2018-04-22 20:19:20 +0300 | [diff] [blame] | 616 | #define PSA_ALG_IS_STREAM_CIPHER(alg) \ |
| 617 | (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \ |
| 618 | PSA_ALG_STREAM_CIPHER) |
| 619 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 620 | #define PSA_ALG_CCM ((psa_algorithm_t)0x06000001) |
| 621 | #define PSA_ALG_GCM ((psa_algorithm_t)0x06000002) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 622 | |
Gilles Peskine | a592623 | 2018-03-28 14:16:50 +0200 | [diff] [blame] | 623 | #define PSA_ALG_RSA_PKCS1V15_SIGN_RAW ((psa_algorithm_t)0x10010000) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 624 | #define PSA_ALG_RSA_PSS_MGF1 ((psa_algorithm_t)0x10020000) |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 625 | #define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12010000) |
| 626 | #define PSA_ALG_RSA_OAEP_MGF1_BASE ((psa_algorithm_t)0x12020000) |
Gilles Peskine | a592623 | 2018-03-28 14:16:50 +0200 | [diff] [blame] | 627 | #define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \ |
| 628 | (PSA_ALG_RSA_PKCS1V15_SIGN_RAW | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 629 | #define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \ |
Gilles Peskine | 9673cc8 | 2018-04-11 16:57:49 +0200 | [diff] [blame] | 630 | (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_RAW) |
| 631 | #define PSA_ALG_RSA_OAEP_MGF1(hash_alg) \ |
| 632 | (PSA_ALG_RSA_OAEP_MGF1_RAW | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 633 | #define PSA_ALG_IS_RSA_OAEP_MGF1(alg) \ |
Gilles Peskine | 625b01c | 2018-06-08 17:43:16 +0200 | [diff] [blame] | 634 | (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_MGF1_BASE) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 635 | #define PSA_ALG_RSA_GET_HASH(alg) \ |
| 636 | (((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 637 | |
Gilles Peskine | d1e8e41 | 2018-06-07 09:49:39 +0200 | [diff] [blame] | 638 | #define PSA_ALG_ECDSA_RAW ((psa_algorithm_t)0x10030000) |
| 639 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 640 | /**@}*/ |
| 641 | |
| 642 | /** \defgroup key_management Key management |
| 643 | * @{ |
| 644 | */ |
| 645 | |
| 646 | /** |
| 647 | * \brief Import a key in binary format. |
| 648 | * |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 649 | * This function supports any output from psa_export_key(). Refer to the |
| 650 | * documentation of psa_export_key() for the format for each key type. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 651 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 652 | * \param key Slot where the key will be stored. This must be a |
| 653 | * valid slot for a key of the chosen type. It must |
| 654 | * be unoccupied. |
| 655 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). |
| 656 | * \param data Buffer containing the key data. |
| 657 | * \param data_length Size of the \c data buffer in bytes. |
| 658 | * |
| 659 | * \retval PSA_SUCCESS |
| 660 | * Success. |
| 661 | * \retval PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 662 | * The key type or key size is not supported, either by the |
| 663 | * implementation in general or in this particular slot. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 664 | * \retval PSA_ERROR_INVALID_ARGUMENT |
| 665 | * The key slot is invalid, |
| 666 | * or the key data is not correctly formatted. |
| 667 | * \retval PSA_ERROR_OCCUPIED_SLOT |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 668 | * There is already a key in the specified slot. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 669 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 670 | * \retval PSA_ERROR_INSUFFICIENT_STORAGE |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 671 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 672 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 673 | * \retval PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 674 | */ |
| 675 | psa_status_t psa_import_key(psa_key_slot_t key, |
| 676 | psa_key_type_t type, |
| 677 | const uint8_t *data, |
| 678 | size_t data_length); |
| 679 | |
| 680 | /** |
Gilles Peskine | 154bd95 | 2018-04-19 08:38:16 +0200 | [diff] [blame] | 681 | * \brief Destroy a key and restore the slot to its default state. |
| 682 | * |
| 683 | * This function destroys the content of the key slot from both volatile |
| 684 | * memory and, if applicable, non-volatile storage. Implementations shall |
| 685 | * make a best effort to ensure that any previous content of the slot is |
| 686 | * unrecoverable. |
| 687 | * |
| 688 | * This function also erases any metadata such as policies. It returns the |
| 689 | * specified slot to its default state. |
| 690 | * |
| 691 | * \param key The key slot to erase. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 692 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 693 | * \retval PSA_SUCCESS |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 694 | * The slot's content, if any, has been erased. |
| 695 | * \retval PSA_ERROR_NOT_PERMITTED |
| 696 | * The slot holds content and cannot be erased because it is |
| 697 | * read-only, either due to a policy or due to physical restrictions. |
| 698 | * \retval PSA_ERROR_INVALID_ARGUMENT |
| 699 | * The specified slot number does not designate a valid slot. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 700 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 701 | * There was an failure in communication with the cryptoprocessor. |
| 702 | * The key material may still be present in the cryptoprocessor. |
| 703 | * \retval PSA_ERROR_STORAGE_FAILURE |
| 704 | * The storage is corrupted. Implementations shall make a best effort |
| 705 | * to erase key material even in this stage, however applications |
| 706 | * should be aware that it may be impossible to guarantee that the |
| 707 | * key material is not recoverable in such cases. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 708 | * \retval PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 65eb858 | 2018-04-19 08:28:58 +0200 | [diff] [blame] | 709 | * An unexpected condition which is not a storage corruption or |
| 710 | * a communication failure occurred. The cryptoprocessor may have |
| 711 | * been compromised. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 712 | */ |
| 713 | psa_status_t psa_destroy_key(psa_key_slot_t key); |
| 714 | |
| 715 | /** |
| 716 | * \brief Get basic metadata about a key. |
| 717 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 718 | * \param key Slot whose content is queried. This must |
| 719 | * be an occupied key slot. |
| 720 | * \param type On success, the key type (a \c PSA_KEY_TYPE_XXX value). |
| 721 | * This may be a null pointer, in which case the key type |
| 722 | * is not written. |
| 723 | * \param bits On success, the key size in bits. |
Gilles Peskine | 9a1ba0d | 2018-03-21 20:49:16 +0100 | [diff] [blame] | 724 | * This may be a null pointer, in which case the key size |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 725 | * is not written. |
| 726 | * |
| 727 | * \retval PSA_SUCCESS |
| 728 | * \retval PSA_ERROR_EMPTY_SLOT |
| 729 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 730 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 731 | * \retval PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 732 | */ |
| 733 | psa_status_t psa_get_key_information(psa_key_slot_t key, |
| 734 | psa_key_type_t *type, |
| 735 | size_t *bits); |
| 736 | |
| 737 | /** |
| 738 | * \brief Export a key in binary format. |
| 739 | * |
| 740 | * The output of this function can be passed to psa_import_key() to |
| 741 | * create an equivalent object. |
| 742 | * |
| 743 | * If a key is created with psa_import_key() and then exported with |
| 744 | * this function, it is not guaranteed that the resulting data is |
| 745 | * identical: the implementation may choose a different representation |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 746 | * of the same key if the format permits it. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 747 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 748 | * For standard key types, the output format is as follows: |
| 749 | * |
| 750 | * - For symmetric keys (including MAC keys), the format is the |
| 751 | * raw bytes of the key. |
| 752 | * - For DES, the key data consists of 8 bytes. The parity bits must be |
| 753 | * correct. |
| 754 | * - For Triple-DES, the format is the concatenation of the |
| 755 | * two or three DES keys. |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 756 | * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 757 | * is the non-encrypted DER representation defined by PKCS\#8 (RFC 5208) |
| 758 | * as PrivateKeyInfo. |
| 759 | * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format |
Gilles Peskine | 971f706 | 2018-03-20 17:52:58 +0100 | [diff] [blame] | 760 | * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 761 | * |
| 762 | * \param key Slot whose content is to be exported. This must |
| 763 | * be an occupied key slot. |
| 764 | * \param data Buffer where the key data is to be written. |
| 765 | * \param data_size Size of the \c data buffer in bytes. |
| 766 | * \param data_length On success, the number of bytes |
| 767 | * that make up the key data. |
| 768 | * |
| 769 | * \retval PSA_SUCCESS |
| 770 | * \retval PSA_ERROR_EMPTY_SLOT |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 771 | * \retval PSA_ERROR_NOT_PERMITTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 772 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 773 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 774 | * \retval PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 775 | */ |
| 776 | psa_status_t psa_export_key(psa_key_slot_t key, |
| 777 | uint8_t *data, |
| 778 | size_t data_size, |
| 779 | size_t *data_length); |
| 780 | |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 781 | /** |
| 782 | * \brief Export a public key or the public part of a key pair in binary format. |
| 783 | * |
| 784 | * The output of this function can be passed to psa_import_key() to |
| 785 | * create an object that is equivalent to the public key. |
| 786 | * |
| 787 | * For standard key types, the output format is as follows: |
| 788 | * |
| 789 | * - 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] | 790 | * 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] | 791 | * as SubjectPublicKeyInfo. |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 792 | * |
| 793 | * \param key Slot whose content is to be exported. This must |
| 794 | * be an occupied key slot. |
| 795 | * \param data Buffer where the key data is to be written. |
| 796 | * \param data_size Size of the \c data buffer in bytes. |
| 797 | * \param data_length On success, the number of bytes |
| 798 | * that make up the key data. |
| 799 | * |
| 800 | * \retval PSA_SUCCESS |
| 801 | * \retval PSA_ERROR_EMPTY_SLOT |
| 802 | * \retval PSA_ERROR_INVALID_ARGUMENT |
| 803 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 804 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 805 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 806 | */ |
| 807 | psa_status_t psa_export_public_key(psa_key_slot_t key, |
| 808 | uint8_t *data, |
| 809 | size_t data_size, |
| 810 | size_t *data_length); |
| 811 | |
| 812 | /**@}*/ |
| 813 | |
| 814 | /** \defgroup policy Key policies |
| 815 | * @{ |
| 816 | */ |
| 817 | |
| 818 | /** \brief Encoding of permitted usage on a key. */ |
| 819 | typedef uint32_t psa_key_usage_t; |
| 820 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 821 | /** Whether the key may be exported. |
| 822 | * |
| 823 | * A public key or the public part of a key pair may always be exported |
| 824 | * regardless of the value of this permission flag. |
| 825 | * |
| 826 | * If a key does not have export permission, implementations shall not |
| 827 | * allow the key to be exported in plain form from the cryptoprocessor, |
| 828 | * whether through psa_export_key() or through a proprietary interface. |
| 829 | * The key may however be exportable in a wrapped form, i.e. in a form |
| 830 | * where it is encrypted by another key. |
| 831 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 832 | #define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001) |
| 833 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 834 | /** Whether the key may be used to encrypt a message. |
| 835 | * |
| 836 | * For a key pair, this concerns the public key. |
| 837 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 838 | #define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100) |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 839 | |
| 840 | /** Whether the key may be used to decrypt a message. |
| 841 | * |
| 842 | * For a key pair, this concerns the private key. |
| 843 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 844 | #define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200) |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 845 | |
| 846 | /** Whether the key may be used to sign a message. |
| 847 | * |
| 848 | * For a key pair, this concerns the private key. |
| 849 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 850 | #define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400) |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 851 | |
| 852 | /** Whether the key may be used to verify a message signature. |
| 853 | * |
| 854 | * For a key pair, this concerns the public key. |
| 855 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 856 | #define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800) |
| 857 | |
| 858 | /** The type of the key policy data structure. |
| 859 | * |
| 860 | * This is an implementation-defined \c struct. Applications should not |
| 861 | * make any assumptions about the content of this structure except |
| 862 | * as directed by the documentation of a specific implementation. */ |
| 863 | typedef struct psa_key_policy_s psa_key_policy_t; |
| 864 | |
| 865 | /** \brief Initialize a key policy structure to a default that forbids all |
| 866 | * usage of the key. */ |
| 867 | void psa_key_policy_init(psa_key_policy_t *policy); |
| 868 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 869 | /** \brief Set the standard fields of a policy structure. |
| 870 | * |
| 871 | * Note that this function does not make any consistency check of the |
| 872 | * parameters. The values are only checked when applying the policy to |
| 873 | * a key slot with psa_set_key_policy(). |
| 874 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 875 | void psa_key_policy_set_usage(psa_key_policy_t *policy, |
| 876 | psa_key_usage_t usage, |
| 877 | psa_algorithm_t alg); |
| 878 | |
| 879 | psa_key_usage_t psa_key_policy_get_usage(psa_key_policy_t *policy); |
| 880 | |
| 881 | psa_algorithm_t psa_key_policy_get_algorithm(psa_key_policy_t *policy); |
| 882 | |
| 883 | /** \brief Set the usage policy on a key slot. |
| 884 | * |
| 885 | * This function must be called on an empty key slot, before importing, |
| 886 | * generating or creating a key in the slot. Changing the policy of an |
| 887 | * existing key is not permitted. |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 888 | * |
| 889 | * Implementations may set restrictions on supported key policies |
| 890 | * depending on the key type and the key slot. |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 891 | */ |
| 892 | psa_status_t psa_set_key_policy(psa_key_slot_t key, |
| 893 | const psa_key_policy_t *policy); |
| 894 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 895 | /** \brief Get the usage policy for a key slot. |
| 896 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 897 | psa_status_t psa_get_key_policy(psa_key_slot_t key, |
| 898 | psa_key_policy_t *policy); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 899 | |
| 900 | /**@}*/ |
| 901 | |
Gilles Peskine | 609b6a5 | 2018-03-03 21:31:50 +0100 | [diff] [blame] | 902 | /** \defgroup persistence Key lifetime |
| 903 | * @{ |
| 904 | */ |
| 905 | |
| 906 | /** Encoding of key lifetimes. |
| 907 | */ |
| 908 | typedef uint32_t psa_key_lifetime_t; |
| 909 | |
| 910 | /** A volatile key slot retains its content as long as the application is |
| 911 | * running. It is guaranteed to be erased on a power reset. |
| 912 | */ |
| 913 | #define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000) |
| 914 | |
| 915 | /** A persistent key slot retains its content as long as it is not explicitly |
| 916 | * destroyed. |
| 917 | */ |
| 918 | #define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001) |
| 919 | |
| 920 | /** A write-once key slot may not be modified once a key has been set. |
| 921 | * It will retain its content as long as the device remains operational. |
| 922 | */ |
| 923 | #define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff) |
| 924 | |
Gilles Peskine | d393e18 | 2018-03-08 07:49:16 +0100 | [diff] [blame] | 925 | /** \brief Retrieve the lifetime of a key slot. |
| 926 | * |
| 927 | * The assignment of lifetimes to slots is implementation-dependent. |
Gilles Peskine | 8ca5602 | 2018-04-17 14:07:59 +0200 | [diff] [blame] | 928 | * |
Gilles Peskine | 9bb53d7 | 2018-04-17 14:09:24 +0200 | [diff] [blame] | 929 | * \param key Slot to query. |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 930 | * \param lifetime On success, the lifetime value. |
Gilles Peskine | 8ca5602 | 2018-04-17 14:07:59 +0200 | [diff] [blame] | 931 | * |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 932 | * \retval PSA_SUCCESS |
| 933 | * Success. |
| 934 | * \retval PSA_ERROR_INVALID_ARGUMENT |
mohammad1603 | a7d245a | 2018-04-17 00:40:08 -0700 | [diff] [blame] | 935 | * The key slot is invalid. |
Gilles Peskine | f0c9dd3 | 2018-04-17 14:11:07 +0200 | [diff] [blame] | 936 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 937 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 938 | * \retval PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | d393e18 | 2018-03-08 07:49:16 +0100 | [diff] [blame] | 939 | */ |
Gilles Peskine | 609b6a5 | 2018-03-03 21:31:50 +0100 | [diff] [blame] | 940 | psa_status_t psa_get_key_lifetime(psa_key_slot_t key, |
| 941 | psa_key_lifetime_t *lifetime); |
| 942 | |
Gilles Peskine | d393e18 | 2018-03-08 07:49:16 +0100 | [diff] [blame] | 943 | /** \brief Change the lifetime of a key slot. |
| 944 | * |
| 945 | * 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] | 946 | * whether the lifetime of an occupied key slot can be changed, is |
Gilles Peskine | d393e18 | 2018-03-08 07:49:16 +0100 | [diff] [blame] | 947 | * implementation-dependent. |
Gilles Peskine | 8ca5602 | 2018-04-17 14:07:59 +0200 | [diff] [blame] | 948 | * |
Gilles Peskine | 9bb53d7 | 2018-04-17 14:09:24 +0200 | [diff] [blame] | 949 | * \param key Slot whose lifetime is to be changed. |
| 950 | * \param lifetime The lifetime value to set for the given key slot. |
Gilles Peskine | 8ca5602 | 2018-04-17 14:07:59 +0200 | [diff] [blame] | 951 | * |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 952 | * \retval PSA_SUCCESS |
| 953 | * Success. |
| 954 | * \retval PSA_ERROR_INVALID_ARGUMENT |
| 955 | * The key slot is invalid, |
mohammad1603 | a7d245a | 2018-04-17 00:40:08 -0700 | [diff] [blame] | 956 | * or the lifetime value is invalid. |
Gilles Peskine | f0c9dd3 | 2018-04-17 14:11:07 +0200 | [diff] [blame] | 957 | * \retval PSA_ERROR_NOT_SUPPORTED |
| 958 | * The implementation does not support the specified lifetime value, |
| 959 | * at least for the specified key slot. |
| 960 | * \retval PSA_ERROR_OCCUPIED_SLOT |
| 961 | * The slot contains a key, and the implementation does not support |
| 962 | * changing the lifetime of an occupied slot. |
| 963 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 964 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 965 | * \retval PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | d393e18 | 2018-03-08 07:49:16 +0100 | [diff] [blame] | 966 | */ |
| 967 | psa_status_t psa_set_key_lifetime(psa_key_slot_t key, |
mohammad1603 | ea05009 | 2018-04-17 00:31:34 -0700 | [diff] [blame] | 968 | psa_key_lifetime_t lifetime); |
Gilles Peskine | d393e18 | 2018-03-08 07:49:16 +0100 | [diff] [blame] | 969 | |
Gilles Peskine | 609b6a5 | 2018-03-03 21:31:50 +0100 | [diff] [blame] | 970 | /**@}*/ |
| 971 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 972 | /** \defgroup hash Message digests |
| 973 | * @{ |
| 974 | */ |
| 975 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 976 | /** The type of the state data structure for multipart hash operations. |
| 977 | * |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 978 | * This is an implementation-defined \c struct. Applications should not |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 979 | * make any assumptions about the content of this structure except |
| 980 | * as directed by the documentation of a specific implementation. */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 981 | typedef struct psa_hash_operation_s psa_hash_operation_t; |
| 982 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 983 | /** The size of the output of psa_hash_finish(), in bytes. |
| 984 | * |
| 985 | * This is also the hash size that psa_hash_verify() expects. |
| 986 | * |
| 987 | * \param alg A hash algorithm (\c PSA_ALG_XXX value such that |
Gilles Peskine | 3585596 | 2018-04-19 08:39:16 +0200 | [diff] [blame] | 988 | * #PSA_ALG_IS_HASH(alg) is true), or an HMAC algorithm |
| 989 | * (`PSA_ALG_HMAC(hash_alg)` where `hash_alg` is a |
| 990 | * hash algorithm). |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 991 | * |
| 992 | * \return The hash size for the specified hash algorithm. |
| 993 | * If the hash algorithm is not recognized, return 0. |
| 994 | * An implementation may return either 0 or the correct size |
| 995 | * for a hash algorithm that it recognizes, but does not support. |
| 996 | */ |
Gilles Peskine | 71bb7b7 | 2018-04-19 08:29:59 +0200 | [diff] [blame] | 997 | #define PSA_HASH_SIZE(alg) \ |
| 998 | ( \ |
| 999 | PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_MD2 ? 16 : \ |
| 1000 | PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_MD4 ? 16 : \ |
| 1001 | PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \ |
| 1002 | PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \ |
| 1003 | PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \ |
| 1004 | PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \ |
| 1005 | PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \ |
| 1006 | PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \ |
| 1007 | PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \ |
| 1008 | PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \ |
| 1009 | PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \ |
| 1010 | PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \ |
| 1011 | PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \ |
| 1012 | PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \ |
| 1013 | PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1014 | 0) |
| 1015 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1016 | /** Start a multipart hash operation. |
| 1017 | * |
| 1018 | * The sequence of operations to calculate a hash (message digest) |
| 1019 | * is as follows: |
| 1020 | * -# Allocate an operation object which will be passed to all the functions |
| 1021 | * listed here. |
| 1022 | * -# Call psa_hash_start() to specify the algorithm. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1023 | * -# Call psa_hash_update() zero, one or more times, passing a fragment |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1024 | * of the message each time. The hash that is calculated is the hash |
| 1025 | * of the concatenation of these messages in order. |
| 1026 | * -# To calculate the hash, call psa_hash_finish(). |
| 1027 | * To compare the hash with an expected value, call psa_hash_verify(). |
| 1028 | * |
| 1029 | * The application may call psa_hash_abort() at any time after the operation |
| 1030 | * has been initialized with psa_hash_start(). |
| 1031 | * |
| 1032 | * After a successful call to psa_hash_start(), the application must |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame] | 1033 | * eventually terminate the operation. The following events terminate an |
| 1034 | * operation: |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1035 | * - A failed call to psa_hash_update(). |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 1036 | * - 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] | 1037 | * |
Gilles Peskine | 36a74b7 | 2018-06-01 16:30:32 +0200 | [diff] [blame] | 1038 | * \param operation The operation object to use. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1039 | * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value |
| 1040 | * such that #PSA_ALG_IS_HASH(alg) is true). |
| 1041 | * |
| 1042 | * \retval PSA_SUCCESS |
| 1043 | * Success. |
| 1044 | * \retval PSA_ERROR_NOT_SUPPORTED |
| 1045 | * \c alg is not supported or is not a hash algorithm. |
| 1046 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 1047 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 1048 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 1049 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 1050 | */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1051 | psa_status_t psa_hash_start(psa_hash_operation_t *operation, |
| 1052 | psa_algorithm_t alg); |
| 1053 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1054 | /** Add a message fragment to a multipart hash operation. |
| 1055 | * |
| 1056 | * The application must call psa_hash_start() before calling this function. |
| 1057 | * |
| 1058 | * If this function returns an error status, the operation becomes inactive. |
| 1059 | * |
| 1060 | * \param operation Active hash operation. |
| 1061 | * \param input Buffer containing the message fragment to hash. |
| 1062 | * \param input_length Size of the \c input buffer in bytes. |
| 1063 | * |
| 1064 | * \retval PSA_SUCCESS |
| 1065 | * Success. |
| 1066 | * \retval PSA_ERROR_BAD_STATE |
| 1067 | * The operation state is not valid (not started, or already completed). |
| 1068 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 1069 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 1070 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 1071 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 1072 | */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1073 | psa_status_t psa_hash_update(psa_hash_operation_t *operation, |
| 1074 | const uint8_t *input, |
| 1075 | size_t input_length); |
| 1076 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1077 | /** Finish the calculation of the hash of a message. |
| 1078 | * |
| 1079 | * The application must call psa_hash_start() before calling this function. |
| 1080 | * This function calculates the hash of the message formed by concatenating |
| 1081 | * the inputs passed to preceding calls to psa_hash_update(). |
| 1082 | * |
| 1083 | * When this function returns, the operation becomes inactive. |
| 1084 | * |
| 1085 | * \warning Applications should not call this function if they expect |
| 1086 | * a specific value for the hash. Call psa_hash_verify() instead. |
| 1087 | * Beware that comparing integrity or authenticity data such as |
| 1088 | * hash values with a function such as \c memcmp is risky |
| 1089 | * because the time taken by the comparison may leak information |
| 1090 | * about the hashed data which could allow an attacker to guess |
| 1091 | * a valid hash and thereby bypass security controls. |
| 1092 | * |
| 1093 | * \param operation Active hash operation. |
| 1094 | * \param hash Buffer where the hash is to be written. |
| 1095 | * \param hash_size Size of the \c hash buffer in bytes. |
| 1096 | * \param hash_length On success, the number of bytes |
| 1097 | * that make up the hash value. This is always |
Gilles Peskine | 71bb7b7 | 2018-04-19 08:29:59 +0200 | [diff] [blame] | 1098 | * #PSA_HASH_SIZE(alg) where \c alg is the |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1099 | * hash algorithm that is calculated. |
| 1100 | * |
| 1101 | * \retval PSA_SUCCESS |
| 1102 | * Success. |
| 1103 | * \retval PSA_ERROR_BAD_STATE |
| 1104 | * The operation state is not valid (not started, or already completed). |
| 1105 | * \retval PSA_ERROR_BUFFER_TOO_SMALL |
| 1106 | * The size of the \c hash buffer is too small. You can determine a |
Gilles Peskine | 71bb7b7 | 2018-04-19 08:29:59 +0200 | [diff] [blame] | 1107 | * sufficient buffer size by calling #PSA_HASH_SIZE(alg) |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1108 | * where \c alg is the hash algorithm that is calculated. |
| 1109 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 1110 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 1111 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 1112 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 1113 | */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1114 | psa_status_t psa_hash_finish(psa_hash_operation_t *operation, |
| 1115 | uint8_t *hash, |
| 1116 | size_t hash_size, |
| 1117 | size_t *hash_length); |
| 1118 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1119 | /** Finish the calculation of the hash of a message and compare it with |
| 1120 | * an expected value. |
| 1121 | * |
| 1122 | * The application must call psa_hash_start() before calling this function. |
| 1123 | * This function calculates the hash of the message formed by concatenating |
| 1124 | * the inputs passed to preceding calls to psa_hash_update(). It then |
| 1125 | * compares the calculated hash with the expected hash passed as a |
| 1126 | * parameter to this function. |
| 1127 | * |
| 1128 | * When this function returns, the operation becomes inactive. |
| 1129 | * |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 1130 | * \note Implementations shall make the best effort to ensure that the |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1131 | * comparison between the actual hash and the expected hash is performed |
| 1132 | * in constant time. |
| 1133 | * |
| 1134 | * \param operation Active hash operation. |
| 1135 | * \param hash Buffer containing the expected hash value. |
| 1136 | * \param hash_length Size of the \c hash buffer in bytes. |
| 1137 | * |
| 1138 | * \retval PSA_SUCCESS |
| 1139 | * The expected hash is identical to the actual hash of the message. |
| 1140 | * \retval PSA_ERROR_INVALID_SIGNATURE |
| 1141 | * The hash of the message was calculated successfully, but it |
| 1142 | * differs from the expected hash. |
| 1143 | * \retval PSA_ERROR_BAD_STATE |
| 1144 | * The operation state is not valid (not started, or already completed). |
| 1145 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 1146 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 1147 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 1148 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 1149 | */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1150 | psa_status_t psa_hash_verify(psa_hash_operation_t *operation, |
| 1151 | const uint8_t *hash, |
| 1152 | size_t hash_length); |
| 1153 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1154 | /** Abort a hash operation. |
| 1155 | * |
| 1156 | * This function may be called at any time after psa_hash_start(). |
| 1157 | * Aborting an operation frees all associated resources except for the |
| 1158 | * \c operation structure itself. |
| 1159 | * |
| 1160 | * Implementation should strive to be robust and handle inactive hash |
| 1161 | * operations safely (do nothing and return #PSA_ERROR_BAD_STATE). However, |
| 1162 | * application writers should beware that uninitialized memory may happen |
| 1163 | * to be indistinguishable from an active hash operation, and the behavior |
| 1164 | * of psa_hash_abort() is undefined in this case. |
| 1165 | * |
| 1166 | * \param operation Active hash operation. |
| 1167 | * |
| 1168 | * \retval PSA_SUCCESS |
| 1169 | * \retval PSA_ERROR_BAD_STATE |
| 1170 | * \c operation is not an active hash operation. |
| 1171 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 1172 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 1173 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 1174 | */ |
| 1175 | psa_status_t psa_hash_abort(psa_hash_operation_t *operation); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1176 | |
| 1177 | /**@}*/ |
| 1178 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1179 | /** \defgroup MAC Message authentication codes |
| 1180 | * @{ |
| 1181 | */ |
| 1182 | |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1183 | /** The type of the state data structure for multipart MAC operations. |
| 1184 | * |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 1185 | * This is an implementation-defined \c struct. Applications should not |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1186 | * make any assumptions about the content of this structure except |
| 1187 | * as directed by the documentation of a specific implementation. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1188 | typedef struct psa_mac_operation_s psa_mac_operation_t; |
| 1189 | |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1190 | /** The size of the output of psa_mac_finish(), in bytes. |
| 1191 | * |
| 1192 | * This is also the MAC size that psa_mac_verify() expects. |
| 1193 | * |
| 1194 | * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that |
| 1195 | * #PSA_ALG_IS_MAC(alg) is true). |
| 1196 | * |
| 1197 | * \return The MAC size for the specified algorithm. |
| 1198 | * If the MAC algorithm is not recognized, return 0. |
| 1199 | * An implementation may return either 0 or the correct size |
| 1200 | * for a MAC algorithm that it recognizes, but does not support. |
| 1201 | */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1202 | #define PSA_MAC_FINAL_SIZE(key_type, key_bits, alg) \ |
Gilles Peskine | 71bb7b7 | 2018-04-19 08:29:59 +0200 | [diff] [blame] | 1203 | (PSA_ALG_IS_HMAC(alg) ? PSA_HASH_SIZE(PSA_ALG_HMAC_HASH(alg)) : \ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1204 | PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) : \ |
| 1205 | 0) |
| 1206 | |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1207 | /** Start a multipart MAC operation. |
| 1208 | * |
| 1209 | * The sequence of operations to calculate a MAC (message authentication code) |
| 1210 | * is as follows: |
| 1211 | * -# Allocate an operation object which will be passed to all the functions |
| 1212 | * listed here. |
| 1213 | * -# Call psa_mac_start() to specify the algorithm and key. |
| 1214 | * The key remains associated with the operation even if the content |
| 1215 | * of the key slot changes. |
| 1216 | * -# Call psa_mac_update() zero, one or more times, passing a fragment |
| 1217 | * of the message each time. The MAC that is calculated is the MAC |
| 1218 | * of the concatenation of these messages in order. |
| 1219 | * -# To calculate the MAC, call psa_mac_finish(). |
| 1220 | * To compare the MAC with an expected value, call psa_mac_verify(). |
| 1221 | * |
| 1222 | * The application may call psa_mac_abort() at any time after the operation |
| 1223 | * has been initialized with psa_mac_start(). |
| 1224 | * |
| 1225 | * After a successful call to psa_mac_start(), the application must |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame] | 1226 | * eventually terminate the operation. The following events terminate an |
| 1227 | * operation: |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1228 | * - A failed call to psa_mac_update(). |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 1229 | * - A call to psa_mac_finish(), psa_mac_verify() or psa_mac_abort(). |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1230 | * |
Gilles Peskine | 36a74b7 | 2018-06-01 16:30:32 +0200 | [diff] [blame] | 1231 | * \param operation The operation object to use. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1232 | * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value |
| 1233 | * such that #PSA_ALG_IS_MAC(alg) is true). |
| 1234 | * |
| 1235 | * \retval PSA_SUCCESS |
| 1236 | * Success. |
| 1237 | * \retval PSA_ERROR_EMPTY_SLOT |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 1238 | * \retval PSA_ERROR_NOT_PERMITTED |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 1239 | * \retval PSA_ERROR_INVALID_ARGUMENT |
| 1240 | * \c key is not compatible with \c alg. |
| 1241 | * \retval PSA_ERROR_NOT_SUPPORTED |
| 1242 | * \c alg is not supported or is not a MAC algorithm. |
| 1243 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 1244 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 1245 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 1246 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 1247 | */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 1248 | psa_status_t psa_mac_start(psa_mac_operation_t *operation, |
| 1249 | psa_key_slot_t key, |
| 1250 | psa_algorithm_t alg); |
| 1251 | |
| 1252 | psa_status_t psa_mac_update(psa_mac_operation_t *operation, |
| 1253 | const uint8_t *input, |
| 1254 | size_t input_length); |
| 1255 | |
| 1256 | psa_status_t psa_mac_finish(psa_mac_operation_t *operation, |
| 1257 | uint8_t *mac, |
| 1258 | size_t mac_size, |
| 1259 | size_t *mac_length); |
| 1260 | |
| 1261 | psa_status_t psa_mac_verify(psa_mac_operation_t *operation, |
| 1262 | const uint8_t *mac, |
| 1263 | size_t mac_length); |
| 1264 | |
| 1265 | psa_status_t psa_mac_abort(psa_mac_operation_t *operation); |
| 1266 | |
| 1267 | /**@}*/ |
| 1268 | |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1269 | /** \defgroup cipher Symmetric ciphers |
| 1270 | * @{ |
| 1271 | */ |
| 1272 | |
| 1273 | /** The type of the state data structure for multipart cipher operations. |
| 1274 | * |
| 1275 | * This is an implementation-defined \c struct. Applications should not |
| 1276 | * make any assumptions about the content of this structure except |
| 1277 | * as directed by the documentation of a specific implementation. */ |
| 1278 | typedef struct psa_cipher_operation_s psa_cipher_operation_t; |
| 1279 | |
| 1280 | /** Set the key for a multipart symmetric encryption operation. |
| 1281 | * |
| 1282 | * The sequence of operations to encrypt a message with a symmetric cipher |
| 1283 | * is as follows: |
| 1284 | * -# Allocate an operation object which will be passed to all the functions |
| 1285 | * listed here. |
| 1286 | * -# Call psa_encrypt_setup() to specify the algorithm and key. |
| 1287 | * The key remains associated with the operation even if the content |
| 1288 | * of the key slot changes. |
| 1289 | * -# Call either psa_encrypt_generate_iv() or psa_encrypt_set_iv() to |
| 1290 | * generate or set the IV (initialization vector). You should use |
| 1291 | * psa_encrypt_generate_iv() unless the protocol you are implementing |
| 1292 | * requires a specific IV value. |
| 1293 | * -# Call psa_cipher_update() zero, one or more times, passing a fragment |
| 1294 | * of the message each time. |
| 1295 | * -# Call psa_cipher_finish(). |
| 1296 | * |
| 1297 | * The application may call psa_cipher_abort() at any time after the operation |
| 1298 | * has been initialized with psa_encrypt_setup(). |
| 1299 | * |
| 1300 | * After a successful call to psa_encrypt_setup(), the application must |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame] | 1301 | * eventually terminate the operation. The following events terminate an |
| 1302 | * operation: |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1303 | * - A failed call to psa_encrypt_generate_iv(), psa_encrypt_set_iv() |
| 1304 | * or psa_cipher_update(). |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 1305 | * - A call to psa_cipher_finish() or psa_cipher_abort(). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1306 | * |
Gilles Peskine | 36a74b7 | 2018-06-01 16:30:32 +0200 | [diff] [blame] | 1307 | * \param operation The operation object to use. |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1308 | * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value |
| 1309 | * such that #PSA_ALG_IS_CIPHER(alg) is true). |
| 1310 | * |
| 1311 | * \retval PSA_SUCCESS |
| 1312 | * Success. |
| 1313 | * \retval PSA_ERROR_EMPTY_SLOT |
| 1314 | * \retval PSA_ERROR_NOT_PERMITTED |
| 1315 | * \retval PSA_ERROR_INVALID_ARGUMENT |
| 1316 | * \c key is not compatible with \c alg. |
| 1317 | * \retval PSA_ERROR_NOT_SUPPORTED |
| 1318 | * \c alg is not supported or is not a cipher algorithm. |
| 1319 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 1320 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 1321 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 1322 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 1323 | */ |
| 1324 | psa_status_t psa_encrypt_setup(psa_cipher_operation_t *operation, |
| 1325 | psa_key_slot_t key, |
| 1326 | psa_algorithm_t alg); |
| 1327 | |
| 1328 | /** Set the key for a multipart symmetric decryption operation. |
| 1329 | * |
| 1330 | * The sequence of operations to decrypt a message with a symmetric cipher |
| 1331 | * is as follows: |
| 1332 | * -# Allocate an operation object which will be passed to all the functions |
| 1333 | * listed here. |
| 1334 | * -# Call psa_decrypt_setup() to specify the algorithm and key. |
| 1335 | * The key remains associated with the operation even if the content |
| 1336 | * of the key slot changes. |
| 1337 | * -# Call psa_cipher_update() with the IV (initialization vector) for the |
| 1338 | * decryption. If the IV is prepended to the ciphertext, you can call |
| 1339 | * psa_cipher_update() on a buffer containing the IV followed by the |
| 1340 | * beginning of the message. |
| 1341 | * -# Call psa_cipher_update() zero, one or more times, passing a fragment |
| 1342 | * of the message each time. |
| 1343 | * -# Call psa_cipher_finish(). |
| 1344 | * |
| 1345 | * The application may call psa_cipher_abort() at any time after the operation |
| 1346 | * has been initialized with psa_encrypt_setup(). |
| 1347 | * |
| 1348 | * After a successful call to psa_decrypt_setup(), the application must |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame] | 1349 | * eventually terminate the operation. The following events terminate an |
| 1350 | * operation: |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1351 | * - A failed call to psa_cipher_update(). |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 1352 | * - A call to psa_cipher_finish() or psa_cipher_abort(). |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1353 | * |
Gilles Peskine | 36a74b7 | 2018-06-01 16:30:32 +0200 | [diff] [blame] | 1354 | * \param operation The operation object to use. |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1355 | * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value |
| 1356 | * such that #PSA_ALG_IS_CIPHER(alg) is true). |
| 1357 | * |
| 1358 | * \retval PSA_SUCCESS |
| 1359 | * Success. |
| 1360 | * \retval PSA_ERROR_EMPTY_SLOT |
| 1361 | * \retval PSA_ERROR_NOT_PERMITTED |
| 1362 | * \retval PSA_ERROR_INVALID_ARGUMENT |
| 1363 | * \c key is not compatible with \c alg. |
| 1364 | * \retval PSA_ERROR_NOT_SUPPORTED |
| 1365 | * \c alg is not supported or is not a cipher algorithm. |
| 1366 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 1367 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 1368 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 1369 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 1370 | */ |
| 1371 | psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation, |
| 1372 | psa_key_slot_t key, |
| 1373 | psa_algorithm_t alg); |
| 1374 | |
| 1375 | psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation, |
| 1376 | unsigned char *iv, |
| 1377 | size_t iv_size, |
| 1378 | size_t *iv_length); |
| 1379 | |
| 1380 | psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation, |
| 1381 | const unsigned char *iv, |
| 1382 | size_t iv_length); |
| 1383 | |
| 1384 | psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, |
| 1385 | const uint8_t *input, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1386 | size_t input_length, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1387 | unsigned char *output, |
| 1388 | size_t output_size, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1389 | size_t *output_length); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1390 | |
| 1391 | psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1392 | uint8_t *output, |
Moran Peker | 0071b87 | 2018-04-22 20:16:58 +0300 | [diff] [blame] | 1393 | size_t output_size, |
mohammad1603 | 503973b | 2018-03-12 15:59:30 +0200 | [diff] [blame] | 1394 | size_t *output_length); |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 1395 | |
| 1396 | psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation); |
| 1397 | |
| 1398 | /**@}*/ |
| 1399 | |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1400 | /** \defgroup aead Authenticated encryption with associated data (AEAD) |
| 1401 | * @{ |
| 1402 | */ |
| 1403 | |
Gilles Peskine | 5e39dc9 | 2018-06-08 11:41:57 +0200 | [diff] [blame] | 1404 | /** The tag size for an AEAD algorithm, in bytes. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1405 | * |
Gilles Peskine | 5e39dc9 | 2018-06-08 11:41:57 +0200 | [diff] [blame] | 1406 | * \param alg An AEAD algorithm |
| 1407 | * (\c PSA_ALG_XXX value such that |
| 1408 | * #PSA_ALG_IS_AEAD(alg) is true). |
| 1409 | * |
| 1410 | * \return The tag size for the specified algorithm. |
| 1411 | * If the AEAD algorithm does not have an identified |
| 1412 | * tag that can be distinguished from the rest of |
| 1413 | * the ciphertext, return 0. |
| 1414 | * If the AEAD algorithm is not recognized, return 0. |
| 1415 | * An implementation may return either 0 or a |
| 1416 | * correct size for an AEAD algorithm that it |
| 1417 | * recognizes, but does not support. |
| 1418 | */ |
| 1419 | #define PSA_AEAD_TAG_SIZE(alg) \ |
| 1420 | ((alg) == PSA_ALG_GCM ? 16 : \ |
| 1421 | (alg) == PSA_ALG_CCM ? 16 : \ |
| 1422 | 0) |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1423 | |
Gilles Peskine | 212e4d8 | 2018-06-08 11:36:37 +0200 | [diff] [blame] | 1424 | /** The maximum size of the output of psa_aead_encrypt(), in bytes. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1425 | * |
Gilles Peskine | 212e4d8 | 2018-06-08 11:36:37 +0200 | [diff] [blame] | 1426 | * If the size of the ciphertext buffer is at least this large, it is |
| 1427 | * guaranteed that psa_aead_encrypt() will not fail due to an |
| 1428 | * insufficient buffer size. Depending on the algorithm, the actual size of |
| 1429 | * the ciphertext may be smaller. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1430 | * |
Gilles Peskine | 212e4d8 | 2018-06-08 11:36:37 +0200 | [diff] [blame] | 1431 | * \param alg An AEAD algorithm |
mohammad1603 | 1347a73 | 2018-06-07 01:38:45 +0300 | [diff] [blame] | 1432 | * (\c PSA_ALG_XXX value such that |
| 1433 | * #PSA_ALG_IS_AEAD(alg) is true). |
Gilles Peskine | 212e4d8 | 2018-06-08 11:36:37 +0200 | [diff] [blame] | 1434 | * \param plaintext_length Size of the plaintext in bytes. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1435 | * |
Gilles Peskine | 212e4d8 | 2018-06-08 11:36:37 +0200 | [diff] [blame] | 1436 | * \return The AEAD ciphertext size for the specified |
| 1437 | * algorithm. |
| 1438 | * If the AEAD algorithm is not recognized, return 0. |
| 1439 | * An implementation may return either 0 or a |
| 1440 | * correct size for an AEAD algorithm that it |
| 1441 | * recognizes, but does not support. |
mohammad1603 | 1347a73 | 2018-06-07 01:38:45 +0300 | [diff] [blame] | 1442 | */ |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1443 | #define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(alg, plaintext_length) \ |
Gilles Peskine | 5e39dc9 | 2018-06-08 11:41:57 +0200 | [diff] [blame] | 1444 | (PSA_AEAD_TAG_SIZE(alg) != 0 ? \ |
| 1445 | (plaintext_length) + PSA_AEAD_TAG_SIZE(alg) : \ |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1446 | 0) |
| 1447 | |
| 1448 | /** Process an authenticated encryption operation. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1449 | * |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1450 | * \param key Slot containing the key to use. |
| 1451 | * \param alg The AEAD algorithm to compute |
| 1452 | * (\c PSA_ALG_XXX value such that |
| 1453 | * #PSA_ALG_IS_AEAD(alg) is true). |
| 1454 | * \param nonce Nonce or IV to use. |
| 1455 | * \param nonce_length Size of the \p nonce buffer in bytes. |
| 1456 | * \param additional_data Additional data that will be authenticated |
| 1457 | * but not encrypted. |
| 1458 | * \param additional_data_length Size of \p additional_data in bytes. |
| 1459 | * \param plaintext Data that will be authenticated and |
| 1460 | * encrypted. |
| 1461 | * \param plaintext_length Size of \p plaintext in bytes. |
| 1462 | * \param ciphertext Output buffer for the authenticated and |
| 1463 | * encrypted data. The additional data is not |
| 1464 | * part of this output. For algorithms where the |
| 1465 | * encrypted data and the authentication tag |
| 1466 | * are defined as separate outputs, the |
| 1467 | * authentication tag is appended to the |
| 1468 | * encrypted data. |
| 1469 | * \param ciphertext_size Size of the \p ciphertext buffer in bytes. |
| 1470 | * This must be at least |
| 1471 | * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg, |
| 1472 | * \p plaintext_length). |
| 1473 | * \param ciphertext_length On success, the size of the output |
| 1474 | * in the \b ciphertext buffer. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1475 | * |
| 1476 | * \retval PSA_SUCCESS |
| 1477 | * Success. |
| 1478 | * \retval PSA_ERROR_EMPTY_SLOT |
| 1479 | * \retval PSA_ERROR_NOT_PERMITTED |
| 1480 | * \retval PSA_ERROR_INVALID_ARGUMENT |
| 1481 | * \c key is not compatible with \c alg. |
| 1482 | * \retval PSA_ERROR_NOT_SUPPORTED |
| 1483 | * \c alg is not supported or is not an AEAD algorithm. |
| 1484 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 1485 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 1486 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 1487 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 1488 | */ |
mohammad1603 | 39ee871 | 2018-04-26 00:51:02 +0300 | [diff] [blame] | 1489 | psa_status_t psa_aead_encrypt( psa_key_slot_t key, |
| 1490 | psa_algorithm_t alg, |
| 1491 | const uint8_t *nonce, |
| 1492 | size_t nonce_length, |
| 1493 | const uint8_t *additional_data, |
| 1494 | size_t additional_data_length, |
| 1495 | const uint8_t *plaintext, |
| 1496 | size_t plaintext_length, |
| 1497 | uint8_t *ciphertext, |
| 1498 | size_t ciphertext_size, |
| 1499 | size_t *ciphertext_length ); |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1500 | |
Gilles Peskine | 212e4d8 | 2018-06-08 11:36:37 +0200 | [diff] [blame] | 1501 | /** The maximum size of the output of psa_aead_decrypt(), in bytes. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1502 | * |
Gilles Peskine | 212e4d8 | 2018-06-08 11:36:37 +0200 | [diff] [blame] | 1503 | * If the size of the plaintext buffer is at least this large, it is |
| 1504 | * guaranteed that psa_aead_decrypt() will not fail due to an |
| 1505 | * insufficient buffer size. Depending on the algorithm, the actual size of |
| 1506 | * the plaintext may be smaller. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1507 | * |
Gilles Peskine | 212e4d8 | 2018-06-08 11:36:37 +0200 | [diff] [blame] | 1508 | * \param alg An AEAD algorithm |
mohammad1603 | 1347a73 | 2018-06-07 01:38:45 +0300 | [diff] [blame] | 1509 | * (\c PSA_ALG_XXX value such that |
| 1510 | * #PSA_ALG_IS_AEAD(alg) is true). |
Gilles Peskine | 212e4d8 | 2018-06-08 11:36:37 +0200 | [diff] [blame] | 1511 | * \param ciphertext_length Size of the plaintext in bytes. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1512 | * |
Gilles Peskine | 212e4d8 | 2018-06-08 11:36:37 +0200 | [diff] [blame] | 1513 | * \return The AEAD ciphertext size for the specified |
| 1514 | * algorithm. |
| 1515 | * If the AEAD algorithm is not recognized, return 0. |
| 1516 | * An implementation may return either 0 or a |
| 1517 | * correct size for an AEAD algorithm that it |
| 1518 | * recognizes, but does not support. |
mohammad1603 | 1347a73 | 2018-06-07 01:38:45 +0300 | [diff] [blame] | 1519 | */ |
Gilles Peskine | 5e39dc9 | 2018-06-08 11:41:57 +0200 | [diff] [blame] | 1520 | #define PSA_AEAD_DECRYPT_OUTPUT_SIZE(alg, ciphertext_length) \ |
| 1521 | (PSA_AEAD_TAG_SIZE(alg) != 0 ? \ |
| 1522 | (plaintext_length) - PSA_AEAD_TAG_SIZE(alg) : \ |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1523 | 0) |
| 1524 | |
| 1525 | /** Process an authenticated decryption operation. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1526 | * |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1527 | * \param key Slot containing the key to use. |
| 1528 | * \param alg The AEAD algorithm to compute |
| 1529 | * (\c PSA_ALG_XXX value such that |
| 1530 | * #PSA_ALG_IS_AEAD(alg) is true). |
| 1531 | * \param nonce Nonce or IV to use. |
| 1532 | * \param nonce_length Size of the \p nonce buffer in bytes. |
| 1533 | * \param additional_data Additional data that has been authenticated |
| 1534 | * but not encrypted. |
| 1535 | * \param additional_data_length Size of \p additional_data in bytes. |
| 1536 | * \param ciphertext Data that has been authenticated and |
| 1537 | * encrypted. For algorithms where the |
| 1538 | * encrypted data and the authentication tag |
| 1539 | * are defined as separate inputs, the buffer |
| 1540 | * must contain the encrypted data followed |
| 1541 | * by the authentication tag. |
| 1542 | * \param ciphertext_length Size of \p ciphertext in bytes. |
| 1543 | * \param plaintext Output buffer for the decrypted data. |
| 1544 | * \param plaintext_size Size of the \p plaintext buffer in bytes. |
| 1545 | * This must be at least |
| 1546 | * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg, |
| 1547 | * \p ciphertext_length). |
| 1548 | * \param plaintext_length On success, the size of the output |
mohammad1603 | fb5b9cb | 2018-06-06 13:44:27 +0300 | [diff] [blame] | 1549 | * in the \b plaintext buffer. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1550 | * |
| 1551 | * \retval PSA_SUCCESS |
| 1552 | * Success. |
| 1553 | * \retval PSA_ERROR_EMPTY_SLOT |
Gilles Peskine | 1e7d8f1 | 2018-06-01 16:29:38 +0200 | [diff] [blame] | 1554 | * \retval PSA_ERROR_INVALID_SIGNATURE |
| 1555 | * The ciphertext is not authentic. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1556 | * \retval PSA_ERROR_NOT_PERMITTED |
| 1557 | * \retval PSA_ERROR_INVALID_ARGUMENT |
| 1558 | * \c key is not compatible with \c alg. |
| 1559 | * \retval PSA_ERROR_NOT_SUPPORTED |
Gilles Peskine | 1906798 | 2018-03-20 17:54:53 +0100 | [diff] [blame] | 1560 | * \c alg is not supported or is not an AEAD algorithm. |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1561 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 1562 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 1563 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 1564 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 1565 | */ |
mohammad1603 | 39ee871 | 2018-04-26 00:51:02 +0300 | [diff] [blame] | 1566 | psa_status_t psa_aead_decrypt( psa_key_slot_t key, |
| 1567 | psa_algorithm_t alg, |
| 1568 | const uint8_t *nonce, |
| 1569 | size_t nonce_length, |
| 1570 | const uint8_t *additional_data, |
| 1571 | size_t additional_data_length, |
| 1572 | const uint8_t *ciphertext, |
| 1573 | size_t ciphertext_length, |
| 1574 | uint8_t *plaintext, |
| 1575 | size_t plaintext_size, |
| 1576 | size_t *plaintext_length ); |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1577 | |
| 1578 | /**@}*/ |
| 1579 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1580 | /** \defgroup asymmetric Asymmetric cryptography |
| 1581 | * @{ |
| 1582 | */ |
| 1583 | |
| 1584 | /** |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 1585 | * \brief Maximum ECDSA signature size for a given curve bit size |
| 1586 | * |
| 1587 | * \param curve_bits Curve size in bits |
| 1588 | * \return Maximum signature size in bytes |
| 1589 | * |
| 1590 | * \note This macro returns a compile-time constant if its argument is one. |
| 1591 | * |
| 1592 | * \warning This macro may evaluate its argument multiple times. |
| 1593 | */ |
| 1594 | /* |
| 1595 | * RFC 4492 page 20: |
| 1596 | * |
| 1597 | * Ecdsa-Sig-Value ::= SEQUENCE { |
| 1598 | * r INTEGER, |
| 1599 | * s INTEGER |
| 1600 | * } |
| 1601 | * |
| 1602 | * Size is at most |
| 1603 | * 1 (tag) + 1 (len) + 1 (initial 0) + curve_bytes for each of r and s, |
| 1604 | * twice that + 1 (tag) + 2 (len) for the sequence |
| 1605 | * (assuming curve_bytes is less than 126 for r and s, |
| 1606 | * and less than 124 (total len <= 255) for the sequence) |
| 1607 | */ |
| 1608 | #define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \ |
| 1609 | ( /*T,L of SEQUENCE*/ ((curve_bits) >= 61 * 8 ? 3 : 2) + \ |
| 1610 | /*T,L of r,s*/ 2 * (((curve_bits) >= 127 * 8 ? 3 : 2) + \ |
| 1611 | /*V of r,s*/ ((curve_bits) + 8) / 8)) |
| 1612 | |
| 1613 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1614 | /** Safe signature buffer size for psa_asymmetric_sign(). |
| 1615 | * |
| 1616 | * This macro returns a safe buffer size for a signature using a key |
| 1617 | * of the specified type and size, with the specified algorithm. |
| 1618 | * Note that the actual size of the signature may be smaller |
| 1619 | * (some algorithms produce a variable-size signature). |
| 1620 | * |
| 1621 | * \warning This function may call its arguments multiple times or |
| 1622 | * zero times, so you should not pass arguments that contain |
| 1623 | * side effects. |
| 1624 | * |
| 1625 | * \param key_type An asymmetric key type (this may indifferently be a |
| 1626 | * key pair type or a public key type). |
| 1627 | * \param key_bits The size of the key in bits. |
| 1628 | * \param alg The signature algorithm. |
| 1629 | * |
| 1630 | * \return If the parameters are valid and supported, return |
| 1631 | * a buffer size in bytes that guarantees that |
| 1632 | * psa_asymmetric_sign() will not fail with |
| 1633 | * #PSA_ERROR_BUFFER_TOO_SMALL. |
| 1634 | * If the parameters are a valid combination that is not supported |
| 1635 | * by the implementation, this macro either shall return either a |
| 1636 | * sensible size or 0. |
| 1637 | * If the parameters are not valid, the |
| 1638 | * return value is unspecified. |
| 1639 | * |
| 1640 | */ |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 1641 | #define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \ |
Gilles Peskine | 2905a7a | 2018-03-07 16:39:31 +0100 | [diff] [blame] | 1642 | (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \ |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 1643 | PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \ |
Gilles Peskine | 8484565 | 2018-03-28 14:17:40 +0200 | [diff] [blame] | 1644 | ((void)alg, 0)) |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 1645 | |
| 1646 | /** |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1647 | * \brief Sign a hash or short message with a private key. |
| 1648 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1649 | * \param key Key slot containing an asymmetric key pair. |
| 1650 | * \param alg A signature algorithm that is compatible with |
| 1651 | * the type of \c key. |
| 1652 | * \param hash The message to sign. |
| 1653 | * \param hash_length Size of the \c hash buffer in bytes. |
| 1654 | * \param salt A salt or label, if supported by the signature |
| 1655 | * algorithm. |
| 1656 | * If the signature algorithm does not support a |
| 1657 | * salt, pass \c NULL. |
| 1658 | * If the signature algorithm supports an optional |
| 1659 | * salt and you do not want to pass a salt, |
| 1660 | * pass \c NULL. |
| 1661 | * \param salt_length Size of the \c salt buffer in bytes. |
| 1662 | * If \c salt is \c NULL, pass 0. |
| 1663 | * \param signature Buffer where the signature is to be written. |
| 1664 | * \param signature_size Size of the \c signature buffer in bytes. |
| 1665 | * \param signature_length On success, the number of bytes |
| 1666 | * that make up the returned signature value. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1667 | * |
| 1668 | * \retval PSA_SUCCESS |
| 1669 | * \retval PSA_ERROR_BUFFER_TOO_SMALL |
| 1670 | * The size of the \c signature buffer is too small. You can |
| 1671 | * determine a sufficient buffer size by calling |
| 1672 | * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) |
| 1673 | * where \c key_type and \c key_bits are the type and bit-size |
| 1674 | * respectively of \c key. |
| 1675 | * \retval PSA_ERROR_NOT_SUPPORTED |
| 1676 | * \retval PSA_ERROR_INVALID_ARGUMENT |
| 1677 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 1678 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 1679 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 1680 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 1681 | * \retval PSA_ERROR_INSUFFICIENT_ENTROPY |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1682 | */ |
| 1683 | psa_status_t psa_asymmetric_sign(psa_key_slot_t key, |
| 1684 | psa_algorithm_t alg, |
| 1685 | const uint8_t *hash, |
| 1686 | size_t hash_length, |
| 1687 | const uint8_t *salt, |
| 1688 | size_t salt_length, |
| 1689 | uint8_t *signature, |
| 1690 | size_t signature_size, |
| 1691 | size_t *signature_length); |
| 1692 | |
| 1693 | /** |
| 1694 | * \brief Verify the signature a hash or short message using a public key. |
| 1695 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1696 | * \param key Key slot containing a public key or an |
| 1697 | * asymmetric key pair. |
| 1698 | * \param alg A signature algorithm that is compatible with |
| 1699 | * the type of \c key. |
| 1700 | * \param hash The message whose signature is to be verified. |
| 1701 | * \param hash_length Size of the \c hash buffer in bytes. |
| 1702 | * \param salt A salt or label, if supported by the signature |
| 1703 | * algorithm. |
| 1704 | * If the signature algorithm does not support a |
| 1705 | * salt, pass \c NULL. |
| 1706 | * If the signature algorithm supports an optional |
| 1707 | * salt and you do not want to pass a salt, |
| 1708 | * pass \c NULL. |
| 1709 | * \param salt_length Size of the \c salt buffer in bytes. |
| 1710 | * If \c salt is \c NULL, pass 0. |
| 1711 | * \param signature Buffer containing the signature to verify. |
| 1712 | * \param signature_size Size of the \c signature buffer in bytes. |
| 1713 | * |
| 1714 | * \retval PSA_SUCCESS |
| 1715 | * The signature is valid. |
| 1716 | * \retval PSA_ERROR_INVALID_SIGNATURE |
| 1717 | * The calculation was perfomed successfully, but the passed |
| 1718 | * signature is not a valid signature. |
| 1719 | * \retval PSA_ERROR_NOT_SUPPORTED |
| 1720 | * \retval PSA_ERROR_INVALID_ARGUMENT |
| 1721 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 1722 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 1723 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 1724 | * \retval PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1725 | */ |
| 1726 | psa_status_t psa_asymmetric_verify(psa_key_slot_t key, |
| 1727 | psa_algorithm_t alg, |
| 1728 | const uint8_t *hash, |
| 1729 | size_t hash_length, |
| 1730 | const uint8_t *salt, |
| 1731 | size_t salt_length, |
| 1732 | uint8_t *signature, |
| 1733 | size_t signature_size); |
| 1734 | |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 1735 | #define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \ |
Gilles Peskine | 0629793 | 2018-04-11 16:58:22 +0200 | [diff] [blame] | 1736 | (PSA_KEY_TYPE_IS_RSA(key_type) ? \ |
| 1737 | ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \ |
| 1738 | 0) |
Gilles Peskine | 723feff | 2018-05-31 20:08:13 +0200 | [diff] [blame] | 1739 | #define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \ |
| 1740 | (PSA_ALG_IS_RSA_OAEP_MGF1(alg) ? \ |
| 1741 | 2 * PSA_HASH_FINAL_SIZE(PSA_ALG_RSA_GET_HASH(alg)) + 1 : \ |
| 1742 | 11 /*PKCS#1v1.5*/) |
| 1743 | #define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \ |
Gilles Peskine | 0629793 | 2018-04-11 16:58:22 +0200 | [diff] [blame] | 1744 | (PSA_KEY_TYPE_IS_RSA(key_type) ? \ |
Gilles Peskine | 723feff | 2018-05-31 20:08:13 +0200 | [diff] [blame] | 1745 | PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \ |
Gilles Peskine | 0629793 | 2018-04-11 16:58:22 +0200 | [diff] [blame] | 1746 | 0) |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 1747 | |
| 1748 | /** |
| 1749 | * \brief Encrypt a short message with a public key. |
| 1750 | * |
| 1751 | * \param key Key slot containing a public key or an asymmetric |
| 1752 | * key pair. |
| 1753 | * \param alg An asymmetric encryption algorithm that is |
| 1754 | * compatible with the type of \c key. |
| 1755 | * \param input The message to encrypt. |
| 1756 | * \param input_length Size of the \c input buffer in bytes. |
| 1757 | * \param salt A salt or label, if supported by the encryption |
| 1758 | * algorithm. |
| 1759 | * If the algorithm does not support a |
| 1760 | * salt, pass \c NULL. |
| 1761 | * If the algorithm supports an optional |
| 1762 | * salt and you do not want to pass a salt, |
| 1763 | * pass \c NULL. |
| 1764 | * |
| 1765 | * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is |
| 1766 | * supported. |
| 1767 | * \param salt_length Size of the \c salt buffer in bytes. |
| 1768 | * If \c salt is \c NULL, pass 0. |
| 1769 | * \param output Buffer where the encrypted message is to be written. |
| 1770 | * \param output_size Size of the \c output buffer in bytes. |
| 1771 | * \param output_length On success, the number of bytes |
| 1772 | * that make up the returned output. |
| 1773 | * |
| 1774 | * \retval PSA_SUCCESS |
| 1775 | * \retval PSA_ERROR_BUFFER_TOO_SMALL |
| 1776 | * The size of the \c output buffer is too small. You can |
| 1777 | * determine a sufficient buffer size by calling |
| 1778 | * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) |
| 1779 | * where \c key_type and \c key_bits are the type and bit-size |
| 1780 | * respectively of \c key. |
| 1781 | * \retval PSA_ERROR_NOT_SUPPORTED |
| 1782 | * \retval PSA_ERROR_INVALID_ARGUMENT |
| 1783 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 1784 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 1785 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 1786 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 1787 | * \retval PSA_ERROR_INSUFFICIENT_ENTROPY |
| 1788 | */ |
| 1789 | psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key, |
| 1790 | psa_algorithm_t alg, |
| 1791 | const uint8_t *input, |
| 1792 | size_t input_length, |
| 1793 | const uint8_t *salt, |
| 1794 | size_t salt_length, |
| 1795 | uint8_t *output, |
| 1796 | size_t output_size, |
| 1797 | size_t *output_length); |
| 1798 | |
| 1799 | /** |
| 1800 | * \brief Decrypt a short message with a private key. |
| 1801 | * |
| 1802 | * \param key Key slot containing an asymmetric key pair. |
| 1803 | * \param alg An asymmetric encryption algorithm that is |
| 1804 | * compatible with the type of \c key. |
| 1805 | * \param input The message to decrypt. |
| 1806 | * \param input_length Size of the \c input buffer in bytes. |
| 1807 | * \param salt A salt or label, if supported by the encryption |
| 1808 | * algorithm. |
| 1809 | * If the algorithm does not support a |
| 1810 | * salt, pass \c NULL. |
| 1811 | * If the algorithm supports an optional |
| 1812 | * salt and you do not want to pass a salt, |
| 1813 | * pass \c NULL. |
| 1814 | * |
| 1815 | * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is |
| 1816 | * supported. |
| 1817 | * \param salt_length Size of the \c salt buffer in bytes. |
| 1818 | * If \c salt is \c NULL, pass 0. |
Gilles Peskine | f48af7f | 2018-03-28 18:44:14 +0200 | [diff] [blame] | 1819 | * \param output Buffer where the decrypted message is to be written. |
Gilles Peskine | 6944f9a | 2018-03-28 14:18:39 +0200 | [diff] [blame] | 1820 | * \param output_size Size of the \c output buffer in bytes. |
| 1821 | * \param output_length On success, the number of bytes |
| 1822 | * that make up the returned output. |
| 1823 | * |
| 1824 | * \retval PSA_SUCCESS |
| 1825 | * \retval PSA_ERROR_BUFFER_TOO_SMALL |
| 1826 | * The size of the \c output buffer is too small. You can |
| 1827 | * determine a sufficient buffer size by calling |
| 1828 | * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) |
| 1829 | * where \c key_type and \c key_bits are the type and bit-size |
| 1830 | * respectively of \c key. |
| 1831 | * \retval PSA_ERROR_NOT_SUPPORTED |
| 1832 | * \retval PSA_ERROR_INVALID_ARGUMENT |
| 1833 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 1834 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 1835 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 1836 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 1837 | * \retval PSA_ERROR_INSUFFICIENT_ENTROPY |
| 1838 | * \retval PSA_ERROR_INVALID_PADDING |
| 1839 | */ |
| 1840 | psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key, |
| 1841 | psa_algorithm_t alg, |
| 1842 | const uint8_t *input, |
| 1843 | size_t input_length, |
| 1844 | const uint8_t *salt, |
| 1845 | size_t salt_length, |
| 1846 | uint8_t *output, |
| 1847 | size_t output_size, |
| 1848 | size_t *output_length); |
| 1849 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1850 | /**@}*/ |
| 1851 | |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 1852 | /** \defgroup generation Key generation |
| 1853 | * @{ |
| 1854 | */ |
| 1855 | |
| 1856 | /** |
| 1857 | * \brief Generate random bytes. |
| 1858 | * |
| 1859 | * \warning This function **can** fail! Callers MUST check the return status |
| 1860 | * and MUST NOT use the content of the output buffer if the return |
| 1861 | * status is not #PSA_SUCCESS. |
| 1862 | * |
| 1863 | * \note To generate a key, use psa_generate_key() instead. |
| 1864 | * |
| 1865 | * \param output Output buffer for the generated data. |
| 1866 | * \param output_size Number of bytes to generate and output. |
| 1867 | * |
| 1868 | * \retval PSA_SUCCESS |
| 1869 | * \retval PSA_ERROR_NOT_SUPPORTED |
| 1870 | * \retval PSA_ERROR_INSUFFICIENT_ENTROPY |
| 1871 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 1872 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 1873 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 1874 | */ |
| 1875 | psa_status_t psa_generate_random(uint8_t *output, |
| 1876 | size_t output_size); |
| 1877 | |
| 1878 | /** |
| 1879 | * \brief Generate a key or key pair. |
| 1880 | * |
Gilles Peskine | 4e69d7a | 2018-06-19 20:19:14 +0200 | [diff] [blame] | 1881 | * \param key Slot where the key will be stored. This must be a |
| 1882 | * valid slot for a key of the chosen type. It must |
| 1883 | * be unoccupied. |
| 1884 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). |
| 1885 | * \param bits Key size in bits. |
| 1886 | * \param parameters Extra parameters for key generation. The |
| 1887 | * interpretation of this parameter depends on |
| 1888 | * \c type. All types support \c NULL to use |
| 1889 | * the default parameters specified below. |
| 1890 | * \param parameters_size Size of the buffer that \param parameters |
| 1891 | * points to, in bytes. |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 1892 | * |
| 1893 | * For any symmetric key type (type such that |
| 1894 | * `PSA_KEY_TYPE_IS_ASYMMETRIC(type)` is false), \c parameters must be |
| 1895 | * \c NULL. For asymmetric key types defined by this specification, |
| 1896 | * the parameter type and the default parameters are defined by the |
| 1897 | * table below. For vendor-defined key types, the vendor documentation |
| 1898 | * shall define the parameter type and the default parameters. |
| 1899 | * |
Gilles Peskine | f48af7f | 2018-03-28 18:44:14 +0200 | [diff] [blame] | 1900 | * Type | Parameter type | Meaning | Parameters used if `parameters == NULL` |
| 1901 | * ---- | -------------- | ------- | --------------------------------------- |
| 1902 | * `PSA_KEY_TYPE_RSA_KEYPAIR` | `unsigned int` | Public exponent | 65537 |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 1903 | * |
| 1904 | * \retval PSA_SUCCESS |
| 1905 | * \retval PSA_ERROR_NOT_SUPPORTED |
| 1906 | * \retval PSA_ERROR_INVALID_ARGUMENT |
| 1907 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 1908 | * \retval PSA_ERROR_INSUFFICIENT_ENTROPY |
| 1909 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 1910 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 1911 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 1912 | */ |
| 1913 | psa_status_t psa_generate_key(psa_key_slot_t key, |
| 1914 | psa_key_type_t type, |
| 1915 | size_t bits, |
Gilles Peskine | 4e69d7a | 2018-06-19 20:19:14 +0200 | [diff] [blame] | 1916 | const void *parameters, |
| 1917 | size_t parameters_size); |
Gilles Peskine | 9e7dc71 | 2018-03-28 14:18:50 +0200 | [diff] [blame] | 1918 | |
| 1919 | /**@}*/ |
| 1920 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1921 | #ifdef __cplusplus |
| 1922 | } |
| 1923 | #endif |
| 1924 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1925 | /* The file "crypto_struct.h" contains definitions for |
| 1926 | * implementation-specific structs that are declared above. */ |
| 1927 | #include "crypto_struct.h" |
| 1928 | |
| 1929 | /* The file "crypto_extra.h" contains vendor-specific definitions. This |
| 1930 | * can include vendor-defined algorithms, extra functions, etc. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1931 | #include "crypto_extra.h" |
| 1932 | |
| 1933 | #endif /* PSA_CRYPTO_H */ |