[Cryptech-Commits] [sw/libhal] 02/02: Add support for the SHA-3 core.

git at cryptech.is git at cryptech.is
Mon Jun 7 19:42:21 UTC 2021


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

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

commit 8ef2a4e5f54c8623c98c396e378ec093629b849b
Author: Paul Selkirk <paul at psgd.org>
AuthorDate: Sun Jun 6 23:01:11 2021 -0400

    Add support for the SHA-3 core.
---
 hal.h                 |  16 +++++-
 hal_internal.h        |  15 +++--
 hash.c                | 131 ++++++++++++++++++++++++++++++++-----------
 rpc_hash.c            |  12 +++-
 tests/test-rpc_hash.c | 150 ++++++++++++++++++++++++++++++++++++++++++++++++--
 verilog_constants.h   |  13 +++++
 6 files changed, 289 insertions(+), 48 deletions(-)

diff --git a/hal.h b/hal.h
index 99f0456..7adfd39 100644
--- a/hal.h
+++ b/hal.h
@@ -5,7 +5,7 @@
  *
  * Authors: Joachim Strombergson, Paul Selkirk, Rob Austein
  * Copyright (c) 2015-2017, NORDUnet A/S All rights reserved.
- * Copyright: 2019-2020, The Commons Conservancy Cryptech Project
+ * Copyright: 2019-2021, The Commons Conservancy Cryptech Project
  * SPDX-License-Identifier: BSD-3-Clause
  *
  * Redistribution and use in source and binary forms, with or without
@@ -96,6 +96,9 @@
 #define SHA512_NAME             "sha2-512"
 #define SHA512_VERSION          "0.81"
 
+#define SHA3_NAME               "sha3    "
+#define SHA3_VERSION            "0.10"
+
 #define AES_CORE_NAME           "aes     "
 #define AES_CORE_VERSION        "0.70"
 
@@ -304,13 +307,18 @@ typedef enum {
   HAL_DIGEST_ALGORITHM_SHA512_224,
   HAL_DIGEST_ALGORITHM_SHA512_256,
   HAL_DIGEST_ALGORITHM_SHA384,
-  HAL_DIGEST_ALGORITHM_SHA512
+  HAL_DIGEST_ALGORITHM_SHA512,
+  HAL_DIGEST_ALGORITHM_SHA3_224,
+  HAL_DIGEST_ALGORITHM_SHA3_256,
+  HAL_DIGEST_ALGORITHM_SHA3_384,
+  HAL_DIGEST_ALGORITHM_SHA3_512
 } hal_digest_algorithm_t;
 
 typedef struct {
   hal_digest_algorithm_t digest_algorithm;
   size_t block_length;
   size_t digest_length;
+  size_t state_length;
   size_t hash_state_length;
   size_t hmac_state_length;
   const uint8_t * const digest_algorithm_id;
@@ -339,6 +347,10 @@ extern const hal_hash_descriptor_t hal_hash_sha512_224[1];
 extern const hal_hash_descriptor_t hal_hash_sha512_256[1];
 extern const hal_hash_descriptor_t hal_hash_sha384[1];
 extern const hal_hash_descriptor_t hal_hash_sha512[1];
+extern const hal_hash_descriptor_t hal_hash_sha3_224[1];
+extern const hal_hash_descriptor_t hal_hash_sha3_256[1];
+extern const hal_hash_descriptor_t hal_hash_sha3_384[1];
+extern const hal_hash_descriptor_t hal_hash_sha3_512[1];
 
 /*
  * Hash and HMAC functions.
diff --git a/hal_internal.h b/hal_internal.h
index 15f4c79..d757027 100644
--- a/hal_internal.h
+++ b/hal_internal.h
@@ -4,7 +4,9 @@
  * Internal API declarations for libhal.
  *
  * Authors: Rob Austein, Paul Selkirk
- * Copyright (c) 2015, NORDUnet A/S All rights reserved.
+ * Copyright (c) 2015-2018, NORDUnet A/S All rights reserved.
+ * Copyright: 2021, The Commons Conservancy Cryptech Project
+ * SPDX-License-Identifier: BSD-3-Clause
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -16,9 +18,9 @@
  *   notice, this list of conditions and the following disclaimer in the
  *   documentation and/or other materials provided with the distribution.
  *
- * - Neither the name of the NORDUnet nor the names of its contributors may
- *   be used to endorse or promote products derived from this software
- *   without specific prior written permission.
+ * - Neither the name of the the copyright holder nor the names of its
+ *   contributors may be used to endorse or promote products derived from
+ *   this software without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
@@ -154,8 +156,9 @@ 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       SHA512_BLOCK_LEN
-#define HAL_MAX_HASH_DIGEST_LENGTH      SHA512_DIGEST_LEN
+#define HAL_MAX_HASH_BLOCK_LENGTH       SHA3_STATE_LEN
+#define HAL_MAX_HASH_DIGEST_LENGTH      SHA3_512_DIGEST_LEN
+#define HAL_MAX_HASH_STATE_LENGTH       SHA3_STATE_LEN
 
 /*
  * Locks and critical sections.
diff --git a/hash.c b/hash.c
index a496e87..06227af 100644
--- a/hash.c
+++ b/hash.c
@@ -5,7 +5,7 @@
  *
  * Authors: Joachim Str�mbergson, Paul Selkirk, Rob Austein
  * Copyright (c) 2014-2018, NORDUnet A/S All rights reserved.
- * Copyright: 2020, The Commons Conservancy Cryptech Project
+ * Copyright: 2020-2021, The Commons Conservancy Cryptech Project
  * SPDX-License-Identifier: BSD-3-Clause
  *
  * Redistribution and use in source and binary forms, with or without
@@ -18,9 +18,9 @@
  *   notice, this list of conditions and the following disclaimer in the
  *   documentation and/or other materials provided with the distribution.
  *
- * - Neither the name of the NORDUnet nor the names of its contributors may
- *   be used to endorse or promote products derived from this software
- *   without specific prior written permission.
+ * - Neither the name of the copyright holder nor the names of its
+ *   contributors may be used to endorse or promote products derived from
+ *   this software without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
@@ -120,7 +120,7 @@ struct hal_hash_state {
   uint64_t msg_length_high;                     /* Total data hashed in this message */
   uint64_t msg_length_low;                      /* (128 bits in SHA-512 cases) */
   uint8_t block[HAL_MAX_HASH_BLOCK_LENGTH],     /* Block we're accumulating */
-    core_state[HAL_MAX_HASH_DIGEST_LENGTH];     /* Saved core state */
+    core_state[HAL_MAX_HASH_STATE_LENGTH];      /* Saved core state */
   size_t block_used;                            /* How much of the block we've used */
   unsigned block_count;                         /* Blocks sent */
   unsigned flags;
@@ -176,6 +176,10 @@ 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
+};
+
 /*
  * Digest algorithm identifiers: DER encoded full TLV of an
  * DigestAlgorithmIdentifier SEQUENCE including OID for the algorithm in
@@ -197,7 +201,11 @@ static const uint8_t
   dalgid_sha512[]     = { 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00 },
   dalgid_sha224[]     = { 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04, 0x05, 0x00 },
   dalgid_sha512_224[] = { 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x05, 0x05, 0x00 },
-  dalgid_sha512_256[] = { 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x06, 0x05, 0x00 };
+  dalgid_sha512_256[] = { 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x06, 0x05, 0x00 },
+  dalgid_sha3_224[]   = { 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x07, 0x05, 0x00 },
+  dalgid_sha3_256[]   = { 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x08, 0x05, 0x00 },
+  dalgid_sha3_384[]   = { 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x09, 0x05, 0x00 },
+  dalgid_sha3_512[]   = { 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x0a, 0x05, 0x00 };
 
 /*
  * Descriptors.  Yes, the {hash,hmac}_state_length fields are a bit
@@ -208,15 +216,15 @@ static const uint8_t
 
 const hal_hash_descriptor_t hal_hash_sha1[1] = {{
   HAL_DIGEST_ALGORITHM_SHA1,
-  SHA1_BLOCK_LEN, SHA1_DIGEST_LEN,
+  SHA1_BLOCK_LEN, SHA1_DIGEST_LEN, SHA1_DIGEST_LEN,
   sizeof(hal_hash_state_t), sizeof(hal_hmac_state_t),
   dalgid_sha1, sizeof(dalgid_sha1),
-  &sha1_driver, SHA1_NAME, 0
+  &sha1_driver, SHA1_NAME, 1
 }};
 
 const hal_hash_descriptor_t hal_hash_sha224[1] = {{
   HAL_DIGEST_ALGORITHM_SHA256,
-  SHA256_BLOCK_LEN, SHA224_DIGEST_LEN,
+  SHA256_BLOCK_LEN, SHA224_DIGEST_LEN, SHA224_DIGEST_LEN,
   sizeof(hal_hash_state_t), sizeof(hal_hmac_state_t),
   dalgid_sha224, sizeof(dalgid_sha224),
   &sha224_driver, SHA256_NAME, 1
@@ -224,7 +232,7 @@ const hal_hash_descriptor_t hal_hash_sha224[1] = {{
 
 const hal_hash_descriptor_t hal_hash_sha256[1] = {{
   HAL_DIGEST_ALGORITHM_SHA256,
-  SHA256_BLOCK_LEN, SHA256_DIGEST_LEN,
+  SHA256_BLOCK_LEN, SHA256_DIGEST_LEN, SHA256_DIGEST_LEN,
   sizeof(hal_hash_state_t), sizeof(hal_hmac_state_t),
   dalgid_sha256, sizeof(dalgid_sha256),
   &sha256_driver, SHA256_NAME, 1
@@ -232,7 +240,7 @@ const hal_hash_descriptor_t hal_hash_sha256[1] = {{
 
 const hal_hash_descriptor_t hal_hash_sha512_224[1] = {{
   HAL_DIGEST_ALGORITHM_SHA512_224,
-  SHA512_BLOCK_LEN, SHA512_224_DIGEST_LEN,
+  SHA512_BLOCK_LEN, SHA512_224_DIGEST_LEN, SHA512_224_DIGEST_LEN,
   sizeof(hal_hash_state_t), sizeof(hal_hmac_state_t),
   dalgid_sha512_224, sizeof(dalgid_sha512_224),
   &sha512_224_driver, SHA512_NAME, 1
@@ -240,7 +248,7 @@ const hal_hash_descriptor_t hal_hash_sha512_224[1] = {{
 
 const hal_hash_descriptor_t hal_hash_sha512_256[1] = {{
   HAL_DIGEST_ALGORITHM_SHA512_256,
-  SHA512_BLOCK_LEN, SHA512_256_DIGEST_LEN,
+  SHA512_BLOCK_LEN, SHA512_256_DIGEST_LEN, SHA512_256_DIGEST_LEN,
   sizeof(hal_hash_state_t), sizeof(hal_hmac_state_t),
   dalgid_sha512_256, sizeof(dalgid_sha512_256),
   &sha512_256_driver, SHA512_NAME, 1
@@ -248,7 +256,7 @@ const hal_hash_descriptor_t hal_hash_sha512_256[1] = {{
 
 const hal_hash_descriptor_t hal_hash_sha384[1] = {{
   HAL_DIGEST_ALGORITHM_SHA384,
-  SHA512_BLOCK_LEN, SHA384_DIGEST_LEN,
+  SHA512_BLOCK_LEN, SHA384_DIGEST_LEN, SHA384_DIGEST_LEN,
   sizeof(hal_hash_state_t), sizeof(hal_hmac_state_t),
   dalgid_sha384, sizeof(dalgid_sha384),
   &sha384_driver, SHA512_NAME, 1
@@ -256,12 +264,58 @@ const hal_hash_descriptor_t hal_hash_sha384[1] = {{
 
 const hal_hash_descriptor_t hal_hash_sha512[1] = {{
   HAL_DIGEST_ALGORITHM_SHA512,
-  SHA512_BLOCK_LEN, SHA512_DIGEST_LEN,
+  SHA512_BLOCK_LEN, SHA512_DIGEST_LEN, SHA512_DIGEST_LEN,
   sizeof(hal_hash_state_t), sizeof(hal_hmac_state_t),
   dalgid_sha512, sizeof(dalgid_sha512),
   &sha512_driver, SHA512_NAME, 1
 }};
 
+const hal_hash_descriptor_t hal_hash_sha3_224[1] = {{
+  HAL_DIGEST_ALGORITHM_SHA3_224,
+  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
+}};
+
+const hal_hash_descriptor_t hal_hash_sha3_256[1] = {{
+  HAL_DIGEST_ALGORITHM_SHA3_256,
+  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
+}};
+
+const hal_hash_descriptor_t hal_hash_sha3_384[1] = {{
+  HAL_DIGEST_ALGORITHM_SHA3_384,
+  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
+}};
+
+const hal_hash_descriptor_t hal_hash_sha3_512[1] = {{
+  HAL_DIGEST_ALGORITHM_SHA3_512,
+  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
+}};
+
+static inline int is_sha3(hal_hash_state_t *state)
+{
+  switch (state->descriptor->digest_algorithm) {
+  case HAL_DIGEST_ALGORITHM_SHA3_224:
+  case HAL_DIGEST_ALGORITHM_SHA3_256:
+  case HAL_DIGEST_ALGORITHM_SHA3_384:
+  case HAL_DIGEST_ALGORITHM_SHA3_512:
+    return 1;
+
+  default:
+    return 0;
+  }
+}
+
 /*
  * Static state blocks.  This library is intended for a style of
  * embedded programming in which one avoids heap-based allocation
@@ -545,7 +599,6 @@ static hal_error_t hash_write_block(hal_hash_state_t * const state)
 #endif
 
 #if ! HAL_ONLY_USE_SOFTWARE_HASH_CORES
-  uint8_t ctrl_cmd[4];
   hal_error_t err;
 
   if ((err = hal_io_wait_ready(state->core)) != HAL_OK)
@@ -554,23 +607,29 @@ static hal_error_t hash_write_block(hal_hash_state_t * const state)
   if (state->descriptor->can_restore_state &&
       state->block_count != 0 &&
       (err = hash_write_digest(state->core, state->driver, state->core_state,
-                               state->descriptor->digest_length)) != HAL_OK)
+                               state->descriptor->state_length)) != HAL_OK)
     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;
 
-  ctrl_cmd[0] = ctrl_cmd[1] = ctrl_cmd[2] = 0;
+  uint8_t ctrl_cmd[4] = {0};
+
+  /* reset the control register */
+  if ((err = hal_io_write(state->core, ADDR_CTRL, ctrl_cmd, sizeof(ctrl_cmd))) != HAL_OK)
+    return err;
+
+  /* write the init or next command */
   ctrl_cmd[3] = state->block_count == 0 ? CTRL_INIT : CTRL_NEXT;
   ctrl_cmd[3] |= state->driver->ctrl_mode;
-
   if ((err = hal_io_write(state->core, ADDR_CTRL, ctrl_cmd, sizeof(ctrl_cmd))) != HAL_OK)
     return err;
 
   if (state->descriptor->can_restore_state &&
       (err = hash_read_digest(state->core, state->driver, state->core_state,
-                              state->descriptor->digest_length)) != HAL_OK)
+                              state->descriptor->state_length)) != HAL_OK)
     return err;
 
   return hal_io_wait_valid(state->core);
@@ -657,7 +716,6 @@ hal_error_t hal_hash_finalize(hal_hash_state_t *state,                  /* Opaqu
                               uint8_t *digest_buffer,                   /* Returned digest */
                               const size_t digest_buffer_length)        /* Length of digest_buffer */
 {
-  uint64_t bit_length_high, bit_length_low;
   hal_error_t err;
   uint8_t *p;
   size_t n;
@@ -687,12 +745,12 @@ hal_error_t hal_hash_finalize(hal_hash_state_t *state,                  /* Opaqu
    * Add padding, then pull result from the core
    */
 
-  bit_length_low  = (state->msg_length_low  << 3);
-  bit_length_high = (state->msg_length_high << 3) | (state->msg_length_low >> 61);
-
-  /* Initial pad byte */
-  hal_assert(state->block_used < state->descriptor->block_length);
-  state->block[state->block_used++] = 0x80;
+  /* Initial pad byte.
+   * SHA-3 appends an instance ID (01) to the message before padding,
+   * so the effective start of padding is 011. Except that it uses
+   * little-endian bit ordering, so it's xxxxx110. WTF.
+   */
+  state->block[state->block_used++] = is_sha3(state) ? 0x06 : 0x80;
 
   /* If not enough room for bit count, zero and push current block */
   if ((n = state->descriptor->block_length - state->block_used) < state->driver->length_length) {
@@ -716,12 +774,21 @@ hal_error_t hal_hash_finalize(hal_hash_state_t *state,                  /* Opaqu
     hal_log(HAL_LOG_DEBUG, "[ Final block, used %lu, n %lu, msg_length %llu ]\n",
             (unsigned long) state->block_used, (unsigned long) n, (unsigned long long)state->msg_length_low);
   p = state->block + state->descriptor->block_length;
-  for (i = 0; (bit_length_low || bit_length_high) && i < state->driver->length_length; i++) {
-    *--p = (uint8_t) (bit_length_low & 0xFF);
-    bit_length_low >>= 8;
-    if (bit_length_high) {
-      bit_length_low |= ((bit_length_high & 0xFF) << 56);
-      bit_length_high >>= 8;
+  if (is_sha3(state)) {
+    /* SHA-3 ends padding with a single 1 bit, rather than message length. */
+    *--p |= 0x80;
+  }
+  else {
+    uint64_t bit_length_low  = (state->msg_length_low  << 3);
+    uint64_t bit_length_high = (state->msg_length_high << 3) | (state->msg_length_low >> 61);
+
+    for (i = 0; (bit_length_low || bit_length_high) && i < state->driver->length_length; i++) {
+      *--p = (uint8_t) (bit_length_low & 0xFF);
+      bit_length_low >>= 8;
+      if (bit_length_high) {
+        bit_length_low |= ((bit_length_high & 0xFF) << 56);
+        bit_length_high >>= 8;
+      }
     }
   }
 
diff --git a/rpc_hash.c b/rpc_hash.c
index 13b6891..70c97f4 100644
--- a/rpc_hash.c
+++ b/rpc_hash.c
@@ -5,6 +5,8 @@
  *
  * Authors: Rob Austein
  * Copyright (c) 2015-2016, NORDUnet A/S All rights reserved.
+ * Copyright: 2021, The Commons Conservancy Cryptech Project
+ * SPDX-License-Identifier: BSD-3-Clause
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -16,9 +18,9 @@
  *   notice, this list of conditions and the following disclaimer in the
  *   documentation and/or other materials provided with the distribution.
  *
- * - Neither the name of the NORDUnet nor the names of its contributors may
- *   be used to endorse or promote products derived from this software
- *   without specific prior written permission.
+ * - Neither the name of the copyright holder nor the names of its
+ *   contributors may be used to endorse or promote products derived from
+ *   this software without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
@@ -163,6 +165,10 @@ static inline const hal_hash_descriptor_t *alg_to_descriptor(const hal_digest_al
   case HAL_DIGEST_ALGORITHM_SHA512_256: return hal_hash_sha512_256;
   case HAL_DIGEST_ALGORITHM_SHA384:     return hal_hash_sha384;
   case HAL_DIGEST_ALGORITHM_SHA512:     return hal_hash_sha512;
+  case HAL_DIGEST_ALGORITHM_SHA3_224:   return hal_hash_sha3_224;
+  case HAL_DIGEST_ALGORITHM_SHA3_256:   return hal_hash_sha3_256;
+  case HAL_DIGEST_ALGORITHM_SHA3_384:   return hal_hash_sha3_384;
+  case HAL_DIGEST_ALGORITHM_SHA3_512:   return hal_hash_sha3_512;
   default:                              return NULL;
   }
 }
diff --git a/tests/test-rpc_hash.c b/tests/test-rpc_hash.c
index 24f8ede..51fa2be 100644
--- a/tests/test-rpc_hash.c
+++ b/tests/test-rpc_hash.c
@@ -4,8 +4,9 @@
  * Test code for RPC interface to Cryptech hash cores.
  *
  * Authors: Rob Austein, Paul Selkirk
- * Copyright (c) 2015-2016, NORDUnet A/S
- * All rights reserved.
+ * Copyright (c) 2015-2016, NORDUnet A/S All rights reserved.
+ * Copyright: 2021, The Commons Conservancy Cryptech Project
+ * SPDX-License-Identifier: BSD-3-Clause
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -17,9 +18,9 @@
  *   notice, this list of conditions and the following disclaimer in the
  *   documentation and/or other materials provided with the distribution.
  *
- * - Neither the name of the NORDUnet nor the names of its contributors may
- *   be used to endorse or promote products derived from this software
- *   without specific prior written permission.
+ * - Neither the name of the copyright holder nor the names of its
+ *   contributors may be used to endorse or promote products derived from
+ *   this software without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
@@ -43,6 +44,21 @@
 
 /* Usual NIST sample messages. */
 
+/* "" */
+static const uint8_t nist_512_empty[] = { /* 0 bytes */
+};
+
+static const uint8_t sha1_empty_digest[] = { /* 20 bytes */
+  0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef,
+  0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09
+};
+
+static const uint8_t sha256_empty_digest[] = { /* 32 bytes */
+  0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
+  0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
+  0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
+};
+
 /* "abc" */
 static const uint8_t nist_512_single[] = { /* 3 bytes */
   0x61, 0x62, 0x63
@@ -79,6 +95,54 @@ static const uint8_t sha256_double_digest[] = { /* 32 bytes */
   0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1
 };
 
+/* "" */
+static const uint8_t nist_1024_empty[] = { /* 0 bytes */
+};
+
+static const uint8_t sha384_empty_digest[] = { /* 48 bytes */
+  0x38, 0xb0, 0x60, 0xa7, 0x51, 0xac, 0x96, 0x38, 0x4c, 0xd9, 0x32, 0x7e,
+  0xb1, 0xb1, 0xe3, 0x6a, 0x21, 0xfd, 0xb7, 0x11, 0x14, 0xbe, 0x07, 0x43,
+  0x4c, 0x0c, 0xc7, 0xbf, 0x63, 0xf6, 0xe1, 0xda, 0x27, 0x4e, 0xde, 0xbf,
+  0xe7, 0x6f, 0x65, 0xfb, 0xd5, 0x1a, 0xd2, 0xf1, 0x48, 0x98, 0xb9, 0x5b
+};
+
+static const uint8_t sha512_empty_digest[] = { /* 64 bytes */
+  0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd, 0xf1, 0x54, 0x28, 0x50, 
+  0xd6, 0x6d, 0x80, 0x07, 0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57, 0x15, 0xdc, 
+  0x83, 0xf4, 0xa9, 0x21, 0xd3, 0x6c, 0xe9, 0xce, 0x47, 0xd0, 0xd1, 0x3c, 
+  0x5d, 0x85, 0xf2, 0xb0, 0xff, 0x83, 0x18, 0xd2, 0x87, 0x7e, 0xec, 0x2f, 
+  0x63, 0xb9, 0x31, 0xbd, 0x47, 0x41, 0x7a, 0x81, 0xa5, 0x38, 0x32, 0x7a, 
+  0xf9, 0x27, 0xda, 0x3e
+};
+
+static const uint8_t sha3_224_empty_digest[] = { /* 28 bytes */
+  0x6b, 0x4e, 0x03, 0x42, 0x36, 0x67, 0xdb, 0xb7, 0x3b, 0x6e, 0x15, 0x45, 
+  0x4f, 0x0e, 0xb1, 0xab, 0xd4, 0x59, 0x7f, 0x9a, 0x1b, 0x07, 0x8e, 0x3f, 
+  0x5b, 0x5a, 0x6b, 0xc7
+};
+
+static const uint8_t sha3_256_empty_digest[] = { /* 32 bytes */
+  0xa7, 0xff, 0xc6, 0xf8, 0xbf, 0x1e, 0xd7, 0x66, 0x51, 0xc1, 0x47, 0x56, 
+  0xa0, 0x61, 0xd6, 0x62, 0xf5, 0x80, 0xff, 0x4d, 0xe4, 0x3b, 0x49, 0xfa, 
+  0x82, 0xd8, 0x0a, 0x4b, 0x80, 0xf8, 0x43, 0x4a
+};
+
+static const uint8_t sha3_384_empty_digest[] = { /* 48 bytes */
+  0x0c, 0x63, 0xa7, 0x5b, 0x84, 0x5e, 0x4f, 0x7d, 0x01, 0x10, 0x7d, 0x85, 
+  0x2e, 0x4c, 0x24, 0x85, 0xc5, 0x1a, 0x50, 0xaa, 0xaa, 0x94, 0xfc, 0x61, 
+  0x99, 0x5e, 0x71, 0xbb, 0xee, 0x98, 0x3a, 0x2a, 0xc3, 0x71, 0x38, 0x31, 
+  0x26, 0x4a, 0xdb, 0x47, 0xfb, 0x6b, 0xd1, 0xe0, 0x58, 0xd5, 0xf0, 0x04
+};
+
+static const uint8_t sha3_512_empty_digest[] = { /* 64 bytes */
+  0xa6, 0x9f, 0x73, 0xcc, 0xa2, 0x3a, 0x9a, 0xc5, 0xc8, 0xb5, 0x67, 0xdc, 
+  0x18, 0x5a, 0x75, 0x6e, 0x97, 0xc9, 0x82, 0x16, 0x4f, 0xe2, 0x58, 0x59, 
+  0xe0, 0xd1, 0xdc, 0xc1, 0x47, 0x5c, 0x80, 0xa6, 0x15, 0xb2, 0x12, 0x3a, 
+  0xf1, 0xf5, 0xf9, 0x4c, 0x11, 0xe3, 0xe9, 0x40, 0x2c, 0x3a, 0xc5, 0x58, 
+  0xf5, 0x00, 0x19, 0x9d, 0x95, 0xb6, 0xd3, 0xe3, 0x01, 0x75, 0x85, 0x86, 
+  0x28, 0x1d, 0xcd, 0x26
+};
+
 /* "abc" */
 static const uint8_t nist_1024_single[] = { /* 3 bytes */
   0x61, 0x62, 0x63
@@ -112,6 +176,34 @@ static const uint8_t sha512_single_digest[] = { /* 64 bytes */
   0xa5, 0x4c, 0xa4, 0x9f
 };
 
+static const uint8_t sha3_224_single_digest[] = { /* 28 bytes */
+  0xe6, 0x42, 0x82, 0x4c, 0x3f, 0x8c, 0xf2, 0x4a, 0xd0, 0x92, 0x34, 0xee, 
+  0x7d, 0x3c, 0x76, 0x6f, 0xc9, 0xa3, 0xa5, 0x16, 0x8d, 0x0c, 0x94, 0xad, 
+  0x73, 0xb4, 0x6f, 0xdf
+};
+
+static const uint8_t sha3_256_single_digest[] = { /* 32 bytes */
+  0x3a, 0x98, 0x5d, 0xa7, 0x4f, 0xe2, 0x25, 0xb2, 0x04, 0x5c, 0x17, 0x2d, 
+  0x6b, 0xd3, 0x90, 0xbd, 0x85, 0x5f, 0x08, 0x6e, 0x3e, 0x9d, 0x52, 0x5b, 
+  0x46, 0xbf, 0xe2, 0x45, 0x11, 0x43, 0x15, 0x32
+};
+
+static const uint8_t sha3_384_single_digest[] = { /* 48 bytes */
+  0xec, 0x01, 0x49, 0x82, 0x88, 0x51, 0x6f, 0xc9, 0x26, 0x45, 0x9f, 0x58, 
+  0xe2, 0xc6, 0xad, 0x8d, 0xf9, 0xb4, 0x73, 0xcb, 0x0f, 0xc0, 0x8c, 0x25, 
+  0x96, 0xda, 0x7c, 0xf0, 0xe4, 0x9b, 0xe4, 0xb2, 0x98, 0xd8, 0x8c, 0xea, 
+  0x92, 0x7a, 0xc7, 0xf5, 0x39, 0xf1, 0xed, 0xf2, 0x28, 0x37, 0x6d, 0x25
+};
+
+static const uint8_t sha3_512_single_digest[] = { /* 64 bytes */
+  0xb7, 0x51, 0x85, 0x0b, 0x1a, 0x57, 0x16, 0x8a, 0x56, 0x93, 0xcd, 0x92, 
+  0x4b, 0x6b, 0x09, 0x6e, 0x08, 0xf6, 0x21, 0x82, 0x74, 0x44, 0xf7, 0x0d, 
+  0x88, 0x4f, 0x5d, 0x02, 0x40, 0xd2, 0x71, 0x2e, 0x10, 0xe1, 0x16, 0xe9, 
+  0x19, 0x2a, 0xf3, 0xc9, 0x1a, 0x7e, 0xc5, 0x76, 0x47, 0xe3, 0x93, 0x40, 
+  0x57, 0x34, 0x0b, 0x4c, 0xf4, 0x08, 0xd5, 0xa5, 0x65, 0x92, 0xf8, 0x27, 
+  0x4e, 0xec, 0x53, 0xf0
+};
+
 /* "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
    "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" */
 static const uint8_t nist_1024_double[] = { /* 112 bytes */
@@ -155,6 +247,34 @@ static const uint8_t sha512_double_digest[] = { /* 64 bytes */
   0x87, 0x4b, 0xe9, 0x09
 };
 
+static const uint8_t sha3_224_double_digest[] = { /* 28 bytes */
+  0x54, 0x3e, 0x68, 0x68, 0xe1, 0x66, 0x6c, 0x1a, 0x64, 0x36, 0x30, 0xdf, 
+  0x77, 0x36, 0x7a, 0xe5, 0xa6, 0x2a, 0x85, 0x07, 0x0a, 0x51, 0xc1, 0x4c, 
+  0xbf, 0x66, 0x5c, 0xbc
+};
+
+static const uint8_t sha3_256_double_digest[] = { /* 32 bytes */
+  0x91, 0x6f, 0x60, 0x61, 0xfe, 0x87, 0x97, 0x41, 0xca, 0x64, 0x69, 0xb4, 
+  0x39, 0x71, 0xdf, 0xdb, 0x28, 0xb1, 0xa3, 0x2d, 0xc3, 0x6c, 0xb3, 0x25, 
+  0x4e, 0x81, 0x2b, 0xe2, 0x7a, 0xad, 0x1d, 0x18
+};
+
+static const uint8_t sha3_384_double_digest[] = { /* 48 bytes */
+  0x79, 0x40, 0x7d, 0x3b, 0x59, 0x16, 0xb5, 0x9c, 0x3e, 0x30, 0xb0, 0x98, 
+  0x22, 0x97, 0x47, 0x91, 0xc3, 0x13, 0xfb, 0x9e, 0xcc, 0x84, 0x9e, 0x40, 
+  0x6f, 0x23, 0x59, 0x2d, 0x04, 0xf6, 0x25, 0xdc, 0x8c, 0x70, 0x9b, 0x98, 
+  0xb4, 0x3b, 0x38, 0x52, 0xb3, 0x37, 0x21, 0x61, 0x79, 0xaa, 0x7f, 0xc7
+};
+
+static const uint8_t sha3_512_double_digest[] = { /* 64 bytes */
+  0xaf, 0xeb, 0xb2, 0xef, 0x54, 0x2e, 0x65, 0x79, 0xc5, 0x0c, 0xad, 0x06, 
+  0xd2, 0xe5, 0x78, 0xf9, 0xf8, 0xdd, 0x68, 0x81, 0xd7, 0xdc, 0x82, 0x4d, 
+  0x26, 0x36, 0x0f, 0xee, 0xbf, 0x18, 0xa4, 0xfa, 0x73, 0xe3, 0x26, 0x11, 
+  0x22, 0x94, 0x8e, 0xfc, 0xfd, 0x49, 0x2e, 0x74, 0xe8, 0x2e, 0x21, 0x89, 
+  0xed, 0x0f, 0xb4, 0x40, 0xd1, 0x87, 0xf3, 0x82, 0x27, 0x0c, 0xb4, 0x55, 
+  0xf2, 0x1d, 0xd1, 0x85
+};
+
 /* HMAC-SHA-1 test cases from RFC 2202. */
 
 static const uint8_t hmac_sha1_tc_1_key[] = { /* 20 bytes */
@@ -656,9 +776,11 @@ int main (int argc, char *argv[])
 
   ok &= hal_rpc_client_init();
 
+  ok &= test_hash(HAL_DIGEST_ALGORITHM_SHA1,   nist_512_empty, sha1_empty_digest, "SHA-1 empty block");
   ok &= test_hash(HAL_DIGEST_ALGORITHM_SHA1,   nist_512_single, sha1_single_digest, "SHA-1 single block");
   ok &= test_hash(HAL_DIGEST_ALGORITHM_SHA1,   nist_512_double, sha1_double_digest, "SHA-1 double block");
 
+  ok &= test_hash(HAL_DIGEST_ALGORITHM_SHA256, nist_512_empty, sha256_empty_digest, "SHA-256 empty block");
   ok &= test_hash(HAL_DIGEST_ALGORITHM_SHA256, nist_512_single, sha256_single_digest, "SHA-256 single block");
   ok &= test_hash(HAL_DIGEST_ALGORITHM_SHA256, nist_512_double, sha256_double_digest, "SHA-256 double block");
 
@@ -668,12 +790,30 @@ int main (int argc, char *argv[])
   ok &= test_hash(HAL_DIGEST_ALGORITHM_SHA512_256, nist_1024_single, sha512_256_single_digest, "SHA-512/256 single block");
   ok &= test_hash(HAL_DIGEST_ALGORITHM_SHA512_256, nist_1024_double, sha512_256_double_digest, "SHA-512/256 double block");
 
+  ok &= test_hash(HAL_DIGEST_ALGORITHM_SHA384, nist_1024_empty, sha384_empty_digest, "SHA-384 empty block");
   ok &= test_hash(HAL_DIGEST_ALGORITHM_SHA384, nist_1024_single, sha384_single_digest, "SHA-384 single block");
   ok &= test_hash(HAL_DIGEST_ALGORITHM_SHA384, nist_1024_double, sha384_double_digest, "SHA-384 double block");
 
+  ok &= test_hash(HAL_DIGEST_ALGORITHM_SHA512, nist_1024_empty, sha512_empty_digest, "SHA-512 empty block");
   ok &= test_hash(HAL_DIGEST_ALGORITHM_SHA512, nist_1024_single, sha512_single_digest, "SHA-512 single block");
   ok &= test_hash(HAL_DIGEST_ALGORITHM_SHA512, nist_1024_double, sha512_double_digest, "SHA-512 double block");
 
+  ok &= test_hash(HAL_DIGEST_ALGORITHM_SHA3_224, nist_1024_empty, sha3_224_empty_digest, "SHA3-224 empty block");
+  ok &= test_hash(HAL_DIGEST_ALGORITHM_SHA3_224, nist_1024_single, sha3_224_single_digest, "SHA3-224 single block");
+  ok &= test_hash(HAL_DIGEST_ALGORITHM_SHA3_224, nist_1024_double, sha3_224_double_digest, "SHA3-224 double block");
+
+  ok &= test_hash(HAL_DIGEST_ALGORITHM_SHA3_256, nist_1024_empty, sha3_256_empty_digest, "SHA3-256 empty block");
+  ok &= test_hash(HAL_DIGEST_ALGORITHM_SHA3_256, nist_1024_single, sha3_256_single_digest, "SHA3-256 single block");
+  ok &= test_hash(HAL_DIGEST_ALGORITHM_SHA3_256, nist_1024_double, sha3_256_double_digest, "SHA3-256 double block");
+
+  ok &= test_hash(HAL_DIGEST_ALGORITHM_SHA3_384, nist_1024_empty, sha3_384_empty_digest, "SHA3-384 empty block");
+  ok &= test_hash(HAL_DIGEST_ALGORITHM_SHA3_384, nist_1024_single, sha3_384_single_digest, "SHA3-384 single block");
+  ok &= test_hash(HAL_DIGEST_ALGORITHM_SHA3_384, nist_1024_double, sha3_384_double_digest, "SHA3-384 double block");
+
+  ok &= test_hash(HAL_DIGEST_ALGORITHM_SHA3_512, nist_1024_empty, sha3_512_empty_digest, "SHA3-512 empty block");
+  ok &= test_hash(HAL_DIGEST_ALGORITHM_SHA3_512, nist_1024_single, sha3_512_single_digest, "SHA3-512 single block");
+  ok &= test_hash(HAL_DIGEST_ALGORITHM_SHA3_512, nist_1024_double, sha3_512_double_digest, "SHA3-512 double block");
+
   ok &= test_hmac(HAL_DIGEST_ALGORITHM_SHA1, hmac_sha1_tc_1_key, hmac_sha1_tc_1_data, hmac_sha1_tc_1_result_sha1, "HMAC-SHA-1 test case 1");
   ok &= test_hmac(HAL_DIGEST_ALGORITHM_SHA1, hmac_sha1_tc_2_key, hmac_sha1_tc_2_data, hmac_sha1_tc_2_result_sha1, "HMAC-SHA-1 test case 2");
   ok &= test_hmac(HAL_DIGEST_ALGORITHM_SHA1, hmac_sha1_tc_3_key, hmac_sha1_tc_3_data, hmac_sha1_tc_3_result_sha1, "HMAC-SHA-1 test case 3");
diff --git a/verilog_constants.h b/verilog_constants.h
index 4588c20..8db5e29 100644
--- a/verilog_constants.h
+++ b/verilog_constants.h
@@ -90,6 +90,19 @@
 #define SHA512_MODE_SHA_512     (3 << 2)
 #define SHA512_MODE_MASK        (3 << 2)
 
+#define SHA3_ADDR_BLOCK         (0x80)
+#define SHA3_ADDR_DIGEST        (0xC0)
+#define SHA3_STATE_LEN          bitsToBytes(1600)
+#define SHA3_224_BLOCK_LEN      bitsToBytes(1152)
+#define SHA3_256_BLOCK_LEN      bitsToBytes(1088)
+#define SHA3_384_BLOCK_LEN      bitsToBytes(832)
+#define SHA3_512_BLOCK_LEN      bitsToBytes(576)
+#define SHA3_LENGTH_LEN         bitsToBytes(0)
+#define SHA3_224_DIGEST_LEN     bitsToBytes(224)
+#define SHA3_256_DIGEST_LEN     bitsToBytes(256)
+#define SHA3_384_DIGEST_LEN     bitsToBytes(384)
+#define SHA3_512_DIGEST_LEN     bitsToBytes(512)
+
 /*
  * RNG cores.
  */



More information about the Commits mailing list