Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 1 | /* |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2018-2020, 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" |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 17 | #ifdef TFM_PSA_API |
Jamie Fox | cc31d40 | 2019-01-28 17:13:52 +0000 | [diff] [blame] | 18 | #include "psa/service.h" |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 19 | |
| 20 | /** |
| 21 | * \brief This define is a function pointer type to the Uniform Signature API |
| 22 | * prototype. |
| 23 | */ |
| 24 | typedef psa_status_t (*tfm_crypto_us_t)(psa_invec[],size_t,psa_outvec[],size_t); |
| 25 | #endif |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 26 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 27 | #define UNIFORM_SIGNATURE_API(api_name) \ |
| 28 | psa_status_t api_name(psa_invec[], size_t, psa_outvec[], size_t) |
| 29 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 30 | /** |
| 31 | * \brief List of possible operation types supported by the TFM based |
| 32 | * implementation. This type is needed by the operation allocation, |
| 33 | * lookup and release functions. |
| 34 | * |
| 35 | */ |
| 36 | enum tfm_crypto_operation_type { |
| 37 | TFM_CRYPTO_OPERATION_NONE = 0, |
| 38 | TFM_CRYPTO_CIPHER_OPERATION = 1, |
| 39 | TFM_CRYPTO_MAC_OPERATION = 2, |
| 40 | TFM_CRYPTO_HASH_OPERATION = 3, |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame^] | 41 | TFM_CRYPTO_KEY_DERIVATION_OPERATION = 4, |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 42 | |
| 43 | /* Used to force the enum size */ |
| 44 | TFM_CRYPTO_OPERATION_TYPE_MAX = INT_MAX |
| 45 | }; |
| 46 | |
| 47 | /** |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 48 | * \brief Initialise the service |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 49 | * |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 50 | * \return Return values as described in \ref psa_status_t |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 51 | */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 52 | psa_status_t tfm_crypto_init(void); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 53 | |
| 54 | /** |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 55 | * \brief Initialise the Alloc module |
| 56 | * |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 57 | * \return Return values as described in \ref psa_status_t |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 58 | */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 59 | psa_status_t tfm_crypto_init_alloc(void); |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 60 | |
| 61 | /** |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 62 | * \brief Returns the ID of the caller |
| 63 | * |
| 64 | * \param[out] id Pointer to hold the ID of the caller |
| 65 | * |
| 66 | * \return Return values as described in \ref psa_status_t |
| 67 | */ |
| 68 | psa_status_t tfm_crypto_get_caller_id(int32_t *id); |
| 69 | |
| 70 | /** |
| 71 | * \brief Checks that the requested handle belongs to the requesting |
| 72 | * partition |
| 73 | * |
| 74 | * \param[in] handle Handle given as input |
| 75 | * \param[out] index Optionally, pointer to hold the internal index |
| 76 | * corresponding to the input handle. Valid only |
| 77 | * on PSA_SUCCESS, it's returned only if the input |
| 78 | * parameter is not NULL. |
| 79 | * |
| 80 | * \return Return values as described in \ref psa_status_t |
| 81 | */ |
| 82 | psa_status_t tfm_crypto_check_handle_owner(psa_key_handle_t handle, |
| 83 | uint32_t *index); |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame^] | 84 | |
| 85 | /** |
| 86 | * \brief Checks that there is enough local storage in RAM to keep |
| 87 | * another key, and returns the index of the storage to use |
| 88 | * and the ID of the partition to associated to the key |
| 89 | * |
| 90 | * \param[out] partition_id ID of the requesting partition |
| 91 | * \param[out] index Index of the local storage to use |
| 92 | * |
| 93 | * \return Return values as described in \ref psa_status_t |
| 94 | */ |
| 95 | psa_status_t tfm_crypto_check_key_storage(int32_t *partition_id, |
| 96 | uint32_t *index); |
| 97 | |
| 98 | /** |
| 99 | * \brief Sets the index of the local storage in use with a key |
| 100 | * requested by a partition specified by the input parameter |
| 101 | * partition_id, and stores the corresponding key_handle |
| 102 | * |
| 103 | * \param[in] partition_id ID of the requesting partition |
| 104 | * \param[in] index Index of the local storage to use |
| 105 | * \param[in] key_handle Corresponding key handle to associate |
| 106 | * |
| 107 | * \return Return values as described in \ref psa_status_t |
| 108 | */ |
| 109 | psa_status_t tfm_crypto_set_key_storage(int32_t partition_id, |
| 110 | uint32_t index, |
| 111 | psa_key_handle_t key_handle); |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 112 | /** |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 113 | * \brief Allocate an operation context in the backend |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 114 | * |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 115 | * \param[in] type Type of the operation context to allocate |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 116 | * \param[out] handle Pointer to hold the allocated handle |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 117 | * \param[out ctx Double pointer to the corresponding context |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 118 | * |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 119 | * \return Return values as described in \ref psa_status_t |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 120 | */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 121 | psa_status_t tfm_crypto_operation_alloc(enum tfm_crypto_operation_type type, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 122 | uint32_t *handle, |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 123 | void **ctx); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 124 | /** |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 125 | * \brief Release an operation context in the backend |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 126 | * |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 127 | * \param[in] handle Pointer to the handle of the context to release |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 128 | * |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 129 | * \return Return values as described in \ref psa_status_t |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 130 | */ |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 131 | psa_status_t tfm_crypto_operation_release(uint32_t *handle); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 132 | /** |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 133 | * \brief Look up an operation context in the backend for the corresponding |
| 134 | * frontend operation |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 135 | * |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 136 | * \param[in] type Type of the operation context to look up |
| 137 | * \param[in] handle Handle of the context to lookup |
| 138 | * \param[out] ctx Double pointer to the corresponding context |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 139 | * |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 140 | * \return Return values as described in \ref psa_status_t |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 141 | */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 142 | psa_status_t tfm_crypto_operation_lookup(enum tfm_crypto_operation_type type, |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 143 | uint32_t handle, |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 144 | void **ctx); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 145 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 146 | #define LIST_TFM_CRYPTO_UNIFORM_SIGNATURE_API \ |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame^] | 147 | X(tfm_crypto_get_key_attributes) \ |
| 148 | X(tfm_crypto_reset_key_attributes) \ |
Jamie Fox | dadb4e8 | 2019-09-03 17:59:41 +0100 | [diff] [blame] | 149 | X(tfm_crypto_open_key) \ |
| 150 | X(tfm_crypto_close_key) \ |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 151 | X(tfm_crypto_import_key) \ |
| 152 | X(tfm_crypto_destroy_key) \ |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 153 | X(tfm_crypto_export_key) \ |
| 154 | X(tfm_crypto_export_public_key) \ |
| 155 | X(tfm_crypto_copy_key) \ |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame^] | 156 | X(tfm_crypto_hash_compute) \ |
| 157 | X(tfm_crypto_hash_compare) \ |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 158 | X(tfm_crypto_hash_setup) \ |
| 159 | X(tfm_crypto_hash_update) \ |
| 160 | X(tfm_crypto_hash_finish) \ |
| 161 | X(tfm_crypto_hash_verify) \ |
| 162 | X(tfm_crypto_hash_abort) \ |
| 163 | X(tfm_crypto_hash_clone) \ |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame^] | 164 | X(tfm_crypto_mac_compute) \ |
| 165 | X(tfm_crypto_mac_verify) \ |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 166 | X(tfm_crypto_mac_sign_setup) \ |
| 167 | X(tfm_crypto_mac_verify_setup) \ |
| 168 | X(tfm_crypto_mac_update) \ |
| 169 | X(tfm_crypto_mac_sign_finish) \ |
| 170 | X(tfm_crypto_mac_verify_finish) \ |
| 171 | X(tfm_crypto_mac_abort) \ |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame^] | 172 | X(tfm_crypto_cipher_encrypt) \ |
| 173 | X(tfm_crypto_cipher_decrypt) \ |
| 174 | X(tfm_crypto_cipher_encrypt_setup) \ |
| 175 | X(tfm_crypto_cipher_decrypt_setup) \ |
| 176 | X(tfm_crypto_cipher_generate_iv) \ |
| 177 | X(tfm_crypto_cipher_set_iv) \ |
| 178 | X(tfm_crypto_cipher_update) \ |
| 179 | X(tfm_crypto_cipher_finish) \ |
| 180 | X(tfm_crypto_cipher_abort) \ |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 181 | X(tfm_crypto_aead_encrypt) \ |
| 182 | X(tfm_crypto_aead_decrypt) \ |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame^] | 183 | X(tfm_crypto_aead_encrypt_setup) \ |
| 184 | X(tfm_crypto_aead_decrypt_setup) \ |
| 185 | X(tfm_crypto_aead_generate_nonce) \ |
| 186 | X(tfm_crypto_aead_set_nonce) \ |
| 187 | X(tfm_crypto_aead_set_lengths) \ |
| 188 | X(tfm_crypto_aead_update_ad) \ |
| 189 | X(tfm_crypto_aead_update) \ |
| 190 | X(tfm_crypto_aead_finish) \ |
| 191 | X(tfm_crypto_aead_verify) \ |
| 192 | X(tfm_crypto_aead_abort) \ |
| 193 | X(tfm_crypto_sign_hash) \ |
| 194 | X(tfm_crypto_verify_hash) \ |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 195 | X(tfm_crypto_asymmetric_encrypt) \ |
| 196 | X(tfm_crypto_asymmetric_decrypt) \ |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame^] | 197 | X(tfm_crypto_key_derivation_setup) \ |
| 198 | X(tfm_crypto_key_derivation_get_capacity) \ |
| 199 | X(tfm_crypto_key_derivation_set_capacity) \ |
| 200 | X(tfm_crypto_key_derivation_input_bytes) \ |
| 201 | X(tfm_crypto_key_derivation_input_key) \ |
| 202 | X(tfm_crypto_key_derivation_key_agreement)\ |
| 203 | X(tfm_crypto_key_derivation_output_bytes) \ |
| 204 | X(tfm_crypto_key_derivation_output_key) \ |
| 205 | X(tfm_crypto_key_derivation_abort) \ |
| 206 | X(tfm_crypto_raw_key_agreement) \ |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 207 | X(tfm_crypto_generate_random) \ |
| 208 | X(tfm_crypto_generate_key) \ |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame^] | 209 | X(tfm_crypto_set_key_domain_parameters) \ |
| 210 | X(tfm_crypto_get_key_domain_parameters) \ |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 211 | |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 212 | #define X(api_name) UNIFORM_SIGNATURE_API(api_name); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 213 | LIST_TFM_CRYPTO_UNIFORM_SIGNATURE_API |
| 214 | #undef X |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 215 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 216 | #ifdef __cplusplus |
| 217 | } |
| 218 | #endif |
| 219 | |
| 220 | #endif /* __TFM_CRYPTO_API_H__ */ |