OpenSSL uses the bare names like `MD5` itself. We could add a lot
more #ifdefs instead, but it seems easier to just rename the toybox
constants. I did check, and thanks to -ffunction-sections and
-fdata-sections none of the built-in hash implementations end up in a
libcrypto-using toybox, so it doesn't seem like there would be any other
advantage to more #ifdefs.
---
 toys/lsb/md5sum.c | 36 +++++++++++++++++++-----------------
 1 file changed, 19 insertions(+), 17 deletions(-)
From b19b6b2ffd82cac403f96951b6725a326b210dff Mon Sep 17 00:00:00 2001
From: Elliott Hughes <e...@google.com>
Date: Thu, 3 Jun 2021 14:13:48 -0700
Subject: [PATCH] md5sum: fix libcrypto build.

OpenSSL uses the bare names like `MD5` itself. We could add a lot
more #ifdefs instead, but it seems easier to just rename the toybox
constants. I did check, and thanks to -ffunction-sections and
-fdata-sections none of the built-in hash implementations end up in a
libcrypto-using toybox, so it doesn't seem like there would be any other
advantage to more #ifdefs.
---
 toys/lsb/md5sum.c | 36 +++++++++++++++++++-----------------
 1 file changed, 19 insertions(+), 17 deletions(-)

diff --git a/toys/lsb/md5sum.c b/toys/lsb/md5sum.c
index ab4957d5..a4c1c8f4 100644
--- a/toys/lsb/md5sum.c
+++ b/toys/lsb/md5sum.c
@@ -88,7 +88,9 @@ typedef int SHA512_CTX;
 
 GLOBALS(
   int sawline;
-  enum hashmethods { MD5, SHA1, SHA224, SHA256, SHA384, SHA512 } hashmethod;
+  enum hashmethods {
+    HASH_MD5, HASH_SHA1, HASH_SHA224, HASH_SHA256, HASH_SHA384, HASH_SHA512
+  } hashmethod;
   unsigned *rconsttable32;
   unsigned long long *rconsttable64; // for sha384,sha512
 
@@ -397,17 +399,17 @@ static void do_builtin_hash(int fd, char *name)
   transform = (void *[]){md5_transform, sha1_transform, sha2_32_transform,
     sha2_32_transform, sha2_64_transform, sha2_64_transform}[TT.hashmethod];
   digestlen = (char []){16, 20, 28, 32, 48, 64}[TT.hashmethod];
-  chunksize = 64<<(TT.hashmethod>=SHA384);
-  if (TT.hashmethod<=SHA1)
+  chunksize = 64<<(TT.hashmethod>=HASH_SHA384);
+  if (TT.hashmethod<=HASH_SHA1)
     memcpy(TT.state.i32, (unsigned []){0x67452301, 0xEFCDAB89, 0x98BADCFE,
       0x10325476, 0xC3D2E1F0}, 20);
-  else if (TT.hashmethod==SHA224)
+  else if (TT.hashmethod==HASH_SHA224)
     memcpy(TT.state.i32, (unsigned []){0xc1059ed8, 0x367cd507, 0x3070dd17,
       0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4}, 32);
-  else if (TT.hashmethod==SHA256)
+  else if (TT.hashmethod==HASH_SHA256)
     memcpy(TT.state.i32, (unsigned []){0x6a09e667, 0xbb67ae85, 0x3c6ef372,
       0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19}, 32);
-  else if (TT.hashmethod==SHA384)
+  else if (TT.hashmethod==HASH_SHA384)
     memcpy(TT.state.i64, (unsigned long long []){0xcbbb9d5dc1059ed8,
       0x629a292a367cd507, 0x9159015a3070dd17, 0x152fecd8f70e5939,
       0x67332667ffc00b31, 0x8eb44a8768581511, 0xdb0c2e0d64f98fa7,
@@ -436,14 +438,14 @@ static void do_builtin_hash(int fd, char *name)
     hash_update(&buf, 1, transform, chunksize);
     buf = 0;
   } while ((TT.count & (chunksize - 1)) != (chunksize - 8));
-  count = (TT.hashmethod == MD5) ? SWAP_LE64(count) : SWAP_BE64(count);
+  count = (TT.hashmethod == HASH_MD5) ? SWAP_LE64(count) : SWAP_BE64(count);
   hash_update((void *)&count, 8, transform, chunksize);
 
   // write digest to toybuf
-  if (TT.hashmethod>=SHA384) for (i=0; i<digestlen/8; i++)
+  if (TT.hashmethod>=HASH_SHA384) for (i=0; i<digestlen/8; i++)
     sprintf(toybuf+16*i, "%016llx", TT.state.i64[i]);
   else for (i=0; i<digestlen/4; i++)
-    sprintf(toybuf+8*i, "%08x", (TT.hashmethod == MD5)
+    sprintf(toybuf+8*i, "%08x", (TT.hashmethod == HASH_MD5)
       ? bswap_32(TT.state.i32[i]) : TT.state.i32[i]);
   // Wipe variables. Cryptographer paranoia.
   i = sizeof(struct md5sum_data)-offsetof(struct md5sum_data, state.i64);
@@ -514,15 +516,15 @@ void md5sum_main(void)
   // Calculate table if we have floating point. Static version should drop
   // out at compile time when we don't need it.
   if (!CFG_TOYBOX_LIBCRYPTO) {
-    if (TT.hashmethod==MD5) {
+    if (TT.hashmethod==HASH_MD5) {
       if (CFG_TOYBOX_FLOAT) {
         TT.rconsttable32 = xmalloc(64*4);
         for (i = 0; i<64; i++) TT.rconsttable32[i] = fabs(sin(i+1))*(1LL<<32);
       } else TT.rconsttable32 = md5nofloat;
-    } else if (TT.hashmethod==SHA224 || TT.hashmethod==SHA256) {
+    } else if (TT.hashmethod==HASH_SHA224 || TT.hashmethod==HASH_SHA256) {
       TT.rconsttable32 = xmalloc(64*4);
       for (i=0; i<64; i++) TT.rconsttable32[i] = sha512nofloat[i] >> 32;
-    } else if (TT.hashmethod>=SHA384) TT.rconsttable64 = sha512nofloat;
+    } else if (TT.hashmethod>=HASH_SHA384) TT.rconsttable64 = sha512nofloat;
   }
 
   if (FLAG(c)) for (i = 0; toys.optargs[i]; i++) do_c_file(toys.optargs[i]);
@@ -534,30 +536,30 @@ void md5sum_main(void)
 
 void sha1sum_main(void)
 {
-  TT.hashmethod = SHA1;
+  TT.hashmethod = HASH_SHA1;
   md5sum_main();
 }
 
 void sha224sum_main(void)
 {
-  TT.hashmethod = SHA224;
+  TT.hashmethod = HASH_SHA224;
   md5sum_main();
 }
 
 void sha256sum_main(void)
 {
-  TT.hashmethod = SHA256;
+  TT.hashmethod = HASH_SHA256;
   md5sum_main();
 }
 
 void sha384sum_main(void)
 {
-  TT.hashmethod = SHA384;
+  TT.hashmethod = HASH_SHA384;
   md5sum_main();
 }
 
 void sha512sum_main(void)
 {
-  TT.hashmethod = SHA512;
+  TT.hashmethod = HASH_SHA512;
   md5sum_main();
 }
-- 
2.32.0.rc1.229.g3e70b5a671-goog

_______________________________________________
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to