Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 1 | /* |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019, Arm Limited. All rights reserved. |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef __TFM_CRYPTO_API_H__ |
| 9 | #define __TFM_CRYPTO_API_H__ |
| 10 | |
| 11 | #ifdef __cplusplus |
| 12 | extern "C" { |
| 13 | #endif |
| 14 | |
| 15 | #include <stdint.h> |
| 16 | #include "tfm_crypto_defs.h" |
| 17 | #include "psa_crypto.h" |
| 18 | |
| 19 | /** |
| 20 | * \brief List of possible operation types supported by the TFM based |
| 21 | * implementation. This type is needed by the operation allocation, |
| 22 | * lookup and release functions. |
| 23 | * |
| 24 | */ |
| 25 | enum tfm_crypto_operation_type { |
| 26 | TFM_CRYPTO_OPERATION_NONE = 0, |
| 27 | TFM_CRYPTO_CIPHER_OPERATION = 1, |
| 28 | TFM_CRYPTO_MAC_OPERATION = 2, |
| 29 | TFM_CRYPTO_HASH_OPERATION = 3, |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 30 | |
| 31 | /* Used to force the enum size */ |
| 32 | TFM_CRYPTO_OPERATION_TYPE_MAX = INT_MAX |
| 33 | }; |
| 34 | |
| 35 | /** |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 36 | * \brief Initialise the service |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 37 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 38 | * \return Return values as described in \ref tfm_crypto_err_t |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 39 | */ |
| 40 | enum tfm_crypto_err_t tfm_crypto_init(void); |
| 41 | |
| 42 | /** |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 43 | * \brief Initialise the Key module |
| 44 | * |
| 45 | * \return Return values as described in \ref tfm_crypto_err_t |
| 46 | */ |
| 47 | enum tfm_crypto_err_t tfm_crypto_init_key(void); |
| 48 | |
| 49 | /** |
| 50 | * \brief Initialise the Alloc module |
| 51 | * |
| 52 | * \return Return values as described in \ref tfm_crypto_err_t |
| 53 | */ |
| 54 | enum tfm_crypto_err_t tfm_crypto_init_alloc(void); |
| 55 | |
| 56 | /** |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame^] | 57 | * \brief Allocate an operation context in the backend |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 58 | * |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame^] | 59 | * \param[in] type Type of the operation context to allocate |
| 60 | * \param[out] oper Pointer to the frontend operation |
| 61 | * \param[out ctx Double pointer to the corresponding context |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 62 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 63 | * \return Return values as described in \ref tfm_crypto_err_t |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 64 | */ |
| 65 | enum tfm_crypto_err_t tfm_crypto_operation_alloc( |
| 66 | enum tfm_crypto_operation_type type, |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame^] | 67 | void *oper, |
| 68 | void **ctx); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 69 | /** |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame^] | 70 | * \brief Release an operation context in the backend |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 71 | * |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame^] | 72 | * \param[in] type Type of the operation context to release |
| 73 | * \param[in/out] oper Pointer to the frontend operation for the release |
| 74 | * of the corresponding backend context |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 75 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 76 | * \return Return values as described in \ref tfm_crypto_err_t |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 77 | */ |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame^] | 78 | enum tfm_crypto_err_t tfm_crypto_operation_release( |
| 79 | enum tfm_crypto_operation_type type, |
| 80 | void *oper); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 81 | /** |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame^] | 82 | * \brief Look up an operation context in the backend for the corresponding |
| 83 | * frontend operation |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 84 | * |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame^] | 85 | * \param[in] type Type of the operation context to look up |
| 86 | * \param[in] oper Pointer to the frontend operation |
| 87 | * \param[out] ctx Double pointer to the corresponding context |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 88 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 89 | * \return Return values as described in \ref tfm_crypto_err_t |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 90 | */ |
| 91 | enum tfm_crypto_err_t tfm_crypto_operation_lookup( |
| 92 | enum tfm_crypto_operation_type type, |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame^] | 93 | void *oper, |
| 94 | void **ctx); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 95 | /** |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 96 | * \brief Retrieve a key from the provided key slot according to the key |
| 97 | * policy and algorithm provided. This function is expected to be |
| 98 | * called intra-service |
| 99 | * |
| 100 | * \param[in] key Key slot |
| 101 | * \param[in] usage Usage policy to be used on the retrieved key |
| 102 | * \param[in] alg Algorithm to be used for the retrieved key |
| 103 | * \param[out] data Buffer to hold the exported key |
| 104 | * \param[in] data_size Length of the buffer pointed to by data |
| 105 | * \param[out] data_length Length of the exported key |
| 106 | * |
| 107 | * \return Return values as described in \ref tfm_crypto_err_t |
| 108 | */ |
| 109 | enum tfm_crypto_err_t tfm_crypto_get_key(psa_key_slot_t key, |
| 110 | psa_key_usage_t usage, |
| 111 | psa_algorithm_t alg, |
| 112 | uint8_t *data, |
| 113 | size_t data_size, |
| 114 | size_t *data_length); |
| 115 | /** |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 116 | * \brief Import the key data in the provided key slot |
| 117 | * |
| 118 | * \param[in] key Key slot |
| 119 | * \param[in] type Key type |
| 120 | * \param[in] data Key data to import |
| 121 | * \param[in] data_length Length in bytes of the data field |
| 122 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 123 | * \return Return values as described in \ref tfm_crypto_err_t |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 124 | */ |
| 125 | enum tfm_crypto_err_t tfm_crypto_import_key(psa_key_slot_t key, |
| 126 | psa_key_type_t type, |
| 127 | const uint8_t *data, |
| 128 | size_t data_length); |
| 129 | /** |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 130 | * \brief Destroy the key in the provided key slot |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 131 | * |
| 132 | * \param[in] key Key slot |
| 133 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 134 | * \return Return values as described in \ref tfm_crypto_err_t |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 135 | */ |
| 136 | enum tfm_crypto_err_t tfm_crypto_destroy_key(psa_key_slot_t key); |
| 137 | |
| 138 | /** |
| 139 | * \brief Retrieve key information for the provided key slot |
| 140 | * |
| 141 | * \param[in] key Key slot |
| 142 | * \param[out] type Key type associated to the key slot requested |
| 143 | * \param[out] bits Length in bits of the key in the requested slot |
| 144 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 145 | * \return Return values as described in \ref tfm_crypto_err_t |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 146 | */ |
| 147 | enum tfm_crypto_err_t tfm_crypto_get_key_information(psa_key_slot_t key, |
| 148 | psa_key_type_t *type, |
| 149 | size_t *bits); |
| 150 | /** |
| 151 | * \brief Export the key contained in the provided key slot |
| 152 | * |
| 153 | * \param[in] key Key slot |
| 154 | * \param[out] data Buffer to hold the exported key |
| 155 | * \param[in] data_size Length of the buffer pointed to by data |
| 156 | * \param[out] data_length Length of the exported key |
| 157 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 158 | * \return Return values as described in \ref tfm_crypto_err_t |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 159 | */ |
| 160 | enum tfm_crypto_err_t tfm_crypto_export_key(psa_key_slot_t key, |
| 161 | uint8_t *data, |
| 162 | size_t data_size, |
| 163 | size_t *data_length); |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 164 | |
| 165 | /** |
| 166 | * \brief Initialise the key policy to a default that forbids any use of the |
| 167 | * key |
| 168 | * |
| 169 | * \param[out] policy Key policy to initialise |
| 170 | * |
| 171 | * \return Return values as described in \ref tfm_crypto_err_t |
| 172 | */ |
| 173 | enum tfm_crypto_err_t tfm_crypto_key_policy_init(psa_key_policy_t *policy); |
| 174 | |
| 175 | /** |
| 176 | * \brief Set the permitted usage and algorithm for the provided key policy |
| 177 | * |
| 178 | * \param[out] policy Key policy to modify |
| 179 | * \param[in] usage Permitted usage |
| 180 | * \param[in] alg Permitted algorithm |
| 181 | * |
| 182 | * \return Return values as described in \ref tfm_crypto_err_t |
| 183 | */ |
| 184 | enum tfm_crypto_err_t tfm_crypto_key_policy_set_usage(psa_key_policy_t *policy, |
| 185 | psa_key_usage_t usage, |
| 186 | psa_algorithm_t alg); |
| 187 | |
| 188 | /** |
| 189 | * \brief Get the permitted usage for the provided key policy |
| 190 | * |
| 191 | * \param[in] policy Key policy |
| 192 | * \param[out] usage Permitted usage for this key policy |
| 193 | * |
| 194 | * \return Return values as described in \ref tfm_crypto_err_t |
| 195 | */ |
| 196 | enum tfm_crypto_err_t tfm_crypto_key_policy_get_usage( |
| 197 | const psa_key_policy_t *policy, |
| 198 | psa_key_usage_t *usage); |
| 199 | |
| 200 | /** |
| 201 | * \brief Get the permitted algorithm for the provided key policy |
| 202 | * |
| 203 | * \param[in] policy Key policy |
| 204 | * \param[out] alg Permitted algorithm for this key policy |
| 205 | * |
| 206 | * \return Return values as described in \ref tfm_crypto_err_t |
| 207 | */ |
| 208 | enum tfm_crypto_err_t tfm_crypto_key_policy_get_algorithm( |
| 209 | const psa_key_policy_t *policy, |
| 210 | psa_algorithm_t *alg); |
| 211 | |
| 212 | /** |
| 213 | * \brief Set the key policy for the provided key slot |
| 214 | * |
| 215 | * \param[in] key Key slot |
| 216 | * \param[in] policy Key policy |
| 217 | * |
| 218 | * \return Return values as described in \ref tfm_crypto_err_t |
| 219 | */ |
| 220 | enum tfm_crypto_err_t tfm_crypto_set_key_policy(psa_key_slot_t key, |
| 221 | const psa_key_policy_t *policy); |
| 222 | |
| 223 | /** |
| 224 | * \brief Get the key policy for the provided key slot |
| 225 | * |
| 226 | * \param[in] key Key slot |
| 227 | * \param[out] policy Key policy |
| 228 | * |
| 229 | * \return Return values as described in \ref tfm_crypto_err_t |
| 230 | */ |
| 231 | enum tfm_crypto_err_t tfm_crypto_get_key_policy(psa_key_slot_t key, |
| 232 | psa_key_policy_t *policy); |
| 233 | |
| 234 | /** |
| 235 | * \brief Set the lifetime for the provided key slot |
| 236 | * |
| 237 | * \param[in] key Key slot |
| 238 | * \param[in] lifetime Lifetime value |
| 239 | * |
| 240 | * \return Return values as described in \ref tfm_crypto_err_t |
| 241 | */ |
| 242 | enum tfm_crypto_err_t tfm_crypto_set_key_lifetime(psa_key_slot_t key, |
| 243 | psa_key_lifetime_t lifetime); |
| 244 | |
| 245 | /** |
| 246 | * \brief Get the lifetime for the provided key slot |
| 247 | * |
| 248 | * \param[in] key Key slot |
| 249 | * \param[out] lifetime Lifetime value |
| 250 | * |
| 251 | * \return Return values as described in \ref tfm_crypto_err_t |
| 252 | */ |
| 253 | enum tfm_crypto_err_t tfm_crypto_get_key_lifetime(psa_key_slot_t key, |
| 254 | psa_key_lifetime_t *lifetime); |
| 255 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 256 | /** |
| 257 | * \brief Export the public key contained in the provided key slot |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 258 | * for an asymmetric key pair |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 259 | * |
| 260 | * \param[in] key Key slot |
| 261 | * \param[out] data Buffer to hold the exported key |
| 262 | * \param[in] data_size Length of the buffer pointed to by data |
| 263 | * \param[out] data_length Length of the exported key |
| 264 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 265 | * \return Return values as described in \ref tfm_crypto_err_t |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 266 | */ |
| 267 | enum tfm_crypto_err_t tfm_crypto_export_public_key(psa_key_slot_t key, |
| 268 | uint8_t *data, |
| 269 | size_t data_size, |
| 270 | size_t *data_length); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 271 | /** |
| 272 | * \brief Set the initialisation vector on the provided cipher operation |
| 273 | * |
| 274 | * \param[in] operation Cipher operation context |
| 275 | * \param[in] iv Buffer that contains the IV |
| 276 | * \param[in] iv_length Length of the provided IV |
| 277 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 278 | * \return Return values as described in \ref tfm_crypto_err_t |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 279 | */ |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 280 | enum tfm_crypto_err_t tfm_crypto_cipher_set_iv( |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 281 | psa_cipher_operation_t *operation, |
| 282 | const unsigned char *iv, |
| 283 | size_t iv_length); |
| 284 | /** |
| 285 | * \brief Set the cipher operation using the provided algorithm and key slot, |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 286 | * for encryption context |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 287 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 288 | * \note A successful call to this function initialises a cipher operation |
| 289 | * context which will be referred using the operation parameter |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 290 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 291 | * \param[out] operation Cipher operation context |
| 292 | * \param[in] key Key slot to bind to the cipher context |
| 293 | * \param[in] alg Algorithm to use for the cipher operation |
| 294 | * |
| 295 | * \return Return values as described in \ref tfm_crypto_err_t |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 296 | */ |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 297 | enum tfm_crypto_err_t tfm_crypto_cipher_encrypt_setup( |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 298 | psa_cipher_operation_t *operation, |
| 299 | psa_key_slot_t key, |
| 300 | psa_algorithm_t alg); |
| 301 | /** |
| 302 | * \brief Set the cipher operation using the provided algorithm and key slot, |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 303 | * for decryption context |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 304 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 305 | * \note A successful call to this function initialises a cipher operation |
| 306 | * context which will be referred using the operation parameter |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 307 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 308 | * \param[out] operation Cipher operation context |
| 309 | * \param[in] key Key slot to bind to the cipher context |
| 310 | * \param[in] alg Algorithm to use for the cipher operation |
| 311 | * |
| 312 | * \return Return values as described in \ref tfm_crypto_err_t |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 313 | */ |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 314 | enum tfm_crypto_err_t tfm_crypto_cipher_decrypt_setup( |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 315 | psa_cipher_operation_t *operation, |
| 316 | psa_key_slot_t key, |
| 317 | psa_algorithm_t alg); |
| 318 | /** |
| 319 | * \brief Update the cipher context with a chunk of input data to create a |
| 320 | * chunk of encrypted output data (for encryption contexts), or to |
| 321 | * decrypt a chunk of encrypted input data to obtain decrypted data |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 322 | * (for decryption contexts) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 323 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 324 | * \param[in/out] operation Cipher operation context |
| 325 | * \param[in] input Buffer containing input data |
| 326 | * \param[in] input_length Input length |
| 327 | * \param[out] output Buffer containing output data |
| 328 | * \param[in] output_size Size of the output buffer |
| 329 | * \param[out] output_length Size of the produced output |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 330 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 331 | * \return Return values as described in \ref tfm_crypto_err_t |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 332 | */ |
| 333 | enum tfm_crypto_err_t tfm_crypto_cipher_update( |
| 334 | psa_cipher_operation_t *operation, |
| 335 | const uint8_t *input, |
| 336 | size_t input_length, |
| 337 | unsigned char *output, |
| 338 | size_t output_size, |
| 339 | size_t *output_length); |
| 340 | /** |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 341 | * \brief Finalise a cipher context flushing out any remaining block of |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 342 | * output data |
| 343 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 344 | * \note A successful call to this function de-initialises the cipher operation |
| 345 | * context provided as parameter |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 346 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 347 | * \param[in/out] operation Cipher operation context |
| 348 | * \param[out] output Buffer containing output data |
| 349 | * \param[in] output_size Size of the output buffer |
| 350 | * \param[out] output_length Size of the produced output |
| 351 | * |
| 352 | * \return Return values as described in \ref tfm_crypto_err_t |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 353 | */ |
| 354 | enum tfm_crypto_err_t tfm_crypto_cipher_finish( |
| 355 | psa_cipher_operation_t *operation, |
| 356 | uint8_t *output, |
| 357 | size_t output_size, |
| 358 | size_t *output_length); |
| 359 | /** |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 360 | * \brief Abort a cipher operation, clears the operation context provided |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 361 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 362 | * \note A successful call to this function de-initialises the cipher operation |
| 363 | * context provided as parameter |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 364 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 365 | * \param[in/out] operation Cipher operation context |
| 366 | * |
| 367 | * \return Return values as described in \ref tfm_crypto_err_t |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 368 | */ |
| 369 | enum tfm_crypto_err_t tfm_crypto_cipher_abort( |
| 370 | psa_cipher_operation_t *operation); |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 371 | /** |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 372 | * \brief Start a hash operation with the provided algorithm |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 373 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 374 | * \note A successful call to this function initialises a hash operation |
| 375 | * context which will be referred using the operation parameter |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 376 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 377 | * \param[out] operation Hash operation context |
| 378 | * \param[in] alg Algorithm chosen as hash |
| 379 | * |
| 380 | * \return Return values as described in \ref tfm_crypto_err_t |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 381 | */ |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 382 | enum tfm_crypto_err_t tfm_crypto_hash_setup(psa_hash_operation_t *operation, |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 383 | psa_algorithm_t alg); |
| 384 | /** |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 385 | * \brief Add a new input chunk to the data for which the final hash value |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 386 | * will be computed |
| 387 | * |
| 388 | * \param[in] operation Hash operation context |
| 389 | * \param[in] input Buffer containing the input data |
| 390 | * \param[in] input_length Size of the provided input data |
| 391 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 392 | * \return Return values as described in \ref tfm_crypto_err_t |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 393 | */ |
| 394 | enum tfm_crypto_err_t tfm_crypto_hash_update(psa_hash_operation_t *operation, |
| 395 | const uint8_t *input, |
| 396 | size_t input_length); |
| 397 | /** |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 398 | * \brief Finalise a hash context operation producing the final hash value |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 399 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 400 | * \note A successful call to this function de-initialises the hash operation |
| 401 | * context provided as parameter |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 402 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 403 | * \param[in/out] operation Hash operation context |
| 404 | * \param[out] hash Buffer containing hash data |
| 405 | * \param[in] hash_size Size of the hash buffer |
| 406 | * \param[out] hash_length Size of the produced hash |
| 407 | * |
| 408 | * \return Return values as described in \ref tfm_crypto_err_t |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 409 | */ |
| 410 | enum tfm_crypto_err_t tfm_crypto_hash_finish(psa_hash_operation_t *operation, |
| 411 | uint8_t *hash, |
| 412 | size_t hash_size, |
| 413 | size_t *hash_length); |
| 414 | /** |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 415 | * \brief Finalise a hash context operation, verifying that the final hash |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 416 | * value matches the one provided as input |
| 417 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 418 | * \note A successful call to this function de-initialises the hash operation |
| 419 | * context provided as parameter. The hash operation is de-initialised |
| 420 | * also in case TFM_CRYPTO_ERR_PSA_ERROR_INVALID_SIGNATURE is returned |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 421 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 422 | * \param[in/out] operation Hash operation context |
| 423 | * \param[in] hash Buffer containing the provided hash value |
| 424 | * \param[in] hash_length Size of the provided hash value |
| 425 | * |
| 426 | * \return Return values as described in \ref tfm_crypto_err_t |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 427 | */ |
| 428 | enum tfm_crypto_err_t tfm_crypto_hash_verify(psa_hash_operation_t *operation, |
| 429 | const uint8_t *hash, |
| 430 | size_t hash_length); |
| 431 | /** |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 432 | * \brief Abort a hash operation, clears the operation context provided |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 433 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 434 | * \note A successful call to this function de-initialises the hash operation |
| 435 | * context provided as parameter |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 436 | * |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 437 | * \param[in/out] operation Hash operation context |
| 438 | * |
| 439 | * \return Return values as described in \ref tfm_crypto_err_t |
Antonio de Angelis | a6f7216 | 2018-09-05 11:00:37 +0100 | [diff] [blame] | 440 | */ |
| 441 | enum tfm_crypto_err_t tfm_crypto_hash_abort(psa_hash_operation_t *operation); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 442 | |
Louis Mayencourt | 7a36f78 | 2018-09-24 14:00:57 +0100 | [diff] [blame] | 443 | /** |
| 444 | * \brief Start a MAC operation with the provided algorithm (for signing) |
| 445 | * |
| 446 | * \note A successful call to this function initialises a MAC operation |
| 447 | * context which will be referred using the operation parameter |
| 448 | * |
| 449 | * \param[out] operation MAC operation context |
| 450 | * \param[in] key Key slot to bind to the MAC context |
| 451 | * \param[in] alg Algorithm chosen as MAC |
| 452 | * |
| 453 | * \return Return values as described in \ref tfm_crypto_err_t |
| 454 | */ |
| 455 | enum tfm_crypto_err_t tfm_crypto_mac_sign_setup(psa_mac_operation_t *operation, |
| 456 | psa_key_slot_t key, |
| 457 | psa_algorithm_t alg); |
| 458 | /** |
| 459 | * \brief Start a MAC operation with the provided algorithm (for verifying) |
| 460 | * |
| 461 | * \note A successful call to this function initialises a MAC operation |
| 462 | * context which will be referred using the operation parameter |
| 463 | * |
| 464 | * \param[out] operation MAC operation context |
| 465 | * \param[in] key Key slot to bind to the MAC context |
| 466 | * \param[in] alg Algorithm chosen as MAC |
| 467 | * |
| 468 | * \return Return values as described in \ref tfm_crypto_err_t |
| 469 | */ |
| 470 | enum tfm_crypto_err_t tfm_crypto_mac_verify_setup( |
| 471 | psa_mac_operation_t *operation, |
| 472 | psa_key_slot_t key, |
| 473 | psa_algorithm_t alg); |
| 474 | /** |
| 475 | * \brief Adds a new input chunk to the data for which the final MAC value |
| 476 | * will be computed |
| 477 | * |
| 478 | * \param[in] operation MAC operation context |
| 479 | * \param[in] input Buffer containing the input data |
| 480 | * \param[in] input_length Size of the provided input data |
| 481 | * |
| 482 | * \return Return values as described in \ref tfm_crypto_err_t |
| 483 | */ |
| 484 | enum tfm_crypto_err_t tfm_crypto_mac_update(psa_mac_operation_t *operation, |
| 485 | const uint8_t *input, |
| 486 | size_t input_length); |
| 487 | /** |
| 488 | * \brief Finalise a MAC context operation producing the final MAC value |
| 489 | * |
| 490 | * \param[in/out] operation Mac operation context |
| 491 | * \param[out] mac Buffer containing MAC data |
| 492 | * \param[in] mac_size Size of the mac buffer |
| 493 | * \param[out] mac_length Size of the produced mac |
| 494 | * |
| 495 | * \return Return values as described in \ref tfm_crypto_err_t |
| 496 | */ |
| 497 | enum tfm_crypto_err_t tfm_crypto_mac_sign_finish(psa_mac_operation_t *operation, |
| 498 | uint8_t *mac, |
| 499 | size_t mac_size, |
| 500 | size_t *mac_length); |
| 501 | /** |
| 502 | * \brief Finalise a MAC context operation, verifying that the final MAC value |
| 503 | * matches the one provided as input |
| 504 | * |
| 505 | * \param[in/out] operation MAC operation context |
| 506 | * \param[in] mac Buffer containing the provided MAC value |
| 507 | * \param[in] mac_length Size of the provided MAC value |
| 508 | * |
| 509 | * \return Return values as described in \ref tfm_crypto_err_t |
| 510 | */ |
| 511 | enum tfm_crypto_err_t tfm_crypto_mac_verify_finish( |
| 512 | psa_mac_operation_t *operation, |
| 513 | const uint8_t *mac, |
| 514 | size_t mac_length); |
| 515 | /** |
| 516 | * \brief Abort a MAC operation, clear the operation context provided |
| 517 | * |
| 518 | * \param[in/out] operation MAC operation context |
| 519 | * |
| 520 | * \return Return values as described in \ref tfm_crypto_err_t |
| 521 | */ |
| 522 | enum tfm_crypto_err_t tfm_crypto_mac_abort(psa_mac_operation_t *operation); |
| 523 | |
Antonio de Angelis | 3a48099 | 2018-11-07 11:53:28 +0000 | [diff] [blame] | 524 | /** |
| 525 | * \brief Perform an AEAD encryption operation on input data with additional |
| 526 | * data to be authenticated, producing ciphertext in output with an |
| 527 | * appended authentication tag |
| 528 | * |
| 529 | * \param[in] key Key slot for the key |
| 530 | * \param[in] alg Algorithm to be used |
| 531 | * \param[in] nonce Pointer to a buffer holding a nonce or IV |
| 532 | * to use |
| 533 | * \param[in] nonce_length Size in bytes of the nonce or IV data |
| 534 | * \param[in] additional_data Additional information to be authenticated |
| 535 | * \param[in] additional_data_length Size in bytes of the additional data |
| 536 | * \param[in] plaintext Buffer pointing to data to be encrypted |
| 537 | * \param[in] plaintext_length Size in bytes of the plain text buffer |
| 538 | * \param[out] ciphertext Output encrypted data, with the |
| 539 | * authentication tag appended |
| 540 | * \param[in] ciphertext_size Size in bytes of the buffer to hold the |
| 541 | * cipher text plus authentication tag |
| 542 | * \param[out] ciphertext_length Size of the ciphertext plus tag produced |
| 543 | * as output |
| 544 | * |
| 545 | * \return Return values as described in \ref tfm_crypto_err_t |
| 546 | */ |
| 547 | enum tfm_crypto_err_t tfm_crypto_aead_encrypt(psa_key_slot_t key, |
| 548 | psa_algorithm_t alg, |
| 549 | const uint8_t *nonce, |
| 550 | size_t nonce_length, |
| 551 | const uint8_t *additional_data, |
| 552 | size_t additional_data_length, |
| 553 | const uint8_t *plaintext, |
| 554 | size_t plaintext_length, |
| 555 | uint8_t *ciphertext, |
| 556 | size_t ciphertext_size, |
| 557 | size_t *ciphertext_length); |
| 558 | /** |
| 559 | * \brief Perform an AEAD decryption operation on input data with additional |
| 560 | * data to be verified, producing back the original plain text in case |
| 561 | * the verification of the authentication tag is successful |
| 562 | * |
| 563 | * \param[in] key Key slot for the key |
| 564 | * \param[in] alg Algorithm to be used |
| 565 | * \param[in] nonce Pointer to a buffer holding a nonce or IV |
| 566 | * to use |
| 567 | * \param[in] nonce_length Size in bytes of the nonce or IV data |
| 568 | * \param[in] additional_data Additional information which was |
| 569 | * authenticated but not encrypted |
| 570 | * \param[in] additional_data_length Size in bytes of the additional data |
| 571 | * \param[in] ciphertext Buffer pointing to data be decrypted |
| 572 | * \param[in] ciphertext_length Size in bytes of the cipher text buffer |
| 573 | * \param[out] plaintext Buffer for decrypted output data |
| 574 | * \param[in] plaintext_size Size in bytes of the buffer to hold the |
| 575 | * plain text |
| 576 | * \param[out] plaintext_length Size of the plain text actually produced |
| 577 | * |
| 578 | * \return Return values as described in \ref tfm_crypto_err_t |
| 579 | */ |
| 580 | enum tfm_crypto_err_t tfm_crypto_aead_decrypt(psa_key_slot_t key, |
| 581 | psa_algorithm_t alg, |
| 582 | const uint8_t *nonce, |
| 583 | size_t nonce_length, |
| 584 | const uint8_t *additional_data, |
| 585 | size_t additional_data_length, |
| 586 | const uint8_t *ciphertext, |
| 587 | size_t ciphertext_length, |
| 588 | uint8_t *plaintext, |
| 589 | size_t plaintext_size, |
| 590 | size_t *plaintext_length); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 591 | #ifdef __cplusplus |
| 592 | } |
| 593 | #endif |
| 594 | |
| 595 | #endif /* __TFM_CRYPTO_API_H__ */ |