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