aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2025-07-08 00:14:24 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2025-07-08 00:16:56 +0200
commitd18ac080e4bb7d63e0ec0dea16bacc6ac455f390 (patch)
treed5503d1fc82317b92c62a1746b4b453179befb84
parent8466c3e78fa10d1a3e2bf1a75657fd6d1f4aec30 (diff)
downloadbusybox-w32-d18ac080e4bb7d63e0ec0dea16bacc6ac455f390.tar.gz
busybox-w32-d18ac080e4bb7d63e0ec0dea16bacc6ac455f390.tar.bz2
busybox-w32-d18ac080e4bb7d63e0ec0dea16bacc6ac455f390.zip
libbb/yescrypt: code shrink
Setting EINVAL in errno is not necessary, just error return works. function old new delta yescrypt_kdf32_body 1434 1423 -11 yescrypt_r 1029 990 -39 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-50) Total: -50 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--libbb/yescrypt/alg-yescrypt-common.c4
-rw-r--r--libbb/yescrypt/alg-yescrypt-kdf.c64
2 files changed, 49 insertions, 19 deletions
diff --git a/libbb/yescrypt/alg-yescrypt-common.c b/libbb/yescrypt/alg-yescrypt-common.c
index 5d8be587a..e5d045058 100644
--- a/libbb/yescrypt/alg-yescrypt-common.c
+++ b/libbb/yescrypt/alg-yescrypt-common.c
@@ -288,8 +288,10 @@ char *yescrypt_r(
288 if (need > buflen || need < prefixlen) 288 if (need > buflen || need < prefixlen)
289 goto fail; 289 goto fail;
290 290
291 if (yescrypt_kdf32(yctx, passwd, passwdlen, hashbin32)) 291 if (yescrypt_kdf32(yctx, passwd, passwdlen, hashbin32)) {
292 dbg("error in yescrypt_kdf32");
292 goto fail; 293 goto fail;
294 }
293 295
294 dst = mempcpy(buf, setting, prefixlen); 296 dst = mempcpy(buf, setting, prefixlen);
295 *dst++ = '$'; 297 *dst++ = '$';
diff --git a/libbb/yescrypt/alg-yescrypt-kdf.c b/libbb/yescrypt/alg-yescrypt-kdf.c
index 4c2cfe849..c998de51d 100644
--- a/libbb/yescrypt/alg-yescrypt-kdf.c
+++ b/libbb/yescrypt/alg-yescrypt-kdf.c
@@ -927,22 +927,42 @@ static int yescrypt_kdf32_body(
927 927
928 r = YCTX_param_r; 928 r = YCTX_param_r;
929 p = YCTX_param_p; 929 p = YCTX_param_p;
930 if ((uint64_t)r * (uint64_t)p >= 1 << 30) 930 if ((uint64_t)r * (uint64_t)p >= 1 << 30) {
931 dbg("r * n >= 2^30");
931 goto out_EINVAL; 932 goto out_EINVAL;
932 if (N > UINT32_MAX) 933 }
934 if (N > UINT32_MAX) {
935 dbg("N > 0x%lx", (long)UINT32_MAX);
933 goto out_EINVAL; 936 goto out_EINVAL;
934 if ((N & (N - 1)) != 0 || N <= 3 || r < 1 || p < 1) 937 }
938 if ((N & (N - 1)) != 0
939//TODO: ^^^^^^^^^^^^^^^^^^^^^^ do not check this, code guarantees power-of-2
940 || N <= 3
941 || r < 1
942 || p < 1
943 ) {
944 dbg("bad N, r or p");
935 goto out_EINVAL; 945 goto out_EINVAL;
936 if (r > SIZE_MAX / 256 / p || 946 }
937 N > SIZE_MAX / 128 / r) 947 if (r > SIZE_MAX / 256 / p
948 || N > SIZE_MAX / 128 / r
949 ) {
950 /* 32-bit testcase: mkpasswd qweRTY123@-+ '$y$jHT$123'
951 * (works on 64-bit, needs buffer > 4Gbytes)
952 */
953 dbg("r > SIZE_MAX / 256 / p? %c", "NY"[r > SIZE_MAX / 256 / p]);
954 dbg("N > SIZE_MAX / 128 / r? %c", "NY"[N > SIZE_MAX / 128 / r]);
938 goto out_EINVAL; 955 goto out_EINVAL;
956 }
939 if (flags___YESCRYPT_RW) { 957 if (flags___YESCRYPT_RW) {
940 /* p cannot be greater than SIZE_MAX/Salloc on 64-bit systems, 958 /* p cannot be greater than SIZE_MAX/Salloc on 64-bit systems,
941 but it can on 32-bit systems. */ 959 but it can on 32-bit systems. */
942#pragma GCC diagnostic push 960#pragma GCC diagnostic push
943#pragma GCC diagnostic ignored "-Wtype-limits" 961#pragma GCC diagnostic ignored "-Wtype-limits"
944 if (N / p <= 3 || p > SIZE_MAX / Salloc) 962 if (N / p <= 3 || p > SIZE_MAX / Salloc) {
963 dbg("bad p:%ld", (long)p);
945 goto out_EINVAL; 964 goto out_EINVAL;
965 }
946#pragma GCC diagnostic pop 966#pragma GCC diagnostic pop
947 } 967 }
948 968
@@ -956,17 +976,23 @@ static int yescrypt_kdf32_body(
956 need = V_size; 976 need = V_size;
957 B_size = (size_t)128 * r * p; 977 B_size = (size_t)128 * r * p;
958 need += B_size; 978 need += B_size;
959 if (need < B_size) 979 if (need < B_size) {
980 dbg("integer overflow at += B_size(%lu)", (long)B_size);
960 goto out_EINVAL; 981 goto out_EINVAL;
982 }
961 XY_size = (size_t)256 * r; 983 XY_size = (size_t)256 * r;
962 need += XY_size; 984 need += XY_size;
963 if (need < XY_size) 985 if (need < XY_size) {
986 dbg("integer overflow at += XY_size(%lu)", (long)XY_size);
964 goto out_EINVAL; 987 goto out_EINVAL;
988 }
965 if (flags___YESCRYPT_RW) { 989 if (flags___YESCRYPT_RW) {
966 size_t S_size = (size_t)Salloc * p; 990 size_t S_size = (size_t)Salloc * p;
967 need += S_size; 991 need += S_size;
968 if (need < S_size) 992 if (need < S_size) {
993 dbg("integer overflow at += S_size(%lu)", (long)S_size);
969 goto out_EINVAL; 994 goto out_EINVAL;
995 }
970 } 996 }
971 if (yctx->local->aligned_size < need) { 997 if (yctx->local->aligned_size < need) {
972 free_region(yctx->local); 998 free_region(yctx->local);
@@ -1050,8 +1076,8 @@ static int yescrypt_kdf32_body(
1050 /* Success! */ 1076 /* Success! */
1051 return 0; 1077 return 0;
1052 1078
1053out_EINVAL: 1079 out_EINVAL:
1054 errno = EINVAL; 1080 //bbox does not need this: errno = EINVAL;
1055 return -1; 1081 return -1;
1056} 1082}
1057 1083
@@ -1079,7 +1105,7 @@ int yescrypt_kdf32(
1079 1105
1080 /* Support for hash upgrades has been temporarily removed */ 1106 /* Support for hash upgrades has been temporarily removed */
1081 if (g) { 1107 if (g) {
1082 errno = EINVAL; 1108 //bbox does not need this: errno = EINVAL;
1083 return -1; 1109 return -1;
1084 } 1110 }
1085 1111
@@ -1093,15 +1119,17 @@ int yescrypt_kdf32(
1093 flags | YESCRYPT_ALLOC_ONLY, N, t, 1119 flags | YESCRYPT_ALLOC_ONLY, N, t,
1094 buf32) != -3 1120 buf32) != -3
1095 ) { 1121 ) {
1096 errno = EINVAL; 1122 dbg("yescrypt_kdf32_body: not -3");
1097 return -1; 1123 return -1;
1098 } 1124 }
1099 retval = yescrypt_kdf32_body(yctx, 1125 retval = yescrypt_kdf32_body(yctx,
1100 passwd, passwdlen, 1126 passwd, passwdlen,
1101 flags | YESCRYPT_PREHASH, N >> 6, 0, 1127 flags | YESCRYPT_PREHASH, N >> 6, 0,
1102 dk32); 1128 dk32);
1103 if (retval) 1129 if (retval) {
1130 dbg("yescrypt_kdf32_body(PREHASH):%d", retval);
1104 return retval; 1131 return retval;
1132 }
1105 passwd = dk32; 1133 passwd = dk32;
1106 passwdlen = sizeof(dk32); 1134 passwdlen = sizeof(dk32);
1107 } 1135 }
@@ -1109,9 +1137,9 @@ int yescrypt_kdf32(
1109 retval = yescrypt_kdf32_body(yctx, 1137 retval = yescrypt_kdf32_body(yctx,
1110 passwd, passwdlen, 1138 passwd, passwdlen,
1111 flags, N, t, buf32); 1139 flags, N, t, buf32);
1112#ifndef SKIP_MEMZERO 1140
1113 if (passwd == dk32) 1141 explicit_bzero(dk32, sizeof(dk32));
1114 explicit_bzero(dk32, sizeof(dk32)); 1142
1115#endif 1143 dbg("yescrypt_kdf32_body:%d", retval);
1116 return retval; 1144 return retval;
1117} 1145}