[testing] Add flash initialization and sample parameters to keymgr testutils.

Consolidate some repeated code between keymgr key derivation test and
the AES sideloading test so that these and future tests can set up
keymgr tests more easily.

Signed-off-by: Jade Philipoom <jadep@google.com>
diff --git a/sw/device/lib/testing/BUILD b/sw/device/lib/testing/BUILD
index 3d9d9cc..ad0daf7 100644
--- a/sw/device/lib/testing/BUILD
+++ b/sw/device/lib/testing/BUILD
@@ -152,9 +152,12 @@
     target_compatible_with = [OPENTITAN_CPU],
     deps = [
         "//hw/ip/keymgr/data:keymgr_regs",
+        "//hw/top_earlgrey/sw/autogen:top_earlgrey",
         "//sw/device/lib/base:mmio",
+        "//sw/device/lib/dif:flash_ctrl",
         "//sw/device/lib/dif:keymgr",
         "//sw/device/lib/runtime:ibex",
+        "//sw/device/lib/testing:flash_ctrl_testutils",
         "//sw/device/lib/testing/test_framework:check",
     ],
 )
diff --git a/sw/device/lib/testing/keymgr_testutils.c b/sw/device/lib/testing/keymgr_testutils.c
index df5cbfa..c7cc57b 100644
--- a/sw/device/lib/testing/keymgr_testutils.c
+++ b/sw/device/lib/testing/keymgr_testutils.c
@@ -4,11 +4,74 @@
 
 #include "sw/device/lib/testing/keymgr_testutils.h"
 
+#include "sw/device/lib/dif/dif_flash_ctrl.h"
 #include "sw/device/lib/dif/dif_keymgr.h"
 #include "sw/device/lib/runtime/ibex.h"
 #include "sw/device/lib/runtime/log.h"
+#include "sw/device/lib/testing/flash_ctrl_testutils.h"
 #include "sw/device/lib/testing/test_framework/check.h"
 
+#include "hw/top_earlgrey/sw/autogen/top_earlgrey.h"
+
+enum {
+  /** Flash Secret partition ID. */
+  kFlashInfoPartitionId = 0,
+
+  /** Secret partition flash bank ID. */
+  kFlashInfoBankId = 0,
+
+  /** Creator Secret flash info page ID. */
+  kFlashInfoPageIdCreatorSecret = 1,
+
+  /** Owner Secret flash info page ID. */
+  kFlashInfoPageIdOwnerSecret = 2,
+
+  /** Key manager secret word size. */
+  kSecretWordSize = 8,
+};
+
+/**
+ * Key manager Creator Secret stored in info flash page.
+ */
+static const uint32_t kCreatorSecret[kSecretWordSize] = {
+    0x4e919d54, 0x322288d8, 0x4bd127c7, 0x9f89bc56,
+    0xb4fb0fdf, 0x1ca1567b, 0x13a0e876, 0xa6521d8f};
+
+/**
+ * Key manager Owner Secret stored in info flash page.
+ */
+static const uint32_t kOwnerSecret[kSecretWordSize] = {
+    0xa6521d8f, 0x13a0e876, 0x1ca1567b, 0xb4fb0fdf,
+    0x9f89bc56, 0x4bd127c7, 0x322288d8, 0x4e919d54,
+};
+
+static void write_info_page(dif_flash_ctrl_state_t *flash, uint32_t page_id,
+                            const uint32_t *data) {
+  uint32_t address = flash_ctrl_testutils_info_region_setup(
+      flash, page_id, kFlashInfoBankId, kFlashInfoPartitionId);
+
+  CHECK(flash_ctrl_testutils_erase_and_write_page(
+      flash, address, kFlashInfoPartitionId, data,
+      kDifFlashCtrlPartitionTypeInfo, kSecretWordSize));
+
+  uint32_t readback_data[kSecretWordSize];
+  CHECK(flash_ctrl_testutils_read(flash, address, kFlashInfoPartitionId,
+                                  readback_data, kDifFlashCtrlPartitionTypeInfo,
+                                  kSecretWordSize, 0));
+  CHECK_ARRAYS_EQ(data, readback_data, kSecretWordSize);
+}
+
+void keymgr_testutils_init_flash(void) {
+  dif_flash_ctrl_state_t flash;
+
+  CHECK_DIF_OK(dif_flash_ctrl_init_state(
+      &flash, mmio_region_from_addr(TOP_EARLGREY_FLASH_CTRL_CORE_BASE_ADDR)));
+
+  // Initialize flash secrets.
+  write_info_page(&flash, kFlashInfoPageIdCreatorSecret, kCreatorSecret);
+  write_info_page(&flash, kFlashInfoPageIdOwnerSecret, kOwnerSecret);
+}
+
 void keymgr_testutils_advance_state(const dif_keymgr_t *keymgr,
                                     const dif_keymgr_state_params_t *params) {
   CHECK_DIF_OK(dif_keymgr_advance_state(keymgr, params));
@@ -46,7 +109,5 @@
   do {
     CHECK_DIF_OK(dif_keymgr_get_status_codes(keymgr, &status));
   } while (status == 0);
-  if (status != kDifKeymgrStatusCodeIdle) {
-    LOG_ERROR("Unexpected status: %x", status);
-  }
+  CHECK(status == kDifKeymgrStatusCodeIdle, "Unexpected status: %x", status);
 }
diff --git a/sw/device/lib/testing/keymgr_testutils.h b/sw/device/lib/testing/keymgr_testutils.h
index af8fb7b..20132ce 100644
--- a/sw/device/lib/testing/keymgr_testutils.h
+++ b/sw/device/lib/testing/keymgr_testutils.h
@@ -8,6 +8,50 @@
 #include "sw/device/lib/dif/dif_keymgr.h"
 
 /**
+ * Versioned key parameters for testing.
+ *
+ * Change destination in order to sideload keys to hardware.
+ */
+static const dif_keymgr_versioned_key_params_t kKeyVersionedParams = {
+    .dest = kDifKeymgrVersionedKeyDestSw,
+    .salt =
+        {
+            0xb6521d8f,
+            0x13a0e876,
+            0x1ca1567b,
+            0xb4fb0fdf,
+            0x9f89bc56,
+            0x4bd127c7,
+            0x322288d8,
+            0xde919d54,
+        },
+    .version = 0xaa,
+};
+
+/**
+ * Software binding value for advancing to creator root key state.
+ */
+static const dif_keymgr_state_params_t kCreatorParams = {
+    .binding_value = {0xdc96c23d, 0xaf36e268, 0xcb68ff71, 0xe92f76e2,
+                      0xb8a8379d, 0x426dc745, 0x19f5cff7, 0x4ec9c6d6},
+    .max_key_version = 0x11,
+};
+
+/**
+ * Software binding value for advancing to owner intermediate key state.
+ */
+static const dif_keymgr_state_params_t kOwnerIntParams = {
+    .binding_value = {0xe4987b39, 0x3f83d390, 0xc2f3bbaf, 0x3195dbfa,
+                      0x23fb480c, 0xb012ae5e, 0xf1394d28, 0x1940ceeb},
+    .max_key_version = 0xaa,
+};
+
+/**
+ * Initialize flash for key manager operation.
+ */
+void keymgr_testutils_init_flash(void);
+
+/**
  * Issues a keymgr advance operation and wait for it to complete
  *
  * @param keymgr A key manager handle.