[git commit] libbb/yescrypt: code shrink

Denys Vlasenko vda.linux at googlemail.com
Sun Jul 6 09:12:13 UTC 2025


commit: https://git.busybox.net/busybox/commit/?id=0893bc3bac8705b22679ad77f39ee56d3ba728c9
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

static.PBKDF2_SHA256                                   -     189    +189
HMAC_SHA256_Init                                       -     159    +159
HMAC_SHA256_Buf                                        -      58     +58
HMAC_SHA256_Final                                      -      53     +53
i2a64                                                  -      42     +42
yescrypt_r                                          1221    1215      -6
yescrypt_kdf32_body                                 1064    1046     -18
i64c                                                  42       -     -42
libcperciva_HMAC_SHA256_Final                         53       -     -53
libcperciva_HMAC_SHA256_Buf                           58       -     -58
ascii64                                               65       -     -65
libcperciva_HMAC_SHA256_Init                         159       -    -159
PBKDF2_SHA256                                        386       -    -386
------------------------------------------------------------------------------
(add/remove: 5/6 grow/shrink: 0/2 up/down: 501/-787)         Total: -286 bytes

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 include/libbb.h                      |  2 ++
 libbb/pw_encrypt.c                   | 12 +++++-------
 libbb/pw_encrypt_des.c               |  8 ++++----
 libbb/yescrypt/alg-sha256.c          |  2 +-
 libbb/yescrypt/alg-sha256.h          | 18 ------------------
 libbb/yescrypt/alg-yescrypt-common.c |  2 +-
 libbb/yescrypt/y.c                   |  6 ------
 7 files changed, 13 insertions(+), 37 deletions(-)

diff --git a/include/libbb.h b/include/libbb.h
index 270a9d593..e88499a80 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -2189,6 +2189,8 @@ char *decode_base64(char *dst, const char **pp_src) FAST_FUNC;
 char *decode_base32(char *dst, const char **pp_src) FAST_FUNC;
 void read_base64(FILE *src_stream, FILE *dst_stream, int flags) FAST_FUNC;
 
+int FAST_FUNC i2a64(int i);
+
 typedef struct md5_ctx_t {
 	uint8_t wbuffer[64]; /* always correctly aligned for uint64_t */
 	void (*process_block)(struct md5_ctx_t*) FAST_FUNC;
diff --git a/libbb/pw_encrypt.c b/libbb/pw_encrypt.c
index 1d530974e..97dee7229 100644
--- a/libbb/pw_encrypt.c
+++ b/libbb/pw_encrypt.c
@@ -13,11 +13,10 @@
 #endif
 #include "libbb.h"
 
-/* static const uint8_t ascii64[] ALIGN1 =
+/* 0..63 ->
  * "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  */
-
-static int i64c(int i)
+int FAST_FUNC i2a64(int i)
 {
 	i &= 0x3f;
 	if (i == 0)
@@ -45,8 +44,8 @@ int FAST_FUNC crypt_make_rand64encoded(char *p, int cnt /*, int x */)
 		 * It has no problem with visibly alternating lowest bit
 		 * but is also weak in cryptographic sense + needs div,
 		 * which needs more code (and slower) on many CPUs */
-		*p++ = i64c(x >> 16);
-		*p++ = i64c(x >> 22);
+		*p++ = i2a64(x >> 16);
+		*p++ = i2a64(x >> 22);
 	} while (--cnt);
 	*p = '\0';
 	return x;
@@ -120,8 +119,7 @@ static char*
 to64(char *s, unsigned v, int n)
 {
 	while (--n >= 0) {
-		/* *s++ = ascii64[v & 0x3f]; */
-		*s++ = i64c(v);
+		*s++ = i2a64(v);
 		v >>= 6;
 	}
 	return s;
diff --git a/libbb/pw_encrypt_des.c b/libbb/pw_encrypt_des.c
index fe8237cfe..c836ab684 100644
--- a/libbb/pw_encrypt_des.c
+++ b/libbb/pw_encrypt_des.c
@@ -703,10 +703,10 @@ to64_msb_first(char *s, unsigned v)
 	*s++ = ascii64[(v >> 6) & 0x3f]; /* bits 11..6 */
 	*s   = ascii64[v & 0x3f]; /* bits 5..0 */
 #endif
-	*s++ = i64c(v >> 18); /* bits 23..18 */
-	*s++ = i64c(v >> 12); /* bits 17..12 */
-	*s++ = i64c(v >> 6); /* bits 11..6 */
-	*s   = i64c(v); /* bits 5..0 */
+	*s++ = i2a64(v >> 18); /* bits 23..18 */
+	*s++ = i2a64(v >> 12); /* bits 17..12 */
+	*s++ = i2a64(v >> 6); /* bits 11..6 */
+	*s   = i2a64(v); /* bits 5..0 */
 }
 
 static char *
diff --git a/libbb/yescrypt/alg-sha256.c b/libbb/yescrypt/alg-sha256.c
index a1d4275e6..a17028b6b 100644
--- a/libbb/yescrypt/alg-sha256.c
+++ b/libbb/yescrypt/alg-sha256.c
@@ -107,7 +107,7 @@ HMAC_SHA256_Buf(const void *K, size_t Klen, const void *in, size_t len,
  * Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and
  * write the output to buf.  The value dkLen must be at most 32 * (2^32 - 1).
  */
-void
+static void
 PBKDF2_SHA256(const uint8_t *passwd, size_t passwdlen,
 		const uint8_t *salt, size_t saltlen,
 		uint64_t c, uint8_t *buf, size_t dkLen)
diff --git a/libbb/yescrypt/alg-sha256.h b/libbb/yescrypt/alg-sha256.h
index 6d2cc0a04..862f49dbe 100644
--- a/libbb/yescrypt/alg-sha256.h
+++ b/libbb/yescrypt/alg-sha256.h
@@ -24,26 +24,8 @@
  * SUCH DAMAGE.
  */
 
-/*
- * Use #defines in order to avoid namespace collisions with anyone else's
- * SHA256 code (e.g., the code in OpenSSL).
- */
-#define HMAC_SHA256_Init libcperciva_HMAC_SHA256_Init
-#define HMAC_SHA256_Update libcperciva_HMAC_SHA256_Update
-#define HMAC_SHA256_Final libcperciva_HMAC_SHA256_Final
-#define HMAC_SHA256_Buf libcperciva_HMAC_SHA256_Buf
-#define HMAC_SHA256_CTX libcperciva_HMAC_SHA256_CTX
-
 /* Context structure for HMAC-SHA256 operations. */
 typedef struct {
 	sha256_ctx_t ictx;
 	sha256_ctx_t octx;
 } HMAC_SHA256_CTX;
-
-/**
- * PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
- * Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and
- * write the output to buf.  The value dkLen must be at most 32 * (2^32 - 1).
- */
-extern void PBKDF2_SHA256(const uint8_t *, size_t, const uint8_t *, size_t,
-    uint64_t, uint8_t *, size_t);
diff --git a/libbb/yescrypt/alg-yescrypt-common.c b/libbb/yescrypt/alg-yescrypt-common.c
index da7fa5e0f..7a1e92cab 100644
--- a/libbb/yescrypt/alg-yescrypt-common.c
+++ b/libbb/yescrypt/alg-yescrypt-common.c
@@ -88,7 +88,7 @@ static uint8_t *encode64_uint32_fixed(
 	for (bits = 0; bits < srcbits; bits += 6) {
 		if (dstlen < 2)
 			return NULL;
-		*dst++ = itoa64[src & 0x3f];
+		*dst++ = i2a64(src);
 		dstlen--;
 		src >>= 6;
 	}
diff --git a/libbb/yescrypt/y.c b/libbb/yescrypt/y.c
index 042c439a0..2c6afd4f8 100644
--- a/libbb/yescrypt/y.c
+++ b/libbb/yescrypt/y.c
@@ -124,12 +124,6 @@ VECTOR_TO_CPU(be,32);
 VECTOR_TO_CPU(be,64);
 
 
-const unsigned char ascii64[65] =
-  "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
-/* 0000000000111111111122222222223333333333444444444455555555556666 */
-/* 0123456789012345678901234567890123456789012345678901234567890123 */
-#define itoa64 ascii64
-
 #define YESCRYPT_INTERNAL
 #include "alg-sha256.h"
 #include "alg-yescrypt.h"


More information about the busybox-cvs mailing list