libbb: code shrink in des encryption, in setup_salt()

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@googlemail.com>
This commit is contained in:
Denys Vlasenko 2021-11-28 12:21:23 +01:00
parent cfb615781d
commit 00b5051cd2
2 changed files with 28 additions and 15 deletions

View File

@ -363,7 +363,7 @@ des_init(struct des_ctx *ctx, const struct const_des_ctx *cctx)
old_rawkey0 = old_rawkey1 = 0; old_rawkey0 = old_rawkey1 = 0;
old_salt = 0; old_salt = 0;
#endif #endif
saltbits = 0; //saltbits = 0; /* not needed: we call setup_salt() before do_des() */
bits28 = bits32 + 4; bits28 = bits32 + 4;
bits24 = bits28 + 4; bits24 = bits28 + 4;
@ -481,12 +481,11 @@ des_init(struct des_ctx *ctx, const struct const_des_ctx *cctx)
return ctx; return ctx;
} }
/* Accepts 24-bit salt at max */
static void static void
setup_salt(struct des_ctx *ctx, uint32_t salt) setup_salt(struct des_ctx *ctx, uint32_t salt)
{ {
uint32_t obit, saltbit; uint32_t invbits;
int i;
#if USE_REPETITIVE_SPEEDUP #if USE_REPETITIVE_SPEEDUP
if (salt == old_salt) if (salt == old_salt)
@ -494,15 +493,15 @@ setup_salt(struct des_ctx *ctx, uint32_t salt)
old_salt = salt; old_salt = salt;
#endif #endif
saltbits = 0; invbits = 0;
saltbit = 1;
obit = 0x800000; salt |= (1 << 24);
for (i = 0; i < 24; i++) { do {
if (salt & saltbit) invbits = (invbits << 1) + (salt & 1);
saltbits |= obit; salt >>= 1;
saltbit <<= 1; } while (salt != 1);
obit >>= 1;
} saltbits = invbits;
} }
static void static void
@ -736,14 +735,14 @@ des_crypt(struct des_ctx *ctx, char output[DES_OUT_BUFSIZE],
des_setkey(ctx, (char *)keybuf); 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 * key - up to 8 characters
*/ */
output[0] = salt_str[0]; output[0] = salt_str[0];
output[1] = salt_str[1]; output[1] = salt_str[1];
salt = (ascii_to_bin(salt_str[1]) << 6) salt = (ascii_to_bin(salt_str[1]) << 6)
| ascii_to_bin(salt_str[0]); | ascii_to_bin(salt_str[0]);
setup_salt(ctx, salt); setup_salt(ctx, salt); /* set ctx->saltbits for do_des() */
/* Do it. */ /* Do it. */
do_des(ctx, /*0, 0,*/ &r0, &r1, 25 /* count */); do_des(ctx, /*0, 0,*/ &r0, &r1, 25 /* count */);

View File

@ -7,6 +7,20 @@
# testing "description" "command" "result" "infile" "stdin" # 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 optional USE_BB_CRYPT_SHA
testing "cryptpw sha256" \ testing "cryptpw sha256" \
"cryptpw -m sha256 QWErty '123456789012345678901234567890'" \ "cryptpw -m sha256 QWErty '123456789012345678901234567890'" \