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