Rename libpsa to libpsats
Instead of using a generic name for the library, give it a TS specific
name to avoid possible naming collisions with other psa libraries in
the future.
Change-Id: Icea9be4d836f7d22300b20c8d6a5f8bd8fae1133
Signed-off-by: Gabor Toth <gabor.toth2@arm.com>
diff --git a/components/common/libpsats/component.cmake b/components/common/libpsats/component.cmake
new file mode 100644
index 0000000..e1ea32d
--- /dev/null
+++ b/components/common/libpsats/component.cmake
@@ -0,0 +1,16 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+if (NOT DEFINED TGT)
+ message(FATAL_ERROR "mandatory parameter TGT is not defined.")
+endif()
+
+target_sources(${TGT} PRIVATE
+ "${CMAKE_CURRENT_LIST_DIR}/libpsats-attestation.c"
+ "${CMAKE_CURRENT_LIST_DIR}/libpsats-crypto.c"
+ "${CMAKE_CURRENT_LIST_DIR}/libpsats-its.c"
+ "${CMAKE_CURRENT_LIST_DIR}/libpsats-ps.c"
+ )
diff --git a/components/common/libpsats/libpsats-attestation.c b/components/common/libpsats/libpsats-attestation.c
new file mode 100644
index 0000000..c67422b
--- /dev/null
+++ b/components/common/libpsats/libpsats-attestation.c
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <protocols/rpc/common/packed-c/encoding.h>
+#include <psa/initial_attestation.h>
+#include <service/attestation/client/provision/attest_provision_client.h>
+#include <service/attestation/client/psa/iat_client.h>
+#include <service_locator.h>
+#include <stdio.h>
+
+#include "libpsats.h"
+#include "trace.h"
+
+static struct rpc_caller_session *rpc_session;
+static struct service_context *attestation_service_context;
+
+LIBPSATS_EXPORTED psa_status_t libpsats_init_attestation_context(const char *service_name)
+{
+ psa_status_t result = PSA_ERROR_GENERIC_ERROR;
+ psa_status_t provision_result = PSA_ERROR_GENERIC_ERROR;
+
+ if (rpc_session || attestation_service_context) {
+ EMSG("The client is already initialized\n");
+ return result;
+ }
+
+ service_locator_init();
+
+ attestation_service_context = service_locator_query(service_name);
+
+ if (!attestation_service_context) {
+ EMSG("Failed to discover service\n");
+ return result;
+ }
+
+ rpc_session = service_context_open(attestation_service_context);
+
+ if (!rpc_session) {
+ EMSG("Failed to open rpc session\n");
+ libpsats_deinit_attestation_context();
+ return result;
+ }
+
+ result = psa_iat_client_init(rpc_session);
+
+ if (result) {
+ EMSG("psa_iat_client_init failed\n");
+ return result;
+ }
+
+ provision_result = attest_provision_client_init(rpc_session);
+
+ /* If external IAK is used this call can fail */
+ if (provision_result)
+ EMSG(
+ "attest_provision_client_init failed. Are you using external IAK key?\n");
+
+ return result;
+}
+
+LIBPSATS_EXPORTED void libpsats_deinit_attestation_context(void)
+{
+ psa_iat_client_deinit();
+ attest_provision_client_deinit();
+
+ if (attestation_service_context && rpc_session) {
+ service_context_close(attestation_service_context, rpc_session);
+ rpc_session = NULL;
+ }
+
+ if (attestation_service_context) {
+ service_context_relinquish(attestation_service_context);
+ attestation_service_context = NULL;
+ }
+}
diff --git a/components/common/libpsats/libpsats-crypto.c b/components/common/libpsats/libpsats-crypto.c
new file mode 100644
index 0000000..cb4b6f0
--- /dev/null
+++ b/components/common/libpsats/libpsats-crypto.c
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <protocols/rpc/common/packed-c/encoding.h>
+#include <service/crypto/client/psa/psa_crypto_client.h>
+#include <service_locator.h>
+#include <stdio.h>
+
+#include "libpsats.h"
+#include "trace.h"
+
+static struct rpc_caller_session *rpc_session;
+static struct service_context *crypto_service_context;
+
+LIBPSATS_EXPORTED psa_status_t libpsats_init_crypto_context(const char *service_name)
+{
+ psa_status_t result = PSA_ERROR_GENERIC_ERROR;
+
+ if (rpc_session || crypto_service_context) {
+ EMSG("The client is already initialized\n");
+ return result;
+ }
+
+ service_locator_init();
+
+ crypto_service_context = service_locator_query(service_name);
+
+ if (!crypto_service_context) {
+ EMSG("Failed to discover service\n");
+ return result;
+ }
+
+ rpc_session = service_context_open(crypto_service_context);
+
+ if (!rpc_session) {
+ EMSG("Failed to open rpc session\n");
+ libpsats_deinit_crypto_context();
+ return result;
+ }
+
+ result = psa_crypto_client_init(rpc_session);
+
+ if (result)
+ EMSG("psa_crypto_client_init failed\n");
+
+ return result;
+}
+
+LIBPSATS_EXPORTED void libpsats_deinit_crypto_context(void)
+{
+ psa_crypto_client_deinit();
+
+ if (crypto_service_context && rpc_session) {
+ service_context_close(crypto_service_context, rpc_session);
+ rpc_session = NULL;
+ }
+
+ if (crypto_service_context) {
+ service_context_relinquish(crypto_service_context);
+ crypto_service_context = NULL;
+ }
+}
diff --git a/components/common/libpsats/libpsats-its.c b/components/common/libpsats/libpsats-its.c
new file mode 100644
index 0000000..db98c1d
--- /dev/null
+++ b/components/common/libpsats/libpsats-its.c
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <protocols/rpc/common/packed-c/encoding.h>
+#include <service/secure_storage/backend/secure_storage_client/secure_storage_client.h>
+#include <service/secure_storage/frontend/psa/its/its_frontend.h>
+#include <service_locator.h>
+#include <stdio.h>
+
+#include "libpsats.h"
+#include "trace.h"
+
+static struct rpc_caller_session *rpc_session;
+static struct service_context *its_service_context;
+static struct secure_storage_client its_storage_client;
+
+LIBPSATS_EXPORTED psa_status_t libpsats_init_its_context(const char *service_name)
+{
+ psa_status_t result = PSA_ERROR_GENERIC_ERROR;
+
+ if (rpc_session || its_service_context) {
+ EMSG("The client is already initialized\n");
+ return result;
+ }
+
+ service_locator_init();
+
+ its_service_context = service_locator_query(service_name);
+
+ if (!its_service_context) {
+ EMSG("Failed to discover service\n");
+ return result;
+ }
+
+ rpc_session = service_context_open(its_service_context);
+
+ if (!rpc_session) {
+ EMSG("Failed to open rpc session\n");
+ libpsats_deinit_its_context();
+ return result;
+ }
+
+ struct storage_backend *its_storage_backend =
+ secure_storage_client_init(&its_storage_client, rpc_session);
+
+ if (!its_storage_backend) {
+ EMSG("Failed to initialize storage backend\n");
+ libpsats_deinit_its_context();
+ return result;
+ }
+
+ result = psa_its_frontend_init(its_storage_backend);
+
+ return result;
+}
+
+LIBPSATS_EXPORTED void libpsats_deinit_its_context(void)
+{
+ psa_its_frontend_init(NULL);
+ secure_storage_client_deinit(&its_storage_client);
+
+ if (its_service_context && rpc_session) {
+ service_context_close(its_service_context, rpc_session);
+ rpc_session = NULL;
+ }
+
+ if (its_service_context) {
+ service_context_relinquish(its_service_context);
+ its_service_context = NULL;
+ }
+}
diff --git a/components/common/libpsats/libpsats-ps.c b/components/common/libpsats/libpsats-ps.c
new file mode 100644
index 0000000..a776460
--- /dev/null
+++ b/components/common/libpsats/libpsats-ps.c
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <protocols/rpc/common/packed-c/encoding.h>
+#include <service/secure_storage/backend/secure_storage_client/secure_storage_client.h>
+#include <service/secure_storage/frontend/psa/ps/ps_frontend.h>
+#include <service_locator.h>
+#include <stdio.h>
+
+#include "libpsats.h"
+#include "trace.h"
+
+static struct rpc_caller_session *rpc_session;
+static struct service_context *ps_service_context;
+static struct secure_storage_client ps_storage_client;
+
+LIBPSATS_EXPORTED psa_status_t libpsats_init_ps_context(const char *service_name)
+{
+ psa_status_t result = PSA_ERROR_GENERIC_ERROR;
+
+ if (rpc_session || ps_service_context) {
+ EMSG("The client is already initialized\n");
+ return result;
+ }
+
+ service_locator_init();
+
+ ps_service_context = service_locator_query(service_name);
+
+ if (!ps_service_context) {
+ EMSG("Failed to discover service\n");
+ return result;
+ }
+
+ rpc_session = service_context_open(ps_service_context);
+
+ if (!rpc_session) {
+ EMSG("Failed to open rpc session\n");
+ libpsats_deinit_ps_context();
+ return result;
+ }
+
+ struct storage_backend *ps_storage_backend =
+ secure_storage_client_init(&ps_storage_client, rpc_session);
+
+ if (!ps_storage_backend) {
+ EMSG("Failed to initialize storage backend\n");
+ libpsats_deinit_ps_context();
+ return result;
+ }
+
+ result = psa_ps_frontend_init(ps_storage_backend);
+
+ return result;
+}
+
+LIBPSATS_EXPORTED void libpsats_deinit_ps_context(void)
+{
+ psa_ps_frontend_init(NULL);
+ secure_storage_client_deinit(&ps_storage_client);
+
+ if (ps_service_context && rpc_session) {
+ service_context_close(ps_service_context, rpc_session);
+ rpc_session = NULL;
+ }
+
+ if (ps_service_context) {
+ service_context_relinquish(ps_service_context);
+ ps_service_context = NULL;
+ }
+}
diff --git a/components/common/libpsats/libpsats.h b/components/common/libpsats/libpsats.h
new file mode 100644
index 0000000..b4ca9ca
--- /dev/null
+++ b/components/common/libpsats/libpsats.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include "psa/crypto_types.h"
+#include "psa/initial_attestation.h"
+#include "psa/internal_trusted_storage.h"
+#include "psa/protected_storage.h"
+
+#ifndef LIBPSATS_H
+#define LIBPSATS_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * The functions may be exported as a public interface to
+ * a shared library.
+ */
+#ifdef EXPORT_PUBLIC_INTERFACE_LIBPSATS
+#define LIBPSATS_EXPORTED __attribute__((__visibility__("default")))
+#else
+#define LIBPSATS_EXPORTED
+#endif
+
+LIBPSATS_EXPORTED psa_status_t libpsats_init_crypto_context(const char *service_name);
+LIBPSATS_EXPORTED void libpsats_deinit_crypto_context(void);
+
+LIBPSATS_EXPORTED psa_status_t libpsats_init_attestation_context(const char *service_name);
+LIBPSATS_EXPORTED void libpsats_deinit_attestation_context(void);
+
+LIBPSATS_EXPORTED psa_status_t libpsats_init_its_context(const char *service_name);
+LIBPSATS_EXPORTED void libpsats_deinit_its_context(void);
+
+LIBPSATS_EXPORTED psa_status_t libpsats_init_ps_context(const char *service_name);
+LIBPSATS_EXPORTED void libpsats_deinit_ps_context(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* LIBPSATS_H */