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