[boot_rom] Remove references to hw_sha256.h

Signed-off-by: Miguel Young de la Sota <mcyoung@google.com>
diff --git a/sw/device/boot_rom/bootstrap.c b/sw/device/boot_rom/bootstrap.c
index 9eee13b..3607719 100644
--- a/sw/device/boot_rom/bootstrap.c
+++ b/sw/device/boot_rom/bootstrap.c
@@ -11,9 +11,10 @@
 #include "sw/device/lib/base/memory.h"
 #include "sw/device/lib/base/mmio.h"
 #include "sw/device/lib/dif/dif_gpio.h"
+#include "sw/device/lib/dif/dif_hmac.h"
 #include "sw/device/lib/dif/dif_spi_device.h"
 #include "sw/device/lib/flash_ctrl.h"
-#include "sw/device/lib/hw_sha256.h"
+#include "sw/device/lib/runtime/check.h"
 #include "sw/device/lib/runtime/hart.h"
 #include "sw/device/lib/runtime/log.h"
 #include "sw/device/lib/testing/check.h"
@@ -65,16 +66,47 @@
 }
 
 /**
+ * Computes the SHA256 of the given data.
+ */
+static void compute_sha256(const dif_hmac_t *hmac, const void *data, size_t len,
+                           dif_hmac_digest_t *digest) {
+  CHECK(dif_hmac_mode_sha256_start(hmac) == kDifHmacOk);
+  const char *data8 = (const char *)data;
+  size_t data_left = len;
+  while (data_left > 0) {
+    size_t bytes_sent;
+    dif_hmac_fifo_result_t result =
+        dif_hmac_fifo_push(hmac, data8, data_left, &bytes_sent);
+    if (result == kDifHmacFifoOk) {
+      break;
+    }
+    CHECK(result == kDifHmacFifoFull, "Error while pushing to FIFO.");
+    data8 += bytes_sent;
+    data_left -= bytes_sent;
+  }
+
+  CHECK(dif_hmac_process(hmac) == kDifHmacOk);
+  dif_hmac_digest_result_t digest_result = kDifHmacDigestProcessing;
+  while (digest_result == kDifHmacDigestProcessing) {
+    digest_result = dif_hmac_digest_read(hmac, digest);
+  }
+  CHECK(digest_result == kDifHmacDigestOk, "Error reading the digest.");
+}
+
+/**
  * Compares the SHA256 hash of the recieved data with the recieved hash.
  *
  * Returns true if the hashes match.
  */
-static bool check_frame_hash(const spiflash_frame_t *frame) {
-  uint8_t hash[sizeof(frame->header.hash)];
-  uint8_t *data = ((uint8_t *)frame) + sizeof(hash);
-  hw_SHA256_hash(data, sizeof(spiflash_frame_t) - sizeof(hash), hash);
+static bool check_frame_hash(const dif_hmac_t *hmac,
+                             const spiflash_frame_t *frame) {
+  dif_hmac_digest_t digest;
+  size_t digest_len = sizeof(digest.digest);
 
-  return memcmp(hash, frame->header.hash, sizeof(hash)) == 0;
+  uint8_t *data = ((uint8_t *)frame) + digest_len;
+  compute_sha256(hmac, data, sizeof(spiflash_frame_t) - digest_len, &digest);
+
+  return memcmp(digest.digest, frame->header.hash.digest, digest_len) == 0;
 }
 
 /**
@@ -83,8 +115,8 @@
  * This function checks that the sequence numbers and hashes of the frames are
  * correct before programming them into flash.
  */
-static int bootstrap_flash(dif_spi_device_t *spi) {
-  uint8_t ack[SHA256_DIGEST_SIZE] = {0};
+static int bootstrap_flash(dif_spi_device_t *spi, dif_hmac_t *hmac) {
+  dif_hmac_digest_t ack = {0};
   uint32_t expected_frame_num = 0;
   while (true) {
     size_t bytes_available;
@@ -101,18 +133,20 @@
                expected_frame_num);
 
       if (frame_num == expected_frame_num) {
-        if (!check_frame_hash(&frame)) {
+        if (!check_frame_hash(hmac, &frame)) {
           LOG_ERROR("Detected hash mismatch on frame #%d", frame_num);
-          CHECK(dif_spi_device_send(spi, ack, sizeof(ack),
+          CHECK(dif_spi_device_send(spi, (uint8_t *)&ack.digest,
+                                    sizeof(ack.digest),
                                     /*bytes_received=*/NULL) == kDifSpiDeviceOk,
                 "Failed to send bytes to SPI.");
           continue;
         }
 
-        hw_SHA256_hash(&frame, sizeof(spiflash_frame_t), ack);
-        CHECK(dif_spi_device_send(spi, ack, sizeof(ack),
-                                  /*bytes_received=*/NULL) == kDifSpiDeviceOk,
-              "Failed to send bytes to SPI.");
+        compute_sha256(hmac, &frame, sizeof(spiflash_frame_t), &ack);
+        CHECK(
+            dif_spi_device_send(spi, (uint8_t *)&ack.digest, sizeof(ack.digest),
+                                /*bytes_received=*/NULL) == kDifSpiDeviceOk,
+            "Failed to send bytes to SPI.");
 
         if (expected_frame_num == 0) {
           flash_default_region_access(/*rd_en=*/true, /*prog_en=*/true,
@@ -136,9 +170,10 @@
         }
       } else {
         // Send previous ack if unable to verify current frame.
-        CHECK(dif_spi_device_send(spi, ack, sizeof(ack),
-                                  /*bytes_received=*/NULL) == kDifSpiDeviceOk,
-              "Failed to send bytes to SPI.");
+        CHECK(
+            dif_spi_device_send(spi, (uint8_t *)&ack.digest, sizeof(ack.digest),
+                                /*bytes_received=*/NULL) == kDifSpiDeviceOk,
+            "Failed to send bytes to SPI.");
       }
     }
   }
@@ -174,8 +209,17 @@
                                }) == kDifSpiDeviceOk,
       "Failed to configure SPI.");
 
+  dif_hmac_t hmac;
+  dif_hmac_config_t config = {
+      .base_addr = mmio_region_from_addr(TOP_EARLGREY_HMAC_BASE_ADDR),
+      .message_endianness = kDifHmacEndiannessBig,
+      .digest_endianness = kDifHmacEndiannessBig,
+  };
+  CHECK(dif_hmac_init(&config, &hmac) == kDifHmacOk,
+        "Failed to configure HMAC.");
+
   LOG_INFO("HW initialisation completed, waiting for SPI input...");
-  int error = bootstrap_flash(&spi);
+  int error = bootstrap_flash(&spi, &hmac);
   if (error != 0) {
     error |= erase_flash();
     LOG_ERROR("Bootstrap error: 0x%x", error);
diff --git a/sw/device/boot_rom/meson.build b/sw/device/boot_rom/meson.build
index 286f1f2..9c78f11 100644
--- a/sw/device/boot_rom/meson.build
+++ b/sw/device/boot_rom/meson.build
@@ -56,7 +56,7 @@
       sw_lib_pinmux,
       sw_lib_dif_gpio,
       sw_lib_dif_spi_device,
-      sw_lib_hmac,
+      sw_lib_dif_hmac,
       sw_lib_mmio,
       sw_lib_runtime_log,
       sw_lib_dif_uart,
diff --git a/sw/device/boot_rom/spiflash_frame.h b/sw/device/boot_rom/spiflash_frame.h
index 40d0818..de8867a 100644
--- a/sw/device/boot_rom/spiflash_frame.h
+++ b/sw/device/boot_rom/spiflash_frame.h
@@ -5,10 +5,9 @@
 #ifndef OPENTITAN_SW_DEVICE_BOOT_ROM_SPIFLASH_FRAME_H_
 #define OPENTITAN_SW_DEVICE_BOOT_ROM_SPIFLASH_FRAME_H_
 
-#include <stdalign.h>
 #include <stdint.h>
 
-#include "sw/device/lib/hw_sha256.h"
+#include "sw/device/lib/dif/dif_hmac.h"
 
 /**
  * The total size of a spiflash frame.
@@ -45,7 +44,7 @@
   /**
    * SHA256 of the entire frame_t message starting at the `frame_num` offset.
    */
-  uint32_t hash[SHA256_DIGEST_SIZE / sizeof(uint32_t)];
+  dif_hmac_digest_t hash;
   /**
    * Frame number starting at 0. The last frame should be OR'd with
    * FRAME_EOF_MARKER.