blob: cb4b6f05af7e8348c02d82065610375c05d1e9cf [file] [log] [blame]
Gabor Toth172659d2023-04-27 08:51:21 +02001/*
2 * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <protocols/rpc/common/packed-c/encoding.h>
8#include <service/crypto/client/psa/psa_crypto_client.h>
9#include <service_locator.h>
10#include <stdio.h>
11
Gabor Tothee2e7cb2024-10-07 17:02:56 +020012#include "libpsats.h"
Gabor Toth172659d2023-04-27 08:51:21 +020013#include "trace.h"
14
15static struct rpc_caller_session *rpc_session;
16static struct service_context *crypto_service_context;
17
Gabor Tothee2e7cb2024-10-07 17:02:56 +020018LIBPSATS_EXPORTED psa_status_t libpsats_init_crypto_context(const char *service_name)
Gabor Toth172659d2023-04-27 08:51:21 +020019{
20 psa_status_t result = PSA_ERROR_GENERIC_ERROR;
21
22 if (rpc_session || crypto_service_context) {
23 EMSG("The client is already initialized\n");
24 return result;
25 }
26
27 service_locator_init();
28
29 crypto_service_context = service_locator_query(service_name);
30
31 if (!crypto_service_context) {
32 EMSG("Failed to discover service\n");
33 return result;
34 }
35
36 rpc_session = service_context_open(crypto_service_context);
37
38 if (!rpc_session) {
39 EMSG("Failed to open rpc session\n");
Gabor Tothee2e7cb2024-10-07 17:02:56 +020040 libpsats_deinit_crypto_context();
Gabor Toth172659d2023-04-27 08:51:21 +020041 return result;
42 }
43
44 result = psa_crypto_client_init(rpc_session);
45
46 if (result)
47 EMSG("psa_crypto_client_init failed\n");
48
49 return result;
50}
51
Gabor Tothee2e7cb2024-10-07 17:02:56 +020052LIBPSATS_EXPORTED void libpsats_deinit_crypto_context(void)
Gabor Toth172659d2023-04-27 08:51:21 +020053{
54 psa_crypto_client_deinit();
55
56 if (crypto_service_context && rpc_session) {
57 service_context_close(crypto_service_context, rpc_session);
58 rpc_session = NULL;
59 }
60
61 if (crypto_service_context) {
62 service_context_relinquish(crypto_service_context);
63 crypto_service_context = NULL;
64 }
65}