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