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/arch_test_runner.c b/deployments/psa-api-test/arch_test_runner.c
index 8fd3dac..b5cd69c 100644
--- a/deployments/psa-api-test/arch_test_runner.c
+++ b/deployments/psa-api-test/arch_test_runner.c
@@ -5,163 +5,180 @@
  */
 
 #include <ctype.h>
-#include <stdint.h>
+#include <rpc_caller.h>
 #include <stdbool.h>
+#include <stdint.h>
 #include <stdio.h>
-#include <string.h>
 #include <stdlib.h>
-#include "service_under_test.h"
+#include <string.h>
 
-#define TEST_ID_OFFSET      (5)
+#include "psa_api_test_common.h"
+#include "trace.h"
+
+#define TEST_ID_OFFSET	    (5)
 #define TEST_POSTFIX_OFFSET (8)
 #define TEST_ENTRY_LENGTH   (9)
 
-
 int32_t val_entry(void);
-extern size_t val_get_test_list(uint32_t *test_id_list, size_t size);
-extern void pal_set_custom_test_list(char *custom_test_list);
+size_t val_get_test_list(uint32_t *test_id_list, size_t size);
+void pal_set_custom_test_list(char *custom_test_list);
 
 /* Returns whether option_switch is in the argv list and provide its index in the array */
 static bool option_selected(const char *option_switch, int argc, char *argv[], int *index)
 {
-    bool selected = false;
-    *index = 0;
+	bool selected = false;
+	*index = 0;
 
-    for (int i = 1; (i < argc) && !selected; ++i) {
+	for (int i = 1; (i < argc) && !selected; ++i) {
+		selected = (strcmp(argv[i], option_switch) == 0);
+		*index = i;
+	}
 
-        selected = (strcmp(argv[i], option_switch) == 0);
-        *index = i;
-    }
-
-    return selected;
+	return selected;
 }
 
 /* Print the supported command line arguments */
 static void print_help(void)
 {
-    printf("Supported command line arguments:\n\n");
-    printf("\t -l: Print list of tests.\n");
-    printf("\t -t <test_list>: Run only the listed tests (e.g: test_201;test_202;). test_list = ^(test_[0-9]{3};)+ \n");
-    printf("\t -v: Verbose mode.\n");
-    printf("\t -h: Print this help message.\n");
-    printf("\n");
+	EMSG("Supported command line arguments:\n");
+	EMSG("\t -l: Print list of tests.");
+	EMSG("\t -t <test_list>: Run only the listed tests (e.g: test_201;test_202;). test_list = ^(test_[0-9]{3};)+");
+	EMSG("\t -c: <service name>: Crypto service name");
+	EMSG("\t -a: <service name>: Initial attestation service name");
+	EMSG("\t -p: <service name>: Protected Storage service name");
+	EMSG("\t -i: <service name>: Internal Trusted Storage service name");
+	EMSG("\t -v: Verbose mode.");
+	EMSG("\t -h: Print this help message.\n");
 }
 
 /* Prints the list of selectable psa-api tests */
 static void print_psa_api_tests(void)
 {
-    /* Request the number of tests to find out the size of the area needed to store the test ID-s. */
-    size_t n_test = val_get_test_list(NULL, 0);
+	/*
+	 * Request the number of tests to find out the size
+	 * of the area needed to store the test ID-s.
+	 */
+	size_t n_test = val_get_test_list(NULL, 0);
 
-    uint32_t *test_id_list = (uint32_t *)calloc(n_test, sizeof(uint32_t));
+	uint32_t *test_id_list = (uint32_t *)calloc(n_test, sizeof(uint32_t));
 
-    if (test_id_list) {
-        n_test = val_get_test_list(test_id_list, n_test);
+	if (test_id_list) {
+		n_test = val_get_test_list(test_id_list, n_test);
 
-        printf("Available psa-api tests:\n");
-        for (int i = 0; i < n_test; i++) {
-                printf("\t test_%d;\n", test_id_list[i]);
-        }
+		EMSG("Available psa-api tests:");
+		for (int i = 0; i < n_test; i++)
+			EMSG("\t test_%d;", test_id_list[i]);
 
-        free(test_id_list);
-    }
-    else {
-        printf("Could not allocate enough memory to store the list of tests\n");
-    }
+		free(test_id_list);
+	} else {
+		EMSG("Could not allocate enough memory to store the list of tests");
+	}
 }
 
 /* Check if the received test list string is formatted as expected */
-static bool is_test_list_wrong(char* test_list)
+static bool is_test_list_wrong(char *test_list)
 {
-    size_t len = strlen(test_list);
+	size_t len = strlen(test_list);
 
-    for (unsigned i = 0; i < len; i += TEST_ENTRY_LENGTH) {
+	for (unsigned int i = 0; i < len; i += TEST_ENTRY_LENGTH) {
+		/* Report error when the test entry is not properly finished */
+		if (i + TEST_ENTRY_LENGTH > len) {
+			EMSG("Expecting \"test_xxx;\" test entry at the %dth character, got \"%s\" instead.",
+			     i, &test_list[i]);
+			return true;
+		}
 
-        /* Report error when the test entry is not properly finished */
-        if (i + TEST_ENTRY_LENGTH > len) {
-            printf("Expecting \"test_xxx;\" test entry at the %dth character, got \"%s\" instead.\n", i, &test_list[i]);
-            return true;
-        }
+		/* Report error at incorrect test entry prefix */
+		if (memcmp(&test_list[i], "test_", TEST_ID_OFFSET)) {
+			EMSG("Expecting \"test_\" at the %dth character, got \"%.5s\" instead.", i,
+			     &test_list[i]);
+			return true;
+		}
 
-        /* Report error at incorrect test entry prefix */
-        if (memcmp(&test_list[i], "test_", TEST_ID_OFFSET)) {
-            printf("Expecting \"test_\" at the %dth character, got \"%.5s\" instead.\n", i, &test_list[i]);
-            return true;
-        }
+		/* Report error if the test ID is incorrect */
+		if (!(isdigit(test_list[i + TEST_ID_OFFSET]) &&
+		      isdigit(test_list[i + TEST_ID_OFFSET + 1]) &&
+		      isdigit(test_list[i + TEST_ID_OFFSET + 2]))) {
+			EMSG("Expecting three digits at %dth character, got \"%.3s\" instead.",
+			     i + TEST_ID_OFFSET, &test_list[i + TEST_ID_OFFSET]);
+			return true;
+		}
 
-        /* Report error if the test ID is incorrect */
-        if (!(isdigit(test_list[i + TEST_ID_OFFSET]) &&
-              isdigit(test_list[i + TEST_ID_OFFSET + 1]) &&
-              isdigit(test_list[i + TEST_ID_OFFSET + 2]))) {
-            printf("Expecting three digits at the %dth character, got \"%.3s\" instead.\n",
-                i + TEST_ID_OFFSET,
-                &test_list[i + TEST_ID_OFFSET]);
-            return true;
-        }
+		/* Report error at incorrect test entry postfix */
+		if (test_list[i + TEST_POSTFIX_OFFSET] != ';') {
+			EMSG("Expecting ; at the %dth character, got \"%.1s\" instead.",
+			     i + TEST_POSTFIX_OFFSET, &test_list[i + TEST_POSTFIX_OFFSET]);
+			return true;
+		}
+	}
 
-        /* Report error at incorrect test entry postfix */
-        if (test_list[i + TEST_POSTFIX_OFFSET] != ';') {
-            printf("Expecting ; at the %dth character, got \"%.1s\" instead.\n",
-                i + TEST_POSTFIX_OFFSET,
-                &test_list[i + TEST_POSTFIX_OFFSET]);
-            return true;
-        }
-    }
-
-    return false;
+	return false;
 }
 
 /* Entry point */
 int main(int argc, char *argv[])
 {
-    int rval = -1;
-    int option_index = 0;
+	int rval = -1;
+	int option_index = 0;
+	char *service_name_crypto = NULL;
+	char *service_name_iat = NULL;
+	char *service_name_ps = NULL;
+	char *service_name_its = NULL;
 
-     /* Print available tests */
-    if (option_selected("-l", argc, argv, &option_index)) {
-        print_psa_api_tests();
-        return 0;
-    }
+	/* Print available tests */
+	if (option_selected("-l", argc, argv, &option_index)) {
+		print_psa_api_tests();
+		return 0;
+	}
 
-    /* Create custom test list */
-    if (option_selected("-t", argc, argv, &option_index)) {
-        /* Avoid overindexing of argv and detect if the option is followed by another option */
-        char *test_list_values = argv[option_index + 1];
-        if ((option_index >= argc) || (test_list_values[0] == '-')) {
-            printf("Testlist string is expected after -t argument!\n");
-            return -1;
-        }
+	/* Create custom test list */
+	if (option_selected("-t", argc, argv, &option_index)) {
+		/*
+		 * Avoid overindexing of argv and detect if the
+		 * option is followed by another option
+		 */
+		char *test_list_values = argv[option_index + 1];
 
-        if (is_test_list_wrong(test_list_values)) {
-            printf("Testlist string is not valid!\n");
-            print_psa_api_tests();
-            return -1;
-        }
+		if ((option_index >= argc) || (test_list_values[0] == '-')) {
+			EMSG("Testlist string is expected after -t argument!");
+			return -1;
+		}
 
-        /* Filter tests */
-        pal_set_custom_test_list(test_list_values);
-    }
+		if (is_test_list_wrong(test_list_values)) {
+			EMSG("Testlist string is not valid!");
+			print_psa_api_tests();
+			return -1;
+		}
 
-    /* Print help */
-    if (option_selected("-h", argc, argv, &option_index)) {
-        print_help();
-        return 0;
-    }
+		/* Filter tests */
+		pal_set_custom_test_list(test_list_values);
+	}
 
-    /* Locate service under test */
-    rval = locate_service_under_test();
+	/* Print help */
+	if (option_selected("-h", argc, argv, &option_index)) {
+		print_help();
+		return 0;
+	}
 
-    /* Run tests */
-    if (!rval) {
+	/* Process optional service names */
+	if (option_selected("-c", argc, argv, &option_index))
+		service_name_crypto = argv[option_index + 1];
 
-        rval = val_entry();
+	if (option_selected("-a", argc, argv, &option_index))
+		service_name_iat = argv[option_index + 1];
 
-        relinquish_service_under_test();
-    }
-    else {
+	if (option_selected("-p", argc, argv, &option_index))
+		service_name_ps = argv[option_index + 1];
 
-        printf("Failed to locate service under test.  Error code: %d\n", rval);
-    }
+	if (option_selected("-i", argc, argv, &option_index))
+		service_name_its = argv[option_index + 1];
 
-    return rval;
+	rval = test_setup(service_name_crypto, service_name_iat, service_name_ps, service_name_its);
+
+	if (!rval)
+		rval = val_entry();
+
+	test_teardown();
+
+	return rval;
 }
diff --git a/deployments/psa-api-test/crypto/arm-linux/CMakeLists.txt b/deployments/psa-api-test/crypto/arm-linux/CMakeLists.txt
index 2ef6303..8e72bc4 100644
--- a/deployments/psa-api-test/crypto/arm-linux/CMakeLists.txt
+++ b/deployments/psa-api-test/crypto/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 "PSACRYPTO" 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/crypto
diff --git a/deployments/psa-api-test/crypto/crypto-api-test.cmake b/deployments/psa-api-test/crypto/crypto-api-test.cmake
index 9cc97e3..8d4f5e1 100644
--- a/deployments/psa-api-test/crypto/crypto-api-test.cmake
+++ b/deployments/psa-api-test/crypto/crypto-api-test.cmake
@@ -1,5 +1,5 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+# Copyright (c) 2021-2023, Arm Limited and Contributors. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -29,16 +29,15 @@
 #  Crypto specific components
 #
 #-------------------------------------------------------------------------------
-add_components(
-	TARGET "${PROJECT_NAME}"
-	BASE_DIR ${TS_ROOT}
-	COMPONENTS
-		"components/service/crypto/include"
-		"components/service/crypto/client/psa"
+target_sources(${PROJECT_NAME} PRIVATE
+	${TS_ROOT}/deployments/psa-api-test/crypto/crypto.c
 )
 
-target_sources(${PROJECT_NAME} PRIVATE
-	${TS_ROOT}/deployments/psa-api-test/crypto/crypto_locator.c
+add_components(TARGET ${PROJECT_NAME}
+	BASE_DIR ${TS_ROOT}
+	COMPONENTS
+		"components/service/common/include"
+		"components/service/crypto/include"
 )
 
 #-------------------------------------------------------------------------------
diff --git a/deployments/psa-api-test/crypto/crypto.c b/deployments/psa-api-test/crypto/crypto.c
new file mode 100644
index 0000000..9cbc67b
--- /dev/null
+++ b/deployments/psa-api-test/crypto/crypto.c
@@ -0,0 +1,37 @@
+/*
+ * 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";
+
+	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 = 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();
+}
diff --git a/deployments/psa-api-test/crypto/crypto_locator.c b/deployments/psa-api-test/crypto/crypto_locator.c
deleted file mode 100644
index c57a501..0000000
--- a/deployments/psa-api-test/crypto/crypto_locator.c
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- */
-
-#include <stddef.h>
-#include <service_locator.h>
-#include <service/crypto/client/psa/psa_crypto_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;
-
-	if (!session && !attestation_service_context) {
-
-		service_locator_init();
-
-		attestation_service_context =
-			service_locator_query("sn:trustedfirmware.org:crypto:0");
-
-		if (attestation_service_context) {
-
-			session = service_context_open(attestation_service_context);
-
-			if (session) {
-
-				psa_crypto_client_init(session);
-
-				status = 0;
-			}
-			else {
-
-				status = -1;
-				relinquish_service_under_test();
-			}
-		}
-	}
-
-	return status;
-}
-
-int relinquish_service_under_test(void)
-{
-	psa_crypto_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/crypto/linux-pc/CMakeLists.txt b/deployments/psa-api-test/crypto/linux-pc/CMakeLists.txt
index b99604a..d9ad385 100644
--- a/deployments/psa-api-test/crypto/linux-pc/CMakeLists.txt
+++ b/deployments/psa-api-test/crypto/linux-pc/CMakeLists.txt
@@ -20,6 +20,12 @@
 add_executable(${PROJECT_NAME})
 target_include_directories(${PROJECT_NAME} PRIVATE "${TOP_LEVEL_INCLUDE_DIRS}")
 
+set(TRACE_PREFIX "PSACRYPTO" 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/crypto
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
diff --git a/deployments/psa-api-test/internal_trusted_storage/arm-linux/CMakeLists.txt b/deployments/psa-api-test/internal_trusted_storage/arm-linux/CMakeLists.txt
index 5ccd665..80f6c8e 100644
--- a/deployments/psa-api-test/internal_trusted_storage/arm-linux/CMakeLists.txt
+++ b/deployments/psa-api-test/internal_trusted_storage/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 "PSAITS" 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/internal_trusted_storage
diff --git a/deployments/psa-api-test/internal_trusted_storage/its-api-test.cmake b/deployments/psa-api-test/internal_trusted_storage/its-api-test.cmake
index d373e19..f23faa9 100644
--- a/deployments/psa-api-test/internal_trusted_storage/its-api-test.cmake
+++ b/deployments/psa-api-test/internal_trusted_storage/its-api-test.cmake
@@ -22,17 +22,15 @@
 #  Internal trusted storage specific components
 #
 #-------------------------------------------------------------------------------
-add_components(
-	TARGET "${PROJECT_NAME}"
-	BASE_DIR ${TS_ROOT}
-	COMPONENTS
-		"components/service/secure_storage/include"
-		"components/service/secure_storage/frontend/psa/its"
-		"components/service/secure_storage/backend/secure_storage_client"
+target_sources(${PROJECT_NAME} PRIVATE
+	${TS_ROOT}/deployments/psa-api-test/internal_trusted_storage/its.c
 )
 
-target_sources(${PROJECT_NAME} PRIVATE
-	${TS_ROOT}/deployments/psa-api-test/internal_trusted_storage/its_locator.c
+add_components(TARGET ${PROJECT_NAME}
+	BASE_DIR ${TS_ROOT}
+	COMPONENTS
+		"components/service/common/include"
+		"components/service/secure_storage/include"
 )
 
 #-------------------------------------------------------------------------------
diff --git a/deployments/psa-api-test/internal_trusted_storage/its.c b/deployments/psa-api-test/internal_trusted_storage/its.c
new file mode 100644
index 0000000..9f3860b
--- /dev/null
+++ b/deployments/psa-api-test/internal_trusted_storage/its.c
@@ -0,0 +1,31 @@
+/*
+ * 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_its)
+		service_name_its = "sn:trustedfirmware.org:internal-trusted-storage:0";
+
+	psa_status = libpsa_init_its_context(service_name_its);
+	if (psa_status) {
+		EMSG("libpsa_init_its_context failed: %d\n", psa_status);
+		return psa_status;
+	}
+
+	return psa_status;
+}
+
+void test_teardown(void)
+{
+	libpsa_deinit_its_context();
+}
diff --git a/deployments/psa-api-test/internal_trusted_storage/its_locator.c b/deployments/psa-api-test/internal_trusted_storage/its_locator.c
deleted file mode 100644
index 7119d21..0000000
--- a/deployments/psa-api-test/internal_trusted_storage/its_locator.c
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- */
-
-#include <stddef.h>
-#include <service_locator.h>
-#include <service/secure_storage/frontend/psa/its/its_frontend.h>
-#include <service/secure_storage/backend/secure_storage_client/secure_storage_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 *ps_service_context = NULL;
-static struct secure_storage_client storage_client;
-
-int locate_service_under_test(void)
-{
-	int status = -1;
-
-	service_locator_init();
-
-	if (!session && !ps_service_context) {
-
-		ps_service_context =
-			service_locator_query("sn:trustedfirmware.org:internal-trusted-storage:0");
-
-		if (ps_service_context) {
-
-			session =
-				service_context_open(ps_service_context);
-
-			if (session) {
-
-				struct storage_backend *storage_backend = NULL;
-				status = -1;
-
-				storage_backend = secure_storage_client_init(&storage_client, session);
-
-				if (storage_backend) {
-
-					psa_its_frontend_init(storage_backend);
-					status = 0;
-				}
-			}
-
-			if (status < 0) relinquish_service_under_test();
-		}
-	}
-
-	return status;
-}
-
-int relinquish_service_under_test(void)
-{
-	psa_its_frontend_init(NULL);
-	secure_storage_client_deinit(&storage_client);
-
-	if (ps_service_context && session) {
-
-		service_context_close(ps_service_context, session);
-		session = NULL;
-	}
-
-	if (ps_service_context) {
-
-		service_context_relinquish(ps_service_context);
-		ps_service_context = NULL;
-	}
-
-	return 0;
-}
diff --git a/deployments/psa-api-test/internal_trusted_storage/linux-pc/CMakeLists.txt b/deployments/psa-api-test/internal_trusted_storage/linux-pc/CMakeLists.txt
index a123e30..506d1b0 100644
--- a/deployments/psa-api-test/internal_trusted_storage/linux-pc/CMakeLists.txt
+++ b/deployments/psa-api-test/internal_trusted_storage/linux-pc/CMakeLists.txt
@@ -20,6 +20,13 @@
 add_executable(${PROJECT_NAME})
 target_include_directories(${PROJECT_NAME} PRIVATE "${TOP_LEVEL_INCLUDE_DIRS}")
 
+set(TRACE_PREFIX "PSAITS" CACHE STRING "Trace prefix")
+include(${TS_ROOT}/environments/linux-pc/env.cmake)
+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/internal_trusted_storage
diff --git a/deployments/psa-api-test/protected_storage/arm-linux/CMakeLists.txt b/deployments/psa-api-test/protected_storage/arm-linux/CMakeLists.txt
index 412833f..8ff1ce6 100644
--- a/deployments/psa-api-test/protected_storage/arm-linux/CMakeLists.txt
+++ b/deployments/psa-api-test/protected_storage/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 "PSAPS" 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/protected_storage
diff --git a/deployments/psa-api-test/protected_storage/linux-pc/CMakeLists.txt b/deployments/psa-api-test/protected_storage/linux-pc/CMakeLists.txt
index 2131d34..ebfca44 100644
--- a/deployments/psa-api-test/protected_storage/linux-pc/CMakeLists.txt
+++ b/deployments/psa-api-test/protected_storage/linux-pc/CMakeLists.txt
@@ -20,6 +20,13 @@
 add_executable(${PROJECT_NAME})
 target_include_directories(${PROJECT_NAME} PRIVATE "${TOP_LEVEL_INCLUDE_DIRS}")
 
+set(TRACE_PREFIX "PSAPS" CACHE STRING "Trace prefix")
+include(${TS_ROOT}/environments/linux-pc/env.cmake)
+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/protected_storage
diff --git a/deployments/psa-api-test/protected_storage/ps-api-test.cmake b/deployments/psa-api-test/protected_storage/ps-api-test.cmake
index 9fa88fe..75bc974 100644
--- a/deployments/psa-api-test/protected_storage/ps-api-test.cmake
+++ b/deployments/psa-api-test/protected_storage/ps-api-test.cmake
@@ -22,17 +22,15 @@
 #  Protected storage specific components
 #
 #-------------------------------------------------------------------------------
-add_components(
-	TARGET "${PROJECT_NAME}"
-	BASE_DIR ${TS_ROOT}
-	COMPONENTS
-		"components/service/secure_storage/include"
-		"components/service/secure_storage/frontend/psa/ps"
-		"components/service/secure_storage/backend/secure_storage_client"
+target_sources(${PROJECT_NAME} PRIVATE
+	${TS_ROOT}/deployments/psa-api-test/protected_storage/ps.c
 )
 
-target_sources(${PROJECT_NAME} PRIVATE
-	${TS_ROOT}/deployments/psa-api-test/protected_storage/ps_locator.c
+add_components(TARGET ${PROJECT_NAME}
+	BASE_DIR ${TS_ROOT}
+	COMPONENTS
+		"components/service/common/include"
+		"components/service/secure_storage/include"
 )
 
 #-------------------------------------------------------------------------------
diff --git a/deployments/psa-api-test/protected_storage/ps.c b/deployments/psa-api-test/protected_storage/ps.c
new file mode 100644
index 0000000..c20a860
--- /dev/null
+++ b/deployments/psa-api-test/protected_storage/ps.c
@@ -0,0 +1,31 @@
+/*
+ * 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_ps)
+		service_name_ps = "sn:trustedfirmware.org:protected-storage:0";
+
+	psa_status = libpsa_init_ps_context(service_name_ps);
+	if (psa_status) {
+		EMSG("libpsa_init_ps_context failed: %d\n", psa_status);
+		return psa_status;
+	}
+
+	return psa_status;
+}
+
+void test_teardown(void)
+{
+	libpsa_deinit_ps_context();
+}
diff --git a/deployments/psa-api-test/protected_storage/ps_locator.c b/deployments/psa-api-test/protected_storage/ps_locator.c
deleted file mode 100644
index aba8547..0000000
--- a/deployments/psa-api-test/protected_storage/ps_locator.c
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- */
-
-#include <stddef.h>
-#include <service_locator.h>
-#include <service/secure_storage/frontend/psa/ps/ps_frontend.h>
-#include <service/secure_storage/backend/secure_storage_client/secure_storage_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 *ps_service_context = NULL;
-static struct secure_storage_client storage_client;
-
-int locate_service_under_test(void)
-{
-	int status = -1;
-
-	if (!session && !ps_service_context) {
-
-		service_locator_init();
-
-		ps_service_context =
-			service_locator_query("sn:trustedfirmware.org:protected-storage:0");
-
-		if (ps_service_context) {
-
-			session = service_context_open(ps_service_context);
-
-			if (session) {
-
-				struct storage_backend *storage_backend = NULL;
-
-				storage_backend = secure_storage_client_init(&storage_client,  session);
-
-				if (storage_backend) {
-
-					psa_ps_frontend_init(storage_backend);
-					status = 0;
-				}
-			}
-
-			if (status < 0) relinquish_service_under_test();
-		}
-	}
-
-	return status;
-}
-
-int relinquish_service_under_test(void)
-{
-	psa_ps_frontend_init(NULL);
-	secure_storage_client_deinit(&storage_client);
-
-	if (ps_service_context && session) {
-
-		service_context_close(ps_service_context, session);
-		session = NULL;
-	}
-
-	if (ps_service_context) {
-
-		service_context_relinquish(ps_service_context);
-		ps_service_context = NULL;
-	}
-
-	return 0;
-}
\ No newline at end of file
diff --git a/deployments/psa-api-test/psa-api-test.cmake b/deployments/psa-api-test/psa-api-test.cmake
index 739ed26..ba2a74e 100644
--- a/deployments/psa-api-test/psa-api-test.cmake
+++ b/deployments/psa-api-test/psa-api-test.cmake
@@ -1,5 +1,5 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
+# Copyright (c) 2021-2023, Arm Limited and Contributors. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -11,45 +11,31 @@
 #-------------------------------------------------------------------------------
 
 #-------------------------------------------------------------------------------
-#  Use libts for locating and accessing services. An appropriate version of
-#  libts will be imported for the environment in which service tests are
-#  deployed.
-#-------------------------------------------------------------------------------
-include(${TS_ROOT}/deployments/libts/libts-import.cmake)
-target_link_libraries(${PROJECT_NAME} PRIVATE libts::ts)
-
-#-------------------------------------------------------------------------------
 #  Components that are common across all deployments
 #
 #-------------------------------------------------------------------------------
-add_components(
-	TARGET "${PROJECT_NAME}"
-	BASE_DIR ${TS_ROOT}
-	COMPONENTS
-		"components/common/tlv"
-		"components/service/common/client"
-		"components/service/common/include"
-)
 
 target_sources(${PROJECT_NAME} PRIVATE
 	${TS_ROOT}/deployments/psa-api-test/arch_test_runner.c
 )
 
 #-------------------------------------------------------------------------------
+#  Use libpsa for locating PSA services. An appropriate version of
+#  libpsa will be imported for the environment. Making sure the link order is
+#  correct.
+#-------------------------------------------------------------------------------
+include(${TS_ROOT}/deployments/libpsa/libpsa-import.cmake)
+target_link_libraries( ${PROJECT_NAME} PRIVATE libpsa::psa)
+
+target_link_libraries(${PROJECT_NAME} PRIVATE val_nspe test_combine pal_nspe)
+
+#-------------------------------------------------------------------------------
 #  Export project header paths for arch tests
 #
 #-------------------------------------------------------------------------------
 get_target_property(_include_paths ${PROJECT_NAME} INCLUDE_DIRECTORIES)
 list(APPEND PSA_ARCH_TESTS_EXTERNAL_INCLUDE_PATHS ${_include_paths})
-
-#-------------------------------------------------------------------------------
-#  Components used from external projects
-#
-#-------------------------------------------------------------------------------
-
-# psa-arch-tests
 include(${TS_ROOT}/external/psa_arch_tests/psa_arch_tests.cmake)
-target_link_libraries(${PROJECT_NAME} PRIVATE val_nspe test_combine pal_nspe)
 
 #-------------------------------------------------------------------------------
 #  Define install content.
diff --git a/deployments/psa-api-test/psa_api_test_common.h b/deployments/psa-api-test/psa_api_test_common.h
new file mode 100644
index 0000000..568cf10
--- /dev/null
+++ b/deployments/psa-api-test/psa_api_test_common.h
@@ -0,0 +1,16 @@
+/*
+ * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef COMMON_H
+#define COMMON_H
+
+#include "psa/error.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);
+void test_teardown(void);
+
+#endif /* COMMON_H */
diff --git a/deployments/psa-api-test/service_under_test.h b/deployments/psa-api-test/service_under_test.h
deleted file mode 100644
index 834384e..0000000
--- a/deployments/psa-api-test/service_under_test.h
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- */
-
-#ifndef SERVICE_UNDER_TEST_H
-#define SERVICE_UNDER_TEST_H
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/**
- * Locate and open an RPC session for the service under test.  Concrete
- * implementations of this function will locate a specific service and
- * associate an RPC Caller with the singleton PSA API client used by
- * the API tests.
- */
-int locate_service_under_test(void);
-
-/**
- * Reliquish the RPC session when the test run is complete.
- */
-int relinquish_service_under_test(void);
-
-
-#ifdef __cplusplus
-} /* extern "C" */
-#endif
-
-#endif /* SERVICE_UNDER_TEST_H */