Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 1 | /* |
Antonio de Angelis | dab11b4 | 2022-01-10 14:21:04 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2022, 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 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
Ken Liu | b671d68 | 2022-05-12 20:39:29 +0800 | [diff] [blame] | 10 | #include <string.h> |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 11 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 12 | #include "tfm_mbedcrypto_include.h" |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 13 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 14 | #include "tfm_crypto_api.h" |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 15 | #include "tfm_crypto_defs.h" |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 16 | |
| 17 | /** |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 18 | * \def TFM_CRYPTO_CONC_OPER_NUM |
| 19 | * |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 20 | * \brief This is the default value for the maximum number of concurrent |
| 21 | * operations that can be active (allocated) at any time, supported |
| 22 | * by the implementation |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 23 | */ |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 24 | #ifndef TFM_CRYPTO_CONC_OPER_NUM |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 25 | #define TFM_CRYPTO_CONC_OPER_NUM (8) |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 26 | #endif |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 27 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 28 | struct tfm_crypto_operation_s { |
| 29 | uint32_t in_use; /*!< Indicates if the operation is in use */ |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 30 | int32_t owner; /*!< Indicates an ID of the owner of |
| 31 | * the context |
| 32 | */ |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 33 | enum tfm_crypto_operation_type type; /*!< Type of the operation */ |
| 34 | union { |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 35 | psa_cipher_operation_t cipher; /*!< Cipher operation context */ |
| 36 | psa_mac_operation_t mac; /*!< MAC operation context */ |
| 37 | psa_hash_operation_t hash; /*!< Hash operation context */ |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 38 | psa_key_derivation_operation_t key_deriv; /*!< Key derivation operation context */ |
Antonio de Angelis | c26af63 | 2021-10-07 15:04:12 +0100 | [diff] [blame] | 39 | psa_aead_operation_t aead; /*!< AEAD operation context */ |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 40 | } operation; |
| 41 | }; |
| 42 | |
| 43 | static struct tfm_crypto_operation_s operation[TFM_CRYPTO_CONC_OPER_NUM] ={{0}}; |
| 44 | |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 45 | /* |
| 46 | * \brief Function used to clear the memory associated to a backend context |
| 47 | * |
| 48 | * \param[in] index Numerical index in the database of the backend contexts |
| 49 | * |
| 50 | * \return None |
| 51 | * |
| 52 | */ |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 53 | static void memset_operation_context(uint32_t index) |
| 54 | { |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 55 | uint32_t mem_size; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 56 | |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 57 | uint8_t *mem_ptr = (uint8_t *) &(operation[index].operation); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 58 | |
| 59 | switch(operation[index].type) { |
| 60 | case TFM_CRYPTO_CIPHER_OPERATION: |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 61 | mem_size = sizeof(psa_cipher_operation_t); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 62 | break; |
| 63 | case TFM_CRYPTO_MAC_OPERATION: |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 64 | mem_size = sizeof(psa_mac_operation_t); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 65 | break; |
| 66 | case TFM_CRYPTO_HASH_OPERATION: |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 67 | mem_size = sizeof(psa_hash_operation_t); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 68 | break; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 69 | case TFM_CRYPTO_KEY_DERIVATION_OPERATION: |
| 70 | mem_size = sizeof(psa_key_derivation_operation_t); |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 71 | break; |
Antonio de Angelis | c26af63 | 2021-10-07 15:04:12 +0100 | [diff] [blame] | 72 | case TFM_CRYPTO_AEAD_OPERATION: |
| 73 | mem_size = sizeof(psa_aead_operation_t); |
| 74 | break; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 75 | case TFM_CRYPTO_OPERATION_NONE: |
| 76 | default: |
| 77 | mem_size = 0; |
| 78 | break; |
| 79 | } |
| 80 | |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 81 | /* Clear the contents of the backend context */ |
Ken Liu | b671d68 | 2022-05-12 20:39:29 +0800 | [diff] [blame] | 82 | (void)memset(mem_ptr, 0, mem_size); |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 85 | /*! |
Antonio de Angelis | 202425a | 2022-04-06 11:13:15 +0100 | [diff] [blame^] | 86 | * \defgroup alloc Function that implement allocation and deallocation of |
| 87 | * contexts to be stored in the secure world for multipart |
| 88 | * operations |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 89 | */ |
| 90 | |
| 91 | /*!@{*/ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 92 | psa_status_t tfm_crypto_init_alloc(void) |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 93 | { |
| 94 | /* Clear the contents of the local contexts */ |
Ken Liu | b671d68 | 2022-05-12 20:39:29 +0800 | [diff] [blame] | 95 | (void)memset(operation, 0, sizeof(operation)); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 96 | return PSA_SUCCESS; |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 97 | } |
| 98 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 99 | 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] | 100 | uint32_t *handle, |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 101 | void **ctx) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 102 | { |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 103 | uint32_t i = 0; |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 104 | int32_t partition_id = 0; |
| 105 | psa_status_t status; |
| 106 | |
| 107 | status = tfm_crypto_get_caller_id(&partition_id); |
| 108 | if (status != PSA_SUCCESS) { |
| 109 | return status; |
| 110 | } |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 111 | |
Jamie Fox | 707caf7 | 2019-05-29 15:14:18 +0100 | [diff] [blame] | 112 | /* Handle must be initialised before calling a setup function */ |
| 113 | if (*handle != TFM_CRYPTO_INVALID_HANDLE) { |
Antonio de Angelis | 5cbaf32 | 2022-01-12 11:55:54 +0000 | [diff] [blame] | 114 | if ((*handle <= TFM_CRYPTO_CONC_OPER_NUM) && |
| 115 | (operation[*handle - 1].in_use == TFM_CRYPTO_IN_USE) && |
| 116 | (operation[*handle - 1].owner == partition_id)) { |
| 117 | /* The handle is a valid one for already in progress operation */ |
| 118 | return PSA_ERROR_BAD_STATE; |
| 119 | } |
| 120 | |
Antonio de Angelis | dab11b4 | 2022-01-10 14:21:04 +0000 | [diff] [blame] | 121 | return PSA_ERROR_INVALID_HANDLE; |
Jamie Fox | 707caf7 | 2019-05-29 15:14:18 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 124 | /* Init to invalid values */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 125 | if (ctx == NULL) { |
| 126 | return PSA_ERROR_INVALID_ARGUMENT; |
| 127 | } |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 128 | *ctx = NULL; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 129 | |
| 130 | for (i=0; i<TFM_CRYPTO_CONC_OPER_NUM; i++) { |
| 131 | if (operation[i].in_use == TFM_CRYPTO_NOT_IN_USE) { |
| 132 | operation[i].in_use = TFM_CRYPTO_IN_USE; |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 133 | operation[i].owner = partition_id; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 134 | operation[i].type = type; |
Jamie Fox | 707caf7 | 2019-05-29 15:14:18 +0100 | [diff] [blame] | 135 | *handle = i + 1; |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 136 | *ctx = (void *) &(operation[i].operation); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 137 | return PSA_SUCCESS; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 138 | } |
| 139 | } |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 140 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 141 | return PSA_ERROR_NOT_PERMITTED; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 144 | psa_status_t tfm_crypto_operation_release(uint32_t *handle) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 145 | { |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 146 | uint32_t h_val = *handle; |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 147 | int32_t partition_id = 0; |
| 148 | psa_status_t status; |
| 149 | |
| 150 | status = tfm_crypto_get_caller_id(&partition_id); |
| 151 | if (status != PSA_SUCCESS) { |
| 152 | return status; |
| 153 | } |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 154 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 155 | if ( (h_val != TFM_CRYPTO_INVALID_HANDLE) && |
Jamie Fox | 707caf7 | 2019-05-29 15:14:18 +0100 | [diff] [blame] | 156 | (h_val <= TFM_CRYPTO_CONC_OPER_NUM) && |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 157 | (operation[h_val - 1].in_use == TFM_CRYPTO_IN_USE) && |
| 158 | (operation[h_val - 1].owner == partition_id)) { |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 159 | |
Jamie Fox | 707caf7 | 2019-05-29 15:14:18 +0100 | [diff] [blame] | 160 | memset_operation_context(h_val - 1); |
| 161 | operation[h_val - 1].in_use = TFM_CRYPTO_NOT_IN_USE; |
| 162 | operation[h_val - 1].type = TFM_CRYPTO_OPERATION_NONE; |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 163 | operation[h_val - 1].owner = 0; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 164 | *handle = TFM_CRYPTO_INVALID_HANDLE; |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 165 | return PSA_SUCCESS; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 166 | } |
| 167 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 168 | return PSA_ERROR_INVALID_ARGUMENT; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 171 | 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] | 172 | uint32_t handle, |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 173 | void **ctx) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 174 | { |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 175 | int32_t partition_id = 0; |
| 176 | psa_status_t status; |
| 177 | |
| 178 | status = tfm_crypto_get_caller_id(&partition_id); |
| 179 | if (status != PSA_SUCCESS) { |
| 180 | return status; |
| 181 | } |
| 182 | |
Antonio de Angelis | 819c2f3 | 2019-02-06 14:32:02 +0000 | [diff] [blame] | 183 | if ( (handle != TFM_CRYPTO_INVALID_HANDLE) && |
Jamie Fox | 707caf7 | 2019-05-29 15:14:18 +0100 | [diff] [blame] | 184 | (handle <= TFM_CRYPTO_CONC_OPER_NUM) && |
| 185 | (operation[handle - 1].in_use == TFM_CRYPTO_IN_USE) && |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 186 | (operation[handle - 1].type == type) && |
| 187 | (operation[handle - 1].owner == partition_id)) { |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 188 | |
Jamie Fox | 707caf7 | 2019-05-29 15:14:18 +0100 | [diff] [blame] | 189 | *ctx = (void *) &(operation[handle - 1].operation); |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 190 | return PSA_SUCCESS; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 191 | } |
| 192 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 193 | return PSA_ERROR_BAD_STATE; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 194 | } |
Antonio de Angelis | 202425a | 2022-04-06 11:13:15 +0100 | [diff] [blame^] | 195 | |
| 196 | psa_status_t tfm_crypto_operation_handling(enum tfm_crypto_operation_type type, |
| 197 | enum tfm_crypto_function_type function_type, |
| 198 | uint32_t *handle, |
| 199 | void **ctx) |
| 200 | { |
| 201 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 202 | |
| 203 | /* Multipart context retrieving and handling if required */ |
| 204 | switch (function_type) { |
| 205 | case TFM_CRYPTO_FUNCTION_TYPE_SETUP: |
| 206 | /* Allocate the operation context in the secure world */ |
| 207 | status = tfm_crypto_operation_alloc(type, |
| 208 | handle, |
| 209 | ctx); |
| 210 | break; |
| 211 | case TFM_CRYPTO_FUNCTION_TYPE_LOOKUP: |
| 212 | /* Look up the corresponding operation context */ |
| 213 | status = tfm_crypto_operation_lookup(type, |
| 214 | *handle, |
| 215 | ctx); |
| 216 | break; |
| 217 | /* All the other APIs don't deal with multipart */ |
| 218 | case TFM_CRYPTO_FUNCTION_TYPE_NON_MULTIPART: |
| 219 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 220 | default: |
| 221 | break; |
| 222 | } |
| 223 | |
| 224 | return status; |
| 225 | } |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 226 | /*!@}*/ |