Merge pull request #9185 from ronald-cron-arm/adapt-components-with-psa-crypto-config-enabled

Adapt components with MBEDTLS_PSA_CRYPTO_CONFIG enabled
diff --git a/tests/psa-client-server/psasim/include/util.h b/tests/psa-client-server/psasim/include/util.h
index 558149f..5eb8238 100644
--- a/tests/psa-client-server/psasim/include/util.h
+++ b/tests/psa-client-server/psasim/include/util.h
@@ -13,20 +13,18 @@
 #if defined(DEBUG)
 #define INFO(fmt, ...) \
     fprintf(stdout, "Info (%s - %d): " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)
+#else /* !DEBUG */
+#define INFO(...)
+#endif /* DEBUG*/
 
 #define ERROR(fmt, ...) \
-    fprintf(stdout, "Error (%s - %d): " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)
+    fprintf(stderr, "Error (%s - %d): " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)
 
 #define FATAL(fmt, ...) \
     { \
-        fprintf(stdout, "Fatal (%s - %d): " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__); \
+        fprintf(stderr, "Fatal (%s - %d): " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__); \
         abort(); \
     }
-#else /* DEBUG */
-#define INFO(...)
-#define ERROR(...)
-#define FATAL(...)
-#endif /* DEBUG*/
 
 #define PROJECT_ID              'M'
 #define PATHNAMESIZE            256
diff --git a/tests/psa-client-server/psasim/src/client.c b/tests/psa-client-server/psasim/src/client.c
index a8c9e08..4c63abf 100644
--- a/tests/psa-client-server/psasim/src/client.c
+++ b/tests/psa-client-server/psasim/src/client.c
@@ -7,12 +7,14 @@
 
 /* Includes from mbedtls */
 #include "psa/crypto.h"
+#include "util.h"
 
 int main()
 {
     /* psa_crypto_init() connects to the server */
     psa_status_t status = psa_crypto_init();
     if (status != PSA_SUCCESS) {
+        ERROR("psa_crypto_init returned %d", status);
         return 1;
     }
 
diff --git a/tests/psa-client-server/psasim/src/psa_ff_client.c b/tests/psa-client-server/psasim/src/psa_ff_client.c
index 21a43b3..0d6bbf3 100644
--- a/tests/psa-client-server/psasim/src/psa_ff_client.c
+++ b/tests/psa-client-server/psasim/src/psa_ff_client.c
@@ -199,7 +199,6 @@
             default:
                 FATAL("   ERROR: unknown internal message type: %ld",
                       response.message_type);
-                return ret;
         }
     }
 }
@@ -301,10 +300,10 @@
                 handles[idx].valid = 1;
                 return idx;
             } else {
-                INFO("Server didn't like you");
+                ERROR("Server didn't like you");
             }
         } else {
-            INFO("Couldn't contact RoT service. Does it exist?");
+            ERROR("Couldn't contact RoT service. Does it exist?");
 
             if (__psa_ff_client_security_state == 0) {
                 ERROR("Invalid SID");
@@ -339,7 +338,7 @@
             }
         }
     }
-    INFO("psa_version failed: does the service exist?");
+    ERROR("psa_version failed: does the service exist?");
     return PSA_VERSION_NONE;
 }
 
diff --git a/tests/psa-client-server/psasim/src/psa_sim_crypto_client.c b/tests/psa-client-server/psasim/src/psa_sim_crypto_client.c
index 2ffb615..758e9b2 100644
--- a/tests/psa-client-server/psasim/src/psa_sim_crypto_client.c
+++ b/tests/psa-client-server/psasim/src/psa_sim_crypto_client.c
@@ -22,7 +22,7 @@
 #include "psa/crypto.h"
 
 #define CLIENT_PRINT(fmt, ...) \
-    PRINT("Client: " fmt, ##__VA_ARGS__)
+    INFO("Client: " fmt, ##__VA_ARGS__)
 
 static psa_handle_t handle = -1;
 
diff --git a/tests/psa-client-server/psasim/src/psa_sim_crypto_server.c b/tests/psa-client-server/psasim/src/psa_sim_crypto_server.c
index da3adb0..30d4b26 100644
--- a/tests/psa-client-server/psasim/src/psa_sim_crypto_server.c
+++ b/tests/psa-client-server/psasim/src/psa_sim_crypto_server.c
@@ -2314,3 +2314,8 @@
 
     return ok ? PSA_SUCCESS : PSA_ERROR_GENERIC_ERROR;
 }
+
+void psa_crypto_close(void)
+{
+    psa_sim_serialize_reset();
+}
diff --git a/tests/psa-client-server/psasim/src/psa_sim_generate.pl b/tests/psa-client-server/psasim/src/psa_sim_generate.pl
index ee3894f..43de1db 100755
--- a/tests/psa-client-server/psasim/src/psa_sim_generate.pl
+++ b/tests/psa-client-server/psasim/src/psa_sim_generate.pl
@@ -244,6 +244,16 @@
 }
 EOF
 
+    # Finally, add psa_crypto_close()
+
+    print $fh <<EOF;
+
+void psa_crypto_close(void)
+{
+    psa_sim_serialize_reset();
+}
+EOF
+
     close($fh);
 }
 
@@ -302,7 +312,7 @@
 #include "psa/crypto.h"
 
 #define CLIENT_PRINT(fmt, ...) \
-    PRINT("Client: " fmt, ##__VA_ARGS__)
+    INFO("Client: " fmt, ##__VA_ARGS__)
 
 static psa_handle_t handle = -1;
 
diff --git a/tests/psa-client-server/psasim/src/psa_sim_serialise.c b/tests/psa-client-server/psasim/src/psa_sim_serialise.c
index 9e8c38b..651e046 100644
--- a/tests/psa-client-server/psasim/src/psa_sim_serialise.c
+++ b/tests/psa-client-server/psasim/src/psa_sim_serialise.c
@@ -10,6 +10,7 @@
  */
 
 #include "psa_sim_serialise.h"
+#include "util.h"
 #include <stdlib.h>
 #include <string.h>
 
@@ -71,8 +72,7 @@
 {
     psasim_client_handle_t handle = next_hash_operation_handle++;
     if (next_hash_operation_handle == 0) {      /* wrapped around */
-        fprintf(stderr, "MAX HASH HANDLES REACHED\n");
-        exit(1);
+        FATAL("Hash operation handle wrapped");
     }
 
     for (ssize_t i = 0; i < MAX_LIVE_HANDLES_PER_CLASS; i++) {
@@ -82,6 +82,8 @@
         }
     }
 
+    ERROR("All slots are currently used. Unable to allocate a new one.");
+
     return -1;  /* all in use */
 }
 
@@ -94,7 +96,9 @@
         }
     }
 
-    return -1;  /* all in use */
+    ERROR("Unable to find slot by handle %u", handle);
+
+    return -1;  /* not found */
 }
 
 static psa_aead_operation_t aead_operations[MAX_LIVE_HANDLES_PER_CLASS];
@@ -106,8 +110,7 @@
 {
     psasim_client_handle_t handle = next_aead_operation_handle++;
     if (next_aead_operation_handle == 0) {      /* wrapped around */
-        fprintf(stderr, "MAX HASH HANDLES REACHED\n");
-        exit(1);
+        FATAL("Aead operation handle wrapped");
     }
 
     for (ssize_t i = 0; i < MAX_LIVE_HANDLES_PER_CLASS; i++) {
@@ -117,6 +120,8 @@
         }
     }
 
+    ERROR("All slots are currently used. Unable to allocate a new one.");
+
     return -1;  /* all in use */
 }
 
@@ -129,7 +134,9 @@
         }
     }
 
-    return -1;  /* all in use */
+    ERROR("Unable to find slot by handle %u", handle);
+
+    return -1;  /* not found */
 }
 
 size_t psasim_serialise_begin_needs(void)
@@ -706,3 +713,11 @@
 
     return 1;
 }
+
+void psa_sim_serialize_reset(void)
+{
+    memset(hash_operation_handles, 0, sizeof(hash_operation_handles));
+    memset(hash_operations, 0, sizeof(hash_operations));
+    memset(aead_operation_handles, 0, sizeof(aead_operation_handles));
+    memset(aead_operations, 0, sizeof(aead_operations));
+}
diff --git a/tests/psa-client-server/psasim/src/psa_sim_serialise.h b/tests/psa-client-server/psasim/src/psa_sim_serialise.h
index 9c69e65..537730c 100644
--- a/tests/psa-client-server/psasim/src/psa_sim_serialise.h
+++ b/tests/psa-client-server/psasim/src/psa_sim_serialise.h
@@ -54,6 +54,12 @@
  * don't contain pointers.
  */
 
+/** Reset all operation slots.
+ *
+ * Should be called when all clients have disconnected.
+ */
+void psa_sim_serialize_reset(void);
+
 /** Return how much buffer space is needed by \c psasim_serialise_begin().
  *
  * \return                   The number of bytes needed in the buffer for
diff --git a/tests/psa-client-server/psasim/src/psa_sim_serialise.pl b/tests/psa-client-server/psasim/src/psa_sim_serialise.pl
index e09bb81..e89fafe 100755
--- a/tests/psa-client-server/psasim/src/psa_sim_serialise.pl
+++ b/tests/psa-client-server/psasim/src/psa_sim_serialise.pl
@@ -105,6 +105,7 @@
         }
     }
 
+    print define_server_serialize_reset(@types);
 } else {
     die("internal error - shouldn't happen");
 }
@@ -329,6 +330,12 @@
  * don't contain pointers.
  */
 
+/** Reset all operation slots.
+ *
+ * Should be called when all clients have disconnected.
+ */
+void psa_sim_serialize_reset(void);
+
 /** Return how much buffer space is needed by \c psasim_serialise_begin().
  *
  * \return                   The number of bytes needed in the buffer for
@@ -724,6 +731,7 @@
  */
 
 #include "psa_sim_serialise.h"
+#include "util.h"
 #include <stdlib.h>
 #include <string.h>
 
@@ -788,6 +796,8 @@
 {
     my ($type) = @_;    # e.g. 'hash' rather than 'psa_hash_operation_t'
 
+    my $utype = ucfirst($type);
+
     return <<EOF;
 
 static psa_${type}_operation_t ${type}_operations[MAX_LIVE_HANDLES_PER_CLASS];
@@ -799,8 +809,7 @@
 {
     psasim_client_handle_t handle = next_${type}_operation_handle++;
     if (next_${type}_operation_handle == 0) {      /* wrapped around */
-        fprintf(stderr, "MAX HASH HANDLES REACHED\\n");
-        exit(1);
+        FATAL("$utype operation handle wrapped");
     }
 
     for (ssize_t i = 0; i < MAX_LIVE_HANDLES_PER_CLASS; i++) {
@@ -810,6 +819,8 @@
         }
     }
 
+    ERROR("All slots are currently used. Unable to allocate a new one.");
+
     return -1;  /* all in use */
 }
 
@@ -822,7 +833,9 @@
         }
     }
 
-    return -1;  /* all in use */
+    ERROR("Unable to find slot by handle %u", handle);
+
+    return -1;  /* not found */
 }
 EOF
 }
@@ -907,6 +920,33 @@
 EOF
 }
 
+# Return the code for psa_sim_serialize_reset()
+sub define_server_serialize_reset
+{
+    my @types = @_;
+
+    my $code = <<EOF;
+
+void psa_sim_serialize_reset(void)
+{
+EOF
+
+    for my $type (@types) {
+        next unless $type =~ /^psa_(\w+_operation)_t$/;
+
+        my $what = $1;  # e.g. "hash_operation"
+
+        $code .= <<EOF;
+    memset(${what}_handles, 0, sizeof(${what}_handles));
+    memset(${what}s, 0, sizeof(${what}s));
+EOF
+    }
+
+    $code .= <<EOF;
+}
+EOF
+}
+
 # Horrible way to align first, second and third lines of function signature to
 # appease uncrustify (these are the 2nd-4th lines of code, indices 1, 2 and 3)
 #
diff --git a/tests/psa-client-server/psasim/src/server.c b/tests/psa-client-server/psasim/src/server.c
index 77ce269..10ab5a2 100644
--- a/tests/psa-client-server/psasim/src/server.c
+++ b/tests/psa-client-server/psasim/src/server.c
@@ -54,6 +54,7 @@
     int client_disconnected = 0;
     char mbedtls_version[18];
     extern psa_status_t psa_crypto_call(psa_msg_t msg);
+    extern psa_status_t psa_crypto_close(void);
 
     mbedtls_version_get_string_full(mbedtls_version);
     SERVER_PRINT("%s", mbedtls_version);
@@ -81,6 +82,7 @@
                         SERVER_PRINT("Got a disconnection message");
                         ret = PSA_SUCCESS;
                         client_disconnected = 1;
+                        psa_crypto_close();
                         break;
                     default:
                         SERVER_PRINT("Got an IPC call of type %d", msg.type);