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