Patch for adding sha384sum

Can Sun cansun at arista.com
Thu Nov 21 17:50:25 UTC 2024


Bump the patch and add cc to Denys Vlasenko.

Can

On Wed, Nov 6, 2024 at 10:22 AM Can Sun <cansun at arista.com> wrote:
>
> Hi BusyBox maintainers,
>
> We are looking at measuring system components and it will be ideal to
> have sha384sum in BusyBox, since certain chips do not support sha512 hashes.
> Here's a patch to add sha384sum applet and config.
> It is my first time making changes to BusyBox. Please let me know what
> you think about this patch.
>
> Thanks,
> Can
>
> ---
>  NOFORK_NOEXEC.lst               |  1 +
>  configs/TEST_nommu_defconfig    |  1 +
>  configs/TEST_noprintf_defconfig |  1 +
>  configs/TEST_rh9_defconfig      |  1 +
>  configs/android2_defconfig      |  1 +
>  configs/android_502_defconfig   |  1 +
>  configs/android_defconfig       |  1 +
>  configs/android_ndk_defconfig   |  1 +
>  configs/cygwin_defconfig        |  1 +
>  configs/freebsd_defconfig       |  1 +
>  coreutils/md5_sha1_sum.c        | 35 ++++++++++++++++++++++++++++++---
>  include/libbb.h                 |  1 +
>  libbb/hash_md5_sha.c            | 31 ++++++++++++++++++++++++++++-
>  testsuite/md5sum.tests          |  1 +
>  testsuite/sha384sum.tests       |  3 +++
>  15 files changed, 77 insertions(+), 4 deletions(-)
>  create mode 100755 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/configs/TEST_nommu_defconfig b/configs/TEST_nommu_defconfig
> index fa3e96326..7c5577ae8 100644
> --- a/configs/TEST_nommu_defconfig
> +++ b/configs/TEST_nommu_defconfig
> @@ -220,6 +220,7 @@ CONFIG_FEATURE_RMDIR_LONG_OPTIONS=y
>  CONFIG_SEQ=y
>  CONFIG_SHA1SUM=y
>  CONFIG_SHA256SUM=y
> +CONFIG_SHA384SUM=y
>  CONFIG_SHA512SUM=y
>  CONFIG_SLEEP=y
>  CONFIG_FEATURE_FANCY_SLEEP=y
> diff --git a/configs/TEST_noprintf_defconfig b/configs/TEST_noprintf_defconfig
> index 9b378fd55..91581b6e3 100644
> --- a/configs/TEST_noprintf_defconfig
> +++ b/configs/TEST_noprintf_defconfig
> @@ -242,6 +242,7 @@ CONFIG_FALSE=y
>  # CONFIG_SEQ is not set
>  # CONFIG_SHA1SUM is not set
>  # CONFIG_SHA256SUM is not set
> +# CONFIG_SHA384SUM is not set
>  # CONFIG_SHA512SUM is not set
>  # CONFIG_SLEEP is not set
>  # CONFIG_FEATURE_FANCY_SLEEP is not set
> diff --git a/configs/TEST_rh9_defconfig b/configs/TEST_rh9_defconfig
> index 4ac62b9da..c85e0c1d9 100644
> --- a/configs/TEST_rh9_defconfig
> +++ b/configs/TEST_rh9_defconfig
> @@ -231,6 +231,7 @@ CONFIG_FEATURE_RMDIR_LONG_OPTIONS=y
>  CONFIG_SEQ=y
>  CONFIG_SHA1SUM=y
>  CONFIG_SHA256SUM=y
> +CONFIG_SHA384SUM=y
>  CONFIG_SHA512SUM=y
>  CONFIG_SLEEP=y
>  CONFIG_FEATURE_FANCY_SLEEP=y
> diff --git a/configs/android2_defconfig b/configs/android2_defconfig
> index d4b8f1616..b71c03d0e 100644
> --- a/configs/android2_defconfig
> +++ b/configs/android2_defconfig
> @@ -248,6 +248,7 @@ CONFIG_RMDIR=y
>  CONFIG_SEQ=y
>  CONFIG_SHA1SUM=y
>  CONFIG_SHA256SUM=y
> +CONFIG_SHA384SUM=y
>  CONFIG_SHA512SUM=y
>  CONFIG_SLEEP=y
>  CONFIG_FEATURE_FANCY_SLEEP=y
> diff --git a/configs/android_502_defconfig b/configs/android_502_defconfig
> index 104e70f23..6d172a04f 100644
> --- a/configs/android_502_defconfig
> +++ b/configs/android_502_defconfig
> @@ -344,6 +344,7 @@ CONFIG_FEATURE_RMDIR_LONG_OPTIONS=y
>  CONFIG_SEQ=y
>  CONFIG_SHA1SUM=y
>  CONFIG_SHA256SUM=y
> +CONFIG_SHA384SUM=y
>  CONFIG_SHA512SUM=y
>  CONFIG_SHA3SUM=y
>  CONFIG_SLEEP=y
> diff --git a/configs/android_defconfig b/configs/android_defconfig
> index 92a66048a..cddb30421 100644
> --- a/configs/android_defconfig
> +++ b/configs/android_defconfig
> @@ -272,6 +272,7 @@ CONFIG_RMDIR=y
>  CONFIG_SEQ=y
>  CONFIG_SHA1SUM=y
>  CONFIG_SHA256SUM=y
> +CONFIG_SHA384SUM=y
>  CONFIG_SHA512SUM=y
>  CONFIG_SLEEP=y
>  CONFIG_FEATURE_FANCY_SLEEP=y
> diff --git a/configs/android_ndk_defconfig b/configs/android_ndk_defconfig
> index 425593454..86af28a92 100644
> --- a/configs/android_ndk_defconfig
> +++ b/configs/android_ndk_defconfig
> @@ -269,6 +269,7 @@ CONFIG_RMDIR=y
>  CONFIG_SEQ=y
>  CONFIG_SHA1SUM=y
>  CONFIG_SHA256SUM=y
> +CONFIG_SHA384SUM=y
>  CONFIG_SHA512SUM=y
>  CONFIG_SHA3SUM=y
>  CONFIG_SLEEP=y
> diff --git a/configs/cygwin_defconfig b/configs/cygwin_defconfig
> index 61e2c2463..46f778eb2 100644
> --- a/configs/cygwin_defconfig
> +++ b/configs/cygwin_defconfig
> @@ -284,6 +284,7 @@ CONFIG_FEATURE_LS_COLOR_IS_DEFAULT=y
>  CONFIG_MD5SUM=y
>  CONFIG_SHA1SUM=y
>  CONFIG_SHA256SUM=y
> +CONFIG_SHA384SUM=y
>  CONFIG_SHA512SUM=y
>  CONFIG_SHA3SUM=y
>
> diff --git a/configs/freebsd_defconfig b/configs/freebsd_defconfig
> index 6cbd54895..8c7212b4c 100644
> --- a/configs/freebsd_defconfig
> +++ b/configs/freebsd_defconfig
> @@ -245,6 +245,7 @@ CONFIG_RMDIR=y
>  CONFIG_SEQ=y
>  CONFIG_SHA1SUM=y
>  CONFIG_SHA256SUM=y
> +CONFIG_SHA384SUM=y
>  CONFIG_SHA512SUM=y
>  CONFIG_SLEEP=y
>  CONFIG_FEATURE_FANCY_SLEEP=y
> diff --git a/coreutils/md5_sha1_sum.c b/coreutils/md5_sha1_sum.c
> index 978d328f1..70fe2fbf7 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,
> sha384sum, sha512sum, 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',
>  };
>
> @@ -178,6 +197,10 @@ static uint8_t *hash_file(unsigned char *in_buf,
> const char *filename, unsigned
>
>   hash_algo = applet_name[3];
>
> + if (applet_name[4] == HASH_SHA384) {
> + hash_algo = HASH_SHA384;
> + }
> +
>   /* figure specific hash algorithms */
>   if (ENABLE_MD5SUM && hash_algo == HASH_MD5) {
>   md5_begin(&context.md5);
> @@ -197,6 +220,12 @@ static uint8_t *hash_file(unsigned char *in_buf,
> const char *filename, unsigned
>   final = (void*)sha256_end;
>   hash_len = 32;
>   }
> + else if (ENABLE_SHA384SUM && hash_algo == HASH_SHA384) {
> + sha384_begin(&context.sha512);
> + update = (void*)sha512_hash;
> + final = (void*)sha512_end;
> + hash_len = 48;
> + }
>   else if (ENABLE_SHA512SUM && hash_algo == HASH_SHA512) {
>   sha512_begin(&context.sha512);
>   update = (void*)sha512_hash;
> diff --git a/include/libbb.h b/include/libbb.h
> index 01cdb1bdc..88ad6c69b 100644
> --- a/include/libbb.h
> +++ b/include/libbb.h
> @@ -2195,6 +2195,7 @@ unsigned sha1_end(sha1_ctx_t *ctx, void *resbuf)
> FAST_FUNC;
>  void sha256_begin(sha256_ctx_t *ctx) FAST_FUNC;
>  #define sha256_hash md5_hash
>  #define sha256_end  sha1_end
> +void sha384_begin(sha512_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;
> diff --git a/libbb/hash_md5_sha.c b/libbb/hash_md5_sha.c
> index 57a801459..28f28296e 100644
> --- a/libbb/hash_md5_sha.c
> +++ b/libbb/hash_md5_sha.c
> @@ -11,7 +11,8 @@
>  #define STR1(s) #s
>  #define STR(s) STR1(s)
>
> -#define NEED_SHA512 (ENABLE_SHA512SUM || ENABLE_USE_BB_CRYPT_SHA)
> +#define NEED_SHA384 (ENABLE_SHA384SUM)
> +#define NEED_SHA512 (ENABLE_SHA384SUM || ENABLE_SHA512SUM ||
> ENABLE_USE_BB_CRYPT_SHA)
>
>  #if ENABLE_SHA1_HWACCEL || ENABLE_SHA256_HWACCEL
>  # if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
> @@ -1215,6 +1216,20 @@ static const uint32_t init256[] ALIGN4 = {
>   0x1f83d9ab,
>   0x5be0cd19,
>  };
> +#if NEED_SHA384
> +static const uint64_t init384[] ALIGN4 = {
> + 0,
> + 0,
> + 0xcbbb9d5dc1059ed8,
> + 0x629a292a367cd507,
> + 0x9159015a3070dd17,
> + 0x152fecd8f70e5939,
> + 0x67332667ffc00b31,
> + 0x8eb44a8768581511,
> + 0xdb0c2e0d64f98fa7,
> + 0x47b5481dbefa4fa4,
> +};
> +#endif /* NEED_SHA384 */
>  #if NEED_SHA512
>  static const uint32_t init512_lo[] ALIGN4 = {
>   0,
> @@ -1255,6 +1270,20 @@ void FAST_FUNC sha256_begin(sha256_ctx_t *ctx)
>  #endif
>  }
>
> +#if NEED_SHA384
> +/* Initialize structure containing state of computation.
> +   (FIPS 180-2:5.3.3)  */
> +void FAST_FUNC sha384_begin(sha512_ctx_t *ctx)
> +{
> + int i;
> + /* Two extra iterations zero out ctx->total64[2] */
> + uint64_t *tp = ctx->total64;
> + for (i = 0; i < 8 + 2; i++)
> + tp[i] = init384[i];
> + /*ctx->total64[0] = ctx->total64[1] = 0; - already done */
> +}
> +#endif /* NEED_SHA384 */
> +
>  #if NEED_SHA512
>  /* Initialize structure containing state of computation.
>     (FIPS 180-2:5.3.3)  */
> 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 100755
> index 000000000..f1449d195
> --- /dev/null
> +++ b/testsuite/sha384sum.tests
> @@ -0,0 +1,3 @@
> +#!/bin/sh
> +
> +. ./md5sum.tests sha384sum
> c01420bb6613d4bd00396a82033e59e81486cbb045ae0dd2b4c3332e581b3ce09fb1946d6e283acec685778ff205d485
> --
> 2.45.2


More information about the busybox mailing list