[sw/sca] Add 's' (seed PRNG) simple serial command

Signed-off-by: Alphan Ulusoy <alphan@google.com>
diff --git a/sw/device/sca/aes_serial/meson.build b/sw/device/sca/aes_serial/meson.build
index 3247f6c..7b696f2 100644
--- a/sw/device/sca/aes_serial/meson.build
+++ b/sw/device/sca/aes_serial/meson.build
@@ -34,6 +34,7 @@
       sw_lib_dif_uart,
       sw_lib_mmio,
       sw_lib_runtime_print,
+      sw_sca_aes_serial_prng,
     ]
   ),
 )
diff --git a/sw/device/sca/aes_serial/simple_serial.c b/sw/device/sca/aes_serial/simple_serial.c
index b57fa54..6fde3fd 100644
--- a/sw/device/sca/aes_serial/simple_serial.c
+++ b/sw/device/sca/aes_serial/simple_serial.c
@@ -7,6 +7,7 @@
 #include "sw/device/lib/base/memory.h"
 #include "sw/device/lib/dif/dif_uart.h"
 #include "sw/device/lib/runtime/print.h"
+#include "sw/device/sca/aes_serial/prng.h"
 
 #include "hw/top_earlgrey/sw/autogen/top_earlgrey.h"
 
@@ -31,12 +32,12 @@
  * Command handlers.
  *
  * Clients can register handlers for commands 'a'-'z' using
- * `simple_serial_register_handler()` except for 'v' (version), which is handled
- * by this library. This array has an extra element (27) that is initialized in
- * `simple_serial_init()` to point to `simple_serial_unknown_command()` in order
- * to simplify handling of invalid commands in `simple_serial_process_packet()`.
+ * `simple_serial_register_handler()` except for 'v' (version) and 's' (seed
+ * PRNG), which are handled by this library. This array has an extra element
+ * (27) that is initialized in `simple_serial_init()` to point to
+ * `simple_serial_unknown_command()` in order to simplify handling of invalid
+ * commands in `simple_serial_process_packet()`.
  */
-
 static simple_serial_command_handler handlers[27];
 static const dif_uart_t *uart;
 
@@ -146,6 +147,19 @@
 }
 
 /**
+ * Simple serial 's' (seed PRNG) command handler.
+ *
+ * This function only supports 4-byte seeds.
+ *
+ * @param seed A buffer holding the seed.
+ * @param seed_len Seed length.
+ */
+static void simple_serial_seed_prng(const uint8_t *seed, size_t seed_len) {
+  SS_CHECK(seed_len == sizeof(uint32_t));
+  prng_seed(read_32(seed));
+}
+
+/**
  * Handler for uninmplemented simple serial commands.
  *
  * Sends an error packet over UART.
@@ -164,6 +178,7 @@
   for (size_t i = 0; i < ARRAYSIZE(handlers); ++i) {
     handlers[i] = simple_serial_unknown_command;
   }
+  handlers[simple_serial_get_handler_index('s')] = simple_serial_seed_prng;
   handlers[simple_serial_get_handler_index('v')] = simple_serial_version;
 }
 
diff --git a/sw/device/sca/aes_serial/simple_serial.h b/sw/device/sca/aes_serial/simple_serial.h
index 4405026..4d8fd23 100644
--- a/sw/device/sca/aes_serial/simple_serial.h
+++ b/sw/device/sca/aes_serial/simple_serial.h
@@ -14,9 +14,9 @@
  * @file
  * @brief Simple serial protocol for side-channel analysis.
  *
- * This library implements simple serial protocol version 1.1 and provides a
- * built-in handler for the 'v' (version) command. Clients can implement
- * additional command by registering their handlers using
+ * This library implements simple serial protocol version 1.1 and provides
+ * built-in handlers for 'v' (version) and 's' (seed PRNG) commands. Clients
+ * can implement additional command by registering their handlers using
  * `simple_serial_register_handler()`. See https://wiki.newae.com/SimpleSerial
  * for details on the protocol.
  */
@@ -48,7 +48,8 @@
 /**
  * Initializes the data structures used by simple serial.
  *
- * This function also registers the handler for 'v' (version) command.
+ * This function also registers handlers for 'v' (version) and 's' (seed PRNG)
+ * commands.
  *
  * @param uart Handle to an initialized UART device.
  */
@@ -57,8 +58,8 @@
 /**
  * Registers a handler for a simple serial command.
  *
- * Clients cannot register a handler for the 'v' (version)
- * command since it is handled by this library.
+ * Clients cannot register handlers for 'v' (version) and 's' (seed PRNG)
+ * commands since these are handled by this library.
  *
  * @param cmd Simple serial command.
  * @param handler Command handler.