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