libbb/yescrypt: code shrink

function                                             old     new   delta
static.yescrypt_kdf32_body                           847     823     -24
yescrypt_r                                           805     767     -38
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-62)             Total: -62 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2025-07-09 10:38:11 +02:00
parent 11d4c08d75
commit 95f169f3bb
2 changed files with 28 additions and 15 deletions

View file

@ -23,8 +23,8 @@
#define decode64_uint32(dst, src, min) \
({ \
uint32_t d32 = a2i64(*(src)); \
if (d32 > 47) \
goto fail; \
if (d32 > 47) \
goto fail; \
*(dst) = d32 + (min); \
++src; \
})
@ -292,8 +292,12 @@ char *yescrypt_r(
const uint8_t *setting,
char *buf, size_t buflen)
{
yescrypt_ctx_t yctx[1];
unsigned char hashbin32[32];
struct {
yescrypt_ctx_t yctx[1];
unsigned char hashbin32[32];
} u;
#define yctx u.yctx
#define hashbin32 u.hashbin32
char *dst;
const uint8_t *src, *saltend;
size_t need, prefixlen;
@ -375,7 +379,7 @@ char *yescrypt_r(
prefixlen = saltend - setting;
need = prefixlen + 1 + YESCRYPT_HASH_LEN + 1;
if (need > buflen || need < prefixlen)
if (need > buflen /*overflow is quite unlikely: || need < prefixlen*/)
goto fail;
if (yescrypt_kdf32(yctx, passwd, passwdlen, hashbin32)) {
@ -390,10 +394,11 @@ char *yescrypt_r(
goto fail;
ret:
free_region(yctx->local);
explicit_bzero(yctx, sizeof(yctx));
explicit_bzero(hashbin32, sizeof(hashbin32));
explicit_bzero(&u, sizeof(u));
return buf;
fail:
buf = NULL;
goto ret;
#undef yctx
#undef hashbin32
}

View file

@ -915,8 +915,13 @@ static int yescrypt_kdf32_body(
size_t B_size, V_size, XY_size, need;
uint8_t *B, *S;
salsa20_blk_t *V, *XY;
uint8_t sha256[32];
uint8_t dk[sizeof(sha256)], *dkp = buf32;
struct {
uint8_t sha256[32];
uint8_t dk[32];
} u;
#define sha256 u.sha256
#define dk u.dk
uint8_t *dkp = buf32;
uint32_t r, p;
/* Sanity-check parameters */
@ -1083,15 +1088,16 @@ static int yescrypt_kdf32_body(
size_t clen = /*buflen:*/32;
if (clen > sizeof(dk))
clen = sizeof(dk);
sha256_block(sha256, sizeof(sha256), dk);
memcpy(buf32, dk, clen);
if (sizeof(dk) != 32) { /* not true, optimize it out */
sha256_block(sha256, sizeof(sha256), dk);
memcpy(buf32, dk, clen);
} else {
sha256_block(sha256, sizeof(sha256), buf32);
}
}
}
if (flags) {
explicit_bzero(sha256, sizeof(sha256));
explicit_bzero(dk, sizeof(dk));
}
explicit_bzero(&u, sizeof(u));
/* Success! */
return 0;
@ -1099,6 +1105,8 @@ static int yescrypt_kdf32_body(
out_EINVAL:
//bbox does not need this: errno = EINVAL;
return -1;
#undef sha256
#undef dk
}
/**