Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 1 | /* |
Antonio de Angelis | 01a93bc | 2023-01-20 11:17:14 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2023, 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 | */ |
Antonio de Angelis | 202425a | 2022-04-06 11:13:15 +0100 | [diff] [blame] | 7 | #include <stdbool.h> |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 8 | |
Xinyu Zhang | d755b82 | 2022-10-25 11:18:09 +0800 | [diff] [blame] | 9 | #include "config_crypto.h" |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 10 | #include "tfm_mbedcrypto_include.h" |
| 11 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 12 | #include "tfm_crypto_api.h" |
Antonio de Angelis | 7557e68 | 2022-11-30 15:37:51 +0000 | [diff] [blame] | 13 | #include "tfm_crypto_key.h" |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 14 | #include "tfm_crypto_defs.h" |
Summer Qin | c737ece | 2020-08-28 10:47:26 +0800 | [diff] [blame] | 15 | #include "tfm_sp_log.h" |
Summer Qin | ca6c152 | 2022-06-17 14:25:55 +0800 | [diff] [blame] | 16 | #include "crypto_check_config.h" |
Raef Coles | 79809c7 | 2022-03-02 13:48:20 +0000 | [diff] [blame] | 17 | #include "tfm_plat_crypto_keys.h" |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 18 | |
Antonio de Angelis | 8f7db7d | 2022-12-01 12:09:16 +0000 | [diff] [blame] | 19 | #include "crypto_library.h" |
| 20 | |
Xinyu Zhang | d755b82 | 2022-10-25 11:18:09 +0800 | [diff] [blame] | 21 | #if CRYPTO_NV_SEED |
Raef Coles | 618fc15 | 2021-06-18 09:26:46 +0100 | [diff] [blame] | 22 | #include "tfm_plat_crypto_nv_seed.h" |
| 23 | #endif /* CRYPTO_NV_SEED */ |
Summer Qin | a5448d6 | 2020-12-07 14:03:37 +0800 | [diff] [blame] | 24 | |
Raef Coles | d2485af | 2019-10-30 10:15:33 +0000 | [diff] [blame] | 25 | #ifdef CRYPTO_HW_ACCELERATOR |
| 26 | #include "crypto_hw.h" |
Michel Jaouen | f41c642 | 2021-10-07 14:38:08 +0200 | [diff] [blame] | 27 | #endif /* CRYPTO_HW_ACCELERATOR */ |
Raef Coles | d2485af | 2019-10-30 10:15:33 +0000 | [diff] [blame] | 28 | |
Ken Liu | b671d68 | 2022-05-12 20:39:29 +0800 | [diff] [blame] | 29 | #include <string.h> |
Kevin Peng | fe730cc | 2022-04-11 17:48:42 +0800 | [diff] [blame] | 30 | #include "psa/framework_feature.h" |
Jamie Fox | cc31d40 | 2019-01-28 17:13:52 +0000 | [diff] [blame] | 31 | #include "psa/service.h" |
Edison Ai | cc4c616 | 2019-06-21 13:52:49 +0800 | [diff] [blame] | 32 | #include "psa_manifest/tfm_crypto.h" |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 33 | |
| 34 | /** |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 35 | * \brief Aligns a value x up to an alignment a. |
| 36 | */ |
| 37 | #define ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1)) |
| 38 | |
| 39 | /** |
| 40 | * \brief Maximum alignment required by any iovec parameters to the TF-M Crypto |
| 41 | * partition. |
| 42 | */ |
| 43 | #define TFM_CRYPTO_IOVEC_ALIGNMENT (4u) |
| 44 | |
Kevin Peng | fe730cc | 2022-04-11 17:48:42 +0800 | [diff] [blame] | 45 | #if PSA_FRAMEWORK_HAS_MM_IOVEC == 1 |
| 46 | static int32_t g_client_id; |
| 47 | |
| 48 | static void tfm_crypto_set_caller_id(int32_t id) |
| 49 | { |
| 50 | g_client_id = id; |
| 51 | } |
| 52 | |
| 53 | psa_status_t tfm_crypto_get_caller_id(int32_t *id) |
| 54 | { |
| 55 | *id = g_client_id; |
| 56 | return PSA_SUCCESS; |
| 57 | } |
| 58 | |
| 59 | static psa_status_t tfm_crypto_init_iovecs(const psa_msg_t *msg, |
| 60 | psa_invec in_vec[], |
| 61 | size_t in_len, |
| 62 | psa_outvec out_vec[], |
| 63 | size_t out_len) |
| 64 | { |
| 65 | uint32_t i; |
| 66 | |
| 67 | /* Map from the second element as the first is read when parsing */ |
| 68 | for (i = 1; i < in_len; i++) { |
| 69 | in_vec[i].len = msg->in_size[i]; |
| 70 | if (in_vec[i].len != 0) { |
| 71 | in_vec[i].base = psa_map_invec(msg->handle, i); |
| 72 | } else { |
| 73 | in_vec[i].base = NULL; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | for (i = 0; i < out_len; i++) { |
| 78 | out_vec[i].len = msg->out_size[i]; |
| 79 | if (out_vec[i].len != 0) { |
| 80 | out_vec[i].base = psa_map_outvec(msg->handle, i); |
| 81 | } else { |
| 82 | out_vec[i].base = NULL; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return PSA_SUCCESS; |
| 87 | } |
| 88 | #else /* PSA_FRAMEWORK_HAS_MM_IOVEC == 1 */ |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 89 | /** |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 90 | * \brief Internal scratch used for IOVec allocations |
| 91 | * |
| 92 | */ |
| 93 | static struct tfm_crypto_scratch { |
| 94 | __attribute__((__aligned__(TFM_CRYPTO_IOVEC_ALIGNMENT))) |
Xinyu Zhang | d755b82 | 2022-10-25 11:18:09 +0800 | [diff] [blame] | 95 | uint8_t buf[CRYPTO_IOVEC_BUFFER_SIZE]; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 96 | uint32_t alloc_index; |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 97 | int32_t owner; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 98 | } scratch = {.buf = {0}, .alloc_index = 0}; |
| 99 | |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 100 | static psa_status_t tfm_crypto_set_scratch_owner(int32_t id) |
| 101 | { |
| 102 | scratch.owner = id; |
| 103 | return PSA_SUCCESS; |
| 104 | } |
| 105 | |
| 106 | static psa_status_t tfm_crypto_get_scratch_owner(int32_t *id) |
| 107 | { |
| 108 | *id = scratch.owner; |
| 109 | return PSA_SUCCESS; |
| 110 | } |
| 111 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 112 | static psa_status_t tfm_crypto_alloc_scratch(size_t requested_size, void **buf) |
| 113 | { |
| 114 | /* Ensure alloc_index remains aligned to the required iovec alignment */ |
| 115 | requested_size = ALIGN(requested_size, TFM_CRYPTO_IOVEC_ALIGNMENT); |
| 116 | |
| 117 | if (requested_size > (sizeof(scratch.buf) - scratch.alloc_index)) { |
| 118 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 119 | } |
| 120 | |
| 121 | /* Compute the pointer to the allocated space */ |
| 122 | *buf = (void *)&scratch.buf[scratch.alloc_index]; |
| 123 | |
| 124 | /* Increase the allocated size */ |
| 125 | scratch.alloc_index += requested_size; |
| 126 | |
| 127 | return PSA_SUCCESS; |
| 128 | } |
| 129 | |
Kevin Peng | fe730cc | 2022-04-11 17:48:42 +0800 | [diff] [blame] | 130 | static void tfm_crypto_clear_scratch(void) |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 131 | { |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 132 | scratch.owner = 0; |
Ken Liu | b671d68 | 2022-05-12 20:39:29 +0800 | [diff] [blame] | 133 | (void)memset(scratch.buf, 0, scratch.alloc_index); |
Summer Qin | 0a9e537 | 2020-11-27 16:04:05 +0800 | [diff] [blame] | 134 | scratch.alloc_index = 0; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Kevin Peng | fe730cc | 2022-04-11 17:48:42 +0800 | [diff] [blame] | 137 | static void tfm_crypto_set_caller_id(int32_t id) |
| 138 | { |
| 139 | /* Set the owner of the data in the scratch */ |
| 140 | (void)tfm_crypto_set_scratch_owner(id); |
| 141 | } |
| 142 | |
| 143 | psa_status_t tfm_crypto_get_caller_id(int32_t *id) |
| 144 | { |
| 145 | return tfm_crypto_get_scratch_owner(id); |
| 146 | } |
| 147 | |
| 148 | static psa_status_t tfm_crypto_init_iovecs(const psa_msg_t *msg, |
| 149 | psa_invec in_vec[], |
| 150 | size_t in_len, |
| 151 | psa_outvec out_vec[], |
| 152 | size_t out_len) |
| 153 | { |
| 154 | uint32_t i; |
| 155 | void *alloc_buf_ptr = NULL; |
| 156 | psa_status_t status; |
| 157 | |
| 158 | /* Alloc/read from the second element as the first is read when parsing */ |
| 159 | for (i = 1; i < in_len; i++) { |
| 160 | /* Allocate necessary space in the internal scratch */ |
| 161 | status = tfm_crypto_alloc_scratch(msg->in_size[i], &alloc_buf_ptr); |
| 162 | if (status != PSA_SUCCESS) { |
| 163 | tfm_crypto_clear_scratch(); |
| 164 | return status; |
| 165 | } |
| 166 | /* Read from the IPC framework inputs into the scratch */ |
| 167 | in_vec[i].len = |
| 168 | psa_read(msg->handle, i, alloc_buf_ptr, msg->in_size[i]); |
| 169 | /* Populate the fields of the input to the secure function */ |
| 170 | in_vec[i].base = alloc_buf_ptr; |
| 171 | } |
| 172 | |
| 173 | for (i = 0; i < out_len; i++) { |
| 174 | /* Allocate necessary space for the output in the internal scratch */ |
| 175 | status = tfm_crypto_alloc_scratch(msg->out_size[i], &alloc_buf_ptr); |
| 176 | if (status != PSA_SUCCESS) { |
| 177 | tfm_crypto_clear_scratch(); |
| 178 | return status; |
| 179 | } |
| 180 | /* Populate the fields of the output to the secure function */ |
| 181 | out_vec[i].base = alloc_buf_ptr; |
| 182 | out_vec[i].len = msg->out_size[i]; |
| 183 | } |
| 184 | |
| 185 | return PSA_SUCCESS; |
| 186 | } |
| 187 | #endif /* PSA_FRAMEWORK_HAS_MM_IOVEC == 1 */ |
| 188 | |
Kevin Peng | 2d4bc2e | 2022-01-28 16:19:30 +0800 | [diff] [blame] | 189 | static psa_status_t tfm_crypto_call_srv(const psa_msg_t *msg) |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 190 | { |
| 191 | psa_status_t status = PSA_SUCCESS; |
TTornblom | faf74f5 | 2020-03-04 17:56:27 +0100 | [diff] [blame] | 192 | size_t in_len = PSA_MAX_IOVEC, out_len = PSA_MAX_IOVEC, i; |
Soby Mathew | d8abdfd | 2020-10-14 10:28:01 +0100 | [diff] [blame] | 193 | psa_invec in_vec[PSA_MAX_IOVEC] = { {NULL, 0} }; |
| 194 | psa_outvec out_vec[PSA_MAX_IOVEC] = { {NULL, 0} }; |
Kevin Peng | 2d4bc2e | 2022-01-28 16:19:30 +0800 | [diff] [blame] | 195 | struct tfm_crypto_pack_iovec iov = {0}; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 196 | |
| 197 | /* Check the number of in_vec filled */ |
Jamie Fox | 9a234e2 | 2019-04-30 11:12:05 +0100 | [diff] [blame] | 198 | while ((in_len > 0) && (msg->in_size[in_len - 1] == 0)) { |
| 199 | in_len--; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 200 | } |
| 201 | |
Kevin Peng | fe730cc | 2022-04-11 17:48:42 +0800 | [diff] [blame] | 202 | /* Check the number of out_vec filled */ |
| 203 | while ((out_len > 0) && (msg->out_size[out_len - 1] == 0)) { |
| 204 | out_len--; |
| 205 | } |
| 206 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 207 | /* There will always be a tfm_crypto_pack_iovec in the first iovec */ |
| 208 | if (in_len < 1) { |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 209 | return PSA_ERROR_GENERIC_ERROR; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 210 | } |
Kevin Peng | 2d4bc2e | 2022-01-28 16:19:30 +0800 | [diff] [blame] | 211 | |
| 212 | if (psa_read(msg->handle, 0, &iov, sizeof(iov)) != sizeof(iov)) { |
| 213 | return PSA_ERROR_GENERIC_ERROR; |
| 214 | } |
| 215 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 216 | /* Initialise the first iovec with the IOV read when parsing */ |
Kevin Peng | 2d4bc2e | 2022-01-28 16:19:30 +0800 | [diff] [blame] | 217 | in_vec[0].base = &iov; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 218 | in_vec[0].len = sizeof(struct tfm_crypto_pack_iovec); |
| 219 | |
Kevin Peng | fe730cc | 2022-04-11 17:48:42 +0800 | [diff] [blame] | 220 | status = tfm_crypto_init_iovecs(msg, in_vec, in_len, out_vec, out_len); |
| 221 | if (status != PSA_SUCCESS) { |
| 222 | return status; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 223 | } |
| 224 | |
Kevin Peng | fe730cc | 2022-04-11 17:48:42 +0800 | [diff] [blame] | 225 | tfm_crypto_set_caller_id(msg->client_id); |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 226 | |
Antonio de Angelis | 202425a | 2022-04-06 11:13:15 +0100 | [diff] [blame] | 227 | /* Call the dispatcher to the functions that implement the PSA Crypto API */ |
| 228 | status = tfm_crypto_api_dispatcher(in_vec, in_len, out_vec, out_len); |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 229 | |
Kevin Peng | fe730cc | 2022-04-11 17:48:42 +0800 | [diff] [blame] | 230 | #if PSA_FRAMEWORK_HAS_MM_IOVEC == 1 |
| 231 | for (i = 0; i < out_len; i++) { |
| 232 | if (out_vec[i].base != NULL) { |
| 233 | psa_unmap_outvec(msg->handle, i, out_vec[i].len); |
| 234 | } |
| 235 | } |
| 236 | #else |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 237 | /* Write into the IPC framework outputs from the scratch */ |
| 238 | for (i = 0; i < out_len; i++) { |
| 239 | psa_write(msg->handle, i, out_vec[i].base, out_vec[i].len); |
| 240 | } |
| 241 | |
| 242 | /* Clear the allocated internal scratch before returning */ |
Summer Qin | 0a9e537 | 2020-11-27 16:04:05 +0800 | [diff] [blame] | 243 | tfm_crypto_clear_scratch(); |
Kevin Peng | fe730cc | 2022-04-11 17:48:42 +0800 | [diff] [blame] | 244 | #endif |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 245 | |
| 246 | return status; |
| 247 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 248 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 249 | static psa_status_t tfm_crypto_engine_init(void) |
| 250 | { |
Antonio de Angelis | edbafb6 | 2022-12-01 13:52:15 +0000 | [diff] [blame] | 251 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Antonio de Angelis | 8f7db7d | 2022-12-01 12:09:16 +0000 | [diff] [blame] | 252 | char *library_info = NULL; |
Antonio de Angelis | edbafb6 | 2022-12-01 13:52:15 +0000 | [diff] [blame] | 253 | |
Xinyu Zhang | d755b82 | 2022-10-25 11:18:09 +0800 | [diff] [blame] | 254 | #if CRYPTO_NV_SEED |
Antonio de Angelis | 202425a | 2022-04-06 11:13:15 +0100 | [diff] [blame] | 255 | LOG_INFFMT("[INF][Crypto] "); |
Antonio de Angelis | 202425a | 2022-04-06 11:13:15 +0100 | [diff] [blame] | 256 | LOG_INFFMT("Provisioning entropy seed... "); |
Raef Coles | 618fc15 | 2021-06-18 09:26:46 +0100 | [diff] [blame] | 257 | if (tfm_plat_crypto_provision_entropy_seed() != TFM_CRYPTO_NV_SEED_SUCCESS) { |
Summer Qin | a5448d6 | 2020-12-07 14:03:37 +0800 | [diff] [blame] | 258 | return PSA_ERROR_GENERIC_ERROR; |
| 259 | } |
Antonio de Angelis | 202425a | 2022-04-06 11:13:15 +0100 | [diff] [blame] | 260 | LOG_INFFMT("\033[0;32mcomplete.\033[0m\r\n"); |
Raef Coles | 618fc15 | 2021-06-18 09:26:46 +0100 | [diff] [blame] | 261 | #endif /* CRYPTO_NV_SEED */ |
Summer Qin | a5448d6 | 2020-12-07 14:03:37 +0800 | [diff] [blame] | 262 | |
Antonio de Angelis | edbafb6 | 2022-12-01 13:52:15 +0000 | [diff] [blame] | 263 | /* Initialise the underlying Cryptographic library that provides the |
| 264 | * PSA Crypto core layer |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 265 | */ |
Antonio de Angelis | edbafb6 | 2022-12-01 13:52:15 +0000 | [diff] [blame] | 266 | library_info = tfm_crypto_library_get_info(); |
| 267 | LOG_DBGFMT("[DBG][Crypto] Initialising \033[0;32m%s\033[0m as PSA Crypto backend library... ", library_info); |
| 268 | status = tfm_crypto_core_library_init(); |
| 269 | if (status != PSA_SUCCESS) { |
| 270 | return status; |
| 271 | } |
| 272 | LOG_DBGFMT("\033[0;32mcomplete.\033[0m\r\n"); |
Sherry Zhang | e152498 | 2022-06-08 16:57:59 +0800 | [diff] [blame] | 273 | |
Antonio de Angelis | 695d75b | 2022-08-22 15:06:24 +0100 | [diff] [blame] | 274 | /* Initialise the crypto accelerator if one is enabled. If the driver API is |
| 275 | * the one defined by the PSA Unified Driver interface, the initialisation is |
| 276 | * performed directly through psa_crypto_init() while the PSA subsystem is |
| 277 | * initialised |
| 278 | */ |
| 279 | #if defined(CRYPTO_HW_ACCELERATOR) && defined(CC312_LEGACY_DRIVER_API_ENABLED) |
Antonio de Angelis | 202425a | 2022-04-06 11:13:15 +0100 | [diff] [blame] | 280 | LOG_INFFMT("[INF][Crypto] Initialising HW accelerator... "); |
Raef Coles | d2485af | 2019-10-30 10:15:33 +0000 | [diff] [blame] | 281 | if (crypto_hw_accelerator_init() != 0) { |
| 282 | return PSA_ERROR_HARDWARE_FAILURE; |
| 283 | } |
Antonio de Angelis | 202425a | 2022-04-06 11:13:15 +0100 | [diff] [blame] | 284 | LOG_INFFMT("\033[0;32mcomplete.\033[0m\r\n"); |
Raef Coles | d2485af | 2019-10-30 10:15:33 +0000 | [diff] [blame] | 285 | #endif /* CRYPTO_HW_ACCELERATOR */ |
| 286 | |
Antonio de Angelis | edbafb6 | 2022-12-01 13:52:15 +0000 | [diff] [blame] | 287 | /* Perform the initialisation of the PSA subsystem available through the chosen |
| 288 | * Cryptographic library. If a driver is built using the PSA Driver interface, |
| 289 | * the function below will perform also the same operations done by the HAL init |
| 290 | * crypto_hw_accelerator_init() |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 291 | */ |
| 292 | return psa_crypto_init(); |
| 293 | } |
| 294 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 295 | static psa_status_t tfm_crypto_module_init(void) |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 296 | { |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 297 | /* Init the Alloc module */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 298 | return tfm_crypto_init_alloc(); |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 299 | } |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 300 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 301 | psa_status_t tfm_crypto_init(void) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 302 | { |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 303 | psa_status_t status; |
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 | /* Initialise other modules of the service */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 306 | status = tfm_crypto_module_init(); |
| 307 | if (status != PSA_SUCCESS) { |
| 308 | return status; |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 309 | } |
| 310 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 311 | /* Initialise the engine layer */ |
Raef Coles | 79809c7 | 2022-03-02 13:48:20 +0000 | [diff] [blame] | 312 | status = tfm_crypto_engine_init(); |
| 313 | if (status != PSA_SUCCESS) { |
| 314 | return status; |
| 315 | } |
| 316 | |
Raef Coles | 79809c7 | 2022-03-02 13:48:20 +0000 | [diff] [blame] | 317 | return PSA_SUCCESS; |
Kevin Peng | 2d4bc2e | 2022-01-28 16:19:30 +0800 | [diff] [blame] | 318 | } |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 319 | |
Kevin Peng | 2d4bc2e | 2022-01-28 16:19:30 +0800 | [diff] [blame] | 320 | psa_status_t tfm_crypto_sfn(const psa_msg_t *msg) |
| 321 | { |
| 322 | /* Process the message type */ |
| 323 | switch (msg->type) { |
| 324 | case PSA_IPC_CALL: |
| 325 | return tfm_crypto_call_srv(msg); |
| 326 | default: |
| 327 | return PSA_ERROR_NOT_SUPPORTED; |
| 328 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 329 | |
Kevin Peng | 2d4bc2e | 2022-01-28 16:19:30 +0800 | [diff] [blame] | 330 | return PSA_ERROR_GENERIC_ERROR; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 331 | } |
Antonio de Angelis | 202425a | 2022-04-06 11:13:15 +0100 | [diff] [blame] | 332 | |
| 333 | psa_status_t tfm_crypto_api_dispatcher(psa_invec in_vec[], |
| 334 | size_t in_len, |
| 335 | psa_outvec out_vec[], |
| 336 | size_t out_len) |
| 337 | { |
| 338 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 339 | const struct tfm_crypto_pack_iovec *iov = in_vec[0].base; |
| 340 | int32_t caller_id = 0; |
Antonio de Angelis | 7557e68 | 2022-11-30 15:37:51 +0000 | [diff] [blame] | 341 | struct tfm_crypto_key_id_s encoded_key = TFM_CRYPTO_KEY_ID_S_INIT; |
Antonio de Angelis | 202425a | 2022-04-06 11:13:15 +0100 | [diff] [blame] | 342 | bool is_key_required = false; |
David Hu | c9679cc | 2022-06-21 13:09:34 +0800 | [diff] [blame] | 343 | enum tfm_crypto_group_id group_id; |
Antonio de Angelis | 202425a | 2022-04-06 11:13:15 +0100 | [diff] [blame] | 344 | |
| 345 | if (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) { |
| 346 | return PSA_ERROR_PROGRAMMER_ERROR; |
| 347 | } |
| 348 | |
David Hu | c9679cc | 2022-06-21 13:09:34 +0800 | [diff] [blame] | 349 | group_id = TFM_CRYPTO_GET_GROUP_ID(iov->function_id); |
| 350 | |
| 351 | is_key_required = !((group_id == TFM_CRYPTO_GROUP_ID_HASH) || |
| 352 | (group_id == TFM_CRYPTO_GROUP_ID_RANDOM)); |
Antonio de Angelis | 202425a | 2022-04-06 11:13:15 +0100 | [diff] [blame] | 353 | |
| 354 | if (is_key_required) { |
| 355 | status = tfm_crypto_get_caller_id(&caller_id); |
| 356 | if (status != PSA_SUCCESS) { |
| 357 | return status; |
| 358 | } |
| 359 | /* The caller_id being set in the owner field is the partition ID |
| 360 | * of the calling partition |
| 361 | */ |
Antonio de Angelis | 7557e68 | 2022-11-30 15:37:51 +0000 | [diff] [blame] | 362 | encoded_key.key_id = iov->key_id; |
| 363 | encoded_key.owner = caller_id; |
Antonio de Angelis | 202425a | 2022-04-06 11:13:15 +0100 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | /* Dispatch to each sub-module based on the Group ID */ |
David Hu | c9679cc | 2022-06-21 13:09:34 +0800 | [diff] [blame] | 367 | switch (group_id) { |
| 368 | case TFM_CRYPTO_GROUP_ID_KEY_MANAGEMENT: |
| 369 | return tfm_crypto_key_management_interface(in_vec, out_vec, |
| 370 | &encoded_key); |
| 371 | case TFM_CRYPTO_GROUP_ID_HASH: |
| 372 | return tfm_crypto_hash_interface(in_vec, out_vec); |
| 373 | case TFM_CRYPTO_GROUP_ID_MAC: |
| 374 | return tfm_crypto_mac_interface(in_vec, out_vec, &encoded_key); |
| 375 | case TFM_CRYPTO_GROUP_ID_CIPHER: |
| 376 | return tfm_crypto_cipher_interface(in_vec, out_vec, &encoded_key); |
| 377 | case TFM_CRYPTO_GROUP_ID_AEAD: |
| 378 | return tfm_crypto_aead_interface(in_vec, out_vec, &encoded_key); |
| 379 | case TFM_CRYPTO_GROUP_ID_ASYM_SIGN: |
| 380 | return tfm_crypto_asymmetric_sign_interface(in_vec, out_vec, |
| 381 | &encoded_key); |
| 382 | case TFM_CRYPTO_GROUP_ID_ASYM_ENCRYPT: |
| 383 | return tfm_crypto_asymmetric_encrypt_interface(in_vec, out_vec, |
| 384 | &encoded_key); |
| 385 | case TFM_CRYPTO_GROUP_ID_KEY_DERIVATION: |
| 386 | return tfm_crypto_key_derivation_interface(in_vec, out_vec, |
| 387 | &encoded_key); |
| 388 | case TFM_CRYPTO_GROUP_ID_RANDOM: |
| 389 | return tfm_crypto_random_interface(in_vec, out_vec); |
| 390 | default: |
Antonio de Angelis | 202425a | 2022-04-06 11:13:15 +0100 | [diff] [blame] | 391 | LOG_ERRFMT("[ERR][Crypto] Unsupported request!\r\n"); |
David Hu | c9679cc | 2022-06-21 13:09:34 +0800 | [diff] [blame] | 392 | return PSA_ERROR_NOT_SUPPORTED; |
Antonio de Angelis | 202425a | 2022-04-06 11:13:15 +0100 | [diff] [blame] | 393 | } |
| 394 | |
David Hu | c9679cc | 2022-06-21 13:09:34 +0800 | [diff] [blame] | 395 | return PSA_ERROR_NOT_SUPPORTED; |
Antonio de Angelis | 202425a | 2022-04-06 11:13:15 +0100 | [diff] [blame] | 396 | } |