From 3a09aa9a2e5dc49cf7d9828be1692258c3b34a64 Mon Sep 17 00:00:00 2001
From: Andy Knowles <aknowles@galleonec.com>
Date: Wed, 21 May 2025 15:57:47 +0200
Subject: [PATCH] Add sha384sum applet

Signed-off-by: Andy Knowles <aknowles@galleonec.com>
---
 NOFORK_NOEXEC.lst         |  1 +
 coreutils/md5_sha1_sum.c  | 40 ++++++++++++++++++++++++++++++++++-----
 include/libbb.h           |  5 +++++
 libbb/hash_md5_sha.c      | 35 ++++++++++++++++++++++++++--------
 testsuite/md5sum.tests    |  1 +
 testsuite/sha384sum.tests |  3 +++
 6 files changed, 72 insertions(+), 13 deletions(-)
 create mode 100644 testsuite/sha384sum.tests

diff --git a/NOFORK_NOEXEC.lst b/NOFORK_NOEXEC.lst
index 055f9fb24..a000de45b 100644
--- a/NOFORK_NOEXEC.lst
+++ b/NOFORK_NOEXEC.lst
@@ -336,6 +336,7 @@ setuidgid - noexec. spawner
 sha1sum - noexec. runner
 sha256sum - noexec. runner
 sha3sum - noexec. runner
+sha384sum - noexec. runner
 sha512sum - noexec. runner
 showkey - interactive, longterm
 shred - runner
diff --git a/coreutils/md5_sha1_sum.c b/coreutils/md5_sha1_sum.c
index 978d328f1..f2909d5b1 100644
--- a/coreutils/md5_sha1_sum.c
+++ b/coreutils/md5_sha1_sum.c
@@ -23,6 +23,12 @@
 //config:	help
 //config:	Compute and check SHA256 message digest
 //config:
+//config:config SHA384SUM
+//config:	bool "sha384sum (7.3 kb)"
+//config:	default y
+//config:	help
+//config:	Compute and check SHA384 message digest
+//config:
 //config:config SHA512SUM
 //config:	bool "sha512sum (7.3 kb)"
 //config:	default y
@@ -35,13 +41,13 @@
 //config:	help
 //config:	Compute and check SHA3 message digest
 //config:
-//config:comment "Common options for md5sum, sha1sum, sha256sum, sha512sum, sha3sum"
-//config:	depends on MD5SUM || SHA1SUM || SHA256SUM || SHA512SUM || SHA3SUM
+//config:comment "Common options for md5sum, sha1sum, sha256sum, ..., sha3sum"
+//config:	depends on MD5SUM || SHA1SUM || SHA256SUM || SHA384SUM || SHA512SUM || SHA3SUM
 //config:
 //config:config FEATURE_MD5_SHA1_SUM_CHECK
 //config:	bool "Enable -c, -s and -w options"
 //config:	default y
-//config:	depends on MD5SUM || SHA1SUM || SHA256SUM || SHA512SUM || SHA3SUM
+//config:	depends on MD5SUM || SHA1SUM || SHA256SUM || SHA384SUM || SHA512SUM || SHA3SUM
 //config:	help
 //config:	Enabling the -c options allows files to be checked
 //config:	against pre-calculated hash values.
@@ -51,11 +57,13 @@
 //applet:IF_SHA1SUM(APPLET_NOEXEC(sha1sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha1sum))
 //applet:IF_SHA3SUM(APPLET_NOEXEC(sha3sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha3sum))
 //applet:IF_SHA256SUM(APPLET_NOEXEC(sha256sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha256sum))
+//applet:IF_SHA384SUM(APPLET_NOEXEC(sha384sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha384sum))
 //applet:IF_SHA512SUM(APPLET_NOEXEC(sha512sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha512sum))
 
 //kbuild:lib-$(CONFIG_MD5SUM)    += md5_sha1_sum.o
 //kbuild:lib-$(CONFIG_SHA1SUM)   += md5_sha1_sum.o
 //kbuild:lib-$(CONFIG_SHA256SUM) += md5_sha1_sum.o
+//kbuild:lib-$(CONFIG_SHA384SUM) += md5_sha1_sum.o
 //kbuild:lib-$(CONFIG_SHA512SUM) += md5_sha1_sum.o
 //kbuild:lib-$(CONFIG_SHA3SUM)   += md5_sha1_sum.o
 
@@ -99,6 +107,16 @@
 //usage:     "\n	-w	Warn about improperly formatted checksum lines"
 //usage:	)
 //usage:
+//usage:#define sha384sum_trivial_usage
+//usage:	IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[FILE]..."
+//usage:#define sha384sum_full_usage "\n\n"
+//usage:       "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " SHA384 checksums"
+//usage:	IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
+//usage:     "\n	-c	Check sums against list in FILEs"
+//usage:     "\n	-s	Don't output anything, status code shows success"
+//usage:     "\n	-w	Warn about improperly formatted checksum lines"
+//usage:	)
+//usage:
 //usage:#define sha512sum_trivial_usage
 //usage:	IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[FILE]..."
 //usage:#define sha512sum_full_usage "\n\n"
@@ -134,6 +152,7 @@ enum {
 	HASH_SHA1 = '1',
 	HASH_SHA256 = '2',
 	HASH_SHA3 = '3',
+	HASH_SHA384 = '8', /* 5th letter... */
 	HASH_SHA512 = '5',
 };
 
@@ -162,6 +181,7 @@ static uint8_t *hash_file(unsigned char *in_buf, const char *filename, unsigned
 	union _ctx_ {
 		sha3_ctx_t sha3;
 		sha512_ctx_t sha512;
+		sha384_ctx_t sha384;
 		sha256_ctx_t sha256;
 		sha1_ctx_t sha1;
 		md5_ctx_t md5;
@@ -170,6 +190,7 @@ static uint8_t *hash_file(unsigned char *in_buf, const char *filename, unsigned
 	void FAST_FUNC (*update)(void*, const void*, size_t);
 	unsigned FAST_FUNC (*final)(void*, void*);
 	char hash_algo;
+	char hash_alg2;
 
 	src_fd = open_or_warn_stdin(filename);
 	if (src_fd < 0) {
@@ -177,6 +198,7 @@ static uint8_t *hash_file(unsigned char *in_buf, const char *filename, unsigned
 	}
 
 	hash_algo = applet_name[3];
+	hash_alg2 = applet_name[4];
 
 	/* figure specific hash algorithms */
 	if (ENABLE_MD5SUM && hash_algo == HASH_MD5) {
@@ -197,6 +219,14 @@ static uint8_t *hash_file(unsigned char *in_buf, const char *filename, unsigned
 		final = (void*)sha256_end;
 		hash_len = 32;
 	}
+	else if (ENABLE_SHA384SUM &&
+	         (!ENABLE_SHA3SUM || hash_alg2 == HASH_SHA384) &&
+	         (ENABLE_SHA3SUM || hash_algo == HASH_SHA3)) {
+		sha384_begin(&context.sha384);
+		update = (void*)sha384_hash;
+		final = (void*)sha384_end;
+		hash_len = 48;
+	}
 	else if (ENABLE_SHA512SUM && hash_algo == HASH_SHA512) {
 		sha512_begin(&context.sha512);
 		update = (void*)sha512_hash;
@@ -262,14 +292,14 @@ int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv)
 		/* -b "binary", -t "text" are ignored (shaNNNsum compat) */
 		/* -s and -w require -c */
 #if ENABLE_SHA3SUM
-		if (applet_name[3] == HASH_SHA3)
+		if (applet_name[3] == HASH_SHA3 && (!ENABLE_SHA384SUM || applet_name[4] != HASH_SHA384))
 			flags = getopt32(argv, "^" "scwbta:+" "\0" "s?c:w?c", &sha3_width);
 		else
 #endif
 			flags = getopt32(argv, "^" "scwbt" "\0" "s?c:w?c");
 	} else {
 #if ENABLE_SHA3SUM
-		if (applet_name[3] == HASH_SHA3)
+		if (applet_name[3] == HASH_SHA3 && (!ENABLE_SHA384SUM || applet_name[4] != HASH_SHA384))
 			getopt32(argv, "a:+", &sha3_width);
 		else
 #endif
diff --git a/include/libbb.h b/include/libbb.h
index 558f3c627..161b3afb9 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -2195,6 +2195,7 @@ typedef struct sha512_ctx_t {
 	uint64_t hash[8];
 	uint8_t wbuffer[128]; /* always correctly aligned for uint64_t */
 } sha512_ctx_t;
+typedef struct sha512_ctx_t sha384_ctx_t;
 typedef struct sha3_ctx_t {
 	uint64_t state[25];
 	unsigned bytes_queued;
@@ -2212,6 +2213,9 @@ void sha256_begin(sha256_ctx_t *ctx) FAST_FUNC;
 void sha512_begin(sha512_ctx_t *ctx) FAST_FUNC;
 void sha512_hash(sha512_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC;
 unsigned sha512_end(sha512_ctx_t *ctx, void *resbuf) FAST_FUNC;
+void sha384_begin(sha384_ctx_t *ctx) FAST_FUNC;
+#define sha384_hash sha512_hash
+#define sha384_end  sha512_end
 void sha3_begin(sha3_ctx_t *ctx) FAST_FUNC;
 void sha3_hash(sha3_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC;
 unsigned sha3_end(sha3_ctx_t *ctx, void *resbuf) FAST_FUNC;
@@ -2223,6 +2227,7 @@ enum {
 	MD5_OUTSIZE    = 16,
 	SHA1_OUTSIZE   = 20,
 	SHA256_OUTSIZE = 32,
+	SHA384_OUTSIZE = 48,
 	SHA512_OUTSIZE = 64,
 	SHA3_OUTSIZE   = 28,
 };
diff --git a/libbb/hash_md5_sha.c b/libbb/hash_md5_sha.c
index 75a61c32c..b18b4105a 100644
--- a/libbb/hash_md5_sha.c
+++ b/libbb/hash_md5_sha.c
@@ -11,7 +11,7 @@
 #define STR1(s) #s
 #define STR(s) STR1(s)
 
-#define NEED_SHA512 (ENABLE_SHA512SUM || ENABLE_USE_BB_CRYPT_SHA)
+#define NEED_SHA512 (ENABLE_SHA512SUM || ENABLE_SHA384SUM || ENABLE_USE_BB_CRYPT_SHA)
 
 #if ENABLE_SHA1_HWACCEL || ENABLE_SHA256_HWACCEL
 # if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
@@ -1032,7 +1032,7 @@ static const sha_K_int sha_K[] ALIGN8 = {
 	K(0x84c87814a1f0ab72ULL), K(0x8cc702081a6439ecULL),
 	K(0x90befffa23631e28ULL), K(0xa4506cebde82bde9ULL),
 	K(0xbef9a3f7b2c67915ULL), K(0xc67178f2e372532bULL),
-#if NEED_SHA512  /* [64]+ are used for sha512 only */
+#if NEED_SHA512  /* [64]+ are used for sha384 and sha512 only */
 	K(0xca273eceea26619cULL), K(0xd186b8c721c0c207ULL),
 	K(0xeada7dd6cde0eb1eULL), K(0xf57d4f7fee6ed178ULL),
 	K(0x06f067aa72176fbaULL), K(0x0a637dc5a2c898a6ULL),
@@ -1229,11 +1229,20 @@ static const uint32_t init512_lo[] ALIGN4 = {
 	0x137e2179,
 };
 #endif /* NEED_SHA512 */
-
-// Note: SHA-384 is identical to SHA-512, except that initial hash values are
-// 0xcbbb9d5dc1059ed8, 0x629a292a367cd507, 0x9159015a3070dd17, 0x152fecd8f70e5939,
-// 0x67332667ffc00b31, 0x8eb44a8768581511, 0xdb0c2e0d64f98fa7, 0x47b5481dbefa4fa4,
-// and the output is constructed by omitting last two 64-bit words of it.
+#if ENABLE_SHA384SUM
+static const uint64_t init384[] ALIGN8 = {
+	0,
+	0,
+	0xcbbb9d5dc1059ed8,
+	0x629a292a367cd507,
+	0x9159015a3070dd17,
+	0x152fecd8f70e5939,
+	0x67332667ffc00b31,
+	0x8eb44a8768581511,
+	0xdb0c2e0d64f98fa7,
+	0x47b5481dbefa4fa4,
+};
+#endif
 
 /* Initialize structure containing state of computation.
    (FIPS 180-2:5.3.2)  */
@@ -1255,9 +1264,19 @@ void FAST_FUNC sha256_begin(sha256_ctx_t *ctx)
 #endif
 }
 
-#if NEED_SHA512
+#if ENABLE_SHA384SUM
 /* Initialize structure containing state of computation.
    (FIPS 180-2:5.3.3)  */
+void FAST_FUNC sha384_begin(sha512_ctx_t *ctx)
+{
+	memcpy(&ctx->total64, init384, sizeof(init384));
+	/*ctx->total64[0] = ctx->total64[1] = 0; - already done */
+}
+#endif
+
+#if NEED_SHA512
+/* Initialize structure containing state of computation.
+   (FIPS 180-2:5.3.4)  */
 void FAST_FUNC sha512_begin(sha512_ctx_t *ctx)
 {
 	int i;
diff --git a/testsuite/md5sum.tests b/testsuite/md5sum.tests
index cca26dc64..634dcd4aa 100755
--- a/testsuite/md5sum.tests
+++ b/testsuite/md5sum.tests
@@ -9,6 +9,7 @@
 # efe30c482e0b687e0cca0612f42ca29b
 # d41337e834377140ae7f98460d71d908598ef04f
 # 8e1d3ed57ebc130f0f72508446559eeae06451ae6d61b1e8ce46370cfb8963c3
+# c01420bb6613d4bd00396a82033e59e81486cbb045ae0dd2b4c3332e581b3ce09fb1946d6e283acec685778ff205d485
 # fe413e0f177324d1353893ca0772ceba83fd41512ba63895a0eebb703ef9feac2fb4e92b2cb430b3bda41b46b0cb4ea8307190a5cc795157cfb680a9cd635d0f
 
 if ! test "$1"; then
diff --git a/testsuite/sha384sum.tests b/testsuite/sha384sum.tests
new file mode 100644
index 000000000..f1449d195
--- /dev/null
+++ b/testsuite/sha384sum.tests
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+. ./md5sum.tests sha384sum c01420bb6613d4bd00396a82033e59e81486cbb045ae0dd2b4c3332e581b3ce09fb1946d6e283acec685778ff205d485
-- 
2.49.0.windows.1

