blob: 1854b8faf817a24d44e3a2e29d235d514652223d [file] [log] [blame]
Antonio de Angelis8908f472018-08-31 15:44:25 +01001/*
Antonio de Angelis01a93bc2023-01-20 11:17:14 +00002 * Copyright (c) 2018-2023, Arm Limited. All rights reserved.
Antonio de Angelis8908f472018-08-31 15:44:25 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
Antonio de Angelis202425a2022-04-06 11:13:15 +01007#include <stdbool.h>
Antonio de Angelis8908f472018-08-31 15:44:25 +01008
David Hu6d1a9b62023-02-22 16:54:04 +08009#include "config_tfm.h"
10#include "config_crypto_check.h"
Jamie Fox0e54ebc2019-04-09 14:21:04 +010011#include "tfm_mbedcrypto_include.h"
12
Antonio de Angelis8908f472018-08-31 15:44:25 +010013#include "tfm_crypto_api.h"
Antonio de Angelis7557e682022-11-30 15:37:51 +000014#include "tfm_crypto_key.h"
Jamie Fox0e54ebc2019-04-09 14:21:04 +010015#include "tfm_crypto_defs.h"
Summer Qinc737ece2020-08-28 10:47:26 +080016#include "tfm_sp_log.h"
Summer Qinca6c1522022-06-17 14:25:55 +080017#include "crypto_check_config.h"
Raef Coles79809c72022-03-02 13:48:20 +000018#include "tfm_plat_crypto_keys.h"
Jamie Fox0e54ebc2019-04-09 14:21:04 +010019
Antonio de Angelis8f7db7d2022-12-01 12:09:16 +000020#include "crypto_library.h"
21
Xinyu Zhangd755b822022-10-25 11:18:09 +080022#if CRYPTO_NV_SEED
Raef Coles618fc152021-06-18 09:26:46 +010023#include "tfm_plat_crypto_nv_seed.h"
24#endif /* CRYPTO_NV_SEED */
Summer Qina5448d62020-12-07 14:03:37 +080025
Raef Colesd2485af2019-10-30 10:15:33 +000026#ifdef CRYPTO_HW_ACCELERATOR
27#include "crypto_hw.h"
Michel Jaouenf41c6422021-10-07 14:38:08 +020028#endif /* CRYPTO_HW_ACCELERATOR */
Raef Colesd2485af2019-10-30 10:15:33 +000029
Ken Liub671d682022-05-12 20:39:29 +080030#include <string.h>
Kevin Pengfe730cc2022-04-11 17:48:42 +080031#include "psa/framework_feature.h"
Jamie Foxcc31d402019-01-28 17:13:52 +000032#include "psa/service.h"
Edison Aicc4c6162019-06-21 13:52:49 +080033#include "psa_manifest/tfm_crypto.h"
Antonio de Angelis4743e672019-04-11 11:38:48 +010034
35/**
Antonio de Angelis4743e672019-04-11 11:38:48 +010036 * \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 Pengfe730cc2022-04-11 17:48:42 +080046#if PSA_FRAMEWORK_HAS_MM_IOVEC == 1
47static int32_t g_client_id;
48
49static void tfm_crypto_set_caller_id(int32_t id)
50{
51 g_client_id = id;
52}
53
54psa_status_t tfm_crypto_get_caller_id(int32_t *id)
55{
56 *id = g_client_id;
57 return PSA_SUCCESS;
58}
59
60static 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 Angelis4743e672019-04-11 11:38:48 +010090/**
Antonio de Angelis4743e672019-04-11 11:38:48 +010091 * \brief Internal scratch used for IOVec allocations
92 *
93 */
94static struct tfm_crypto_scratch {
95 __attribute__((__aligned__(TFM_CRYPTO_IOVEC_ALIGNMENT)))
Xinyu Zhangd755b822022-10-25 11:18:09 +080096 uint8_t buf[CRYPTO_IOVEC_BUFFER_SIZE];
Antonio de Angelis4743e672019-04-11 11:38:48 +010097 uint32_t alloc_index;
Antonio de Angelis60a6fe62019-06-18 15:27:34 +010098 int32_t owner;
Antonio de Angelis4743e672019-04-11 11:38:48 +010099} scratch = {.buf = {0}, .alloc_index = 0};
100
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100101static psa_status_t tfm_crypto_set_scratch_owner(int32_t id)
102{
103 scratch.owner = id;
104 return PSA_SUCCESS;
105}
106
107static psa_status_t tfm_crypto_get_scratch_owner(int32_t *id)
108{
109 *id = scratch.owner;
110 return PSA_SUCCESS;
111}
112
Antonio de Angelis4743e672019-04-11 11:38:48 +0100113static psa_status_t tfm_crypto_alloc_scratch(size_t requested_size, void **buf)
114{
115 /* Ensure alloc_index remains aligned to the required iovec alignment */
116 requested_size = ALIGN(requested_size, TFM_CRYPTO_IOVEC_ALIGNMENT);
117
118 if (requested_size > (sizeof(scratch.buf) - scratch.alloc_index)) {
119 return PSA_ERROR_INSUFFICIENT_MEMORY;
120 }
121
122 /* Compute the pointer to the allocated space */
123 *buf = (void *)&scratch.buf[scratch.alloc_index];
124
125 /* Increase the allocated size */
126 scratch.alloc_index += requested_size;
127
128 return PSA_SUCCESS;
129}
130
Kevin Pengfe730cc2022-04-11 17:48:42 +0800131static void tfm_crypto_clear_scratch(void)
Antonio de Angelis4743e672019-04-11 11:38:48 +0100132{
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100133 scratch.owner = 0;
Ken Liub671d682022-05-12 20:39:29 +0800134 (void)memset(scratch.buf, 0, scratch.alloc_index);
Summer Qin0a9e5372020-11-27 16:04:05 +0800135 scratch.alloc_index = 0;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100136}
137
Kevin Pengfe730cc2022-04-11 17:48:42 +0800138static void tfm_crypto_set_caller_id(int32_t id)
139{
140 /* Set the owner of the data in the scratch */
141 (void)tfm_crypto_set_scratch_owner(id);
142}
143
144psa_status_t tfm_crypto_get_caller_id(int32_t *id)
145{
146 return tfm_crypto_get_scratch_owner(id);
147}
148
149static psa_status_t tfm_crypto_init_iovecs(const psa_msg_t *msg,
150 psa_invec in_vec[],
151 size_t in_len,
152 psa_outvec out_vec[],
153 size_t out_len)
154{
155 uint32_t i;
156 void *alloc_buf_ptr = NULL;
157 psa_status_t status;
158
159 /* Alloc/read from the second element as the first is read when parsing */
160 for (i = 1; i < in_len; i++) {
161 /* Allocate necessary space in the internal scratch */
162 status = tfm_crypto_alloc_scratch(msg->in_size[i], &alloc_buf_ptr);
163 if (status != PSA_SUCCESS) {
164 tfm_crypto_clear_scratch();
165 return status;
166 }
167 /* Read from the IPC framework inputs into the scratch */
168 in_vec[i].len =
169 psa_read(msg->handle, i, alloc_buf_ptr, msg->in_size[i]);
170 /* Populate the fields of the input to the secure function */
171 in_vec[i].base = alloc_buf_ptr;
172 }
173
174 for (i = 0; i < out_len; i++) {
175 /* Allocate necessary space for the output in the internal scratch */
176 status = tfm_crypto_alloc_scratch(msg->out_size[i], &alloc_buf_ptr);
177 if (status != PSA_SUCCESS) {
178 tfm_crypto_clear_scratch();
179 return status;
180 }
181 /* Populate the fields of the output to the secure function */
182 out_vec[i].base = alloc_buf_ptr;
183 out_vec[i].len = msg->out_size[i];
184 }
185
186 return PSA_SUCCESS;
187}
188#endif /* PSA_FRAMEWORK_HAS_MM_IOVEC == 1 */
189
Kevin Peng2d4bc2e2022-01-28 16:19:30 +0800190static psa_status_t tfm_crypto_call_srv(const psa_msg_t *msg)
Antonio de Angelis4743e672019-04-11 11:38:48 +0100191{
192 psa_status_t status = PSA_SUCCESS;
TTornblomfaf74f52020-03-04 17:56:27 +0100193 size_t in_len = PSA_MAX_IOVEC, out_len = PSA_MAX_IOVEC, i;
Soby Mathewd8abdfd2020-10-14 10:28:01 +0100194 psa_invec in_vec[PSA_MAX_IOVEC] = { {NULL, 0} };
195 psa_outvec out_vec[PSA_MAX_IOVEC] = { {NULL, 0} };
Kevin Peng2d4bc2e2022-01-28 16:19:30 +0800196 struct tfm_crypto_pack_iovec iov = {0};
Antonio de Angelis4743e672019-04-11 11:38:48 +0100197
198 /* Check the number of in_vec filled */
Jamie Fox9a234e22019-04-30 11:12:05 +0100199 while ((in_len > 0) && (msg->in_size[in_len - 1] == 0)) {
200 in_len--;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100201 }
202
Kevin Pengfe730cc2022-04-11 17:48:42 +0800203 /* Check the number of out_vec filled */
204 while ((out_len > 0) && (msg->out_size[out_len - 1] == 0)) {
205 out_len--;
206 }
207
Antonio de Angelis4743e672019-04-11 11:38:48 +0100208 /* There will always be a tfm_crypto_pack_iovec in the first iovec */
209 if (in_len < 1) {
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100210 return PSA_ERROR_GENERIC_ERROR;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100211 }
Kevin Peng2d4bc2e2022-01-28 16:19:30 +0800212
213 if (psa_read(msg->handle, 0, &iov, sizeof(iov)) != sizeof(iov)) {
214 return PSA_ERROR_GENERIC_ERROR;
215 }
216
Antonio de Angelis4743e672019-04-11 11:38:48 +0100217 /* Initialise the first iovec with the IOV read when parsing */
Kevin Peng2d4bc2e2022-01-28 16:19:30 +0800218 in_vec[0].base = &iov;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100219 in_vec[0].len = sizeof(struct tfm_crypto_pack_iovec);
220
Kevin Pengfe730cc2022-04-11 17:48:42 +0800221 status = tfm_crypto_init_iovecs(msg, in_vec, in_len, out_vec, out_len);
222 if (status != PSA_SUCCESS) {
223 return status;
Antonio de Angelis4743e672019-04-11 11:38:48 +0100224 }
225
Kevin Pengfe730cc2022-04-11 17:48:42 +0800226 tfm_crypto_set_caller_id(msg->client_id);
Antonio de Angelis60a6fe62019-06-18 15:27:34 +0100227
Antonio de Angelis202425a2022-04-06 11:13:15 +0100228 /* Call the dispatcher to the functions that implement the PSA Crypto API */
229 status = tfm_crypto_api_dispatcher(in_vec, in_len, out_vec, out_len);
Antonio de Angelis4743e672019-04-11 11:38:48 +0100230
Kevin Pengfe730cc2022-04-11 17:48:42 +0800231#if PSA_FRAMEWORK_HAS_MM_IOVEC == 1
232 for (i = 0; i < out_len; i++) {
233 if (out_vec[i].base != NULL) {
234 psa_unmap_outvec(msg->handle, i, out_vec[i].len);
235 }
236 }
237#else
Antonio de Angelis4743e672019-04-11 11:38:48 +0100238 /* Write into the IPC framework outputs from the scratch */
239 for (i = 0; i < out_len; i++) {
240 psa_write(msg->handle, i, out_vec[i].base, out_vec[i].len);
241 }
242
243 /* Clear the allocated internal scratch before returning */
Summer Qin0a9e5372020-11-27 16:04:05 +0800244 tfm_crypto_clear_scratch();
Kevin Pengfe730cc2022-04-11 17:48:42 +0800245#endif
Antonio de Angelis4743e672019-04-11 11:38:48 +0100246
247 return status;
248}
Antonio de Angelis4743e672019-04-11 11:38:48 +0100249
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100250static psa_status_t tfm_crypto_engine_init(void)
251{
Antonio de Angelisedbafb62022-12-01 13:52:15 +0000252 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Antonio de Angelis8f7db7d2022-12-01 12:09:16 +0000253 char *library_info = NULL;
Antonio de Angelisedbafb62022-12-01 13:52:15 +0000254
Xinyu Zhangd755b822022-10-25 11:18:09 +0800255#if CRYPTO_NV_SEED
Antonio de Angelisf98fa332024-04-18 11:54:13 +0100256 LOG_INFFMT("[INF][Crypto] Provision entropy seed...\r\n");
Raef Coles618fc152021-06-18 09:26:46 +0100257 if (tfm_plat_crypto_provision_entropy_seed() != TFM_CRYPTO_NV_SEED_SUCCESS) {
Summer Qina5448d62020-12-07 14:03:37 +0800258 return PSA_ERROR_GENERIC_ERROR;
259 }
Antonio de Angelisf98fa332024-04-18 11:54:13 +0100260 LOG_INFFMT("[INF][Crypto] Provision entropy seed... \033[0;32mcomplete\033[0m.\r\n");
Raef Coles618fc152021-06-18 09:26:46 +0100261#endif /* CRYPTO_NV_SEED */
Summer Qina5448d62020-12-07 14:03:37 +0800262
Antonio de Angelisedbafb62022-12-01 13:52:15 +0000263 /* Initialise the underlying Cryptographic library that provides the
264 * PSA Crypto core layer
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100265 */
Antonio de Angelisedbafb62022-12-01 13:52:15 +0000266 library_info = tfm_crypto_library_get_info();
Antonio de Angelisf98fa332024-04-18 11:54:13 +0100267 LOG_DBGFMT("[DBG][Crypto] Init \033[0;32m%s\033[0m...\r\n", library_info);
Antonio de Angelisedbafb62022-12-01 13:52:15 +0000268 status = tfm_crypto_core_library_init();
269 if (status != PSA_SUCCESS) {
270 return status;
271 }
Antonio de Angelisf98fa332024-04-18 11:54:13 +0100272 LOG_DBGFMT("[DBG][Crypto] Init \033[0;32m%s\033[0m... \033[0;32mcomplete\033[0m.\r\n", library_info);
Sherry Zhange1524982022-06-08 16:57:59 +0800273
Antonio de Angelis695d75b2022-08-22 15:06:24 +0100274 /* Initialise the crypto accelerator if one is enabled. If the driver API is
275 * the one defined by the PSA Unified Driver interface, the initialisation is
276 * performed directly through psa_crypto_init() while the PSA subsystem is
277 * initialised
278 */
Michel Jaouene991ffb2023-02-20 12:08:43 +0100279#if defined(CRYPTO_HW_ACCELERATOR) && defined(LEGACY_DRIVER_API_ENABLED)
Antonio de Angelisf98fa332024-04-18 11:54:13 +0100280 LOG_INFFMT("[INF][Crypto] Init HW accelerator...\r\n");
Raef Colesd2485af2019-10-30 10:15:33 +0000281 if (crypto_hw_accelerator_init() != 0) {
282 return PSA_ERROR_HARDWARE_FAILURE;
283 }
Antonio de Angelisf98fa332024-04-18 11:54:13 +0100284 LOG_INFFMT("[INF][Crypto] Init HW accelerator... \033[0;32mcomplete\033[0m.\r\n");
Raef Colesd2485af2019-10-30 10:15:33 +0000285#endif /* CRYPTO_HW_ACCELERATOR */
286
Antonio de Angelisedbafb62022-12-01 13:52:15 +0000287 /* Perform the initialisation of the PSA subsystem available through the chosen
288 * Cryptographic library. If a driver is built using the PSA Driver interface,
289 * the function below will perform also the same operations done by the HAL init
290 * crypto_hw_accelerator_init()
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100291 */
292 return psa_crypto_init();
293}
294
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000295static psa_status_t tfm_crypto_module_init(void)
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100296{
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100297 /* Init the Alloc module */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000298 return tfm_crypto_init_alloc();
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100299}
Antonio de Angelis8908f472018-08-31 15:44:25 +0100300
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000301psa_status_t tfm_crypto_init(void)
Antonio de Angelis8908f472018-08-31 15:44:25 +0100302{
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100303 psa_status_t status;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100304
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100305 /* Initialise other modules of the service */
Antonio de Angelisab85ccd2019-03-25 15:14:29 +0000306 status = tfm_crypto_module_init();
307 if (status != PSA_SUCCESS) {
308 return status;
Antonio de Angeliscf85ba22018-10-09 13:29:40 +0100309 }
310
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100311 /* Initialise the engine layer */
Raef Coles79809c72022-03-02 13:48:20 +0000312 status = tfm_crypto_engine_init();
313 if (status != PSA_SUCCESS) {
314 return status;
315 }
316
Raef Coles79809c72022-03-02 13:48:20 +0000317 return PSA_SUCCESS;
Kevin Peng2d4bc2e2022-01-28 16:19:30 +0800318}
Antonio de Angelis8908f472018-08-31 15:44:25 +0100319
Kevin Peng2d4bc2e2022-01-28 16:19:30 +0800320psa_status_t tfm_crypto_sfn(const psa_msg_t *msg)
321{
322 /* Process the message type */
323 switch (msg->type) {
324 case PSA_IPC_CALL:
325 return tfm_crypto_call_srv(msg);
326 default:
327 return PSA_ERROR_NOT_SUPPORTED;
328 }
Antonio de Angelis4743e672019-04-11 11:38:48 +0100329
Kevin Peng2d4bc2e2022-01-28 16:19:30 +0800330 return PSA_ERROR_GENERIC_ERROR;
Antonio de Angelis8908f472018-08-31 15:44:25 +0100331}
Antonio de Angelis202425a2022-04-06 11:13:15 +0100332
333psa_status_t tfm_crypto_api_dispatcher(psa_invec in_vec[],
334 size_t in_len,
335 psa_outvec out_vec[],
336 size_t out_len)
337{
338 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
339 const struct tfm_crypto_pack_iovec *iov = in_vec[0].base;
340 int32_t caller_id = 0;
Antonio de Angelis7557e682022-11-30 15:37:51 +0000341 struct tfm_crypto_key_id_s encoded_key = TFM_CRYPTO_KEY_ID_S_INIT;
Antonio de Angelis202425a2022-04-06 11:13:15 +0100342 bool is_key_required = false;
David Hu233f6a52023-09-29 08:59:27 +0800343 enum tfm_crypto_group_id_t group_id;
Antonio de Angelis202425a2022-04-06 11:13:15 +0100344
345 if (in_vec[0].len != sizeof(struct tfm_crypto_pack_iovec)) {
346 return PSA_ERROR_PROGRAMMER_ERROR;
347 }
348
David Huc9679cc2022-06-21 13:09:34 +0800349 group_id = TFM_CRYPTO_GET_GROUP_ID(iov->function_id);
350
351 is_key_required = !((group_id == TFM_CRYPTO_GROUP_ID_HASH) ||
352 (group_id == TFM_CRYPTO_GROUP_ID_RANDOM));
Antonio de Angelis202425a2022-04-06 11:13:15 +0100353
354 if (is_key_required) {
355 status = tfm_crypto_get_caller_id(&caller_id);
356 if (status != PSA_SUCCESS) {
357 return status;
358 }
359 /* The caller_id being set in the owner field is the partition ID
360 * of the calling partition
361 */
Antonio de Angelis7557e682022-11-30 15:37:51 +0000362 encoded_key.key_id = iov->key_id;
363 encoded_key.owner = caller_id;
Antonio de Angelis202425a2022-04-06 11:13:15 +0100364 }
365
366 /* Dispatch to each sub-module based on the Group ID */
David Huc9679cc2022-06-21 13:09:34 +0800367 switch (group_id) {
368 case TFM_CRYPTO_GROUP_ID_KEY_MANAGEMENT:
369 return tfm_crypto_key_management_interface(in_vec, out_vec,
370 &encoded_key);
371 case TFM_CRYPTO_GROUP_ID_HASH:
372 return tfm_crypto_hash_interface(in_vec, out_vec);
373 case TFM_CRYPTO_GROUP_ID_MAC:
374 return tfm_crypto_mac_interface(in_vec, out_vec, &encoded_key);
375 case TFM_CRYPTO_GROUP_ID_CIPHER:
376 return tfm_crypto_cipher_interface(in_vec, out_vec, &encoded_key);
377 case TFM_CRYPTO_GROUP_ID_AEAD:
378 return tfm_crypto_aead_interface(in_vec, out_vec, &encoded_key);
379 case TFM_CRYPTO_GROUP_ID_ASYM_SIGN:
380 return tfm_crypto_asymmetric_sign_interface(in_vec, out_vec,
381 &encoded_key);
382 case TFM_CRYPTO_GROUP_ID_ASYM_ENCRYPT:
383 return tfm_crypto_asymmetric_encrypt_interface(in_vec, out_vec,
384 &encoded_key);
385 case TFM_CRYPTO_GROUP_ID_KEY_DERIVATION:
386 return tfm_crypto_key_derivation_interface(in_vec, out_vec,
387 &encoded_key);
388 case TFM_CRYPTO_GROUP_ID_RANDOM:
389 return tfm_crypto_random_interface(in_vec, out_vec);
390 default:
Antonio de Angelis202425a2022-04-06 11:13:15 +0100391 LOG_ERRFMT("[ERR][Crypto] Unsupported request!\r\n");
David Huc9679cc2022-06-21 13:09:34 +0800392 return PSA_ERROR_NOT_SUPPORTED;
Antonio de Angelis202425a2022-04-06 11:13:15 +0100393 }
394
David Huc9679cc2022-06-21 13:09:34 +0800395 return PSA_ERROR_NOT_SUPPORTED;
Antonio de Angelis202425a2022-04-06 11:13:15 +0100396}