aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2025-07-09 10:38:11 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2025-07-09 10:38:11 +0200
commit95f169f3bb07528c9302c604ff2a1f40b41a98d8 (patch)
tree5ac644d615c6d776e5b6917e08df89e6ab641988
parent11d4c08d7541408e4fbb7daaaf63aba1d07685ea (diff)
downloadbusybox-w32-95f169f3bb07528c9302c604ff2a1f40b41a98d8.tar.gz
busybox-w32-95f169f3bb07528c9302c604ff2a1f40b41a98d8.tar.bz2
busybox-w32-95f169f3bb07528c9302c604ff2a1f40b41a98d8.zip
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>
-rw-r--r--libbb/yescrypt/alg-yescrypt-common.c19
-rw-r--r--libbb/yescrypt/alg-yescrypt-kdf.c24
2 files changed, 28 insertions, 15 deletions
diff --git a/libbb/yescrypt/alg-yescrypt-common.c b/libbb/yescrypt/alg-yescrypt-common.c
index e48be6581..1c063b895 100644
--- a/libbb/yescrypt/alg-yescrypt-common.c
+++ b/libbb/yescrypt/alg-yescrypt-common.c
@@ -23,8 +23,8 @@
23#define decode64_uint32(dst, src, min) \ 23#define decode64_uint32(dst, src, min) \
24({ \ 24({ \
25 uint32_t d32 = a2i64(*(src)); \ 25 uint32_t d32 = a2i64(*(src)); \
26 if (d32 > 47) \ 26 if (d32 > 47) \
27 goto fail; \ 27 goto fail; \
28 *(dst) = d32 + (min); \ 28 *(dst) = d32 + (min); \
29 ++src; \ 29 ++src; \
30}) 30})
@@ -292,8 +292,12 @@ char *yescrypt_r(
292 const uint8_t *setting, 292 const uint8_t *setting,
293 char *buf, size_t buflen) 293 char *buf, size_t buflen)
294{ 294{
295 yescrypt_ctx_t yctx[1]; 295 struct {
296 unsigned char hashbin32[32]; 296 yescrypt_ctx_t yctx[1];
297 unsigned char hashbin32[32];
298 } u;
299#define yctx u.yctx
300#define hashbin32 u.hashbin32
297 char *dst; 301 char *dst;
298 const uint8_t *src, *saltend; 302 const uint8_t *src, *saltend;
299 size_t need, prefixlen; 303 size_t need, prefixlen;
@@ -375,7 +379,7 @@ char *yescrypt_r(
375 379
376 prefixlen = saltend - setting; 380 prefixlen = saltend - setting;
377 need = prefixlen + 1 + YESCRYPT_HASH_LEN + 1; 381 need = prefixlen + 1 + YESCRYPT_HASH_LEN + 1;
378 if (need > buflen || need < prefixlen) 382 if (need > buflen /*overflow is quite unlikely: || need < prefixlen*/)
379 goto fail; 383 goto fail;
380 384
381 if (yescrypt_kdf32(yctx, passwd, passwdlen, hashbin32)) { 385 if (yescrypt_kdf32(yctx, passwd, passwdlen, hashbin32)) {
@@ -390,10 +394,11 @@ char *yescrypt_r(
390 goto fail; 394 goto fail;
391 ret: 395 ret:
392 free_region(yctx->local); 396 free_region(yctx->local);
393 explicit_bzero(yctx, sizeof(yctx)); 397 explicit_bzero(&u, sizeof(u));
394 explicit_bzero(hashbin32, sizeof(hashbin32));
395 return buf; 398 return buf;
396 fail: 399 fail:
397 buf = NULL; 400 buf = NULL;
398 goto ret; 401 goto ret;
402#undef yctx
403#undef hashbin32
399} 404}
diff --git a/libbb/yescrypt/alg-yescrypt-kdf.c b/libbb/yescrypt/alg-yescrypt-kdf.c
index 112862ec9..29d9efc07 100644
--- a/libbb/yescrypt/alg-yescrypt-kdf.c
+++ b/libbb/yescrypt/alg-yescrypt-kdf.c
@@ -915,8 +915,13 @@ static int yescrypt_kdf32_body(
915 size_t B_size, V_size, XY_size, need; 915 size_t B_size, V_size, XY_size, need;
916 uint8_t *B, *S; 916 uint8_t *B, *S;
917 salsa20_blk_t *V, *XY; 917 salsa20_blk_t *V, *XY;
918 uint8_t sha256[32]; 918 struct {
919 uint8_t dk[sizeof(sha256)], *dkp = buf32; 919 uint8_t sha256[32];
920 uint8_t dk[32];
921 } u;
922#define sha256 u.sha256
923#define dk u.dk
924 uint8_t *dkp = buf32;
920 uint32_t r, p; 925 uint32_t r, p;
921 926
922 /* Sanity-check parameters */ 927 /* Sanity-check parameters */
@@ -1083,15 +1088,16 @@ static int yescrypt_kdf32_body(
1083 size_t clen = /*buflen:*/32; 1088 size_t clen = /*buflen:*/32;
1084 if (clen > sizeof(dk)) 1089 if (clen > sizeof(dk))
1085 clen = sizeof(dk); 1090 clen = sizeof(dk);
1086 sha256_block(sha256, sizeof(sha256), dk); 1091 if (sizeof(dk) != 32) { /* not true, optimize it out */
1087 memcpy(buf32, dk, clen); 1092 sha256_block(sha256, sizeof(sha256), dk);
1093 memcpy(buf32, dk, clen);
1094 } else {
1095 sha256_block(sha256, sizeof(sha256), buf32);
1096 }
1088 } 1097 }
1089 } 1098 }
1090 1099
1091 if (flags) { 1100 explicit_bzero(&u, sizeof(u));
1092 explicit_bzero(sha256, sizeof(sha256));
1093 explicit_bzero(dk, sizeof(dk));
1094 }
1095 1101
1096 /* Success! */ 1102 /* Success! */
1097 return 0; 1103 return 0;
@@ -1099,6 +1105,8 @@ static int yescrypt_kdf32_body(
1099 out_EINVAL: 1105 out_EINVAL:
1100 //bbox does not need this: errno = EINVAL; 1106 //bbox does not need this: errno = EINVAL;
1101 return -1; 1107 return -1;
1108#undef sha256
1109#undef dk
1102} 1110}
1103 1111
1104/** 1112/**