1 /*
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 
12 #include "libpsats.h"
13 #include "trace.h"
14 
15 static struct rpc_caller_session *rpc_session;
16 static struct service_context *crypto_service_context;
17 
libpsats_init_crypto_context(const char * service_name)18 LIBPSATS_EXPORTED psa_status_t libpsats_init_crypto_context(const char *service_name)
19 {
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");
40 		libpsats_deinit_crypto_context();
41 		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 
libpsats_deinit_crypto_context(void)52 LIBPSATS_EXPORTED void libpsats_deinit_crypto_context(void)
53 {
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 }
66