Enable libpsa in psa-api-tests

Refactor psa-api-test deployments to use libpsa.

Signed-off-by: Gabor Toth <gabor.toth2@arm.com>
Change-Id: If7f8fdbd48ce082a810983bf0696c791db185a9c
diff --git a/deployments/psa-api-test/initial_attestation/arm-linux/CMakeLists.txt b/deployments/psa-api-test/initial_attestation/arm-linux/CMakeLists.txt
index da8e185..fa75297 100644
--- a/deployments/psa-api-test/initial_attestation/arm-linux/CMakeLists.txt
+++ b/deployments/psa-api-test/initial_attestation/arm-linux/CMakeLists.txt
@@ -18,6 +18,14 @@
 add_executable(${PROJECT_NAME})
 target_include_directories(${PROJECT_NAME} PRIVATE "${TOP_LEVEL_INCLUDE_DIRS}")
 
+set(TRACE_PREFIX "PSAIAT" CACHE STRING "Trace prefix")
+add_components(TARGET ${PROJECT_NAME}
+	BASE_DIR ${TS_ROOT}
+	COMPONENTS
+		"components/common/trace"
+		"environments/arm-linux"
+)
+
 #-------------------------------------------------------------------------------
 #  Extend with components that are common across all deployments of
 #  psa-api-test/initial_attestation
diff --git a/deployments/psa-api-test/initial_attestation/iat-api-test.cmake b/deployments/psa-api-test/initial_attestation/iat-api-test.cmake
index 4d1d2b1..807faf6 100644
--- a/deployments/psa-api-test/initial_attestation/iat-api-test.cmake
+++ b/deployments/psa-api-test/initial_attestation/iat-api-test.cmake
@@ -28,30 +28,18 @@
 #  Attestation specific components.
 #
 #-------------------------------------------------------------------------------
-add_components(
-	TARGET "${PROJECT_NAME}"
+target_sources(${PROJECT_NAME} PRIVATE
+	${TS_ROOT}/deployments/psa-api-test/initial_attestation/iat.c
+)
+
+add_components(TARGET ${PROJECT_NAME}
 	BASE_DIR ${TS_ROOT}
 	COMPONENTS
+		"components/service/common/include"
 		"components/service/attestation/include"
-		"components/service/attestation/client/psa"
-		"components/service/attestation/client/provision"
+		"components/service/crypto/include"
 )
 
-target_sources(${PROJECT_NAME} PRIVATE
-	${TS_ROOT}/deployments/psa-api-test/initial_attestation/iat_locator.c
-)
-
-#-------------------------------------------------------------------------------
-#  Add external components used specifically for attestation tests
-#
-#-------------------------------------------------------------------------------
-
-# MbedTLS used for token verification
-set(MBEDTLS_USER_CONFIG_FILE "${TS_ROOT}/external/MbedTLS/config/crypto_posix.h"
-	CACHE STRING "Configuration file for mbedcrypto")
-include(${TS_ROOT}/external/MbedTLS/MbedTLS.cmake)
-target_link_libraries(${PROJECT_NAME} PRIVATE MbedTLS::mbedcrypto)
-
 # Use Mbed TLS to provide the psa crypto api interface files
 set(PSA_CRYPTO_API_INCLUDE ${MBEDTLS_PUBLIC_INCLUDE_PATH})
 
diff --git a/deployments/psa-api-test/initial_attestation/iat.c b/deployments/psa-api-test/initial_attestation/iat.c
new file mode 100644
index 0000000..eb03dc7
--- /dev/null
+++ b/deployments/psa-api-test/initial_attestation/iat.c
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include "../psa_api_test_common.h"
+#include "libpsa.h"
+#include "trace.h"
+
+psa_status_t test_setup(const char *service_name_crypto, const char *service_name_iat,
+			const char *service_name_ps, const char *service_name_its)
+{
+	psa_status_t psa_status = PSA_ERROR_GENERIC_ERROR;
+
+	if (!service_name_crypto)
+		service_name_crypto = "sn:trustedfirmware.org:crypto:0";
+
+	if (!service_name_iat)
+		service_name_iat = "sn:trustedfirmware.org:attestation:0";
+
+	psa_status = libpsa_init_crypto_context(service_name_crypto);
+	if (psa_status) {
+		EMSG("libpsa_init_crypto_context failed: %d\n", psa_status);
+		return psa_status;
+	}
+
+	psa_status = libpsa_init_attestation_context(service_name_iat);
+	if (psa_status) {
+		EMSG("libpsa_init_attestation_context failed: %d\n", psa_status);
+		return psa_status;
+	}
+
+	psa_status = psa_crypto_init();
+	if (psa_status) {
+		EMSG("psa_crypto_init failed: %d\n", psa_status);
+		return psa_status;
+	}
+
+	return PSA_SUCCESS;
+}
+
+void test_teardown(void)
+{
+	libpsa_deinit_crypto_context();
+	libpsa_deinit_attestation_context();
+}
diff --git a/deployments/psa-api-test/initial_attestation/iat_locator.c b/deployments/psa-api-test/initial_attestation/iat_locator.c
deleted file mode 100644
index 1852fea..0000000
--- a/deployments/psa-api-test/initial_attestation/iat_locator.c
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- */
-
-#include <stddef.h>
-#include <psa/crypto.h>
-#include <service_locator.h>
-#include <service/attestation/client/psa/iat_client.h>
-#include <service/attestation/client/provision/attest_provision_client.h>
-#include <protocols/rpc/common/packed-c/encoding.h>
-#include "../service_under_test.h"
-
-/* RPC context */
-static struct rpc_caller_session *session = NULL;
-static struct service_context *attestation_service_context = NULL;
-
-int locate_service_under_test(void)
-{
-	int status = -1;
-
-	/* Attestation tests depend on PSA crypto so ensure library is initialised */
-	psa_status_t psa_status = psa_crypto_init();
-
-	if ((psa_status == PSA_SUCCESS) && !session && !attestation_service_context) {
-
-		service_locator_init();
-
-		attestation_service_context =
-			service_locator_query("sn:trustedfirmware.org:attestation:0");
-
-		if (attestation_service_context) {
-
-			session = service_context_open(attestation_service_context);
-
-			if (session) {
-
-				psa_iat_client_init(session);
-				attest_provision_client_init(session);
-
-				status = 0;
-			}
-			else {
-
-				status = -1;
-				relinquish_service_under_test();
-			}
-		}
-	}
-
-	return status;
-}
-
-int relinquish_service_under_test(void)
-{
-	psa_iat_client_deinit();
-	attest_provision_client_deinit();
-
-	if (attestation_service_context && session) {
-
-		service_context_close(attestation_service_context, session);
-		session = NULL;
-	}
-
-	if (attestation_service_context) {
-
-		service_context_relinquish(attestation_service_context);
-		attestation_service_context = NULL;
-	}
-
-	return 0;
-}
diff --git a/deployments/psa-api-test/initial_attestation/linux-pc/CMakeLists.txt b/deployments/psa-api-test/initial_attestation/linux-pc/CMakeLists.txt
index b7e7a76..7c13eef 100644
--- a/deployments/psa-api-test/initial_attestation/linux-pc/CMakeLists.txt
+++ b/deployments/psa-api-test/initial_attestation/linux-pc/CMakeLists.txt
@@ -19,6 +19,12 @@
 add_executable(${PROJECT_NAME})
 target_include_directories(${PROJECT_NAME} PRIVATE "${TOP_LEVEL_INCLUDE_DIRS}")
 
+set(TRACE_PREFIX "PSAIAT" CACHE STRING "Trace prefix")
+add_components(TARGET ${PROJECT_NAME}
+	BASE_DIR ${TS_ROOT}
+	COMPONENTS "environments/linux-pc"
+)
+
 #-------------------------------------------------------------------------------
 #  Extend with components that are common across all deployments of
 #  psa-api-test/initial_attestation