commit 4325939f19827b660d38857434eb254e4e9653c4
parent ba577b46f19095c5c86057119e5b9e594f3f8e9b
Author: MikoĊaj Lenczewski <mikolaj@lenczewski.org>
Date: Wed, 16 Sep 2026 01:25:00 +0100
Add sha384 and sha512/256 implementations
Diffstat:
3 files changed, 78 insertions(+), 0 deletions(-)
diff --git a/sha.c b/sha.c
@@ -362,11 +362,13 @@ sha384_init(struct sha512_ctx *ctx)
void
sha384_push(struct sha512_ctx *ctx, void const *buf, size_t len)
{
+ sha512_push(ctx, buf, len);
}
void
sha384_dgst(struct sha512_ctx *ctx)
{
+ sha512_dgst(ctx);
}
void
@@ -399,11 +401,13 @@ sha512_256_init(struct sha512_ctx *ctx)
void
sha512_256_push(struct sha512_ctx *ctx, void const *buf, size_t len)
{
+ sha512_push(ctx, buf, len);
}
void
sha512_256_dgst(struct sha512_ctx *ctx)
{
+ sha512_dgst(ctx);
}
void
diff --git a/test-sha384.c b/test-sha384.c
@@ -0,0 +1,38 @@
+#include "sha.h"
+#include <stdio.h>
+
+int
+main(void)
+{
+ char msg[] = "abc";
+
+ uint8_t dgst[SHA384_DIGEST_SIZE];
+ sha384(msg, strlen(msg), dgst);
+
+ uint8_t expected[SHA384_DIGEST_SIZE] = {
+ 0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b,
+ 0xb5, 0xa0, 0x3d, 0x69, 0x9a, 0xc6, 0x50, 0x07,
+ 0x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63,
+ 0x1a, 0x8b, 0x60, 0x5a, 0x43, 0xff, 0x5b, 0xed,
+ 0x80, 0x86, 0x07, 0x2b, 0xa1, 0xe7, 0xcc, 0x23,
+ 0x58, 0xba, 0xec, 0xa1, 0x34, 0xc8, 0x25, 0xa7,
+ };
+
+ printf("msg: \"%.*s\" (%lu bytes)\n", sizeof msg, msg, strlen(msg));
+
+ printf("dgst: ");
+ for (size_t i = 0; i < sizeof dgst; i++) {
+ printf("%.2x", dgst[i]);
+ }
+ printf("\n");
+
+ printf("exp.: ");
+ for (size_t i = 0; i < sizeof expected; i++) {
+ printf("%.2x", expected[i]);
+ }
+ printf("\n");
+
+ return memcmp(expected, dgst, SHA384_DIGEST_SIZE);
+}
+
+#include "sha.c"
diff --git a/test-sha512-256.c b/test-sha512-256.c
@@ -0,0 +1,36 @@
+#include "sha.h"
+#include <stdio.h>
+
+int
+main(void)
+{
+ char msg[] = "abc";
+
+ uint8_t dgst[SHA512_256_DIGEST_SIZE];
+ sha512_256(msg, strlen(msg), dgst);
+
+ uint8_t expected[SHA512_256_DIGEST_SIZE] = {
+ 0x53, 0x04, 0x8e, 0x26, 0x81, 0x94, 0x1e, 0xf9,
+ 0x9b, 0x2e, 0x29, 0xb7, 0x6b, 0x4c, 0x7d, 0xab,
+ 0xe4, 0xc2, 0xd0, 0xc6, 0x34, 0xfc, 0x6d, 0x46,
+ 0xe0, 0xe2, 0xf1, 0x31, 0x07, 0xe7, 0xaf, 0x23,
+ };
+
+ printf("msg: \"%.*s\" (%lu bytes)\n", sizeof msg, msg, strlen(msg));
+
+ printf("dgst: ");
+ for (size_t i = 0; i < sizeof dgst; i++) {
+ printf("%.2x", dgst[i]);
+ }
+ printf("\n");
+
+ printf("exp.: ");
+ for (size_t i = 0; i < sizeof expected; i++) {
+ printf("%.2x", expected[i]);
+ }
+ printf("\n");
+
+ return memcmp(expected, dgst, SHA512_256_DIGEST_SIZE);
+}
+
+#include "sha.c"