Add stubs for unsupported PSA Crypto API client operations

The psa-api-test/crypto deployment (formally called ts-arch-test)
is modified to use PSA API client methods instead of MbedTLS
directly.  This change is the first step to adding missing
operations that the PSA arch tests exercise.

Signed-off-by: Julian Hall <julian.hall@arm.com>
Change-Id: I6179c389d3176e649290e373ddfa9d9f8974770c
diff --git a/deployments/psa-api-test/arch_test_runner.c b/deployments/psa-api-test/arch_test_runner.c
new file mode 100644
index 0000000..90ca304
--- /dev/null
+++ b/deployments/psa-api-test/arch_test_runner.c
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <stdint.h>
+#include <stdio.h>
+#include <service_locator.h>
+#include "service_under_test.h"
+
+int32_t val_entry(void);
+
+int main(int argc, char *argv[])
+{
+    int rval = -1;
+
+    service_locator_init();
+
+    rval = locate_service_under_test();
+
+    if (!rval) {
+
+        rval = val_entry();
+
+        relinquish_service_under_test();
+    }
+    else {
+
+        printf("Failed to locate service under test.  Error code: %d\n", rval);
+    }
+
+    return rval;
+}
diff --git a/deployments/ts-arch-test/crypto/arm-linux/CMakeLists.txt b/deployments/psa-api-test/crypto/arm-linux/CMakeLists.txt
similarity index 80%
rename from deployments/ts-arch-test/crypto/arm-linux/CMakeLists.txt
rename to deployments/psa-api-test/crypto/arm-linux/CMakeLists.txt
index 8d58643..ea1b460 100644
--- a/deployments/ts-arch-test/crypto/arm-linux/CMakeLists.txt
+++ b/deployments/psa-api-test/crypto/arm-linux/CMakeLists.txt
@@ -8,17 +8,17 @@
 include(../../../deployment.cmake REQUIRED)
 
 #-------------------------------------------------------------------------------
-#  The CMakeLists.txt for building the ts-arch-test deployment for arm-linux
+#  The CMakeLists.txt for building the psa-api-test deployment for arm-linux
 #
 #  Used for building and running psa arch tests on an Arm based Linux device
 #-------------------------------------------------------------------------------
 include(${TS_ROOT}/environments/arm-linux/env.cmake)
 project(trusted-services LANGUAGES CXX C)
-add_executable(ts-arch-test)
-target_include_directories(ts-arch-test PRIVATE "${TOP_LEVEL_INCLUDE_DIRS}")
+add_executable(psa-api-test)
+target_include_directories(psa-api-test PRIVATE "${TOP_LEVEL_INCLUDE_DIRS}")
 
 #-------------------------------------------------------------------------------
 #  Extend with components that are common across all deployments of
-#  ts-arch-test/crypto
+#  psa-api-test/crypto
 #-------------------------------------------------------------------------------
-include(../crypto-arch-test.cmake REQUIRED)
+include(../crypto-api-test.cmake REQUIRED)
diff --git a/deployments/ts-arch-test/crypto/crypto-arch-test.cmake b/deployments/psa-api-test/crypto/crypto-api-test.cmake
similarity index 74%
rename from deployments/ts-arch-test/crypto/crypto-arch-test.cmake
rename to deployments/psa-api-test/crypto/crypto-api-test.cmake
index 93ade3b..5ad0930 100644
--- a/deployments/ts-arch-test/crypto/crypto-arch-test.cmake
+++ b/deployments/psa-api-test/crypto/crypto-api-test.cmake
@@ -15,21 +15,23 @@
 #  Crypto specific components
 #
 #-------------------------------------------------------------------------------
+add_components(
+	TARGET "psa-api-test"
+	BASE_DIR ${TS_ROOT}
+	COMPONENTS
+		"components/service/crypto/include"
+		"components/service/crypto/client/psa"
+)
 
-# Configuration for mbedcrypto
-set(MBEDTLS_USER_CONFIG_FILE
-	"${TS_ROOT}/components/service/crypto/client/cpp/config_mbedtls_user.h"
-	CACHE STRING "Configuration file for mbedcrypto")
-
-# Mbed TLS provides libmbedcrypto
-include(${TS_ROOT}/external/MbedTLS/MbedTLS.cmake)
-target_link_libraries(ts-arch-test PRIVATE mbedcrypto)
+target_sources(psa-api-test PRIVATE
+	${TS_ROOT}/deployments/psa-api-test/crypto/crypto_locator.c
+)
 
 # Export psa crypto API
 list(APPEND PSA_ARCH_TESTS_EXTERNAL_INCLUDE_PATHS ${PSA_CRYPTO_API_INCLUDE})
 
 #-------------------------------------------------------------------------------
 #  Extend with components that are common across all deployments of
-#  ts-arch-test
+#  psa-api-test
 #-------------------------------------------------------------------------------
-include(../../ts-arch-test.cmake REQUIRED)
+include(../../psa-api-test.cmake REQUIRED)
diff --git a/deployments/psa-api-test/crypto/crypto_locator.c b/deployments/psa-api-test/crypto/crypto_locator.c
new file mode 100644
index 0000000..8571b23
--- /dev/null
+++ b/deployments/psa-api-test/crypto/crypto_locator.c
@@ -0,0 +1,65 @@
+/*
+ * 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 rpc_session_handle session_handle = NULL;
+static struct service_context *crypto_service_context = NULL;
+
+
+int locate_service_under_test(void)
+{
+	int status = -1;
+
+	if (!session_handle && !crypto_service_context) {
+
+		struct rpc_caller *caller;
+
+		crypto_service_context =
+			service_locator_query("sn:trustedfirmware.org:crypto:0", &status);
+
+		if (crypto_service_context) {
+
+			session_handle =
+				service_context_open(crypto_service_context, TS_RPC_ENCODING_PACKED_C, &caller);
+
+			if (session_handle) {
+
+				psa_crypto_client_init(caller);
+				status = 0;
+			}
+			else {
+
+				status = -1;
+				relinquish_service_under_test();
+			}
+		}
+	}
+
+	return status;
+}
+
+void relinquish_service_under_test(void)
+{
+	psa_crypto_client_deinit();
+
+	if (crypto_service_context && session_handle) {
+
+		service_context_close(crypto_service_context, session_handle);
+		session_handle = NULL;
+	}
+
+	if (crypto_service_context) {
+
+		service_context_relinquish(crypto_service_context);
+		crypto_service_context = NULL;
+	}
+}
diff --git a/deployments/psa-api-test/crypto/linux-pc/CMakeLists.txt b/deployments/psa-api-test/crypto/linux-pc/CMakeLists.txt
new file mode 100644
index 0000000..1737976
--- /dev/null
+++ b/deployments/psa-api-test/crypto/linux-pc/CMakeLists.txt
@@ -0,0 +1,32 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+cmake_minimum_required(VERSION 3.16)
+include(../../../deployment.cmake REQUIRED)
+
+# Prevents symbols in the psa-api-test executable overriding symbols with
+# with same name in libts during dyanmic linking performed by the program
+# loader.  This avoid psa crypto api symbols provided by the mbedcrypto
+# library from being overridden by the same symbols in the psa-api-test
+# executable.
+set(CMAKE_C_VISIBILITY_PRESET hidden)
+
+#-------------------------------------------------------------------------------
+#  The CMakeLists.txt for building the psa-api-test deployment for linux-pc
+#
+#  Used for building and running psa arch tests in a native PC enviroment.
+#  Tests can be run by running the built executable called "psa-api-test"
+#-------------------------------------------------------------------------------
+include(${TS_ROOT}/environments/linux-pc/env.cmake)
+project(trusted-services LANGUAGES CXX C)
+add_executable(psa-api-test)
+target_include_directories(psa-api-test PRIVATE "${TOP_LEVEL_INCLUDE_DIRS}")
+
+#-------------------------------------------------------------------------------
+#  Extend with components that are common across all deployments of
+#  psa-api-test/crypto
+#-------------------------------------------------------------------------------
+include(../crypto-api-test.cmake REQUIRED)
diff --git a/deployments/ts-arch-test/initial_attestation/iat-arch-test.cmake b/deployments/psa-api-test/initial_attestation/iat-api-test.cmake
similarity index 91%
rename from deployments/ts-arch-test/initial_attestation/iat-arch-test.cmake
rename to deployments/psa-api-test/initial_attestation/iat-api-test.cmake
index c672b4c..322829a 100644
--- a/deployments/ts-arch-test/initial_attestation/iat-arch-test.cmake
+++ b/deployments/psa-api-test/initial_attestation/iat-api-test.cmake
@@ -16,7 +16,7 @@
 #
 #-------------------------------------------------------------------------------
 add_components(
-	TARGET "ts-arch-test"
+	TARGET "psa-api-test"
 	BASE_DIR ${TS_ROOT}
 	COMPONENTS
 		"components/service/attestation/include"
@@ -29,13 +29,13 @@
 
 # Mbed TLS provides libmbedcrypto
 include(${TS_ROOT}/external/MbedTLS/MbedTLS.cmake)
-target_link_libraries(ts-arch-test PRIVATE mbedcrypto)
+target_link_libraries(psa-api-test PRIVATE mbedcrypto)
 
 # Export psa crypto API
 list(APPEND PSA_ARCH_TESTS_EXTERNAL_INCLUDE_PATHS ${PSA_CRYPTO_API_INCLUDE})
 
 #-------------------------------------------------------------------------------
 #  Extend with components that are common across all deployments of
-#  ts-arch-test
+#  psa-api-test
 #-------------------------------------------------------------------------------
-include(../../ts-arch-test.cmake REQUIRED)
+include(../../psa-api-test.cmake REQUIRED)
diff --git a/deployments/ts-arch-test/initial_attestation/linux-pc/CMakeLists.txt b/deployments/psa-api-test/initial_attestation/linux-pc/CMakeLists.txt
similarity index 75%
rename from deployments/ts-arch-test/initial_attestation/linux-pc/CMakeLists.txt
rename to deployments/psa-api-test/initial_attestation/linux-pc/CMakeLists.txt
index 7b0922d..a710924 100644
--- a/deployments/ts-arch-test/initial_attestation/linux-pc/CMakeLists.txt
+++ b/deployments/psa-api-test/initial_attestation/linux-pc/CMakeLists.txt
@@ -8,18 +8,18 @@
 include(../../../deployment.cmake REQUIRED)
 
 #-------------------------------------------------------------------------------
-#  The CMakeLists.txt for building the ts-arch-test deployment for linux-pc
+#  The CMakeLists.txt for building the psa-api-test deployment for linux-pc
 #
 #  Used for building and running psa arch tests in a native PC enviroment.
-#  Tests can be run by running the built executable called "ts-arch-test"
+#  Tests can be run by running the built executable called "psa-api-test"
 #-------------------------------------------------------------------------------
 include(${TS_ROOT}/environments/linux-pc/env.cmake)
 project(trusted-services LANGUAGES CXX C)
-add_executable(ts-arch-test)
-target_include_directories(ts-arch-test PRIVATE "${TOP_LEVEL_INCLUDE_DIRS}")
+add_executable(psa-api-test)
+target_include_directories(psa-api-test PRIVATE "${TOP_LEVEL_INCLUDE_DIRS}")
 
 #-------------------------------------------------------------------------------
 #  Extend with components that are common across all deployments of
-#  ts-arch-test/initial_attestation
+#  psa-api-test/initial_attestation
 #-------------------------------------------------------------------------------
-include(../iat-arch-test.cmake REQUIRED)
+include(../iat-api-test.cmake REQUIRED)
diff --git a/deployments/ts-arch-test/ts-arch-test.cmake b/deployments/psa-api-test/psa-api-test.cmake
similarity index 80%
rename from deployments/ts-arch-test/ts-arch-test.cmake
rename to deployments/psa-api-test/psa-api-test.cmake
index 9c2e778..357abd1 100644
--- a/deployments/ts-arch-test/ts-arch-test.cmake
+++ b/deployments/psa-api-test/psa-api-test.cmake
@@ -6,8 +6,8 @@
 #-------------------------------------------------------------------------------
 
 #-------------------------------------------------------------------------------
-#  The base build file shared between deployments of 'ts-arch-test' for
-#  different environments.  Used for running PSA arch tests.
+#  The base build file shared between deployments of 'psa-api-test' for
+#  different environments.  Used for running PSA API tests.
 #-------------------------------------------------------------------------------
 
 #-------------------------------------------------------------------------------
@@ -16,25 +16,29 @@
 #  deployed.
 #-------------------------------------------------------------------------------
 include(${TS_ROOT}/deployments/libts/libts-import.cmake)
-target_link_libraries(ts-arch-test PRIVATE libts)
+target_link_libraries(psa-api-test PRIVATE libts)
 
 #-------------------------------------------------------------------------------
 #  Components that are common accross all deployments
 #
 #-------------------------------------------------------------------------------
 add_components(
-	TARGET "ts-arch-test"
+	TARGET "psa-api-test"
 	BASE_DIR ${TS_ROOT}
 	COMPONENTS
-		"components/app/arch-test-runner"
+		"components/common/tlv"
 		"components/service/common/include"
 )
 
+target_sources(psa-api-test PRIVATE
+	${TS_ROOT}/deployments/psa-api-test/arch_test_runner.c
+)
+
 #-------------------------------------------------------------------------------
 #  Export project header paths for arch tests
 #
 #-------------------------------------------------------------------------------
-get_target_property(_include_paths ts-arch-test INCLUDE_DIRECTORIES)
+get_target_property(_include_paths psa-api-test INCLUDE_DIRECTORIES)
 list(APPEND PSA_ARCH_TESTS_EXTERNAL_INCLUDE_PATHS ${_include_paths})
 
 #-------------------------------------------------------------------------------
@@ -44,7 +48,7 @@
 
 # psa-arch-tests
 include(${TS_ROOT}/external/psa_arch_tests/psa_arch_tests.cmake)
-target_link_libraries(ts-arch-test PRIVATE val_nspe test_combine pal_nspe)
+target_link_libraries(psa-api-test PRIVATE val_nspe test_combine pal_nspe)
 
 #-------------------------------------------------------------------------------
 #  Define install content.
@@ -53,4 +57,4 @@
 if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
 	set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/install CACHE PATH "location to install build output to." FORCE)
 endif()
-install(TARGETS ts-arch-test RUNTIME DESTINATION ${TS_ENV}/bin)
+install(TARGETS psa-api-test RUNTIME DESTINATION ${TS_ENV}/bin)
diff --git a/deployments/psa-api-test/service_under_test.h b/deployments/psa-api-test/service_under_test.h
new file mode 100644
index 0000000..85dc81f
--- /dev/null
+++ b/deployments/psa-api-test/service_under_test.h
@@ -0,0 +1,32 @@
+/*
+ * 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.
+ */
+void relinquish_service_under_test(void);
+
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* SERVICE_UNDER_TEST_H */
diff --git a/deployments/ts-arch-test/crypto/linux-pc/CMakeLists.txt b/deployments/ts-arch-test/crypto/linux-pc/CMakeLists.txt
deleted file mode 100644
index f5b286e..0000000
--- a/deployments/ts-arch-test/crypto/linux-pc/CMakeLists.txt
+++ /dev/null
@@ -1,25 +0,0 @@
-#-------------------------------------------------------------------------------
-# Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
-#
-# SPDX-License-Identifier: BSD-3-Clause
-#
-#-------------------------------------------------------------------------------
-cmake_minimum_required(VERSION 3.16)
-include(../../../deployment.cmake REQUIRED)
-
-#-------------------------------------------------------------------------------
-#  The CMakeLists.txt for building the ts-arch-test deployment for linux-pc
-#
-#  Used for building and running psa arch tests in a native PC enviroment.
-#  Tests can be run by running the built executable called "ts-arch-test"
-#-------------------------------------------------------------------------------
-include(${TS_ROOT}/environments/linux-pc/env.cmake)
-project(trusted-services LANGUAGES CXX C)
-add_executable(ts-arch-test)
-target_include_directories(ts-arch-test PRIVATE "${TOP_LEVEL_INCLUDE_DIRS}")
-
-#-------------------------------------------------------------------------------
-#  Extend with components that are common across all deployments of
-#  ts-arch-test/crypto
-#-------------------------------------------------------------------------------
-include(../crypto-arch-test.cmake REQUIRED)