Welcome to little lamb

Code » limb » commit 2e3d09c

sha3: Tweak interface

author Olivier Brunel
2023-03-26 12:27:33 UTC
committer Olivier Brunel
2023-03-26 14:03:03 UTC
parent d780c0d2592af0e237be5af6fbbfb1fe7788371b

sha3: Tweak interface

doc/sha3.3.md +16 -16
doc/sha3.h.0.md +11 -6
include/limb/sha3.h +7 -6
include/sha3/sha3.h +1 -0
meta/libs/limb +4 -1
src/sha3/{sha3.c => sha3_224_init.c} +3 -5
src/sha3/sha3_256_init.c +10 -0
src/sha3/sha3_384_init.c +10 -0
src/sha3/sha3_512_init.c +10 -0
src/sha3/sha3_final.c +3 -1
src/sha3/sha3_init.c +3 -4
src/sha3/sha3_update.c +12 -10

diff --git a/doc/sha3.3.md b/doc/sha3.3.md
index fd7a06c..3f9aad9 100644
--- a/doc/sha3.3.md
+++ b/doc/sha3.3.md
@@ -5,35 +5,35 @@
 
 # NAME
 
-sha3\_init, sha3\_update, sha3\_final, sha3 - compute the SHA3 of a given block
-of data
+sha3\_224\_init, sha3\_256\_init, sha3\_384\_init, sha3\_512\_init,
+sha3\_update, sha3\_final, sha3 - compute the SHA3 of a given block of data
 
 # SYNOPSIS
 
     #include <limb/sha3.h>
 
 ```pre hl
-void sha3_init(unsigned <em>bits</em>, sha3_ctx *<em>ctx</em>);
-void sha3_update(const void *<em>msg</em>, size_t <em>size</em>, sha3_ctx *<em>ctx</em>);
-void sha3_final(unsigned char * restrict <em>md</em>, sha3_ctx *<em>ctx</em>);
-
-void sha3(unsigned char * restrict <em>md</em>, unsigned <em>bits</em>, const void *<em>msg</em>, size_t <em>size</em>);
+void sha3_224_init(void *<em>ctx</em>);
+void sha3_256_init(void *<em>ctx</em>);
+void sha3_384_init(void *<em>ctx</em>);
+void sha3_512_init(void *<em>ctx</em>);
+void sha3_update(const void *<em>msg</em>, size_t <em>size</em>, void *<em>ctx</em>);
+void sha3_final(void * restrict <em>md</em>, void *<em>ctx</em>);
 ```
 
 # DESCRIPTION
 
-The `sha3_init`() function initializes the given sha3 context `ctx` to calculate
-a digest of the specified number of bits. Commom values for `bits` are 512, 384,
-256 or 224.
+The `sha3_224_init`() function initializes the given sha3 context `ctx` to
+calculate a digest of 224 bits.
+
+The `sha3_256_init`(), `sha3_384_init`() and `sha3_512_init`() functions are
+similar, but initializing the conext for a SHA3 of 256, 384 and 512 bits
+respectively.
 
 The `sha3_update`() function feeds the specified chunk of data pointed by `msg`
 of length `size` (in bytes) to be hashed into the given `ctx`. You can call this
 function repeatedly as many times as needed.
 
 The `sha3_final`() function stores the calculated hash from `ctx` in binary form
-into `md`, which must be able to store as many bytes as needed : `bits / 8`,
-with `bits` taken from the `sha3_init`() call.
-
-As a convenience, the `sha3`() function allows to compute the SHA3 of a given
-`msg` of length `size` in one call. The digest will be stored in `md` and will
-be `bits / 8` bytes long.
+into `md`, which must be able to store as many bytes as needed: `bits / 8`,
+where `bits` corresponds to the `sha3_*_init`() function called.
diff --git a/doc/sha3.h.0.md b/doc/sha3.h.0.md
index 41a0755..7fc1d1b 100644
--- a/doc/sha3.h.0.md
+++ b/doc/sha3.h.0.md
@@ -27,15 +27,20 @@ The following types are defined :
 
 The following functions are defined :
 
-: [sha3_init](3)
-:: To initialize a sha3 context.
+: [sha3_224_init](3)
+:: To initialize a sha3-224 context.
+
+: [sha3_256_init](3)
+:: To initialize a sha3-256 context.
+
+: [sha3_384_init](3)
+:: To initialize a sha3-384 context.
+
+: [sha3_512_init](3)
+:: To initialize a sha3-512 context.
 
 : [sha3_update](3)
 :: To feed data into the sha3 context.
 
 : [sha3_final](3)
 :: To obtain the hash from a sha3 context.
-
-: [sha3](3)
-:: Convenience function to do all of the above at once, when the entire data is
-:: available in a single block.
diff --git a/include/limb/sha3.h b/include/limb/sha3.h
index 096f67a..5765f8f 100644
--- a/include/limb/sha3.h
+++ b/include/limb/sha3.h
@@ -4,7 +4,7 @@
 #ifndef LIMB_SHA3_H
 #define LIMB_SHA3_H
 
-#include <unistd.h>
+#include <stddef.h> /* size_t */
 #include "limb/int.h"
 
 #define sha3_max_permutation_size 25
@@ -23,10 +23,11 @@ struct sha3_ctx
 };
 typedef struct sha3_ctx sha3_ctx;
 
-extern void sha3_init(unsigned bits, sha3_ctx *ctx);
-extern void sha3_update(const void *msg, size_t size, sha3_ctx *ctx);
-extern void sha3_final(unsigned char * restrict md, sha3_ctx *ctx);
-
-extern void sha3(unsigned char * restrict md, unsigned bits, const void *msg, size_t size);
+extern void sha3_224_init(void *ctx);
+extern void sha3_256_init(void *ctx);
+extern void sha3_384_init(void *ctx);
+extern void sha3_512_init(void *ctx);
+extern void sha3_update(const void *msg, size_t mlen, void *ctx);
+extern void sha3_final(void * restrict md, void *ctx);
 
 #endif /* LIMB_SHA3_H */
diff --git a/include/sha3/sha3.h b/include/sha3/sha3.h
index bca8e42..308698e 100644
--- a/include/sha3/sha3.h
+++ b/include/sha3/sha3.h
@@ -11,6 +11,7 @@
 
 #define SHA3_FINALIZED 0x80000000
 
+void sha3_init(unsigned bits, sha3_ctx *ctx);
 void rhash_sha3_process_block(uint64_t hash[25], const uint64_t *block, size_t block_size);
 
 #endif /* LIMB_SHA3_SHA3_H */
diff --git a/meta/libs/limb b/meta/libs/limb
index 12a5ae3..b26bf93 100644
--- a/meta/libs/limb
+++ b/meta/libs/limb
@@ -89,9 +89,12 @@ obj/hmap/hmap_free.o
 obj/sha3/byte_order.o
 obj/sha3/rhash_sha3_process_block.o
 obj/sha3/sha3_init.o
+obj/sha3/sha3_224_init.o
+obj/sha3/sha3_256_init.o
+obj/sha3/sha3_384_init.o
+obj/sha3/sha3_512_init.o
 obj/sha3/sha3_update.o
 obj/sha3/sha3_final.o
-obj/sha3/sha3.o
 # BLAKE3
 obj/blake3/blake3_impl.o
 obj/blake3/blake3_dispatch.o
diff --git a/src/sha3/sha3.c b/src/sha3/sha3_224_init.c
similarity index 54%
rename from src/sha3/sha3.c
rename to src/sha3/sha3_224_init.c
index adee688..d0f1c1d 100644
--- a/src/sha3/sha3.c
+++ b/src/sha3/sha3_224_init.c
@@ -3,10 +3,8 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 #include "sha3/sha3.h"
 
-void sha3(unsigned char * restrict md, unsigned bits, const void *msg, size_t size)
+void
+sha3_224_init(void *ctx)
 {
-    sha3_ctx ctx;
-    sha3_init(bits, &ctx);
-    sha3_update(msg, size, &ctx);
-    sha3_final(md, &ctx);
+    sha3_init(224, ctx);
 }
diff --git a/src/sha3/sha3_256_init.c b/src/sha3/sha3_256_init.c
new file mode 100644
index 0000000..f9031a6
--- /dev/null
+++ b/src/sha3/sha3_256_init.c
@@ -0,0 +1,10 @@
+/* This file is part of limb                           https://lila.oss/limb
+ * Copyright (C) 2023 Olivier Brunel                          jjk@jjacky.com */
+/* SPDX-License-Identifier: GPL-2.0-only */
+#include "sha3/sha3.h"
+
+void
+sha3_256_init(void *ctx)
+{
+    sha3_init(256, ctx);
+}
diff --git a/src/sha3/sha3_384_init.c b/src/sha3/sha3_384_init.c
new file mode 100644
index 0000000..0e17a1f
--- /dev/null
+++ b/src/sha3/sha3_384_init.c
@@ -0,0 +1,10 @@
+/* This file is part of limb                           https://lila.oss/limb
+ * Copyright (C) 2023 Olivier Brunel                          jjk@jjacky.com */
+/* SPDX-License-Identifier: GPL-2.0-only */
+#include "sha3/sha3.h"
+
+void
+sha3_384_init(void *ctx)
+{
+    sha3_init(384, ctx);
+}
diff --git a/src/sha3/sha3_512_init.c b/src/sha3/sha3_512_init.c
new file mode 100644
index 0000000..f419425
--- /dev/null
+++ b/src/sha3/sha3_512_init.c
@@ -0,0 +1,10 @@
+/* This file is part of limb                           https://lila.oss/limb
+ * Copyright (C) 2023 Olivier Brunel                          jjk@jjacky.com */
+/* SPDX-License-Identifier: GPL-2.0-only */
+#include "sha3/sha3.h"
+
+void
+sha3_512_init(void *ctx)
+{
+    sha3_init(512, ctx);
+}
diff --git a/src/sha3/sha3_final.c b/src/sha3/sha3_final.c
index d77d206..cd2f794 100644
--- a/src/sha3/sha3_final.c
+++ b/src/sha3/sha3_final.c
@@ -17,8 +17,10 @@
  * @param md  calculated hash in binary form
  * @param ctx the algorithm context containing current hashing state
  */
-void sha3_final(unsigned char * restrict md, sha3_ctx *ctx)
+void
+sha3_final(void * restrict md, void *ctx_)
 {
+    sha3_ctx *ctx = ctx_;
     size_t digest_length = 100 - ctx->block_size / 2;
     const size_t block_size = ctx->block_size;
 
diff --git a/src/sha3/sha3_init.c b/src/sha3/sha3_init.c
index e0d5636..f9372a1 100644
--- a/src/sha3/sha3_init.c
+++ b/src/sha3/sha3_init.c
@@ -6,7 +6,6 @@
  * Copyright (c) 2013 Aleksey Kravchenko */
 /* SPDX-License-Identifier: 0BSD */
 
-#include <assert.h>
 #include <string.h>
 #include "sha3/sha3.h"
 
@@ -16,12 +15,12 @@
  * @param bits number of output bits
  * @param ctx  context to initialize
  */
-void sha3_init(unsigned bits, sha3_ctx *ctx)
+void
+sha3_init(unsigned bits, sha3_ctx *ctx)
 {
     /* NB: The Keccak capacity parameter = bits * 2 */
     unsigned rate = 1600 - bits * 2;
 
-    memset(ctx, 0, sizeof(sha3_ctx));
+    memset(ctx, 0, sizeof(*ctx));
     ctx->block_size = rate / 8;
-    assert(rate <= 1600 && (rate % 64) == 0);
 }
diff --git a/src/sha3/sha3_update.c b/src/sha3/sha3_update.c
index 1738e51..0246283 100644
--- a/src/sha3/sha3_update.c
+++ b/src/sha3/sha3_update.c
@@ -16,30 +16,32 @@
  * Can be called repeatedly with chunks of the message to be hashed.
  *
  * @param msg  message chunk
- * @param size length of the message chunk
+ * @param mlen length of the message chunk
  * @param ctx  the algorithm context containing current hashing state
  */
-void sha3_update(const void *msg_, size_t size, sha3_ctx *ctx)
+void
+sha3_update(const void *msg_, size_t mlen, void *ctx_)
 {
+    sha3_ctx *ctx = ctx_;
     const unsigned char *msg = msg_;
     size_t index = (size_t) ctx->rest;
     size_t block_size = (size_t) ctx->block_size;
 
     if (ctx->rest & SHA3_FINALIZED) return; /* too late for additional input */
-    ctx->rest = (unsigned) ((ctx->rest + size) % block_size);
+    ctx->rest = (unsigned) ((ctx->rest + mlen) % block_size);
 
     /* fill partial block */
     if (index) {
         size_t left = block_size - index;
-        memcpy((char*) ctx->message + index, msg, (size < left ? size : left));
-        if (size < left) return;
+        memcpy((char*) ctx->message + index, msg, (mlen < left ? mlen : left));
+        if (mlen < left) return;
 
         /* process partial block */
         rhash_sha3_process_block(ctx->hash, ctx->message, block_size);
         msg  += left;
-        size -= left;
+        mlen -= left;
     }
-    while (size >= block_size) {
+    while (mlen >= block_size) {
         u64 *aligned_message_block;
         if (IS_ALIGNED_64(msg)) {
             /* the most common case is processing of an already aligned message
@@ -52,8 +54,8 @@ void sha3_update(const void *msg_, size_t size, sha3_ctx *ctx)
 
         rhash_sha3_process_block(ctx->hash, aligned_message_block, block_size);
         msg  += block_size;
-        size -= block_size;
+        mlen -= block_size;
     }
-    if (size)
-        memcpy(ctx->message, msg, size); /* save leftovers */
+    if (mlen)
+        memcpy(ctx->message, msg, mlen); /* save leftovers */
 }