Add PSA crypto sim serialisation functions needed for the remaining PSA hash APIs

Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
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 264509c..78ae9d6 100644
--- a/tests/psa-client-server/psasim/src/psa_sim_serialise.c
+++ b/tests/psa-client-server/psasim/src/psa_sim_serialise.c
@@ -130,7 +130,9 @@
     return sizeof(value);
 }
 
-int psasim_serialise_unsigned_int(uint8_t **pos, size_t *remaining, unsigned int value)
+int psasim_serialise_unsigned_int(uint8_t **pos,
+                                  size_t *remaining,
+                                  unsigned int value)
 {
     if (*remaining < sizeof(value)) {
         return 0;
@@ -142,7 +144,9 @@
     return 1;
 }
 
-int psasim_deserialise_unsigned_int(uint8_t **pos, size_t *remaining, unsigned int *value)
+int psasim_deserialise_unsigned_int(uint8_t **pos,
+                                    size_t *remaining,
+                                    unsigned int *value)
 {
     if (*remaining < sizeof(*value)) {
         return 0;
@@ -161,7 +165,9 @@
     return sizeof(value);
 }
 
-int psasim_serialise_int(uint8_t **pos, size_t *remaining, int value)
+int psasim_serialise_int(uint8_t **pos,
+                         size_t *remaining,
+                         int value)
 {
     if (*remaining < sizeof(value)) {
         return 0;
@@ -173,7 +179,9 @@
     return 1;
 }
 
-int psasim_deserialise_int(uint8_t **pos, size_t *remaining, int *value)
+int psasim_deserialise_int(uint8_t **pos,
+                           size_t *remaining,
+                           int *value)
 {
     if (*remaining < sizeof(*value)) {
         return 0;
@@ -192,7 +200,9 @@
     return sizeof(value);
 }
 
-int psasim_serialise_size_t(uint8_t **pos, size_t *remaining, size_t value)
+int psasim_serialise_size_t(uint8_t **pos,
+                            size_t *remaining,
+                            size_t value)
 {
     if (*remaining < sizeof(value)) {
         return 0;
@@ -204,7 +214,9 @@
     return 1;
 }
 
-int psasim_deserialise_size_t(uint8_t **pos, size_t *remaining, size_t *value)
+int psasim_deserialise_size_t(uint8_t **pos,
+                              size_t *remaining,
+                              size_t *value)
 {
     if (*remaining < sizeof(*value)) {
         return 0;
@@ -325,12 +337,16 @@
     return psasim_serialise_int_needs(value);
 }
 
-int psasim_serialise_psa_status_t(uint8_t **pos, size_t *remaining, psa_status_t value)
+int psasim_serialise_psa_status_t(uint8_t **pos,
+                                  size_t *remaining,
+                                  psa_status_t value)
 {
     return psasim_serialise_int(pos, remaining, value);
 }
 
-int psasim_deserialise_psa_status_t(uint8_t **pos, size_t *remaining, psa_status_t *value)
+int psasim_deserialise_psa_status_t(uint8_t **pos,
+                                    size_t *remaining,
+                                    psa_status_t *value)
 {
     return psasim_deserialise_int(pos, remaining, value);
 }
@@ -340,12 +356,51 @@
     return psasim_serialise_unsigned_int_needs(value);
 }
 
-int psasim_serialise_psa_algorithm_t(uint8_t **pos, size_t *remaining, psa_algorithm_t value)
+int psasim_serialise_psa_algorithm_t(uint8_t **pos,
+                                     size_t *remaining,
+                                     psa_algorithm_t value)
 {
     return psasim_serialise_unsigned_int(pos, remaining, value);
 }
 
-int psasim_deserialise_psa_algorithm_t(uint8_t **pos, size_t *remaining, psa_algorithm_t *value)
+int psasim_deserialise_psa_algorithm_t(uint8_t **pos,
+                                       size_t *remaining,
+                                       psa_algorithm_t *value)
 {
     return psasim_deserialise_unsigned_int(pos, remaining, value);
 }
+
+size_t psasim_serialise_psa_hash_operation_t_needs(psa_hash_operation_t value)
+{
+    return sizeof(value);
+}
+
+int psasim_serialise_psa_hash_operation_t(uint8_t **pos,
+                                          size_t *remaining,
+                                          psa_hash_operation_t value)
+{
+    if (*remaining < sizeof(value)) {
+        return 0;
+    }
+
+    memcpy(*pos, &value, sizeof(value));
+    *pos += sizeof(value);
+
+    return 1;
+}
+
+int psasim_deserialise_psa_hash_operation_t(uint8_t **pos,
+                                            size_t *remaining,
+                                            psa_hash_operation_t *value)
+{
+    if (*remaining < sizeof(*value)) {
+        return 0;
+    }
+
+    memcpy(value, *pos, sizeof(*value));
+
+    *pos += sizeof(*value);
+    *remaining -= sizeof(*value);
+
+    return 1;
+}
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 9cca7d8..d5eaccf 100644
--- a/tests/psa-client-server/psasim/src/psa_sim_serialise.h
+++ b/tests/psa-client-server/psasim/src/psa_sim_serialise.h
@@ -113,7 +113,9 @@
  *
  * \return                   \c 1 on success ("okay"), \c 0 on error.
  */
-int psasim_serialise_unsigned_int(uint8_t **pos, size_t *remaining, unsigned int value);
+int psasim_serialise_unsigned_int(uint8_t **pos,
+                                  size_t *remaining,
+                                  unsigned int value);
 
 /** Deserialise an `unsigned int` from a buffer.
  *
@@ -126,7 +128,9 @@
  *
  * \return                   \c 1 on success ("okay"), \c 0 on error.
  */
-int psasim_deserialise_unsigned_int(uint8_t **pos, size_t *remaining, unsigned int *value);
+int psasim_deserialise_unsigned_int(uint8_t **pos,
+                                    size_t *remaining,
+                                    unsigned int *value);
 
 /** Return how much buffer space is needed by \c psasim_serialise_int()
  *  to serialise an `int`.
@@ -151,7 +155,9 @@
  *
  * \return                   \c 1 on success ("okay"), \c 0 on error.
  */
-int psasim_serialise_int(uint8_t **pos, size_t *remaining, int value);
+int psasim_serialise_int(uint8_t **pos,
+                         size_t *remaining,
+                         int value);
 
 /** Deserialise an `int` from a buffer.
  *
@@ -164,7 +170,9 @@
  *
  * \return                   \c 1 on success ("okay"), \c 0 on error.
  */
-int psasim_deserialise_int(uint8_t **pos, size_t *remaining, int *value);
+int psasim_deserialise_int(uint8_t **pos,
+                           size_t *remaining,
+                           int *value);
 
 /** Return how much buffer space is needed by \c psasim_serialise_size_t()
  *  to serialise a `size_t`.
@@ -189,7 +197,9 @@
  *
  * \return                   \c 1 on success ("okay"), \c 0 on error.
  */
-int psasim_serialise_size_t(uint8_t **pos, size_t *remaining, size_t value);
+int psasim_serialise_size_t(uint8_t **pos,
+                            size_t *remaining,
+                            size_t value);
 
 /** Deserialise a `size_t` from a buffer.
  *
@@ -202,7 +212,9 @@
  *
  * \return                   \c 1 on success ("okay"), \c 0 on error.
  */
-int psasim_deserialise_size_t(uint8_t **pos, size_t *remaining, size_t *value);
+int psasim_deserialise_size_t(uint8_t **pos,
+                              size_t *remaining,
+                              size_t *value);
 
 /** Return how much space is needed by \c psasim_serialise_buffer()
  *  to serialise a buffer: a (`uint8_t *`, `size_t`) pair.
@@ -294,7 +306,9 @@
  *
  * \return                   \c 1 on success ("okay"), \c 0 on error.
  */
-int psasim_serialise_psa_status_t(uint8_t **pos, size_t *remaining, psa_status_t value);
+int psasim_serialise_psa_status_t(uint8_t **pos,
+                                  size_t *remaining,
+                                  psa_status_t value);
 
 /** Deserialise a `psa_status_t` from a buffer.
  *
@@ -307,7 +321,9 @@
  *
  * \return                   \c 1 on success ("okay"), \c 0 on error.
  */
-int psasim_deserialise_psa_status_t(uint8_t **pos, size_t *remaining, psa_status_t *value);
+int psasim_deserialise_psa_status_t(uint8_t **pos,
+                                    size_t *remaining,
+                                    psa_status_t *value);
 
 /** Return how much buffer space is needed by \c psasim_serialise_psa_algorithm_t()
  *  to serialise a `psa_algorithm_t`.
@@ -332,7 +348,9 @@
  *
  * \return                   \c 1 on success ("okay"), \c 0 on error.
  */
-int psasim_serialise_psa_algorithm_t(uint8_t **pos, size_t *remaining, psa_algorithm_t value);
+int psasim_serialise_psa_algorithm_t(uint8_t **pos,
+                                     size_t *remaining,
+                                     psa_algorithm_t value);
 
 /** Deserialise a `psa_algorithm_t` from a buffer.
  *
@@ -345,4 +363,48 @@
  *
  * \return                   \c 1 on success ("okay"), \c 0 on error.
  */
-int psasim_deserialise_psa_algorithm_t(uint8_t **pos, size_t *remaining, psa_algorithm_t *value);
+int psasim_deserialise_psa_algorithm_t(uint8_t **pos,
+                                       size_t *remaining,
+                                       psa_algorithm_t *value);
+
+/** Return how much buffer space is needed by \c psasim_serialise_psa_hash_operation_t()
+ *  to serialise a `psa_hash_operation_t`.
+ *
+ * \param value              The value that will be serialised into the buffer
+ *                           (needed in case some serialisations are value-
+ *                           dependent).
+ *
+ * \return                   The number of bytes needed in the buffer by
+ *                           \c psasim_serialise_psa_hash_operation_t() to serialise
+ *                           the given value.
+ */
+size_t psasim_serialise_psa_hash_operation_t_needs(psa_hash_operation_t value);
+
+/** Serialise a `psa_hash_operation_t` into a buffer.
+ *
+ * \param pos[in,out]        Pointer to a `uint8_t *` holding current position
+ *                           in the buffer.
+ * \param remaining[in,out]  Pointer to a `size_t` holding number of bytes
+ *                           remaining in the buffer.
+ * \param value              The value to serialise into the buffer.
+ *
+ * \return                   \c 1 on success ("okay"), \c 0 on error.
+ */
+int psasim_serialise_psa_hash_operation_t(uint8_t **pos,
+                                          size_t *remaining,
+                                          psa_hash_operation_t value);
+
+/** Deserialise a `psa_hash_operation_t` from a buffer.
+ *
+ * \param pos[in,out]        Pointer to a `uint8_t *` holding current position
+ *                           in the buffer.
+ * \param remaining[in,out]  Pointer to a `size_t` holding number of bytes
+ *                           remaining in the buffer.
+ * \param value              Pointer to a `psa_hash_operation_t` to receive the value
+ *                           deserialised from the buffer.
+ *
+ * \return                   \c 1 on success ("okay"), \c 0 on error.
+ */
+int psasim_deserialise_psa_hash_operation_t(uint8_t **pos,
+                                            size_t *remaining,
+                                            psa_hash_operation_t *value);
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 2a6c388..b89d058 100644
--- a/tests/psa-client-server/psasim/src/psa_sim_serialise.pl
+++ b/tests/psa-client-server/psasim/src/psa_sim_serialise.pl
@@ -32,7 +32,10 @@
 # deserialisation functions written manually (like those for the "buffer" type
 # are).
 #
-my @types = qw(unsigned-int int size_t buffer psa_status_t psa_algorithm_t);
+my @types = qw(unsigned-int int size_t
+               buffer
+               psa_status_t psa_algorithm_t
+               psa_hash_operation_t);
 grep(s/-/ /g, @types);
 
 # IS-A: Some data types are typedef'd; we serialise them as the other type
@@ -110,7 +113,7 @@
     my $type_d = $type;
     $type_d =~ s/ /_/g;
 
-    return <<EOF;
+    return align_declaration(<<EOF);
 
 /** Serialise $an `$type` into a buffer.
  *
@@ -122,7 +125,9 @@
  *
  * \\return                   \\c 1 on success ("okay"), \\c 0 on error.
  */
-int psasim_serialise_$type_d(uint8_t **pos, size_t *remaining, $type value);
+int psasim_serialise_$type_d(uint8_t **pos,
+                             size_t *remaining,
+                             $type value);
 EOF
 }
 
@@ -134,7 +139,7 @@
     my $type_d = $type;
     $type_d =~ s/ /_/g;
 
-    return <<EOF;
+    return align_declaration(<<EOF);
 
 /** Deserialise $an `$type` from a buffer.
  *
@@ -147,7 +152,9 @@
  *
  * \\return                   \\c 1 on success ("okay"), \\c 0 on error.
  */
-int psasim_deserialise_$type_d(uint8_t **pos, size_t *remaining, $type *value);
+int psasim_deserialise_$type_d(uint8_t **pos,
+                               size_t *remaining,
+                               $type *value);
 EOF
 }
 
@@ -363,9 +370,11 @@
     my $type_d = $type;
     $type_d =~ s/ /_/g;
 
-    return <<EOF;
+    return align_signature(<<EOF);
 
-int psasim_serialise_$type_d(uint8_t **pos, size_t *remaining, $type value)
+int psasim_serialise_$type_d(uint8_t **pos,
+                             size_t *remaining,
+                             $type value)
 {
     if (*remaining < sizeof(value)) {
         return 0;
@@ -389,9 +398,11 @@
     my $isa_d = $isa;
     $isa_d =~ s/ /_/g;
 
-    return <<EOF;
+    return align_signature(<<EOF);
 
-int psasim_serialise_$type_d(uint8_t **pos, size_t *remaining, $type value)
+int psasim_serialise_$type_d(uint8_t **pos,
+                             size_t *remaining,
+                             $type value)
 {
     return psasim_serialise_$isa_d(pos, remaining, value);
 }
@@ -405,9 +416,11 @@
     my $type_d = $type;
     $type_d =~ s/ /_/g;
 
-    return <<EOF;
+    return align_signature(<<EOF);
 
-int psasim_deserialise_$type_d(uint8_t **pos, size_t *remaining, $type *value)
+int psasim_deserialise_$type_d(uint8_t **pos,
+                               size_t *remaining,
+                               $type *value)
 {
     if (*remaining < sizeof(*value)) {
         return 0;
@@ -433,9 +446,11 @@
     my $isa_d = $isa;
     $isa_d =~ s/ /_/g;
 
-    return <<EOF;
+    return align_signature(<<EOF);
 
-int psasim_deserialise_$type_d(uint8_t **pos, size_t *remaining, $type *value)
+int psasim_deserialise_$type_d(uint8_t **pos,
+                              size_t *remaining,
+                              $type *value)
 {
     return psasim_deserialise_$isa_d(pos, remaining, value);
 }
@@ -681,3 +696,49 @@
 }
 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)
+#
+sub align_signature
+{
+    my ($code) = @_;
+
+    my @code = split(/\n/, $code);
+
+    # Find where the ( is
+    my $idx = index($code[1], "(");
+    die("can't find (") if $idx < 0;
+
+    my $indent = " " x ($idx + 1);
+    $code[2] =~ s/^\s+/$indent/;
+    $code[3] =~ s/^\s+/$indent/;
+
+    return join("\n", @code) . "\n";
+}
+
+# Horrible way to align the function declaration to appease uncrustify
+#
+sub align_declaration
+{
+    my ($code) = @_;
+
+    my @code = split(/\n/, $code);
+
+    # Find out which lines we need to massage
+    my $i;
+    for ($i = 0; $i <= $#code; $i++) {
+        last if $code[$i] =~ /^int psasim_/;
+    }
+    die("can't find int psasim_") if $i > $#code;
+
+    # Find where the ( is
+    my $idx = index($code[$i], "(");
+    die("can't find (") if $idx < 0;
+
+    my $indent = " " x ($idx + 1);
+    $code[$i + 1] =~ s/^\s+/$indent/;
+    $code[$i + 2] =~ s/^\s+/$indent/;
+
+    return join("\n", @code) . "\n";
+}