[Cryptech-Commits] [sw/libhal] 01/01: Add mode bits for the various flavors of SHA-3, so that the software driver doesn't have to know that the core's internal block size is actually 1600 bits.

git at cryptech.is git at cryptech.is
Tue Jun 8 01:58:30 UTC 2021


This is an automated email from the git hooks/post-receive script.

paul at psgd.org pushed a commit to branch sha3_mode
in repository sw/libhal.

commit d0d651241fd66d9d56addaf331d153b933134b06
Author: Paul Selkirk <paul at psgd.org>
AuthorDate: Mon Jun 7 15:47:39 2021 -0400

    Add mode bits for the various flavors of SHA-3, so that the software
    driver doesn't have to know that the core's internal block size is
    actually 1600 bits.
---
 hal_internal.h      |  2 +-
 hash.c              | 25 ++++++++++++++++++-------
 verilog_constants.h |  5 +++++
 3 files changed, 24 insertions(+), 8 deletions(-)

diff --git a/hal_internal.h b/hal_internal.h
index d757027..1724807 100644
--- a/hal_internal.h
+++ b/hal_internal.h
@@ -156,7 +156,7 @@ extern hal_error_t hal_free_static_memory(const void * const ptr);
  * Longest hash block and digest we support at the moment.
  */
 
-#define HAL_MAX_HASH_BLOCK_LENGTH       SHA3_STATE_LEN
+#define HAL_MAX_HASH_BLOCK_LENGTH       SHA3_224_BLOCK_LEN
 #define HAL_MAX_HASH_DIGEST_LENGTH      SHA3_512_DIGEST_LEN
 #define HAL_MAX_HASH_STATE_LENGTH       SHA3_STATE_LEN
 
diff --git a/hash.c b/hash.c
index 06227af..527c095 100644
--- a/hash.c
+++ b/hash.c
@@ -176,8 +176,20 @@ static const hal_hash_driver_t sha512_driver = {
   SHA512_LENGTH_LEN, SHA512_ADDR_BLOCK, SHA512_ADDR_DIGEST, SHA512_MODE_SHA_512, sw_hash_core_sha512, sizeof(uint64_t)
 };
 
-static const hal_hash_driver_t sha3_driver = {
-  SHA3_LENGTH_LEN, SHA3_ADDR_BLOCK, SHA3_ADDR_DIGEST, 0, NULL, 0
+static const hal_hash_driver_t sha3_224_driver = {
+  SHA3_LENGTH_LEN, SHA3_ADDR_BLOCK, SHA3_ADDR_DIGEST, SHA3_MODE_SHA3_224, NULL, 0
+};
+
+static const hal_hash_driver_t sha3_256_driver = {
+  SHA3_LENGTH_LEN, SHA3_ADDR_BLOCK, SHA3_ADDR_DIGEST, SHA3_MODE_SHA3_256, NULL, 0
+};
+
+static const hal_hash_driver_t sha3_384_driver = {
+  SHA3_LENGTH_LEN, SHA3_ADDR_BLOCK, SHA3_ADDR_DIGEST, SHA3_MODE_SHA3_384, NULL, 0
+};
+
+static const hal_hash_driver_t sha3_512_driver = {
+  SHA3_LENGTH_LEN, SHA3_ADDR_BLOCK, SHA3_ADDR_DIGEST, SHA3_MODE_SHA3_512, NULL, 0
 };
 
 /*
@@ -275,7 +287,7 @@ const hal_hash_descriptor_t hal_hash_sha3_224[1] = {{
   SHA3_224_BLOCK_LEN, SHA3_224_DIGEST_LEN, SHA3_STATE_LEN,
   sizeof(hal_hash_state_t), sizeof(hal_hmac_state_t),
   dalgid_sha3_224, sizeof(dalgid_sha3_224),
-  &sha3_driver, SHA3_NAME, 1
+  &sha3_224_driver, SHA3_NAME, 1
 }};
 
 const hal_hash_descriptor_t hal_hash_sha3_256[1] = {{
@@ -283,7 +295,7 @@ const hal_hash_descriptor_t hal_hash_sha3_256[1] = {{
   SHA3_256_BLOCK_LEN, SHA3_256_DIGEST_LEN, SHA3_STATE_LEN,
   sizeof(hal_hash_state_t), sizeof(hal_hmac_state_t),
   dalgid_sha3_256, sizeof(dalgid_sha3_256),
-  &sha3_driver, SHA3_NAME, 1
+  &sha3_256_driver, SHA3_NAME, 1
 }};
 
 const hal_hash_descriptor_t hal_hash_sha3_384[1] = {{
@@ -291,7 +303,7 @@ const hal_hash_descriptor_t hal_hash_sha3_384[1] = {{
   SHA3_384_BLOCK_LEN, SHA3_384_DIGEST_LEN, SHA3_STATE_LEN,
   sizeof(hal_hash_state_t), sizeof(hal_hmac_state_t),
   dalgid_sha3_384, sizeof(dalgid_sha3_384),
-  &sha3_driver, SHA3_NAME, 1
+  &sha3_384_driver, SHA3_NAME, 1
 }};
 
 const hal_hash_descriptor_t hal_hash_sha3_512[1] = {{
@@ -299,7 +311,7 @@ const hal_hash_descriptor_t hal_hash_sha3_512[1] = {{
   SHA3_512_BLOCK_LEN, SHA3_512_DIGEST_LEN, SHA3_STATE_LEN,
   sizeof(hal_hash_state_t), sizeof(hal_hmac_state_t),
   dalgid_sha3_512, sizeof(dalgid_sha3_512),
-  &sha3_driver, SHA3_NAME, 1
+  &sha3_512_driver, SHA3_NAME, 1
 }};
 
 static inline int is_sha3(hal_hash_state_t *state)
@@ -611,7 +623,6 @@ static hal_error_t hash_write_block(hal_hash_state_t * const state)
     return err;
 
   if ((err = hal_io_write(state->core, state->driver->block_addr, state->block,
-                          is_sha3(state) ? state->descriptor->state_length :
                           state->descriptor->block_length)) != HAL_OK)
     return err;
 
diff --git a/verilog_constants.h b/verilog_constants.h
index 8db5e29..80ff27d 100644
--- a/verilog_constants.h
+++ b/verilog_constants.h
@@ -102,6 +102,11 @@
 #define SHA3_256_DIGEST_LEN     bitsToBytes(256)
 #define SHA3_384_DIGEST_LEN     bitsToBytes(384)
 #define SHA3_512_DIGEST_LEN     bitsToBytes(512)
+#define SHA3_MODE_SHA3_224      (0 << 2)
+#define SHA3_MODE_SHA3_256      (1 << 2)
+#define SHA3_MODE_SHA3_384      (2 << 2)
+#define SHA3_MODE_SHA3_512      (3 << 2)
+#define SHA3_MODE_MASK          (3 << 2)
 
 /*
  * RNG cores.



More information about the Commits mailing list