Welcome to little lamb

Code » limb » commit 5c9b41a

Add hashers for SHA1, SHA256 & SHA512

author Olivier Brunel
2023-03-26 15:25:31 UTC
committer Olivier Brunel
2023-03-26 15:46:50 UTC
parent d5c6161368c208cb5b30cd16da72f3a8228df637

Add hashers for SHA1, SHA256 & SHA512

doc/hasher_hash.3.md +19 -0
doc/hasher_sha1.h.0.md +21 -0
doc/hasher_sha256.h.0.md +21 -0
doc/hasher_sha512.h.0.md +21 -0
include/limb/hasher_sha1.h +12 -0
include/limb/hasher_sha256.h +12 -0
include/limb/hasher_sha512.h +12 -0
meta/libs/limb +10 -8
src/hasher_sha1.c +38 -0
src/hasher_sha256.c +38 -0
src/hasher_sha512.c +38 -0

diff --git a/doc/hasher_hash.3.md b/doc/hasher_hash.3.md
index b015286..a6d5f74 100644
--- a/doc/hasher_hash.3.md
+++ b/doc/hasher_hash.3.md
@@ -85,6 +85,15 @@ The following hashers are available :
 : *blake3*
 :: Requires [hasher_blake3.h](0).
 
+: *sha1*
+:: Requires [hasher_sha1.h](0).
+
+: *sha256*
+:: Requires [hasher_sha256.h](0).
+
+: *sha512*
+:: Requires [hasher_sha512.h](0).
+
 : *sha3_224*
 :: Requires [hasher_sha3_224.h](0).
 
@@ -97,6 +106,16 @@ The following hashers are available :
 : *sha3_512*
 :: Requires [hasher_sha3_512.h](0).
 
+! NOTE:
+! The hashers for SHA1, SHA256 and SHA512 are wrappers around functions from
+! skalibs. If you want to use said functions directly, you'll need to include
+! the appropriate headers from skalibs - `skalibs/sha1.h`, `skalibs/sha256.h`
+! and `skalibs/sha512.h` respectively.
+!
+! Note that they do /not/ follow the same conventions as functions in limb,
+! meaning they take the context argument first instead of last, and some of the
+! arguments' types differ.
+
 # EXAMPLE
 
 To compute the SHA1 of a given message, and show it on *stdout*, one could do :
diff --git a/doc/hasher_sha1.h.0.md b/doc/hasher_sha1.h.0.md
new file mode 100644
index 0000000..f7ae6c2
--- /dev/null
+++ b/doc/hasher_sha1.h.0.md
@@ -0,0 +1,21 @@
+% limb manual
+% hasher_sha1.h(0)
+
+# NAME
+
+hasher\_sha1.h - hasher for SHA1
+
+# SYNOPSIS
+
+    #include <limb/hasher_sha1.h>
+
+# DESCRIPTION
+
+This header defines the hasher to compute the SHA1 of a given message.
+
+## Hashers
+
+The following hashers are defined :
+
+: *sha1*
+:: A hasher for SHA1. See [hasher_hash](3) for more.
diff --git a/doc/hasher_sha256.h.0.md b/doc/hasher_sha256.h.0.md
new file mode 100644
index 0000000..add43af
--- /dev/null
+++ b/doc/hasher_sha256.h.0.md
@@ -0,0 +1,21 @@
+% limb manual
+% hasher_sha256.h(0)
+
+# NAME
+
+hasher\_sha256.h - hasher for SHA256
+
+# SYNOPSIS
+
+    #include <limb/hasher_sha256.h>
+
+# DESCRIPTION
+
+This header defines the hasher to compute the SHA256 of a given message.
+
+## Hashers
+
+The following hashers are defined :
+
+: *sha256*
+:: A hasher for SHA256. See [hasher_hash](3) for more.
diff --git a/doc/hasher_sha512.h.0.md b/doc/hasher_sha512.h.0.md
new file mode 100644
index 0000000..9331da1
--- /dev/null
+++ b/doc/hasher_sha512.h.0.md
@@ -0,0 +1,21 @@
+% limb manual
+% hasher_sha512.h(0)
+
+# NAME
+
+hasher\_sha512.h - hasher for SHA512
+
+# SYNOPSIS
+
+    #include <limb/hasher_sha512.h>
+
+# DESCRIPTION
+
+This header defines the hasher to compute the SHA512 of a given message.
+
+## Hashers
+
+The following hashers are defined :
+
+: *sha512*
+:: A hasher for SHA512. See [hasher_hash](3) for more.
diff --git a/include/limb/hasher_sha1.h b/include/limb/hasher_sha1.h
new file mode 100644
index 0000000..3049c87
--- /dev/null
+++ b/include/limb/hasher_sha1.h
@@ -0,0 +1,12 @@
+/* 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 */
+#ifndef LIMB_HASHER_SHA1_H
+#define LIMB_HASHER_SHA1_H
+
+#include "limb/hasher.h"
+
+extern struct hsha1 hasher_sha1;
+#define sha1 ((hasher *) (&hasher_sha1))
+
+#endif /* LIMB_HASHER_SHA1_H */
diff --git a/include/limb/hasher_sha256.h b/include/limb/hasher_sha256.h
new file mode 100644
index 0000000..3983caa
--- /dev/null
+++ b/include/limb/hasher_sha256.h
@@ -0,0 +1,12 @@
+/* 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 */
+#ifndef LIMB_HASHER_SHA256_H
+#define LIMB_HASHER_SHA256_H
+
+#include "limb/hasher.h"
+
+extern struct hsha256 hasher_sha256;
+#define sha256 ((hasher *) (&hasher_sha256))
+
+#endif /* LIMB_HASHER_SHA256_H */
diff --git a/include/limb/hasher_sha512.h b/include/limb/hasher_sha512.h
new file mode 100644
index 0000000..184937a
--- /dev/null
+++ b/include/limb/hasher_sha512.h
@@ -0,0 +1,12 @@
+/* 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 */
+#ifndef LIMB_HASHER_SHA512_H
+#define LIMB_HASHER_SHA512_H
+
+#include "limb/hasher.h"
+
+extern struct hsha512 hasher_sha512;
+#define sha512 ((hasher *) (&hasher_sha512))
+
+#endif /* LIMB_HASHER_SHA512_H */
diff --git a/meta/libs/limb b/meta/libs/limb
index b5ac2e3..d57a6eb 100644
--- a/meta/libs/limb
+++ b/meta/libs/limb
@@ -85,8 +85,6 @@ obj/hmap/hmap_init.o
 obj/hmap/hmap_set.o
 obj/hmap/hmap_get.o
 obj/hmap/hmap_free.o
-# hasher.h
-obj/hasher_hash.o
 # SHA3
 obj/sha3/byte_order.o
 obj/sha3/rhash_sha3_process_block.o
@@ -97,17 +95,21 @@ obj/sha3/sha3_384_init.o
 obj/sha3/sha3_512_init.o
 obj/sha3/sha3_update.o
 obj/sha3/sha3_final.o
-# hasher SHA3
-obj/hasher_sha3_224.o
-obj/hasher_sha3_256.o
-obj/hasher_sha3_384.o
-obj/hasher_sha3_512.o
 # BLAKE3
 obj/blake3/blake3_impl.o
 obj/blake3/blake3_dispatch.o
 obj/blake3/blake3_portable.o
 $$(BLAKE3_OPTIMIZ)
-# hasher blake3
+# hasher.h
+obj/hasher_hash.o
+# hashers
 obj/hasher_blake3.o
+obj/hasher_sha1.o
+obj/hasher_sha256.o
+obj/hasher_sha512.o
+obj/hasher_sha3_224.o
+obj/hasher_sha3_256.o
+obj/hasher_sha3_384.o
+obj/hasher_sha3_512.o
 # skalibs dependency
 skalibs
diff --git a/src/hasher_sha1.c b/src/hasher_sha1.c
new file mode 100644
index 0000000..81fc677
--- /dev/null
+++ b/src/hasher_sha1.c
@@ -0,0 +1,38 @@
+/* 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 <skalibs/sha1.h>
+#include "limb/hasher_sha1.h"
+
+static void hsha1_init(void *ctx);
+static void hsha1_update(const void *msg, size_t mlen, void *ctx);
+static void hsha1_final(void *md, void *ctx);
+
+struct hsha1 {
+    hasher h;
+    SHA1Schedule ctx;
+} hasher_sha1 = {
+    .h.hlen = 20,
+    .h.blen = 64,
+    .h.init = hsha1_init,
+    .h.update = hsha1_update,
+    .h.final = hsha1_final,
+};
+
+static void
+hsha1_init(void *ctx)
+{
+    sha1_init(ctx);
+}
+
+static void
+hsha1_update(const void *msg, size_t mlen, void *ctx)
+{
+    sha1_update(ctx, msg, mlen);
+}
+
+static void
+hsha1_final(void *md, void *ctx)
+{
+    sha1_final(ctx, md);
+}
diff --git a/src/hasher_sha256.c b/src/hasher_sha256.c
new file mode 100644
index 0000000..d12440b
--- /dev/null
+++ b/src/hasher_sha256.c
@@ -0,0 +1,38 @@
+/* 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 <skalibs/sha256.h>
+#include "limb/hasher_sha256.h"
+
+static void hsha256_init(void *ctx);
+static void hsha256_update(const void *msg, size_t mlen, void *ctx);
+static void hsha256_final(void *md, void *ctx);
+
+struct hsha256 {
+    hasher h;
+    SHA256Schedule ctx;
+} hasher_sha256 = {
+    .h.hlen = 32,
+    .h.blen = 64,
+    .h.init = hsha256_init,
+    .h.update = hsha256_update,
+    .h.final = hsha256_final,
+};
+
+static void
+hsha256_init(void *ctx)
+{
+    sha256_init(ctx);
+}
+
+static void
+hsha256_update(const void *msg, size_t mlen, void *ctx)
+{
+    sha256_update(ctx, msg, mlen);
+}
+
+static void
+hsha256_final(void *md, void *ctx)
+{
+    sha256_final(ctx, md);
+}
diff --git a/src/hasher_sha512.c b/src/hasher_sha512.c
new file mode 100644
index 0000000..e8ac4ea
--- /dev/null
+++ b/src/hasher_sha512.c
@@ -0,0 +1,38 @@
+/* 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 <skalibs/sha512.h>
+#include "limb/hasher_sha512.h"
+
+static void hsha512_init(void *ctx);
+static void hsha512_update(const void *msg, size_t mlen, void *ctx);
+static void hsha512_final(void *md, void *ctx);
+
+struct hsha512 {
+    hasher h;
+    SHA512Schedule ctx;
+} hasher_sha512 = {
+    .h.hlen = 64,
+    .h.blen = 128,
+    .h.init = hsha512_init,
+    .h.update = hsha512_update,
+    .h.final = hsha512_final,
+};
+
+static void
+hsha512_init(void *ctx)
+{
+    sha512_init(ctx);
+}
+
+static void
+hsha512_update(const void *msg, size_t mlen, void *ctx)
+{
+    sha512_update(ctx, msg, mlen);
+}
+
+static void
+hsha512_final(void *md, void *ctx)
+{
+    sha512_final(ctx, md);
+}