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 | #include <limits.h> |
| 9 | |
| 10 | #include "tfm_crypto_defs.h" |
| 11 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 12 | #include "psa_crypto.h" |
| 13 | #include "tfm_crypto_api.h" |
| 14 | |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 15 | #include "tfm_crypto_struct.h" |
Tamas Ban | 8bd24b7 | 2019-02-19 12:13:13 +0000 | [diff] [blame] | 16 | #include "secure_fw/core/tfm_memory_utils.h" |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 17 | |
| 18 | /** |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame^] | 19 | * \def LIST_OPERATION_LOOKUP |
| 20 | * |
| 21 | * \brief This is an X macro which enforces the correspondence |
| 22 | * between backend operation type (through the enum |
| 23 | * tfm_crypto_operation_type) and the corresponding frontend type |
| 24 | */ |
| 25 | #define LIST_OPERATION_LOOKUP \ |
| 26 | X(TFM_CRYPTO_CIPHER_OPERATION, psa_cipher_operation_t) \ |
| 27 | X(TFM_CRYPTO_MAC_OPERATION, psa_mac_operation_t) \ |
| 28 | X(TFM_CRYPTO_HASH_OPERATION, psa_hash_operation_t) |
| 29 | |
| 30 | /** |
| 31 | * \def CHECK_ALIGNMENT |
| 32 | * |
| 33 | * \brief This macro checks the alignment of the operation object pointer which |
| 34 | * receives as input based on the requirement of the front end operation |
| 35 | * type. This macro expands in a case statement so it must be used in a |
| 36 | * switch-case construct. It sets the handle value it receives in input |
| 37 | * with the proper value or TFM_CRYPTO_INVALID_HANDLE in case the oper |
| 38 | * pointer does not satisfy alignment requirements of the front end type |
| 39 | */ |
| 40 | #define CHECK_ALIGNMENT(e,t,oper,handle) \ |
| 41 | case e: \ |
| 42 | if ((uintptr_t)oper % offsetof(struct {char c; t x;},x)) { \ |
| 43 | handle = TFM_CRYPTO_INVALID_HANDLE; \ |
| 44 | } else { \ |
| 45 | handle = ((t *)oper)->handle; \ |
| 46 | } \ |
| 47 | break; |
| 48 | /** |
| 49 | * \def GET_HANDLE_POINTER |
| 50 | * |
| 51 | * \brief This macro extracts the pointer to handle value from the object |
| 52 | * operation pointer it receives as input. This macro expands in a case |
| 53 | * statement so it must be used in a switch case-case construct. |
| 54 | */ |
| 55 | #define GET_HANDLE_POINTER(e,t,oper,handle) \ |
| 56 | case e: \ |
| 57 | handle = &(((t *)oper)->handle); \ |
| 58 | break; |
| 59 | /** |
| 60 | * \def TFM_CRYPTO_CONC_OPER_NUM |
| 61 | * |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 62 | * \brief This value defines the maximum number of simultaneous operations |
| 63 | * supported by this implementation. |
| 64 | */ |
| 65 | #define TFM_CRYPTO_CONC_OPER_NUM (8) |
| 66 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 67 | struct tfm_crypto_operation_s { |
| 68 | uint32_t in_use; /*!< Indicates if the operation is in use */ |
| 69 | enum tfm_crypto_operation_type type; /*!< Type of the operation */ |
| 70 | union { |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 71 | struct tfm_cipher_operation_s cipher; /*!< Cipher operation context */ |
| 72 | struct tfm_mac_operation_s mac; /*!< MAC operation context */ |
| 73 | struct tfm_hash_operation_s hash; /*!< Hash operation context */ |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 74 | } operation; |
| 75 | }; |
| 76 | |
| 77 | static struct tfm_crypto_operation_s operation[TFM_CRYPTO_CONC_OPER_NUM] ={{0}}; |
| 78 | |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame^] | 79 | /* |
| 80 | * \brief Function used to clear the memory associated to a backend context |
| 81 | * |
| 82 | * \param[in] index Numerical index in the database of the backend contexts |
| 83 | * |
| 84 | * \return None |
| 85 | * |
| 86 | */ |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 87 | static void memset_operation_context(uint32_t index) |
| 88 | { |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame^] | 89 | uint32_t mem_size; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 90 | |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame^] | 91 | uint8_t *mem_ptr = (uint8_t *) &(operation[index].operation); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 92 | |
| 93 | switch(operation[index].type) { |
| 94 | case TFM_CRYPTO_CIPHER_OPERATION: |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 95 | mem_size = sizeof(struct tfm_cipher_operation_s); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 96 | break; |
| 97 | case TFM_CRYPTO_MAC_OPERATION: |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 98 | mem_size = sizeof(struct tfm_mac_operation_s); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 99 | break; |
| 100 | case TFM_CRYPTO_HASH_OPERATION: |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 101 | mem_size = sizeof(struct tfm_hash_operation_s); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 102 | break; |
| 103 | case TFM_CRYPTO_OPERATION_NONE: |
| 104 | default: |
| 105 | mem_size = 0; |
| 106 | break; |
| 107 | } |
| 108 | |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame^] | 109 | /* Clear the contents of the backend context */ |
| 110 | (void)tfm_memset(mem_ptr, 0, mem_size); |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * \brief Function used to extract the handle value from a pointer to a |
| 115 | * frontend operation |
| 116 | * |
| 117 | * \param[in] type Type of the operation context to extract from |
| 118 | * \param[in] oper Pointer to the frontend operation |
| 119 | * |
| 120 | * \return handle 4-byte identifier associated to the context, |
| 121 | * TFM_CRYPTO_INVALID_HANDLE in case of problems |
| 122 | * |
| 123 | */ |
| 124 | static uint32_t get_handle(enum tfm_crypto_operation_type type, void *oper) |
| 125 | { |
| 126 | uint32_t handle = TFM_CRYPTO_INVALID_HANDLE; |
| 127 | |
| 128 | /* Dereference the pointer */ |
| 129 | switch(type) { |
| 130 | /* Generate the list of cases needed to check alignment for all the |
| 131 | * possible operation types listed in LIST_OPERATION_LOOKUP. The default |
| 132 | * case and TFM_CRYPTO_OPERATION_NONE must be created explicitly |
| 133 | */ |
| 134 | #define X(e,t) CHECK_ALIGNMENT(e,t,oper,handle) |
| 135 | LIST_OPERATION_LOOKUP |
| 136 | #undef X |
| 137 | case TFM_CRYPTO_OPERATION_NONE: |
| 138 | default: |
| 139 | break; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 140 | } |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame^] | 141 | |
| 142 | return handle; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * \brief Function used to set the handle value in a pointer to a |
| 147 | * frontend operation |
| 148 | * |
| 149 | * \param[in] type Type of the operation context to extract from |
| 150 | * \param[out] oper Pointer to the frontend operation |
| 151 | * |
| 152 | * \return handle 4-byte identifier associated to the context, |
| 153 | * TFM_CRYPTO_INVALID_HANDLE in case of problems |
| 154 | * |
| 155 | */ |
| 156 | static uint32_t set_handle(enum tfm_crypto_operation_type type, |
| 157 | void *oper, |
| 158 | uint32_t set_value) |
| 159 | { |
| 160 | uint32_t *handle = NULL; |
| 161 | |
| 162 | /* Extract the pointer value */ |
| 163 | switch(type) { |
| 164 | /* Generate the list of cases needed to get the handle pointer for all the |
| 165 | * possible operation types listed in LIST_OPERATION_LOOKUP. The default |
| 166 | * case and TFM_CRYPTO_OPERATION_NONE must be created explicitly |
| 167 | */ |
| 168 | #define X(e,t) GET_HANDLE_POINTER(e,t,oper,handle) |
| 169 | LIST_OPERATION_LOOKUP |
| 170 | #undef X |
| 171 | case TFM_CRYPTO_OPERATION_NONE: |
| 172 | default: |
| 173 | break; |
| 174 | } |
| 175 | |
| 176 | if (handle == NULL || ((uintptr_t)handle % sizeof(uint32_t))) { |
| 177 | return TFM_CRYPTO_INVALID_HANDLE; |
| 178 | } |
| 179 | |
| 180 | /* Set the value by derefencing the pointer, alignment is correct */ |
| 181 | *handle = set_value; |
| 182 | |
| 183 | return set_value; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /*! |
| 187 | * \defgroup public Public functions |
| 188 | * |
| 189 | */ |
| 190 | |
| 191 | /*!@{*/ |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 192 | enum tfm_crypto_err_t tfm_crypto_init_alloc(void) |
| 193 | { |
| 194 | /* Clear the contents of the local contexts */ |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame^] | 195 | (void)tfm_memset(operation, 0, sizeof(operation)); |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 196 | return TFM_CRYPTO_ERR_PSA_SUCCESS; |
| 197 | } |
| 198 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 199 | enum tfm_crypto_err_t tfm_crypto_operation_alloc( |
| 200 | enum tfm_crypto_operation_type type, |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame^] | 201 | void *oper, |
| 202 | void **ctx) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 203 | { |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame^] | 204 | uint32_t i = 0, handle; |
| 205 | |
| 206 | /* Init to invalid values */ |
| 207 | *ctx = NULL; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 208 | |
| 209 | for (i=0; i<TFM_CRYPTO_CONC_OPER_NUM; i++) { |
| 210 | if (operation[i].in_use == TFM_CRYPTO_NOT_IN_USE) { |
| 211 | operation[i].in_use = TFM_CRYPTO_IN_USE; |
| 212 | operation[i].type = type; |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame^] | 213 | handle = set_handle(type, oper, i); |
| 214 | if (handle == TFM_CRYPTO_INVALID_HANDLE) { |
| 215 | return TFM_CRYPTO_ERR_PSA_ERROR_NOT_PERMITTED; |
| 216 | } |
| 217 | *ctx = (void *) &(operation[i].operation); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 218 | return TFM_CRYPTO_ERR_PSA_SUCCESS; |
| 219 | } |
| 220 | } |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 221 | |
| 222 | return TFM_CRYPTO_ERR_PSA_ERROR_NOT_PERMITTED; |
| 223 | } |
| 224 | |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame^] | 225 | enum tfm_crypto_err_t tfm_crypto_operation_release( |
| 226 | enum tfm_crypto_operation_type type, |
| 227 | void *oper) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 228 | { |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame^] | 229 | uint32_t handle = get_handle(type, oper); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 230 | |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame^] | 231 | if ( (handle != TFM_CRYPTO_INVALID_HANDLE) && |
| 232 | (handle < TFM_CRYPTO_CONC_OPER_NUM) && |
| 233 | (operation[handle].in_use == TFM_CRYPTO_IN_USE) ) { |
| 234 | memset_operation_context(handle); |
| 235 | operation[handle].in_use = TFM_CRYPTO_NOT_IN_USE; |
| 236 | operation[handle].type = TFM_CRYPTO_OPERATION_NONE; |
| 237 | (void)set_handle(type, oper, TFM_CRYPTO_INVALID_HANDLE); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 238 | return TFM_CRYPTO_ERR_PSA_SUCCESS; |
| 239 | } |
| 240 | |
| 241 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 242 | } |
| 243 | |
| 244 | enum tfm_crypto_err_t tfm_crypto_operation_lookup( |
| 245 | enum tfm_crypto_operation_type type, |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame^] | 246 | void *oper, |
| 247 | void **ctx) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 248 | { |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame^] | 249 | uint32_t handle = get_handle(type, oper); |
| 250 | |
| 251 | if ( (handle != TFM_CRYPTO_INVALID_HANDLE) && |
| 252 | (handle < TFM_CRYPTO_CONC_OPER_NUM) && |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 253 | (operation[handle].in_use == TFM_CRYPTO_IN_USE) && |
| 254 | (operation[handle].type == type) ) { |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame^] | 255 | *ctx = (void *) &(operation[handle].operation); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 256 | return TFM_CRYPTO_ERR_PSA_SUCCESS; |
| 257 | } |
| 258 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 259 | return TFM_CRYPTO_ERR_PSA_ERROR_BAD_STATE; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 260 | } |
| 261 | /*!@}*/ |