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 | */ |
| 7 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 8 | #include "tfm_mbedcrypto_include.h" |
| 9 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 10 | #include "tfm_crypto_api.h" |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 11 | #include "tfm_crypto_defs.h" |
Summer Qin | c737ece | 2020-08-28 10:47:26 +0800 | [diff] [blame] | 12 | #include "tfm_sp_log.h" |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 13 | |
| 14 | /* |
| 15 | * \brief This Mbed TLS include is needed to initialise the memory allocator |
| 16 | * inside the Mbed TLS layer of Mbed Crypto |
| 17 | */ |
| 18 | #include "mbedtls/memory_buffer_alloc.h" |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 19 | |
Raef Coles | 618fc15 | 2021-06-18 09:26:46 +0100 | [diff] [blame] | 20 | #ifdef CRYPTO_NV_SEED |
| 21 | #include "tfm_plat_crypto_nv_seed.h" |
| 22 | #endif /* CRYPTO_NV_SEED */ |
Summer Qin | a5448d6 | 2020-12-07 14:03:37 +0800 | [diff] [blame] | 23 | |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 24 | #ifndef TFM_PSA_API |
| 25 | #include "tfm_secure_api.h" |
| 26 | #endif |
| 27 | |
Raef Coles | d2485af | 2019-10-30 10:15:33 +0000 | [diff] [blame] | 28 | #ifdef CRYPTO_HW_ACCELERATOR |
| 29 | #include "crypto_hw.h" |
Michel Jaouen | f41c642 | 2021-10-07 14:38:08 +0200 | [diff] [blame] | 30 | #endif /* CRYPTO_HW_ACCELERATOR */ |
Raef Coles | d2485af | 2019-10-30 10:15:33 +0000 | [diff] [blame] | 31 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 32 | #ifdef TFM_PSA_API |
Jamie Fox | cc31d40 | 2019-01-28 17:13:52 +0000 | [diff] [blame] | 33 | #include "psa/service.h" |
Edison Ai | cc4c616 | 2019-06-21 13:52:49 +0800 | [diff] [blame] | 34 | #include "psa_manifest/tfm_crypto.h" |
David Hu | 49a28eb | 2019-08-14 18:18:15 +0800 | [diff] [blame] | 35 | #include "tfm_memory_utils.h" |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * \brief Table containing all the Uniform Signature API exposed |
| 39 | * by the TF-M Crypto partition |
| 40 | */ |
Edison Ai | 080b2e2 | 2019-04-17 16:27:21 +0800 | [diff] [blame] | 41 | static const tfm_crypto_us_t sfid_func_table[TFM_CRYPTO_SID_MAX] = { |
Antonio de Angelis | 25e2b2d | 2019-04-25 14:49:50 +0100 | [diff] [blame] | 42 | #define X(api_name) api_name, |
| 43 | LIST_TFM_CRYPTO_UNIFORM_SIGNATURE_API |
| 44 | #undef X |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | /** |
| 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 | |
| 58 | /** |
| 59 | * \brief Default size of the internal scratch buffer used for IOVec allocations |
| 60 | * in bytes |
| 61 | */ |
| 62 | #ifndef TFM_CRYPTO_IOVEC_BUFFER_SIZE |
Soby Mathew | 7740b60 | 2020-10-07 12:08:28 +0100 | [diff] [blame] | 63 | #error TFM_CRYPTO_IOVEC_BUFFER_SIZE is not defined |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 64 | #endif |
| 65 | |
| 66 | /** |
| 67 | * \brief Internal scratch used for IOVec allocations |
| 68 | * |
| 69 | */ |
| 70 | static struct tfm_crypto_scratch { |
| 71 | __attribute__((__aligned__(TFM_CRYPTO_IOVEC_ALIGNMENT))) |
| 72 | uint8_t buf[TFM_CRYPTO_IOVEC_BUFFER_SIZE]; |
| 73 | uint32_t alloc_index; |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 74 | int32_t owner; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 75 | } scratch = {.buf = {0}, .alloc_index = 0}; |
| 76 | |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 77 | static psa_status_t tfm_crypto_set_scratch_owner(int32_t id) |
| 78 | { |
| 79 | scratch.owner = id; |
| 80 | return PSA_SUCCESS; |
| 81 | } |
| 82 | |
| 83 | static psa_status_t tfm_crypto_get_scratch_owner(int32_t *id) |
| 84 | { |
| 85 | *id = scratch.owner; |
| 86 | return PSA_SUCCESS; |
| 87 | } |
| 88 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 89 | static psa_status_t tfm_crypto_alloc_scratch(size_t requested_size, void **buf) |
| 90 | { |
| 91 | /* Ensure alloc_index remains aligned to the required iovec alignment */ |
| 92 | requested_size = ALIGN(requested_size, TFM_CRYPTO_IOVEC_ALIGNMENT); |
| 93 | |
| 94 | if (requested_size > (sizeof(scratch.buf) - scratch.alloc_index)) { |
| 95 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 96 | } |
| 97 | |
| 98 | /* Compute the pointer to the allocated space */ |
| 99 | *buf = (void *)&scratch.buf[scratch.alloc_index]; |
| 100 | |
| 101 | /* Increase the allocated size */ |
| 102 | scratch.alloc_index += requested_size; |
| 103 | |
| 104 | return PSA_SUCCESS; |
| 105 | } |
| 106 | |
Summer Qin | 0a9e537 | 2020-11-27 16:04:05 +0800 | [diff] [blame] | 107 | void tfm_crypto_clear_scratch(void) |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 108 | { |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 109 | scratch.owner = 0; |
Summer Qin | 0a9e537 | 2020-11-27 16:04:05 +0800 | [diff] [blame] | 110 | (void)tfm_memset(scratch.buf, 0, scratch.alloc_index); |
| 111 | scratch.alloc_index = 0; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Kevin Peng | 2d4bc2e | 2022-01-28 16:19:30 +0800 | [diff] [blame^] | 114 | 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] | 115 | { |
| 116 | psa_status_t status = PSA_SUCCESS; |
TTornblom | faf74f5 | 2020-03-04 17:56:27 +0100 | [diff] [blame] | 117 | 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] | 118 | psa_invec in_vec[PSA_MAX_IOVEC] = { {NULL, 0} }; |
| 119 | psa_outvec out_vec[PSA_MAX_IOVEC] = { {NULL, 0} }; |
Kevin Peng | 2d4bc2e | 2022-01-28 16:19:30 +0800 | [diff] [blame^] | 120 | struct tfm_crypto_pack_iovec iov = {0}; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 121 | void *alloc_buf_ptr = NULL; |
| 122 | |
| 123 | /* Check the number of in_vec filled */ |
Jamie Fox | 9a234e2 | 2019-04-30 11:12:05 +0100 | [diff] [blame] | 124 | while ((in_len > 0) && (msg->in_size[in_len - 1] == 0)) { |
| 125 | in_len--; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /* There will always be a tfm_crypto_pack_iovec in the first iovec */ |
| 129 | if (in_len < 1) { |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 130 | return PSA_ERROR_GENERIC_ERROR; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 131 | } |
Kevin Peng | 2d4bc2e | 2022-01-28 16:19:30 +0800 | [diff] [blame^] | 132 | |
| 133 | if (psa_read(msg->handle, 0, &iov, sizeof(iov)) != sizeof(iov)) { |
| 134 | return PSA_ERROR_GENERIC_ERROR; |
| 135 | } |
| 136 | |
| 137 | if (iov.srv_id >= TFM_CRYPTO_SID_MAX) { |
| 138 | return PSA_ERROR_GENERIC_ERROR; |
| 139 | } |
| 140 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 141 | /* Initialise the first iovec with the IOV read when parsing */ |
Kevin Peng | 2d4bc2e | 2022-01-28 16:19:30 +0800 | [diff] [blame^] | 142 | in_vec[0].base = &iov; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 143 | in_vec[0].len = sizeof(struct tfm_crypto_pack_iovec); |
| 144 | |
| 145 | /* Alloc/read from the second element as the first is read when parsing */ |
| 146 | for (i = 1; i < in_len; i++) { |
| 147 | /* Allocate necessary space in the internal scratch */ |
| 148 | status = tfm_crypto_alloc_scratch(msg->in_size[i], &alloc_buf_ptr); |
| 149 | if (status != PSA_SUCCESS) { |
Summer Qin | 0a9e537 | 2020-11-27 16:04:05 +0800 | [diff] [blame] | 150 | tfm_crypto_clear_scratch(); |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 151 | return status; |
| 152 | } |
| 153 | /* Read from the IPC framework inputs into the scratch */ |
Kevin Peng | 2d4bc2e | 2022-01-28 16:19:30 +0800 | [diff] [blame^] | 154 | in_vec[i].len = |
| 155 | psa_read(msg->handle, i, alloc_buf_ptr, msg->in_size[i]); |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 156 | /* Populate the fields of the input to the secure function */ |
| 157 | in_vec[i].base = alloc_buf_ptr; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /* Check the number of out_vec filled */ |
Jamie Fox | 9a234e2 | 2019-04-30 11:12:05 +0100 | [diff] [blame] | 161 | while ((out_len > 0) && (msg->out_size[out_len - 1] == 0)) { |
| 162 | out_len--; |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | for (i = 0; i < out_len; i++) { |
| 166 | /* Allocate necessary space for the output in the internal scratch */ |
| 167 | status = tfm_crypto_alloc_scratch(msg->out_size[i], &alloc_buf_ptr); |
| 168 | if (status != PSA_SUCCESS) { |
Summer Qin | 0a9e537 | 2020-11-27 16:04:05 +0800 | [diff] [blame] | 169 | tfm_crypto_clear_scratch(); |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 170 | return status; |
| 171 | } |
| 172 | /* Populate the fields of the output to the secure function */ |
| 173 | out_vec[i].base = alloc_buf_ptr; |
| 174 | out_vec[i].len = msg->out_size[i]; |
| 175 | } |
| 176 | |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 177 | /* Set the owner of the data in the scratch */ |
| 178 | (void)tfm_crypto_set_scratch_owner(msg->client_id); |
| 179 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 180 | /* Call the uniform signature API */ |
Kevin Peng | 2d4bc2e | 2022-01-28 16:19:30 +0800 | [diff] [blame^] | 181 | status = sfid_func_table[iov.srv_id](in_vec, in_len, out_vec, out_len); |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 182 | |
| 183 | /* Write into the IPC framework outputs from the scratch */ |
| 184 | for (i = 0; i < out_len; i++) { |
| 185 | psa_write(msg->handle, i, out_vec[i].base, out_vec[i].len); |
| 186 | } |
| 187 | |
| 188 | /* Clear the allocated internal scratch before returning */ |
Summer Qin | 0a9e537 | 2020-11-27 16:04:05 +0800 | [diff] [blame] | 189 | tfm_crypto_clear_scratch(); |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 190 | |
| 191 | return status; |
| 192 | } |
| 193 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 194 | #endif /* TFM_PSA_API */ |
| 195 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 196 | /** |
| 197 | * \brief Default value for the size of the static buffer used by Mbed |
| 198 | * Crypto for its dynamic allocations |
| 199 | */ |
| 200 | #ifndef TFM_CRYPTO_ENGINE_BUF_SIZE |
Soby Mathew | 7740b60 | 2020-10-07 12:08:28 +0100 | [diff] [blame] | 201 | #error TFM_CRYPTO_ENGINE_BUF_SIZE is not defined |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 202 | #endif |
| 203 | |
| 204 | /** |
| 205 | * \brief Static buffer to be used by Mbed Crypto for memory allocations |
| 206 | * |
| 207 | */ |
| 208 | static uint8_t mbedtls_mem_buf[TFM_CRYPTO_ENGINE_BUF_SIZE] = {0}; |
| 209 | |
| 210 | static psa_status_t tfm_crypto_engine_init(void) |
| 211 | { |
Raef Coles | 618fc15 | 2021-06-18 09:26:46 +0100 | [diff] [blame] | 212 | |
| 213 | #ifdef CRYPTO_NV_SEED |
| 214 | #ifdef TFM_PSA_API |
| 215 | if (tfm_plat_crypto_provision_entropy_seed() != TFM_CRYPTO_NV_SEED_SUCCESS) { |
Summer Qin | a5448d6 | 2020-12-07 14:03:37 +0800 | [diff] [blame] | 216 | return PSA_ERROR_GENERIC_ERROR; |
| 217 | } |
Raef Coles | 618fc15 | 2021-06-18 09:26:46 +0100 | [diff] [blame] | 218 | #else |
| 219 | LOG_INFFMT("\033[1;31m[Crypto] "); |
| 220 | LOG_INFFMT("TF-M in library mode uses a dummy NV seed. "); |
| 221 | LOG_INFFMT("This is not suitable for production! "); |
| 222 | LOG_INFFMT("This device is \033[1;1mNOT SECURE"); |
| 223 | LOG_INFFMT("\033[0m\r\n"); |
| 224 | #endif /* TFM_PSA_API */ |
| 225 | #endif /* CRYPTO_NV_SEED */ |
Summer Qin | a5448d6 | 2020-12-07 14:03:37 +0800 | [diff] [blame] | 226 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 227 | /* Initialise the Mbed Crypto memory allocator to use static |
| 228 | * memory allocation from the provided buffer instead of using |
| 229 | * the heap |
| 230 | */ |
| 231 | mbedtls_memory_buffer_alloc_init(mbedtls_mem_buf, |
| 232 | TFM_CRYPTO_ENGINE_BUF_SIZE); |
| 233 | |
Raef Coles | d2485af | 2019-10-30 10:15:33 +0000 | [diff] [blame] | 234 | /* Initialise the crypto accelerator if one is enabled */ |
| 235 | #ifdef CRYPTO_HW_ACCELERATOR |
| 236 | if (crypto_hw_accelerator_init() != 0) { |
| 237 | return PSA_ERROR_HARDWARE_FAILURE; |
| 238 | } |
| 239 | #endif /* CRYPTO_HW_ACCELERATOR */ |
| 240 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 241 | /* Previous function does not return any value, so just call the |
| 242 | * initialisation function of the Mbed Crypto layer |
| 243 | */ |
| 244 | return psa_crypto_init(); |
| 245 | } |
| 246 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 247 | static psa_status_t tfm_crypto_module_init(void) |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 248 | { |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 249 | /* Init the Alloc module */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 250 | return tfm_crypto_init_alloc(); |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 251 | } |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 252 | |
Antonio de Angelis | 60a6fe6 | 2019-06-18 15:27:34 +0100 | [diff] [blame] | 253 | psa_status_t tfm_crypto_get_caller_id(int32_t *id) |
| 254 | { |
| 255 | #ifdef TFM_PSA_API |
| 256 | return tfm_crypto_get_scratch_owner(id); |
| 257 | #else |
| 258 | int32_t res; |
| 259 | |
| 260 | res = tfm_core_get_caller_client_id(id); |
| 261 | if (res != TFM_SUCCESS) { |
| 262 | return PSA_ERROR_NOT_PERMITTED; |
| 263 | } else { |
| 264 | return PSA_SUCCESS; |
| 265 | } |
| 266 | #endif |
| 267 | } |
| 268 | |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 269 | psa_status_t tfm_crypto_init(void) |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 270 | { |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 271 | psa_status_t status; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 272 | |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 273 | /* Initialise other modules of the service */ |
Antonio de Angelis | ab85ccd | 2019-03-25 15:14:29 +0000 | [diff] [blame] | 274 | status = tfm_crypto_module_init(); |
| 275 | if (status != PSA_SUCCESS) { |
| 276 | return status; |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 277 | } |
| 278 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 279 | /* Initialise the engine layer */ |
Kevin Peng | 2d4bc2e | 2022-01-28 16:19:30 +0800 | [diff] [blame^] | 280 | return tfm_crypto_engine_init(); |
| 281 | } |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 282 | |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 283 | #ifdef TFM_PSA_API |
Kevin Peng | 2d4bc2e | 2022-01-28 16:19:30 +0800 | [diff] [blame^] | 284 | psa_status_t tfm_crypto_sfn(const psa_msg_t *msg) |
| 285 | { |
| 286 | /* Process the message type */ |
| 287 | switch (msg->type) { |
| 288 | case PSA_IPC_CALL: |
| 289 | return tfm_crypto_call_srv(msg); |
| 290 | default: |
| 291 | return PSA_ERROR_NOT_SUPPORTED; |
| 292 | } |
Antonio de Angelis | 4743e67 | 2019-04-11 11:38:48 +0100 | [diff] [blame] | 293 | |
Kevin Peng | 2d4bc2e | 2022-01-28 16:19:30 +0800 | [diff] [blame^] | 294 | return PSA_ERROR_GENERIC_ERROR; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 295 | } |
Kevin Peng | 2d4bc2e | 2022-01-28 16:19:30 +0800 | [diff] [blame^] | 296 | #endif |