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