diff options
| author | Denys Vlasenko <vda.linux@googlemail.com> | 2025-07-06 22:43:28 +0200 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2025-07-06 22:43:28 +0200 |
| commit | 53de6e6150ea5538930e1963eb87ada153093ea0 (patch) | |
| tree | 1f6061ab909988da609f7feec3e140b2ec9d8bf7 /libbb | |
| parent | 5e17e3c6f49cef45a86ed9438941ca2d4f6ae906 (diff) | |
| download | busybox-w32-53de6e6150ea5538930e1963eb87ada153093ea0.tar.gz busybox-w32-53de6e6150ea5538930e1963eb87ada153093ea0.tar.bz2 busybox-w32-53de6e6150ea5538930e1963eb87ada153093ea0.zip | |
libbb/yescrypt: use common ascii64 encoding routine
function old new delta
num2str64_lsb_first 33 46 +13
yescrypt_r 1235 1133 -102
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 13/-102) Total: -89 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libbb')
| -rw-r--r-- | libbb/yescrypt/alg-yescrypt-common.c | 98 | ||||
| -rw-r--r-- | libbb/yescrypt/alg-yescrypt.h | 4 |
2 files changed, 36 insertions, 66 deletions
diff --git a/libbb/yescrypt/alg-yescrypt-common.c b/libbb/yescrypt/alg-yescrypt-common.c index 435eaecca..5bdf1893e 100644 --- a/libbb/yescrypt/alg-yescrypt-common.c +++ b/libbb/yescrypt/alg-yescrypt-common.c | |||
| @@ -19,8 +19,8 @@ | |||
| 19 | */ | 19 | */ |
| 20 | 20 | ||
| 21 | /* Not inlining: | 21 | /* Not inlining: |
| 22 | * decode64 fuinctions are only used to read | 22 | * de/encode64 functions are only used to read |
| 23 | * yescrypt_params_t field, and convert salt ti binary - | 23 | * yescrypt_params_t field, and convert salt to binary - |
| 24 | * both of these are negligible compared to main hashing operation | 24 | * both of these are negligible compared to main hashing operation |
| 25 | */ | 25 | */ |
| 26 | static NOINLINE const uint8_t *decode64_uint32( | 26 | static NOINLINE const uint8_t *decode64_uint32( |
| @@ -63,56 +63,6 @@ fail: | |||
| 63 | return NULL; | 63 | return NULL; |
| 64 | } | 64 | } |
| 65 | 65 | ||
| 66 | static uint8_t *encode64_uint32_fixed( | ||
| 67 | uint8_t *dst, size_t dstlen, | ||
| 68 | uint32_t src, uint32_t srcbits) | ||
| 69 | { | ||
| 70 | uint32_t bits; | ||
| 71 | |||
| 72 | for (bits = 0; bits < srcbits; bits += 6) { | ||
| 73 | if (dstlen < 2) | ||
| 74 | return NULL; | ||
| 75 | *dst++ = i2a64(src); | ||
| 76 | dstlen--; | ||
| 77 | src >>= 6; | ||
| 78 | } | ||
| 79 | |||
| 80 | if (src || dstlen < 1) | ||
| 81 | return NULL; | ||
| 82 | |||
| 83 | *dst = 0; /* NUL terminate just in case */ | ||
| 84 | |||
| 85 | return dst; | ||
| 86 | } | ||
| 87 | |||
| 88 | static uint8_t *encode64( | ||
| 89 | uint8_t *dst, size_t dstlen, | ||
| 90 | const uint8_t *src, size_t srclen) | ||
| 91 | { | ||
| 92 | size_t i; | ||
| 93 | |||
| 94 | for (i = 0; i < srclen; ) { | ||
| 95 | uint8_t *dnext; | ||
| 96 | uint32_t value = 0, bits = 0; | ||
| 97 | do { | ||
| 98 | value |= (uint32_t)src[i++] << bits; | ||
| 99 | bits += 8; | ||
| 100 | } while (bits < 24 && i < srclen); | ||
| 101 | dnext = encode64_uint32_fixed(dst, dstlen, value, bits); | ||
| 102 | if (!dnext) | ||
| 103 | return NULL; | ||
| 104 | dstlen -= dnext - dst; | ||
| 105 | dst = dnext; | ||
| 106 | } | ||
| 107 | |||
| 108 | if (dstlen < 1) | ||
| 109 | return NULL; | ||
| 110 | |||
| 111 | *dst = 0; /* NUL terminate just in case */ | ||
| 112 | |||
| 113 | return dst; | ||
| 114 | } | ||
| 115 | |||
| 116 | static const uint8_t *decode64( | 66 | static const uint8_t *decode64( |
| 117 | uint8_t *dst, size_t *dstlen, | 67 | uint8_t *dst, size_t *dstlen, |
| 118 | const uint8_t *src, size_t srclen) | 68 | const uint8_t *src, size_t srclen) |
| @@ -156,21 +106,43 @@ static const uint8_t *decode64( | |||
| 156 | *dstlen = dstpos; | 106 | *dstlen = dstpos; |
| 157 | return src; | 107 | return src; |
| 158 | } | 108 | } |
| 159 | |||
| 160 | fail: | 109 | fail: |
| 161 | *dstlen = 0; | 110 | *dstlen = 0; |
| 162 | return NULL; | 111 | return NULL; |
| 163 | } | 112 | } |
| 164 | 113 | ||
| 165 | uint8_t *yescrypt_r( | 114 | static char *encode64( |
| 115 | char *dst, size_t dstlen, | ||
| 116 | const uint8_t *src, size_t srclen) | ||
| 117 | { | ||
| 118 | while (srclen) { | ||
| 119 | uint32_t value = 0, b = 0; | ||
| 120 | do { | ||
| 121 | value |= (uint32_t)(*src++ << b); | ||
| 122 | b += 8; | ||
| 123 | srclen--; | ||
| 124 | } while (srclen && b < 24); | ||
| 125 | |||
| 126 | b >>= 3; /* number of bits to number of bytes */ | ||
| 127 | b++; /* 1, 2 or 3 bytes will become 2, 3 or 4 ascii64 chars */ | ||
| 128 | dstlen -= b; | ||
| 129 | if ((ssize_t)dstlen <= 0) | ||
| 130 | return NULL; | ||
| 131 | dst = num2str64_lsb_first(dst, value, b); | ||
| 132 | } | ||
| 133 | *dst = '\0'; | ||
| 134 | return dst; | ||
| 135 | } | ||
| 136 | |||
| 137 | char *yescrypt_r( | ||
| 166 | const uint8_t *passwd, size_t passwdlen, | 138 | const uint8_t *passwd, size_t passwdlen, |
| 167 | const uint8_t *setting, | 139 | const uint8_t *setting, |
| 168 | uint8_t *buf, size_t buflen) | 140 | char *buf, size_t buflen) |
| 169 | { | 141 | { |
| 170 | yescrypt_ctx_t yctx[1]; | 142 | yescrypt_ctx_t yctx[1]; |
| 171 | unsigned char hashbin32[32]; | 143 | unsigned char hashbin32[32]; |
| 144 | char *dst; | ||
| 172 | const uint8_t *src, *saltstr, *saltend; | 145 | const uint8_t *src, *saltstr, *saltend; |
| 173 | uint8_t *dst; | ||
| 174 | size_t need, prefixlen, saltstrlen; | 146 | size_t need, prefixlen, saltstrlen; |
| 175 | uint32_t flavor, N_log2; | 147 | uint32_t flavor, N_log2; |
| 176 | 148 | ||
| @@ -241,11 +213,11 @@ uint8_t *yescrypt_r( | |||
| 241 | goto fail; | 213 | goto fail; |
| 242 | yctx->param.NROM = (uint64_t)1 << NROM_log2; | 214 | yctx->param.NROM = (uint64_t)1 << NROM_log2; |
| 243 | } | 215 | } |
| 216 | if (!src) | ||
| 217 | goto fail; | ||
| 218 | if (*src != '$') | ||
| 219 | goto fail; | ||
| 244 | } | 220 | } |
| 245 | if (!src) | ||
| 246 | goto fail; | ||
| 247 | if (*src != '$') | ||
| 248 | goto fail; | ||
| 249 | 221 | ||
| 250 | saltstr = src + 1; | 222 | saltstr = src + 1; |
| 251 | src = (uint8_t *)strchrnul((char *)saltstr, '$'); | 223 | src = (uint8_t *)strchrnul((char *)saltstr, '$'); |
| @@ -268,16 +240,14 @@ uint8_t *yescrypt_r( | |||
| 268 | dst = mempcpy(buf, setting, prefixlen); | 240 | dst = mempcpy(buf, setting, prefixlen); |
| 269 | *dst++ = '$'; | 241 | *dst++ = '$'; |
| 270 | dst = encode64(dst, buflen - (dst - buf), hashbin32, sizeof(hashbin32)); | 242 | dst = encode64(dst, buflen - (dst - buf), hashbin32, sizeof(hashbin32)); |
| 271 | if (!dst || dst >= buf + buflen) | 243 | if (!dst) |
| 272 | goto fail; | 244 | goto fail; |
| 273 | |||
| 274 | *dst = 0; /* NUL termination */ | ||
| 275 | ret: | 245 | ret: |
| 276 | free_region(yctx->local); | 246 | free_region(yctx->local); |
| 277 | explicit_bzero(yctx, sizeof(yctx)); | 247 | explicit_bzero(yctx, sizeof(yctx)); |
| 278 | explicit_bzero(hashbin32, sizeof(hashbin32)); | 248 | explicit_bzero(hashbin32, sizeof(hashbin32)); |
| 279 | return buf; | 249 | return buf; |
| 280 | fail: | 250 | fail: |
| 281 | buf = NULL; | 251 | buf = NULL; |
| 282 | goto ret; | 252 | goto ret; |
| 283 | } | 253 | } |
diff --git a/libbb/yescrypt/alg-yescrypt.h b/libbb/yescrypt/alg-yescrypt.h index 5b442c2c9..996af333f 100644 --- a/libbb/yescrypt/alg-yescrypt.h +++ b/libbb/yescrypt/alg-yescrypt.h | |||
| @@ -151,8 +151,8 @@ typedef struct { | |||
| 151 | * | 151 | * |
| 152 | * MT-safe as long as local and buf are local to the thread. | 152 | * MT-safe as long as local and buf are local to the thread. |
| 153 | */ | 153 | */ |
| 154 | extern uint8_t *yescrypt_r( | 154 | extern char *yescrypt_r( |
| 155 | const uint8_t *passwd, size_t passwdlen, | 155 | const uint8_t *passwd, size_t passwdlen, |
| 156 | const uint8_t *setting, | 156 | const uint8_t *setting, |
| 157 | uint8_t *buf, size_t buflen | 157 | char *buf, size_t buflen |
| 158 | ); | 158 | ); |
