Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file psa/crypto.h |
| 3 | * \brief Platform Security Architecture cryptography module |
| 4 | */ |
| 5 | |
| 6 | #ifndef PSA_CRYPTO_H |
| 7 | #define PSA_CRYPTO_H |
| 8 | |
| 9 | #include "crypto_platform.h" |
| 10 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 11 | #include <stddef.h> |
| 12 | |
Gilles Peskine | 62a7e7e | 2018-02-07 21:54:47 +0100 | [diff] [blame] | 13 | #ifdef __DOXYGEN_ONLY__ |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 14 | /* This __DOXYGEN_ONLY__ block contains mock definitions for things that |
| 15 | * must be defined in the crypto_platform.h header. These mock definitions |
| 16 | * are present in this file as a convenience to generate pretty-printed |
| 17 | * documentation that includes those definitions. */ |
| 18 | |
Gilles Peskine | 62a7e7e | 2018-02-07 21:54:47 +0100 | [diff] [blame] | 19 | /** \defgroup platform Implementation-specific definitions |
| 20 | * @{ |
| 21 | */ |
| 22 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 23 | /** \brief Key slot number. |
| 24 | * |
| 25 | * This type represents key slots. It must be an unsigned integral |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 26 | * type. The choice of type is implementation-dependent. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 27 | * 0 is not a valid key slot number. The meaning of other values is |
| 28 | * implementation dependent. |
| 29 | * |
| 30 | * At any given point in time, each key slot either contains a |
| 31 | * cryptographic object, or is empty. Key slots are persistent: |
| 32 | * once set, the cryptographic object remains in the key slot until |
| 33 | * explicitly destroyed. |
| 34 | */ |
| 35 | typedef _unsigned_integral_type_ psa_key_slot_t; |
| 36 | |
Gilles Peskine | 62a7e7e | 2018-02-07 21:54:47 +0100 | [diff] [blame] | 37 | /**@}*/ |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 38 | #endif /* __DOXYGEN_ONLY__ */ |
Gilles Peskine | 62a7e7e | 2018-02-07 21:54:47 +0100 | [diff] [blame] | 39 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 40 | #ifdef __cplusplus |
| 41 | extern "C" { |
| 42 | #endif |
| 43 | |
| 44 | /** \defgroup basic Basic definitions |
| 45 | * @{ |
| 46 | */ |
| 47 | |
| 48 | /** |
| 49 | * \brief Function return status. |
| 50 | * |
| 51 | * Zero indicates success, anything else indicates an error. |
| 52 | */ |
| 53 | typedef enum { |
| 54 | /** The action was completed successfully. */ |
| 55 | PSA_SUCCESS = 0, |
| 56 | /** The requested operation or a parameter is not supported |
| 57 | by this implementation. */ |
| 58 | PSA_ERROR_NOT_SUPPORTED, |
| 59 | /** The requested action is denied by a policy. */ |
| 60 | PSA_ERROR_NOT_PERMITTED, |
| 61 | /** An output buffer is too small. */ |
| 62 | PSA_ERROR_BUFFER_TOO_SMALL, |
| 63 | /** A slot is occupied, but must be empty to carry out the |
| 64 | requested action. */ |
| 65 | PSA_ERROR_OCCUPIED_SLOT, |
| 66 | /** A slot is empty, but must be occupied to carry out the |
| 67 | requested action. */ |
| 68 | PSA_ERROR_EMPTY_SLOT, |
| 69 | /** The requested action cannot be performed in the current state. */ |
| 70 | PSA_ERROR_BAD_STATE, |
| 71 | /** The parameters passed to the function are invalid. */ |
| 72 | PSA_ERROR_INVALID_ARGUMENT, |
| 73 | /** There is not enough runtime memory. */ |
| 74 | PSA_ERROR_INSUFFICIENT_MEMORY, |
| 75 | /** There is not enough persistent storage. */ |
| 76 | PSA_ERROR_INSUFFICIENT_STORAGE, |
| 77 | /** There was a communication failure inside the implementation. */ |
| 78 | PSA_ERROR_COMMUNICATION_FAILURE, |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 79 | /** There was a storage failure that may have led to data loss. */ |
| 80 | PSA_ERROR_STORAGE_FAILURE, |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 81 | /** A hardware failure was detected. */ |
| 82 | PSA_ERROR_HARDWARE_FAILURE, |
| 83 | /** A tampering attempt was detected. */ |
| 84 | PSA_ERROR_TAMPERING_DETECTED, |
| 85 | /** There is not enough entropy to generate random data needed |
| 86 | for the requested action. */ |
| 87 | PSA_ERROR_INSUFFICIENT_ENTROPY, |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 88 | /** The signature, MAC or hash is incorrect. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 89 | PSA_ERROR_INVALID_SIGNATURE, |
Gilles Peskine | a590529 | 2018-02-07 20:59:33 +0100 | [diff] [blame] | 90 | /** The decrypted padding is incorrect. */ |
| 91 | PSA_ERROR_INVALID_PADDING, |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 92 | /** An error occurred that does not correspond to any defined |
| 93 | failure cause. */ |
| 94 | PSA_ERROR_UNKNOWN_ERROR, |
| 95 | } psa_status_t; |
| 96 | |
| 97 | /** |
| 98 | * \brief Library initialization. |
| 99 | * |
| 100 | * Applications must call this function before calling any other |
| 101 | * function in this module. |
| 102 | * |
| 103 | * Applications may call this function more than once. Once a call |
| 104 | * succeeds, subsequent calls are guaranteed to succeed. |
| 105 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 106 | * \retval PSA_SUCCESS |
| 107 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 108 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 109 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 110 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 111 | * \retval PSA_ERROR_INSUFFICIENT_ENTROPY |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 112 | */ |
| 113 | psa_status_t psa_crypto_init(void); |
| 114 | |
Gilles Peskine | 2905a7a | 2018-03-07 16:39:31 +0100 | [diff] [blame] | 115 | #define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8) |
| 116 | #define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8) |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 117 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 118 | /**@}*/ |
| 119 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 120 | /** \defgroup crypto_types Key and algorithm types |
| 121 | * @{ |
| 122 | */ |
| 123 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 124 | /** \brief Encoding of a key type. |
| 125 | */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 126 | typedef uint32_t psa_key_type_t; |
| 127 | |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 128 | /** An invalid key type value. |
| 129 | * |
| 130 | * Zero is not the encoding of any key type. |
| 131 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 132 | #define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000) |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 133 | |
| 134 | /** Vendor-defined flag |
| 135 | * |
| 136 | * Key types defined by this standard will never have the |
| 137 | * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types |
| 138 | * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should |
| 139 | * respect the bitwise structure used by standard encodings whenever practical. |
| 140 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 141 | #define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 142 | |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 143 | #define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x7e000000) |
| 144 | #define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x02000000) |
| 145 | #define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x04000000) |
| 146 | #define PSA_KEY_TYPE_CATEGORY_ASYMMETRIC ((psa_key_type_t)0x06000000) |
| 147 | #define PSA_KEY_TYPE_PAIR_FLAG ((psa_key_type_t)0x01000000) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 148 | |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 149 | #define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x02000001) |
| 150 | #define PSA_KEY_TYPE_AES ((psa_key_type_t)0x04000001) |
| 151 | #define PSA_KEY_TYPE_DES ((psa_key_type_t)0x04000002) |
| 152 | #define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x04000003) |
| 153 | #define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x04000004) |
| 154 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 155 | /** RSA public key. */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 156 | #define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x06010000) |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 157 | /** RSA key pair (private and public key). */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 158 | #define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x07010000) |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 159 | /** DSA public key. */ |
| 160 | #define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x06020000) |
| 161 | /** DSA key pair (private and public key). */ |
| 162 | #define PSA_KEY_TYPE_DSA_KEYPAIR ((psa_key_type_t)0x07020000) |
| 163 | #define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x06030000) |
| 164 | #define PSA_KEY_TYPE_ECC_KEYPAIR_BASE ((psa_key_type_t)0x07030000) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 165 | #define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff) |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 166 | #define PSA_KEY_TYPE_ECC_KEYPAIR(curve) \ |
| 167 | (PSA_KEY_TYPE_ECC_KEYPAIR_BASE | (curve)) |
| 168 | #define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \ |
| 169 | (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve)) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 170 | |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 171 | /** Whether a key type is vendor-defined. */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 172 | #define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 173 | (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 174 | #define PSA_KEY_TYPE_IS_RAW_BYTES(type) \ |
| 175 | (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_RAW_DATA || \ |
| 176 | ((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC) |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 177 | |
| 178 | /** Whether a key type is asymmetric: either a key pair or a public key. */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 179 | #define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \ |
| 180 | (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_ASYMMETRIC) |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 181 | /** Whether a key type is the public part of a key pair. */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 182 | #define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \ |
| 183 | (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG) == \ |
| 184 | PSA_KEY_TYPE_CATEGORY_ASYMMETRIC)) |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 185 | /** Whether a key type is a key pair containing a private part and a public |
| 186 | * part. */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 187 | #define PSA_KEY_TYPE_IS_KEYPAIR(type) \ |
| 188 | (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \ |
| 189 | (PSA_KEY_TYPE_CATEGORY_ASYMMETRIC | PSA_KEY_TYPE_PAIR_FLAG)) |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 190 | /** Whether a key type is an RSA key pair or public key. */ |
| 191 | /** The key pair type corresponding to a public key type. */ |
| 192 | #define PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY(type) \ |
| 193 | ((type) | PSA_KEY_TYPE_PAIR_FLAG) |
| 194 | /** The public key type corresponding to a key pair type. */ |
| 195 | #define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) \ |
| 196 | ((type) & ~PSA_KEY_TYPE_PAIR_FLAG) |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 197 | #define PSA_KEY_TYPE_IS_RSA(type) \ |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 198 | (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY) |
| 199 | /** Whether a key type is an elliptic curve key pair or public key. */ |
Gilles Peskine | c66ea6a | 2018-02-03 22:43:28 +0100 | [diff] [blame] | 200 | #define PSA_KEY_TYPE_IS_ECC(type) \ |
Gilles Peskine | 06dc263 | 2018-03-08 07:47:25 +0100 | [diff] [blame] | 201 | ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \ |
| 202 | ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 203 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 204 | /** The block size of a block cipher. |
| 205 | * |
| 206 | * \param type A cipher key type (value of type #psa_key_type_t). |
| 207 | * |
| 208 | * \return The block size for a block cipher, or 1 for a stream cipher. |
| 209 | * The return value is undefined if \c type does not identify |
| 210 | * a cipher algorithm. |
| 211 | * |
| 212 | * \note This macro returns a compile-time constant if its argument is one. |
| 213 | * |
| 214 | * \warning This macro may evaluate its argument multiple times. |
| 215 | */ |
Gilles Peskine | 03182e9 | 2018-03-07 16:40:52 +0100 | [diff] [blame] | 216 | #define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 217 | ( \ |
| 218 | (type) == PSA_KEY_TYPE_AES ? 16 : \ |
| 219 | (type) == PSA_KEY_TYPE_DES ? 8 : \ |
| 220 | (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \ |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 221 | (type) == PSA_KEY_TYPE_ARC4 ? 1 : \ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 222 | 0) |
| 223 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 224 | /** \brief Encoding of a cryptographic algorithm. |
| 225 | * |
| 226 | * For algorithms that can be applied to multiple key types, this type |
| 227 | * does not encode the key type. For example, for symmetric ciphers |
| 228 | * based on a block cipher, #psa_algorithm_t encodes the block cipher |
| 229 | * mode and the padding mode while the block cipher itself is encoded |
| 230 | * via #psa_key_type_t. |
| 231 | */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 232 | typedef uint32_t psa_algorithm_t; |
| 233 | |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 234 | #define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000) |
| 235 | #define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000) |
| 236 | #define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000) |
| 237 | #define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000) |
| 238 | #define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000) |
| 239 | #define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000) |
| 240 | #define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000) |
| 241 | #define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000) |
| 242 | #define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000) |
| 243 | #define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 244 | |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 245 | #define PSA_ALG_IS_VENDOR_DEFINED(alg) \ |
| 246 | (((alg) & PSA_ALG_VENDOR_FLAG) != 0) |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 247 | /** Whether the specified algorithm is a hash algorithm. |
| 248 | * |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 249 | * \param alg An algorithm identifier (value of type #psa_algorithm_t). |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 250 | * |
| 251 | * \return 1 if \c alg is a hash algorithm, 0 otherwise. |
| 252 | * This macro may return either 0 or 1 if \c alg is not a valid |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 253 | * algorithm identifier. |
| 254 | */ |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 255 | #define PSA_ALG_IS_HASH(alg) \ |
| 256 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH) |
| 257 | #define PSA_ALG_IS_MAC(alg) \ |
| 258 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC) |
| 259 | #define PSA_ALG_IS_CIPHER(alg) \ |
| 260 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER) |
| 261 | #define PSA_ALG_IS_AEAD(alg) \ |
| 262 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD) |
| 263 | #define PSA_ALG_IS_SIGN(alg) \ |
| 264 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN) |
| 265 | #define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \ |
| 266 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION) |
| 267 | #define PSA_ALG_IS_KEY_AGREEMENT(alg) \ |
| 268 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT) |
| 269 | #define PSA_ALG_IS_KEY_DERIVATION(alg) \ |
| 270 | (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION) |
| 271 | |
| 272 | #define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff) |
| 273 | #define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001) |
| 274 | #define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002) |
| 275 | #define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003) |
Gilles Peskine | e3f694f | 2018-03-08 07:48:40 +0100 | [diff] [blame] | 276 | #define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004) |
| 277 | #define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 278 | #define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008) |
| 279 | #define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009) |
| 280 | #define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a) |
| 281 | #define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b) |
| 282 | #define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c) |
| 283 | #define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d) |
| 284 | #define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010) |
| 285 | #define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011) |
| 286 | #define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012) |
| 287 | #define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013) |
| 288 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 289 | #define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 290 | #define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000) |
| 291 | #define PSA_ALG_HMAC(hash_alg) \ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 292 | (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 293 | #define PSA_ALG_HMAC_HASH(hmac_alg) \ |
| 294 | (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK)) |
| 295 | #define PSA_ALG_IS_HMAC(alg) \ |
| 296 | (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \ |
| 297 | PSA_ALG_HMAC_BASE) |
| 298 | #define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000) |
| 299 | #define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001) |
| 300 | #define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002) |
| 301 | #define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003) |
| 302 | #define PSA_ALG_IS_CIPHER_MAC(alg) \ |
| 303 | (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \ |
| 304 | PSA_ALG_CIPHER_MAC_BASE) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 305 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 306 | #define PSA_ALG_CIPHER_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000) |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 307 | #define PSA_ALG_BLOCK_CIPHER_BASE ((psa_algorithm_t)0x04000000) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 308 | #define PSA_ALG_BLOCK_CIPHER_MODE_MASK ((psa_algorithm_t)0x000000ff) |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 309 | #define PSA_ALG_BLOCK_CIPHER_PADDING_MASK ((psa_algorithm_t)0x003f0000) |
| 310 | #define PSA_ALG_BLOCK_CIPHER_PAD_NONE ((psa_algorithm_t)0x00000000) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 311 | #define PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ((psa_algorithm_t)0x00010000) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 312 | #define PSA_ALG_IS_BLOCK_CIPHER(alg) \ |
| 313 | (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \ |
| 314 | PSA_ALG_BLOCK_CIPHER_BASE) |
| 315 | |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 316 | #define PSA_ALG_CBC_BASE ((psa_algorithm_t)0x04000001) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 317 | #define PSA_ALG_CFB_BASE ((psa_algorithm_t)0x04000002) |
| 318 | #define PSA_ALG_OFB_BASE ((psa_algorithm_t)0x04000003) |
| 319 | #define PSA_ALG_XTS_BASE ((psa_algorithm_t)0x04000004) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 320 | #define PSA_ALG_STREAM_CIPHER ((psa_algorithm_t)0x04800000) |
| 321 | #define PSA_ALG_CTR ((psa_algorithm_t)0x04800001) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 322 | #define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800002) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 323 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 324 | #define PSA_ALG_CCM ((psa_algorithm_t)0x06000001) |
| 325 | #define PSA_ALG_GCM ((psa_algorithm_t)0x06000002) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 326 | |
| 327 | #define PSA_ALG_RSA_PKCS1V15_RAW ((psa_algorithm_t)0x10010000) |
| 328 | #define PSA_ALG_RSA_PSS_MGF1 ((psa_algorithm_t)0x10020000) |
| 329 | #define PSA_ALG_RSA_OAEP ((psa_algorithm_t)0x12020000) |
| 330 | #define PSA_ALG_RSA_PKCS1V15(hash_alg) \ |
| 331 | (PSA_ALG_RSA_PKCS1V15_RAW | ((hash_alg) & PSA_ALG_HASH_MASK)) |
| 332 | #define PSA_ALG_IS_RSA_PKCS1V15(alg) \ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 333 | (((alg) & 0x7fffff00) == PSA_ALG_RSA_PKCS1V15_RAW) |
Gilles Peskine | 98f0a24 | 2018-02-06 18:57:29 +0100 | [diff] [blame] | 334 | #define PSA_ALG_RSA_GET_HASH(alg) \ |
| 335 | (((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 336 | |
| 337 | /**@}*/ |
| 338 | |
| 339 | /** \defgroup key_management Key management |
| 340 | * @{ |
| 341 | */ |
| 342 | |
| 343 | /** |
| 344 | * \brief Import a key in binary format. |
| 345 | * |
Gilles Peskine | f5b9fa1 | 2018-03-07 16:40:18 +0100 | [diff] [blame] | 346 | * This function supports any output from psa_export_key(). Refer to the |
| 347 | * documentation of psa_export_key() for the format for each key type. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 348 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 349 | * \param key Slot where the key will be stored. This must be a |
| 350 | * valid slot for a key of the chosen type. It must |
| 351 | * be unoccupied. |
| 352 | * \param type Key type (a \c PSA_KEY_TYPE_XXX value). |
| 353 | * \param data Buffer containing the key data. |
| 354 | * \param data_length Size of the \c data buffer in bytes. |
| 355 | * |
| 356 | * \retval PSA_SUCCESS |
| 357 | * Success. |
| 358 | * \retval PSA_ERROR_NOT_SUPPORTED |
| 359 | * The key type or key size is not supported. |
| 360 | * \retval PSA_ERROR_INVALID_ARGUMENT |
| 361 | * The key slot is invalid, |
| 362 | * or the key data is not correctly formatted. |
| 363 | * \retval PSA_ERROR_OCCUPIED_SLOT |
| 364 | There is already a key in the specified slot. |
| 365 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 366 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 367 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 368 | * \retval PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 369 | */ |
| 370 | psa_status_t psa_import_key(psa_key_slot_t key, |
| 371 | psa_key_type_t type, |
| 372 | const uint8_t *data, |
| 373 | size_t data_length); |
| 374 | |
| 375 | /** |
| 376 | * \brief Destroy a key. |
| 377 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 378 | * \retval PSA_SUCCESS |
| 379 | * \retval PSA_ERROR_EMPTY_SLOT |
| 380 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 381 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 382 | * \retval PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 383 | */ |
| 384 | psa_status_t psa_destroy_key(psa_key_slot_t key); |
| 385 | |
| 386 | /** |
| 387 | * \brief Get basic metadata about a key. |
| 388 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 389 | * \param key Slot whose content is queried. This must |
| 390 | * be an occupied key slot. |
| 391 | * \param type On success, the key type (a \c PSA_KEY_TYPE_XXX value). |
| 392 | * This may be a null pointer, in which case the key type |
| 393 | * is not written. |
| 394 | * \param bits On success, the key size in bits. |
| 395 | * This may be a null pointer, in which case the key type |
| 396 | * is not written. |
| 397 | * |
| 398 | * \retval PSA_SUCCESS |
| 399 | * \retval PSA_ERROR_EMPTY_SLOT |
| 400 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 401 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 402 | * \retval PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 403 | */ |
| 404 | psa_status_t psa_get_key_information(psa_key_slot_t key, |
| 405 | psa_key_type_t *type, |
| 406 | size_t *bits); |
| 407 | |
| 408 | /** |
| 409 | * \brief Export a key in binary format. |
| 410 | * |
| 411 | * The output of this function can be passed to psa_import_key() to |
| 412 | * create an equivalent object. |
| 413 | * |
| 414 | * If a key is created with psa_import_key() and then exported with |
| 415 | * this function, it is not guaranteed that the resulting data is |
| 416 | * identical: the implementation may choose a different representation |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 417 | * of the same key if the format permits it. |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 418 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 419 | * For standard key types, the output format is as follows: |
| 420 | * |
| 421 | * - For symmetric keys (including MAC keys), the format is the |
| 422 | * raw bytes of the key. |
| 423 | * - For DES, the key data consists of 8 bytes. The parity bits must be |
| 424 | * correct. |
| 425 | * - For Triple-DES, the format is the concatenation of the |
| 426 | * two or three DES keys. |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 427 | * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 428 | * is the non-encrypted DER representation defined by PKCS\#8 (RFC 5208) |
| 429 | * as PrivateKeyInfo. |
| 430 | * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format |
Gilles Peskine | 971f706 | 2018-03-20 17:52:58 +0100 | [diff] [blame] | 431 | * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo. |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 432 | * |
| 433 | * \param key Slot whose content is to be exported. This must |
| 434 | * be an occupied key slot. |
| 435 | * \param data Buffer where the key data is to be written. |
| 436 | * \param data_size Size of the \c data buffer in bytes. |
| 437 | * \param data_length On success, the number of bytes |
| 438 | * that make up the key data. |
| 439 | * |
| 440 | * \retval PSA_SUCCESS |
| 441 | * \retval PSA_ERROR_EMPTY_SLOT |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 442 | * \retval PSA_ERROR_NOT_PERMITTED |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 443 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 444 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 445 | * \retval PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 446 | */ |
| 447 | psa_status_t psa_export_key(psa_key_slot_t key, |
| 448 | uint8_t *data, |
| 449 | size_t data_size, |
| 450 | size_t *data_length); |
| 451 | |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 452 | /** |
| 453 | * \brief Export a public key or the public part of a key pair in binary format. |
| 454 | * |
| 455 | * The output of this function can be passed to psa_import_key() to |
| 456 | * create an object that is equivalent to the public key. |
| 457 | * |
| 458 | * For standard key types, the output format is as follows: |
| 459 | * |
| 460 | * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY), |
Gilles Peskine | 971f706 | 2018-03-20 17:52:58 +0100 | [diff] [blame] | 461 | * is the DER representation of the public key defined by RFC 5280 |
| 462 | * as SubjectPublicKeyInfo. |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 463 | * |
| 464 | * \param key Slot whose content is to be exported. This must |
| 465 | * be an occupied key slot. |
| 466 | * \param data Buffer where the key data is to be written. |
| 467 | * \param data_size Size of the \c data buffer in bytes. |
| 468 | * \param data_length On success, the number of bytes |
| 469 | * that make up the key data. |
| 470 | * |
| 471 | * \retval PSA_SUCCESS |
| 472 | * \retval PSA_ERROR_EMPTY_SLOT |
| 473 | * \retval PSA_ERROR_INVALID_ARGUMENT |
| 474 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 475 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 476 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 477 | */ |
| 478 | psa_status_t psa_export_public_key(psa_key_slot_t key, |
| 479 | uint8_t *data, |
| 480 | size_t data_size, |
| 481 | size_t *data_length); |
| 482 | |
| 483 | /**@}*/ |
| 484 | |
| 485 | /** \defgroup policy Key policies |
| 486 | * @{ |
| 487 | */ |
| 488 | |
| 489 | /** \brief Encoding of permitted usage on a key. */ |
| 490 | typedef uint32_t psa_key_usage_t; |
| 491 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 492 | /** Whether the key may be exported. |
| 493 | * |
| 494 | * A public key or the public part of a key pair may always be exported |
| 495 | * regardless of the value of this permission flag. |
| 496 | * |
| 497 | * If a key does not have export permission, implementations shall not |
| 498 | * allow the key to be exported in plain form from the cryptoprocessor, |
| 499 | * whether through psa_export_key() or through a proprietary interface. |
| 500 | * The key may however be exportable in a wrapped form, i.e. in a form |
| 501 | * where it is encrypted by another key. |
| 502 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 503 | #define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001) |
| 504 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 505 | /** Whether the key may be used to encrypt a message. |
| 506 | * |
| 507 | * For a key pair, this concerns the public key. |
| 508 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 509 | #define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100) |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 510 | |
| 511 | /** Whether the key may be used to decrypt a message. |
| 512 | * |
| 513 | * For a key pair, this concerns the private key. |
| 514 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 515 | #define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200) |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 516 | |
| 517 | /** Whether the key may be used to sign a message. |
| 518 | * |
| 519 | * For a key pair, this concerns the private key. |
| 520 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 521 | #define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400) |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 522 | |
| 523 | /** Whether the key may be used to verify a message signature. |
| 524 | * |
| 525 | * For a key pair, this concerns the public key. |
| 526 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 527 | #define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800) |
| 528 | |
| 529 | /** The type of the key policy data structure. |
| 530 | * |
| 531 | * This is an implementation-defined \c struct. Applications should not |
| 532 | * make any assumptions about the content of this structure except |
| 533 | * as directed by the documentation of a specific implementation. */ |
| 534 | typedef struct psa_key_policy_s psa_key_policy_t; |
| 535 | |
| 536 | /** \brief Initialize a key policy structure to a default that forbids all |
| 537 | * usage of the key. */ |
| 538 | void psa_key_policy_init(psa_key_policy_t *policy); |
| 539 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 540 | /** \brief Set the standard fields of a policy structure. |
| 541 | * |
| 542 | * Note that this function does not make any consistency check of the |
| 543 | * parameters. The values are only checked when applying the policy to |
| 544 | * a key slot with psa_set_key_policy(). |
| 545 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 546 | void psa_key_policy_set_usage(psa_key_policy_t *policy, |
| 547 | psa_key_usage_t usage, |
| 548 | psa_algorithm_t alg); |
| 549 | |
| 550 | psa_key_usage_t psa_key_policy_get_usage(psa_key_policy_t *policy); |
| 551 | |
| 552 | psa_algorithm_t psa_key_policy_get_algorithm(psa_key_policy_t *policy); |
| 553 | |
| 554 | /** \brief Set the usage policy on a key slot. |
| 555 | * |
| 556 | * This function must be called on an empty key slot, before importing, |
| 557 | * generating or creating a key in the slot. Changing the policy of an |
| 558 | * existing key is not permitted. |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 559 | * |
| 560 | * Implementations may set restrictions on supported key policies |
| 561 | * depending on the key type and the key slot. |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 562 | */ |
| 563 | psa_status_t psa_set_key_policy(psa_key_slot_t key, |
| 564 | const psa_key_policy_t *policy); |
| 565 | |
Gilles Peskine | 7e19853 | 2018-03-08 07:50:30 +0100 | [diff] [blame] | 566 | /** \brief Get the usage policy for a key slot. |
| 567 | */ |
Gilles Peskine | 7698bcf | 2018-03-03 21:30:44 +0100 | [diff] [blame] | 568 | psa_status_t psa_get_key_policy(psa_key_slot_t key, |
| 569 | psa_key_policy_t *policy); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 570 | |
| 571 | /**@}*/ |
| 572 | |
Gilles Peskine | 609b6a5 | 2018-03-03 21:31:50 +0100 | [diff] [blame] | 573 | /** \defgroup persistence Key lifetime |
| 574 | * @{ |
| 575 | */ |
| 576 | |
| 577 | /** Encoding of key lifetimes. |
| 578 | */ |
| 579 | typedef uint32_t psa_key_lifetime_t; |
| 580 | |
| 581 | /** A volatile key slot retains its content as long as the application is |
| 582 | * running. It is guaranteed to be erased on a power reset. |
| 583 | */ |
| 584 | #define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000) |
| 585 | |
| 586 | /** A persistent key slot retains its content as long as it is not explicitly |
| 587 | * destroyed. |
| 588 | */ |
| 589 | #define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001) |
| 590 | |
| 591 | /** A write-once key slot may not be modified once a key has been set. |
| 592 | * It will retain its content as long as the device remains operational. |
| 593 | */ |
| 594 | #define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff) |
| 595 | |
Gilles Peskine | d393e18 | 2018-03-08 07:49:16 +0100 | [diff] [blame] | 596 | /** \brief Retrieve the lifetime of a key slot. |
| 597 | * |
| 598 | * The assignment of lifetimes to slots is implementation-dependent. |
| 599 | */ |
Gilles Peskine | 609b6a5 | 2018-03-03 21:31:50 +0100 | [diff] [blame] | 600 | psa_status_t psa_get_key_lifetime(psa_key_slot_t key, |
| 601 | psa_key_lifetime_t *lifetime); |
| 602 | |
Gilles Peskine | d393e18 | 2018-03-08 07:49:16 +0100 | [diff] [blame] | 603 | /** \brief Change the lifetime of a key slot. |
| 604 | * |
| 605 | * Whether the lifetime of a key slot can be changed at all, and if so |
| 606 | * whether the lifetime of an occupied key slot can be chaned, is |
| 607 | * implementation-dependent. |
| 608 | */ |
| 609 | psa_status_t psa_set_key_lifetime(psa_key_slot_t key, |
| 610 | const psa_key_lifetime_t *lifetime); |
| 611 | |
Gilles Peskine | 609b6a5 | 2018-03-03 21:31:50 +0100 | [diff] [blame] | 612 | /**@}*/ |
| 613 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 614 | /** \defgroup hash Message digests |
| 615 | * @{ |
| 616 | */ |
| 617 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 618 | /** The type of the state data structure for multipart hash operations. |
| 619 | * |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 620 | * This is an implementation-defined \c struct. Applications should not |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 621 | * make any assumptions about the content of this structure except |
| 622 | * as directed by the documentation of a specific implementation. */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 623 | typedef struct psa_hash_operation_s psa_hash_operation_t; |
| 624 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 625 | /** The size of the output of psa_hash_finish(), in bytes. |
| 626 | * |
| 627 | * This is also the hash size that psa_hash_verify() expects. |
| 628 | * |
| 629 | * \param alg A hash algorithm (\c PSA_ALG_XXX value such that |
| 630 | * #PSA_ALG_IS_HASH(alg) is true). |
| 631 | * |
| 632 | * \return The hash size for the specified hash algorithm. |
| 633 | * If the hash algorithm is not recognized, return 0. |
| 634 | * An implementation may return either 0 or the correct size |
| 635 | * for a hash algorithm that it recognizes, but does not support. |
| 636 | */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 637 | #define PSA_HASH_FINAL_SIZE(alg) \ |
| 638 | ( \ |
| 639 | (alg) == PSA_ALG_MD2 ? 16 : \ |
| 640 | (alg) == PSA_ALG_MD4 ? 16 : \ |
| 641 | (alg) == PSA_ALG_MD5 ? 16 : \ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 642 | (alg) == PSA_ALG_RIPEMD160 ? 20 : \ |
| 643 | (alg) == PSA_ALG_SHA_1 ? 20 : \ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 644 | (alg) == PSA_ALG_SHA_224 ? 28 : \ |
| 645 | (alg) == PSA_ALG_SHA_256 ? 32 : \ |
| 646 | (alg) == PSA_ALG_SHA_384 ? 48 : \ |
| 647 | (alg) == PSA_ALG_SHA_512 ? 64 : \ |
| 648 | (alg) == PSA_ALG_SHA_512_224 ? 28 : \ |
| 649 | (alg) == PSA_ALG_SHA_512_256 ? 32 : \ |
| 650 | (alg) == PSA_ALG_SHA3_224 ? 28 : \ |
| 651 | (alg) == PSA_ALG_SHA3_256 ? 32 : \ |
| 652 | (alg) == PSA_ALG_SHA3_384 ? 48 : \ |
| 653 | (alg) == PSA_ALG_SHA3_512 ? 64 : \ |
| 654 | 0) |
| 655 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 656 | /** Start a multipart hash operation. |
| 657 | * |
| 658 | * The sequence of operations to calculate a hash (message digest) |
| 659 | * is as follows: |
| 660 | * -# Allocate an operation object which will be passed to all the functions |
| 661 | * listed here. |
| 662 | * -# Call psa_hash_start() to specify the algorithm. |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 663 | * -# Call psa_hash_update() zero, one or more times, passing a fragment |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 664 | * of the message each time. The hash that is calculated is the hash |
| 665 | * of the concatenation of these messages in order. |
| 666 | * -# To calculate the hash, call psa_hash_finish(). |
| 667 | * To compare the hash with an expected value, call psa_hash_verify(). |
| 668 | * |
| 669 | * The application may call psa_hash_abort() at any time after the operation |
| 670 | * has been initialized with psa_hash_start(). |
| 671 | * |
| 672 | * After a successful call to psa_hash_start(), the application must |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame^] | 673 | * eventually terminate the operation. The following events terminate an |
| 674 | * operation: |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 675 | * - A failed call to psa_hash_update(). |
| 676 | * - A call to psa_hash_final(), psa_hash_verify() or psa_hash_abort(). |
| 677 | * |
| 678 | * \param operation |
| 679 | * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value |
| 680 | * such that #PSA_ALG_IS_HASH(alg) is true). |
| 681 | * |
| 682 | * \retval PSA_SUCCESS |
| 683 | * Success. |
| 684 | * \retval PSA_ERROR_NOT_SUPPORTED |
| 685 | * \c alg is not supported or is not a hash algorithm. |
| 686 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 687 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 688 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 689 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 690 | */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 691 | psa_status_t psa_hash_start(psa_hash_operation_t *operation, |
| 692 | psa_algorithm_t alg); |
| 693 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 694 | /** Add a message fragment to a multipart hash operation. |
| 695 | * |
| 696 | * The application must call psa_hash_start() before calling this function. |
| 697 | * |
| 698 | * If this function returns an error status, the operation becomes inactive. |
| 699 | * |
| 700 | * \param operation Active hash operation. |
| 701 | * \param input Buffer containing the message fragment to hash. |
| 702 | * \param input_length Size of the \c input buffer in bytes. |
| 703 | * |
| 704 | * \retval PSA_SUCCESS |
| 705 | * Success. |
| 706 | * \retval PSA_ERROR_BAD_STATE |
| 707 | * The operation state is not valid (not started, or already completed). |
| 708 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 709 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 710 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 711 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 712 | */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 713 | psa_status_t psa_hash_update(psa_hash_operation_t *operation, |
| 714 | const uint8_t *input, |
| 715 | size_t input_length); |
| 716 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 717 | /** Finish the calculation of the hash of a message. |
| 718 | * |
| 719 | * The application must call psa_hash_start() before calling this function. |
| 720 | * This function calculates the hash of the message formed by concatenating |
| 721 | * the inputs passed to preceding calls to psa_hash_update(). |
| 722 | * |
| 723 | * When this function returns, the operation becomes inactive. |
| 724 | * |
| 725 | * \warning Applications should not call this function if they expect |
| 726 | * a specific value for the hash. Call psa_hash_verify() instead. |
| 727 | * Beware that comparing integrity or authenticity data such as |
| 728 | * hash values with a function such as \c memcmp is risky |
| 729 | * because the time taken by the comparison may leak information |
| 730 | * about the hashed data which could allow an attacker to guess |
| 731 | * a valid hash and thereby bypass security controls. |
| 732 | * |
| 733 | * \param operation Active hash operation. |
| 734 | * \param hash Buffer where the hash is to be written. |
| 735 | * \param hash_size Size of the \c hash buffer in bytes. |
| 736 | * \param hash_length On success, the number of bytes |
| 737 | * that make up the hash value. This is always |
| 738 | * #PSA_HASH_FINAL_SIZE(alg) where \c alg is the |
| 739 | * hash algorithm that is calculated. |
| 740 | * |
| 741 | * \retval PSA_SUCCESS |
| 742 | * Success. |
| 743 | * \retval PSA_ERROR_BAD_STATE |
| 744 | * The operation state is not valid (not started, or already completed). |
| 745 | * \retval PSA_ERROR_BUFFER_TOO_SMALL |
| 746 | * The size of the \c hash buffer is too small. You can determine a |
| 747 | * sufficient buffer size by calling #PSA_HASH_FINAL_SIZE(alg) |
| 748 | * where \c alg is the hash algorithm that is calculated. |
| 749 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 750 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 751 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 752 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 753 | */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 754 | psa_status_t psa_hash_finish(psa_hash_operation_t *operation, |
| 755 | uint8_t *hash, |
| 756 | size_t hash_size, |
| 757 | size_t *hash_length); |
| 758 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 759 | /** Finish the calculation of the hash of a message and compare it with |
| 760 | * an expected value. |
| 761 | * |
| 762 | * The application must call psa_hash_start() before calling this function. |
| 763 | * This function calculates the hash of the message formed by concatenating |
| 764 | * the inputs passed to preceding calls to psa_hash_update(). It then |
| 765 | * compares the calculated hash with the expected hash passed as a |
| 766 | * parameter to this function. |
| 767 | * |
| 768 | * When this function returns, the operation becomes inactive. |
| 769 | * |
| 770 | * \note Applications shall make the best effort to ensure that the |
| 771 | * comparison between the actual hash and the expected hash is performed |
| 772 | * in constant time. |
| 773 | * |
| 774 | * \param operation Active hash operation. |
| 775 | * \param hash Buffer containing the expected hash value. |
| 776 | * \param hash_length Size of the \c hash buffer in bytes. |
| 777 | * |
| 778 | * \retval PSA_SUCCESS |
| 779 | * The expected hash is identical to the actual hash of the message. |
| 780 | * \retval PSA_ERROR_INVALID_SIGNATURE |
| 781 | * The hash of the message was calculated successfully, but it |
| 782 | * differs from the expected hash. |
| 783 | * \retval PSA_ERROR_BAD_STATE |
| 784 | * The operation state is not valid (not started, or already completed). |
| 785 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 786 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 787 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 788 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 789 | */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 790 | psa_status_t psa_hash_verify(psa_hash_operation_t *operation, |
| 791 | const uint8_t *hash, |
| 792 | size_t hash_length); |
| 793 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 794 | /** Abort a hash operation. |
| 795 | * |
| 796 | * This function may be called at any time after psa_hash_start(). |
| 797 | * Aborting an operation frees all associated resources except for the |
| 798 | * \c operation structure itself. |
| 799 | * |
| 800 | * Implementation should strive to be robust and handle inactive hash |
| 801 | * operations safely (do nothing and return #PSA_ERROR_BAD_STATE). However, |
| 802 | * application writers should beware that uninitialized memory may happen |
| 803 | * to be indistinguishable from an active hash operation, and the behavior |
| 804 | * of psa_hash_abort() is undefined in this case. |
| 805 | * |
| 806 | * \param operation Active hash operation. |
| 807 | * |
| 808 | * \retval PSA_SUCCESS |
| 809 | * \retval PSA_ERROR_BAD_STATE |
| 810 | * \c operation is not an active hash operation. |
| 811 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 812 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 813 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 814 | */ |
| 815 | psa_status_t psa_hash_abort(psa_hash_operation_t *operation); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 816 | |
| 817 | /**@}*/ |
| 818 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 819 | /** \defgroup MAC Message authentication codes |
| 820 | * @{ |
| 821 | */ |
| 822 | |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 823 | /** The type of the state data structure for multipart MAC operations. |
| 824 | * |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 825 | * This is an implementation-defined \c struct. Applications should not |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 826 | * make any assumptions about the content of this structure except |
| 827 | * as directed by the documentation of a specific implementation. */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 828 | typedef struct psa_mac_operation_s psa_mac_operation_t; |
| 829 | |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 830 | /** The size of the output of psa_mac_finish(), in bytes. |
| 831 | * |
| 832 | * This is also the MAC size that psa_mac_verify() expects. |
| 833 | * |
| 834 | * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that |
| 835 | * #PSA_ALG_IS_MAC(alg) is true). |
| 836 | * |
| 837 | * \return The MAC size for the specified algorithm. |
| 838 | * If the MAC algorithm is not recognized, return 0. |
| 839 | * An implementation may return either 0 or the correct size |
| 840 | * for a MAC algorithm that it recognizes, but does not support. |
| 841 | */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 842 | #define PSA_MAC_FINAL_SIZE(key_type, key_bits, alg) \ |
| 843 | (PSA_ALG_IS_HMAC(alg) ? PSA_HASH_FINAL_SIZE(PSA_ALG_HMAC_HASH(alg)) : \ |
| 844 | PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) : \ |
| 845 | 0) |
| 846 | |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 847 | /** Start a multipart MAC operation. |
| 848 | * |
| 849 | * The sequence of operations to calculate a MAC (message authentication code) |
| 850 | * is as follows: |
| 851 | * -# Allocate an operation object which will be passed to all the functions |
| 852 | * listed here. |
| 853 | * -# Call psa_mac_start() to specify the algorithm and key. |
| 854 | * The key remains associated with the operation even if the content |
| 855 | * of the key slot changes. |
| 856 | * -# Call psa_mac_update() zero, one or more times, passing a fragment |
| 857 | * of the message each time. The MAC that is calculated is the MAC |
| 858 | * of the concatenation of these messages in order. |
| 859 | * -# To calculate the MAC, call psa_mac_finish(). |
| 860 | * To compare the MAC with an expected value, call psa_mac_verify(). |
| 861 | * |
| 862 | * The application may call psa_mac_abort() at any time after the operation |
| 863 | * has been initialized with psa_mac_start(). |
| 864 | * |
| 865 | * After a successful call to psa_mac_start(), the application must |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame^] | 866 | * eventually terminate the operation. The following events terminate an |
| 867 | * operation: |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 868 | * - A failed call to psa_mac_update(). |
| 869 | * - A call to psa_mac_final(), psa_mac_verify() or psa_mac_abort(). |
| 870 | * |
| 871 | * \param operation |
| 872 | * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value |
| 873 | * such that #PSA_ALG_IS_MAC(alg) is true). |
| 874 | * |
| 875 | * \retval PSA_SUCCESS |
| 876 | * Success. |
| 877 | * \retval PSA_ERROR_EMPTY_SLOT |
Gilles Peskine | 92b3073 | 2018-03-03 21:29:30 +0100 | [diff] [blame] | 878 | * \retval PSA_ERROR_NOT_PERMITTED |
Gilles Peskine | 7e4acc5 | 2018-02-16 21:24:11 +0100 | [diff] [blame] | 879 | * \retval PSA_ERROR_INVALID_ARGUMENT |
| 880 | * \c key is not compatible with \c alg. |
| 881 | * \retval PSA_ERROR_NOT_SUPPORTED |
| 882 | * \c alg is not supported or is not a MAC algorithm. |
| 883 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 884 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 885 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 886 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 887 | */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 888 | psa_status_t psa_mac_start(psa_mac_operation_t *operation, |
| 889 | psa_key_slot_t key, |
| 890 | psa_algorithm_t alg); |
| 891 | |
| 892 | psa_status_t psa_mac_update(psa_mac_operation_t *operation, |
| 893 | const uint8_t *input, |
| 894 | size_t input_length); |
| 895 | |
| 896 | psa_status_t psa_mac_finish(psa_mac_operation_t *operation, |
| 897 | uint8_t *mac, |
| 898 | size_t mac_size, |
| 899 | size_t *mac_length); |
| 900 | |
| 901 | psa_status_t psa_mac_verify(psa_mac_operation_t *operation, |
| 902 | const uint8_t *mac, |
| 903 | size_t mac_length); |
| 904 | |
| 905 | psa_status_t psa_mac_abort(psa_mac_operation_t *operation); |
| 906 | |
| 907 | /**@}*/ |
| 908 | |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 909 | /** \defgroup cipher Symmetric ciphers |
| 910 | * @{ |
| 911 | */ |
| 912 | |
| 913 | /** The type of the state data structure for multipart cipher operations. |
| 914 | * |
| 915 | * This is an implementation-defined \c struct. Applications should not |
| 916 | * make any assumptions about the content of this structure except |
| 917 | * as directed by the documentation of a specific implementation. */ |
| 918 | typedef struct psa_cipher_operation_s psa_cipher_operation_t; |
| 919 | |
| 920 | /** Set the key for a multipart symmetric encryption operation. |
| 921 | * |
| 922 | * The sequence of operations to encrypt a message with a symmetric cipher |
| 923 | * is as follows: |
| 924 | * -# Allocate an operation object which will be passed to all the functions |
| 925 | * listed here. |
| 926 | * -# Call psa_encrypt_setup() to specify the algorithm and key. |
| 927 | * The key remains associated with the operation even if the content |
| 928 | * of the key slot changes. |
| 929 | * -# Call either psa_encrypt_generate_iv() or psa_encrypt_set_iv() to |
| 930 | * generate or set the IV (initialization vector). You should use |
| 931 | * psa_encrypt_generate_iv() unless the protocol you are implementing |
| 932 | * requires a specific IV value. |
| 933 | * -# Call psa_cipher_update() zero, one or more times, passing a fragment |
| 934 | * of the message each time. |
| 935 | * -# Call psa_cipher_finish(). |
| 936 | * |
| 937 | * The application may call psa_cipher_abort() at any time after the operation |
| 938 | * has been initialized with psa_encrypt_setup(). |
| 939 | * |
| 940 | * After a successful call to psa_encrypt_setup(), the application must |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame^] | 941 | * eventually terminate the operation. The following events terminate an |
| 942 | * operation: |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 943 | * - A failed call to psa_encrypt_generate_iv(), psa_encrypt_set_iv() |
| 944 | * or psa_cipher_update(). |
| 945 | * - A call to psa_cipher_final() or psa_cipher_abort(). |
| 946 | * |
| 947 | * \param operation |
| 948 | * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value |
| 949 | * such that #PSA_ALG_IS_CIPHER(alg) is true). |
| 950 | * |
| 951 | * \retval PSA_SUCCESS |
| 952 | * Success. |
| 953 | * \retval PSA_ERROR_EMPTY_SLOT |
| 954 | * \retval PSA_ERROR_NOT_PERMITTED |
| 955 | * \retval PSA_ERROR_INVALID_ARGUMENT |
| 956 | * \c key is not compatible with \c alg. |
| 957 | * \retval PSA_ERROR_NOT_SUPPORTED |
| 958 | * \c alg is not supported or is not a cipher algorithm. |
| 959 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 960 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 961 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 962 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 963 | */ |
| 964 | psa_status_t psa_encrypt_setup(psa_cipher_operation_t *operation, |
| 965 | psa_key_slot_t key, |
| 966 | psa_algorithm_t alg); |
| 967 | |
| 968 | /** Set the key for a multipart symmetric decryption operation. |
| 969 | * |
| 970 | * The sequence of operations to decrypt a message with a symmetric cipher |
| 971 | * is as follows: |
| 972 | * -# Allocate an operation object which will be passed to all the functions |
| 973 | * listed here. |
| 974 | * -# Call psa_decrypt_setup() to specify the algorithm and key. |
| 975 | * The key remains associated with the operation even if the content |
| 976 | * of the key slot changes. |
| 977 | * -# Call psa_cipher_update() with the IV (initialization vector) for the |
| 978 | * decryption. If the IV is prepended to the ciphertext, you can call |
| 979 | * psa_cipher_update() on a buffer containing the IV followed by the |
| 980 | * beginning of the message. |
| 981 | * -# Call psa_cipher_update() zero, one or more times, passing a fragment |
| 982 | * of the message each time. |
| 983 | * -# Call psa_cipher_finish(). |
| 984 | * |
| 985 | * The application may call psa_cipher_abort() at any time after the operation |
| 986 | * has been initialized with psa_encrypt_setup(). |
| 987 | * |
| 988 | * After a successful call to psa_decrypt_setup(), the application must |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame^] | 989 | * eventually terminate the operation. The following events terminate an |
| 990 | * operation: |
Gilles Peskine | 428dc5a | 2018-03-03 21:27:18 +0100 | [diff] [blame] | 991 | * - A failed call to psa_cipher_update(). |
| 992 | * - A call to psa_cipher_final() or psa_cipher_abort(). |
| 993 | * |
| 994 | * \param operation |
| 995 | * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value |
| 996 | * such that #PSA_ALG_IS_CIPHER(alg) is true). |
| 997 | * |
| 998 | * \retval PSA_SUCCESS |
| 999 | * Success. |
| 1000 | * \retval PSA_ERROR_EMPTY_SLOT |
| 1001 | * \retval PSA_ERROR_NOT_PERMITTED |
| 1002 | * \retval PSA_ERROR_INVALID_ARGUMENT |
| 1003 | * \c key is not compatible with \c alg. |
| 1004 | * \retval PSA_ERROR_NOT_SUPPORTED |
| 1005 | * \c alg is not supported or is not a cipher algorithm. |
| 1006 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 1007 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 1008 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 1009 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 1010 | */ |
| 1011 | psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation, |
| 1012 | psa_key_slot_t key, |
| 1013 | psa_algorithm_t alg); |
| 1014 | |
| 1015 | psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation, |
| 1016 | unsigned char *iv, |
| 1017 | size_t iv_size, |
| 1018 | size_t *iv_length); |
| 1019 | |
| 1020 | psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation, |
| 1021 | const unsigned char *iv, |
| 1022 | size_t iv_length); |
| 1023 | |
| 1024 | psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, |
| 1025 | const uint8_t *input, |
| 1026 | size_t input_length); |
| 1027 | |
| 1028 | psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, |
| 1029 | uint8_t *mac, |
| 1030 | size_t mac_size, |
| 1031 | size_t *mac_length); |
| 1032 | |
| 1033 | psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation); |
| 1034 | |
| 1035 | /**@}*/ |
| 1036 | |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1037 | /** \defgroup aead Authenticated encryption with associated data (AEAD) |
| 1038 | * @{ |
| 1039 | */ |
| 1040 | |
| 1041 | /** The type of the state data structure for multipart AEAD operations. |
| 1042 | * |
| 1043 | * This is an implementation-defined \c struct. Applications should not |
| 1044 | * make any assumptions about the content of this structure except |
| 1045 | * as directed by the documentation of a specific implementation. */ |
| 1046 | typedef struct psa_aead_operation_s psa_aead_operation_t; |
| 1047 | |
| 1048 | /** Set the key for a multipart authenticated encryption operation. |
| 1049 | * |
| 1050 | * The sequence of operations to authenticate-and-encrypt a message |
| 1051 | * is as follows: |
| 1052 | * -# Allocate an operation object which will be passed to all the functions |
| 1053 | * listed here. |
| 1054 | * -# Call psa_aead_encrypt_setup() to specify the algorithm and key. |
| 1055 | * The key remains associated with the operation even if the content |
| 1056 | * of the key slot changes. |
| 1057 | * -# Call either psa_aead_generate_iv() or psa_aead_set_iv() to |
| 1058 | * generate or set the IV (initialization vector). You should use |
| 1059 | * psa_encrypt_generate_iv() unless the protocol you are implementing |
| 1060 | * requires a specific IV value. |
| 1061 | * -# Call psa_aead_update_ad() to pass the associated data that is |
| 1062 | * to be authenticated but not encrypted. You may omit this step if |
| 1063 | * there is no associated data. |
| 1064 | * -# Call psa_aead_update() zero, one or more times, passing a fragment |
| 1065 | * of the data to encrypt each time. |
| 1066 | * -# Call psa_aead_finish(). |
| 1067 | * |
| 1068 | * The application may call psa_aead_abort() at any time after the operation |
| 1069 | * has been initialized with psa_aead_encrypt_setup(). |
| 1070 | * |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame^] | 1071 | * After a successful call to psa_aead_encrypt_setup(), the application must |
| 1072 | * eventually terminate the operation. The following events terminate an |
| 1073 | * operation: |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1074 | * - A failed call to psa_aead_generate_iv(), psa_aead_set_iv(), |
| 1075 | * psa_aead_update_ad() or psa_aead_update(). |
| 1076 | * - A call to psa_aead_final() or psa_aead_abort(). |
| 1077 | * |
| 1078 | * \param operation |
| 1079 | * \param alg The AEAD algorithm to compute (\c PSA_ALG_XXX value |
| 1080 | * such that #PSA_ALG_IS_AEAD(alg) is true). |
| 1081 | * |
| 1082 | * \retval PSA_SUCCESS |
| 1083 | * Success. |
| 1084 | * \retval PSA_ERROR_EMPTY_SLOT |
| 1085 | * \retval PSA_ERROR_NOT_PERMITTED |
| 1086 | * \retval PSA_ERROR_INVALID_ARGUMENT |
| 1087 | * \c key is not compatible with \c alg. |
| 1088 | * \retval PSA_ERROR_NOT_SUPPORTED |
| 1089 | * \c alg is not supported or is not an AEAD algorithm. |
| 1090 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 1091 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 1092 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 1093 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 1094 | */ |
| 1095 | psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation, |
| 1096 | psa_key_slot_t key, |
| 1097 | psa_algorithm_t alg); |
| 1098 | |
| 1099 | /** Set the key for a multipart authenticated decryption operation. |
| 1100 | * |
| 1101 | * The sequence of operations to authenticated and decrypt a message |
| 1102 | * is as follows: |
| 1103 | * -# Allocate an operation object which will be passed to all the functions |
| 1104 | * listed here. |
| 1105 | * -# Call psa_aead_decrypt_setup() to specify the algorithm and key. |
| 1106 | * The key remains associated with the operation even if the content |
| 1107 | * of the key slot changes. |
| 1108 | * -# Call psa_aead_set_iv() to pass the initialization vector (IV) |
| 1109 | * for the authenticated decryption. |
| 1110 | * -# Call psa_aead_update_ad() to pass the associated data that is |
| 1111 | * to be authenticated but not encrypted. You may omit this step if |
| 1112 | * there is no associated data. |
| 1113 | * -# Call psa_aead_update() zero, one or more times, passing a fragment |
| 1114 | * of the data to decrypt each time. |
| 1115 | * -# Call psa_aead_finish(). |
| 1116 | * |
| 1117 | * The application may call psa_aead_abort() at any time after the operation |
| 1118 | * has been initialized with psa_aead_decrypt_setup(). |
| 1119 | * |
Gilles Peskine | ed52297 | 2018-03-20 17:54:15 +0100 | [diff] [blame^] | 1120 | * After a successful call to psa_aead_decrypt_setup(), the application must |
| 1121 | * eventually terminate the operation. The following events terminate an |
| 1122 | * operation: |
Gilles Peskine | 3b55571 | 2018-03-03 21:27:57 +0100 | [diff] [blame] | 1123 | * - A failed call to psa_aead_update(). |
| 1124 | * - A call to psa_cipher_final() or psa_cipher_abort(). |
| 1125 | * |
| 1126 | * \param operation |
| 1127 | * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value |
| 1128 | * such that #PSA_ALG_IS_CIPHER(alg) is true). |
| 1129 | * |
| 1130 | * \retval PSA_SUCCESS |
| 1131 | * Success. |
| 1132 | * \retval PSA_ERROR_EMPTY_SLOT |
| 1133 | * \retval PSA_ERROR_NOT_PERMITTED |
| 1134 | * \retval PSA_ERROR_INVALID_ARGUMENT |
| 1135 | * \c key is not compatible with \c alg. |
| 1136 | * \retval PSA_ERROR_NOT_SUPPORTED |
| 1137 | * \c alg is not supported or is not a cipher algorithm. |
| 1138 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 1139 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 1140 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 1141 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 1142 | */ |
| 1143 | psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation, |
| 1144 | psa_key_slot_t key, |
| 1145 | psa_algorithm_t alg); |
| 1146 | |
| 1147 | psa_status_t psa_aead_generate_iv(psa_aead_operation_t *operation, |
| 1148 | unsigned char *iv, |
| 1149 | size_t iv_size, |
| 1150 | size_t *iv_length); |
| 1151 | |
| 1152 | psa_status_t psa_aead_set_iv(psa_aead_operation_t *operation, |
| 1153 | const unsigned char *iv, |
| 1154 | size_t iv_length); |
| 1155 | |
| 1156 | psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation, |
| 1157 | const uint8_t *input, |
| 1158 | size_t input_length); |
| 1159 | |
| 1160 | psa_status_t psa_aead_update(psa_aead_operation_t *operation, |
| 1161 | const uint8_t *input, |
| 1162 | size_t input_length); |
| 1163 | |
| 1164 | psa_status_t psa_aead_finish(psa_aead_operation_t *operation, |
| 1165 | uint8_t *tag, |
| 1166 | size_t tag_size, |
| 1167 | size_t *tag_length); |
| 1168 | |
| 1169 | psa_status_t psa_aead_verify(psa_aead_operation_t *operation, |
| 1170 | uint8_t *tag, |
| 1171 | size_t tag_length); |
| 1172 | |
| 1173 | psa_status_t psa_aead_abort(psa_aead_operation_t *operation); |
| 1174 | |
| 1175 | /**@}*/ |
| 1176 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1177 | /** \defgroup asymmetric Asymmetric cryptography |
| 1178 | * @{ |
| 1179 | */ |
| 1180 | |
| 1181 | /** |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 1182 | * \brief Maximum ECDSA signature size for a given curve bit size |
| 1183 | * |
| 1184 | * \param curve_bits Curve size in bits |
| 1185 | * \return Maximum signature size in bytes |
| 1186 | * |
| 1187 | * \note This macro returns a compile-time constant if its argument is one. |
| 1188 | * |
| 1189 | * \warning This macro may evaluate its argument multiple times. |
| 1190 | */ |
| 1191 | /* |
| 1192 | * RFC 4492 page 20: |
| 1193 | * |
| 1194 | * Ecdsa-Sig-Value ::= SEQUENCE { |
| 1195 | * r INTEGER, |
| 1196 | * s INTEGER |
| 1197 | * } |
| 1198 | * |
| 1199 | * Size is at most |
| 1200 | * 1 (tag) + 1 (len) + 1 (initial 0) + curve_bytes for each of r and s, |
| 1201 | * twice that + 1 (tag) + 2 (len) for the sequence |
| 1202 | * (assuming curve_bytes is less than 126 for r and s, |
| 1203 | * and less than 124 (total len <= 255) for the sequence) |
| 1204 | */ |
| 1205 | #define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \ |
| 1206 | ( /*T,L of SEQUENCE*/ ((curve_bits) >= 61 * 8 ? 3 : 2) + \ |
| 1207 | /*T,L of r,s*/ 2 * (((curve_bits) >= 127 * 8 ? 3 : 2) + \ |
| 1208 | /*V of r,s*/ ((curve_bits) + 8) / 8)) |
| 1209 | |
| 1210 | |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1211 | /** Safe signature buffer size for psa_asymmetric_sign(). |
| 1212 | * |
| 1213 | * This macro returns a safe buffer size for a signature using a key |
| 1214 | * of the specified type and size, with the specified algorithm. |
| 1215 | * Note that the actual size of the signature may be smaller |
| 1216 | * (some algorithms produce a variable-size signature). |
| 1217 | * |
| 1218 | * \warning This function may call its arguments multiple times or |
| 1219 | * zero times, so you should not pass arguments that contain |
| 1220 | * side effects. |
| 1221 | * |
| 1222 | * \param key_type An asymmetric key type (this may indifferently be a |
| 1223 | * key pair type or a public key type). |
| 1224 | * \param key_bits The size of the key in bits. |
| 1225 | * \param alg The signature algorithm. |
| 1226 | * |
| 1227 | * \return If the parameters are valid and supported, return |
| 1228 | * a buffer size in bytes that guarantees that |
| 1229 | * psa_asymmetric_sign() will not fail with |
| 1230 | * #PSA_ERROR_BUFFER_TOO_SMALL. |
| 1231 | * If the parameters are a valid combination that is not supported |
| 1232 | * by the implementation, this macro either shall return either a |
| 1233 | * sensible size or 0. |
| 1234 | * If the parameters are not valid, the |
| 1235 | * return value is unspecified. |
| 1236 | * |
| 1237 | */ |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 1238 | #define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \ |
Gilles Peskine | 2905a7a | 2018-03-07 16:39:31 +0100 | [diff] [blame] | 1239 | (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \ |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 1240 | PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \ |
| 1241 | 0) |
| 1242 | |
| 1243 | /** |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1244 | * \brief Sign a hash or short message with a private key. |
| 1245 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1246 | * \param key Key slot containing an asymmetric key pair. |
| 1247 | * \param alg A signature algorithm that is compatible with |
| 1248 | * the type of \c key. |
| 1249 | * \param hash The message to sign. |
| 1250 | * \param hash_length Size of the \c hash buffer in bytes. |
| 1251 | * \param salt A salt or label, if supported by the signature |
| 1252 | * algorithm. |
| 1253 | * If the signature algorithm does not support a |
| 1254 | * salt, pass \c NULL. |
| 1255 | * If the signature algorithm supports an optional |
| 1256 | * salt and you do not want to pass a salt, |
| 1257 | * pass \c NULL. |
| 1258 | * \param salt_length Size of the \c salt buffer in bytes. |
| 1259 | * If \c salt is \c NULL, pass 0. |
| 1260 | * \param signature Buffer where the signature is to be written. |
| 1261 | * \param signature_size Size of the \c signature buffer in bytes. |
| 1262 | * \param signature_length On success, the number of bytes |
| 1263 | * that make up the returned signature value. |
| 1264 | * This is at most #PSA_HASH_FINAL_SIZE(alg) |
| 1265 | * (note that it may be less). |
| 1266 | * |
| 1267 | * \retval PSA_SUCCESS |
| 1268 | * \retval PSA_ERROR_BUFFER_TOO_SMALL |
| 1269 | * The size of the \c signature buffer is too small. You can |
| 1270 | * determine a sufficient buffer size by calling |
| 1271 | * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) |
| 1272 | * where \c key_type and \c key_bits are the type and bit-size |
| 1273 | * respectively of \c key. |
| 1274 | * \retval PSA_ERROR_NOT_SUPPORTED |
| 1275 | * \retval PSA_ERROR_INVALID_ARGUMENT |
| 1276 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 1277 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 1278 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 1279 | * \retval PSA_ERROR_TAMPERING_DETECTED |
| 1280 | * \retval PSA_ERROR_INSUFFICIENT_ENTROPY |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1281 | */ |
| 1282 | psa_status_t psa_asymmetric_sign(psa_key_slot_t key, |
| 1283 | psa_algorithm_t alg, |
| 1284 | const uint8_t *hash, |
| 1285 | size_t hash_length, |
| 1286 | const uint8_t *salt, |
| 1287 | size_t salt_length, |
| 1288 | uint8_t *signature, |
| 1289 | size_t signature_size, |
| 1290 | size_t *signature_length); |
| 1291 | |
| 1292 | /** |
| 1293 | * \brief Verify the signature a hash or short message using a public key. |
| 1294 | * |
Gilles Peskine | 308b91d | 2018-02-08 09:47:44 +0100 | [diff] [blame] | 1295 | * \param key Key slot containing a public key or an |
| 1296 | * asymmetric key pair. |
| 1297 | * \param alg A signature algorithm that is compatible with |
| 1298 | * the type of \c key. |
| 1299 | * \param hash The message whose signature is to be verified. |
| 1300 | * \param hash_length Size of the \c hash buffer in bytes. |
| 1301 | * \param salt A salt or label, if supported by the signature |
| 1302 | * algorithm. |
| 1303 | * If the signature algorithm does not support a |
| 1304 | * salt, pass \c NULL. |
| 1305 | * If the signature algorithm supports an optional |
| 1306 | * salt and you do not want to pass a salt, |
| 1307 | * pass \c NULL. |
| 1308 | * \param salt_length Size of the \c salt buffer in bytes. |
| 1309 | * If \c salt is \c NULL, pass 0. |
| 1310 | * \param signature Buffer containing the signature to verify. |
| 1311 | * \param signature_size Size of the \c signature buffer in bytes. |
| 1312 | * |
| 1313 | * \retval PSA_SUCCESS |
| 1314 | * The signature is valid. |
| 1315 | * \retval PSA_ERROR_INVALID_SIGNATURE |
| 1316 | * The calculation was perfomed successfully, but the passed |
| 1317 | * signature is not a valid signature. |
| 1318 | * \retval PSA_ERROR_NOT_SUPPORTED |
| 1319 | * \retval PSA_ERROR_INVALID_ARGUMENT |
| 1320 | * \retval PSA_ERROR_INSUFFICIENT_MEMORY |
| 1321 | * \retval PSA_ERROR_COMMUNICATION_FAILURE |
| 1322 | * \retval PSA_ERROR_HARDWARE_FAILURE |
| 1323 | * \retval PSA_ERROR_TAMPERING_DETECTED |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1324 | */ |
| 1325 | psa_status_t psa_asymmetric_verify(psa_key_slot_t key, |
| 1326 | psa_algorithm_t alg, |
| 1327 | const uint8_t *hash, |
| 1328 | size_t hash_length, |
| 1329 | const uint8_t *salt, |
| 1330 | size_t salt_length, |
| 1331 | uint8_t *signature, |
| 1332 | size_t signature_size); |
| 1333 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1334 | /**@}*/ |
| 1335 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1336 | #ifdef __cplusplus |
| 1337 | } |
| 1338 | #endif |
| 1339 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 1340 | /* The file "crypto_struct.h" contains definitions for |
| 1341 | * implementation-specific structs that are declared above. */ |
| 1342 | #include "crypto_struct.h" |
| 1343 | |
| 1344 | /* The file "crypto_extra.h" contains vendor-specific definitions. This |
| 1345 | * can include vendor-defined algorithms, extra functions, etc. */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1346 | #include "crypto_extra.h" |
| 1347 | |
| 1348 | #endif /* PSA_CRYPTO_H */ |