[git commit] libbb: code shrink in des encryption, in setup_salt()

Denys Vlasenko vda.linux at googlemail.com
Sun Nov 28 11:21:23 UTC 2021


commit: https://git.busybox.net/busybox/commit/?id=00b5051cd25ef7e42ac62637ba16b70d3ac1014a
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
pw_encrypt                                           978     971      -7
.rodata                                           108208  108192     -16
des_crypt                                           1211    1181     -30
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/3 up/down: 0/-53)             Total: -53 bytes

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 libbb/pw_encrypt_des.c  | 29 ++++++++++++++---------------
 testsuite/cryptpw.tests | 14 ++++++++++++++
 2 files changed, 28 insertions(+), 15 deletions(-)

diff --git a/libbb/pw_encrypt_des.c b/libbb/pw_encrypt_des.c
index dcd3521e2..fe8237cfe 100644
--- a/libbb/pw_encrypt_des.c
+++ b/libbb/pw_encrypt_des.c
@@ -363,7 +363,7 @@ des_init(struct des_ctx *ctx, const struct const_des_ctx *cctx)
 	old_rawkey0 = old_rawkey1 = 0;
 	old_salt = 0;
 #endif
-	saltbits = 0;
+	//saltbits = 0; /* not needed: we call setup_salt() before do_des() */
 	bits28 = bits32 + 4;
 	bits24 = bits28 + 4;
 
@@ -481,12 +481,11 @@ des_init(struct des_ctx *ctx, const struct const_des_ctx *cctx)
 	return ctx;
 }
 
-
+/* Accepts 24-bit salt at max */
 static void
 setup_salt(struct des_ctx *ctx, uint32_t salt)
 {
-	uint32_t obit, saltbit;
-	int i;
+	uint32_t invbits;
 
 #if USE_REPETITIVE_SPEEDUP
 	if (salt == old_salt)
@@ -494,15 +493,15 @@ setup_salt(struct des_ctx *ctx, uint32_t salt)
 	old_salt = salt;
 #endif
 
-	saltbits = 0;
-	saltbit = 1;
-	obit = 0x800000;
-	for (i = 0; i < 24; i++) {
-		if (salt & saltbit)
-			saltbits |= obit;
-		saltbit <<= 1;
-		obit >>= 1;
-	}
+	invbits = 0;
+
+	salt |= (1 << 24);
+	do {
+		invbits = (invbits << 1) + (salt & 1);
+		salt >>= 1;
+	} while (salt != 1);
+
+	saltbits = invbits;
 }
 
 static void
@@ -736,14 +735,14 @@ des_crypt(struct des_ctx *ctx, char output[DES_OUT_BUFSIZE],
 	des_setkey(ctx, (char *)keybuf);
 
 	/*
-	 * salt_str - 2 bytes of salt
+	 * salt_str - 2 chars of salt (converted to 12 bits)
 	 * key - up to 8 characters
 	 */
 	output[0] = salt_str[0];
 	output[1] = salt_str[1];
 	salt = (ascii_to_bin(salt_str[1]) << 6)
 	     |  ascii_to_bin(salt_str[0]);
-	setup_salt(ctx, salt);
+	setup_salt(ctx, salt); /* set ctx->saltbits for do_des() */
 
 	/* Do it. */
 	do_des(ctx, /*0, 0,*/ &r0, &r1, 25 /* count */);
diff --git a/testsuite/cryptpw.tests b/testsuite/cryptpw.tests
index 8ec476c9f..0dd91fe15 100755
--- a/testsuite/cryptpw.tests
+++ b/testsuite/cryptpw.tests
@@ -7,6 +7,20 @@
 
 # testing "description" "command" "result" "infile" "stdin"
 
+#optional USE_BB_CRYPT
+testing "cryptpw des 12" \
+	"cryptpw -m des QWErty '123456789012345678901234567890'" \
+	'12MnB3PqfVbMA\n' "" ""
+
+testing "cryptpw des 55" \
+	"cryptpw -m des QWErty 55" \
+	'55tgFLtkT1Y72\n' "" ""
+
+testing "cryptpw des zz" \
+	"cryptpw -m des QWErty zz" \
+	'zzIZaaXWOkxVk\n' "" ""
+#SKIP=
+
 optional USE_BB_CRYPT_SHA
 testing "cryptpw sha256" \
 	"cryptpw -m sha256 QWErty '123456789012345678901234567890'" \


More information about the busybox-cvs mailing list