diff options
Diffstat (limited to 'libbb')
| -rw-r--r-- | libbb/yescrypt/alg-yescrypt-common.c | 2 | ||||
| -rw-r--r-- | libbb/yescrypt/alg-yescrypt-kdf.c | 27 | ||||
| -rw-r--r-- | libbb/yescrypt/alg-yescrypt-platform.c | 45 | ||||
| -rw-r--r-- | libbb/yescrypt/alg-yescrypt.h | 51 | ||||
| -rw-r--r-- | libbb/yescrypt/y.c | 10 |
5 files changed, 41 insertions, 94 deletions
diff --git a/libbb/yescrypt/alg-yescrypt-common.c b/libbb/yescrypt/alg-yescrypt-common.c index b9a5c51ac..435eaecca 100644 --- a/libbb/yescrypt/alg-yescrypt-common.c +++ b/libbb/yescrypt/alg-yescrypt-common.c | |||
| @@ -258,7 +258,7 @@ uint8_t *yescrypt_r( | |||
| 258 | if (saltend != saltstr + saltstrlen) | 258 | if (saltend != saltstr + saltstrlen) |
| 259 | goto fail; /* salt[] is too small, or bad char during decode */ | 259 | goto fail; /* salt[] is too small, or bad char during decode */ |
| 260 | 260 | ||
| 261 | need = prefixlen + 1 + HASH_LEN + 1; | 261 | need = prefixlen + 1 + YESCRYPT_HASH_LEN + 1; |
| 262 | if (need > buflen || need < prefixlen) | 262 | if (need > buflen || need < prefixlen) |
| 263 | goto fail; | 263 | goto fail; |
| 264 | 264 | ||
diff --git a/libbb/yescrypt/alg-yescrypt-kdf.c b/libbb/yescrypt/alg-yescrypt-kdf.c index 781e1f0bb..27ef2caa4 100644 --- a/libbb/yescrypt/alg-yescrypt-kdf.c +++ b/libbb/yescrypt/alg-yescrypt-kdf.c | |||
| @@ -759,6 +759,33 @@ static void smix(uint8_t *B, size_t r, uint32_t N, uint32_t p, uint32_t t, | |||
| 759 | } | 759 | } |
| 760 | } | 760 | } |
| 761 | 761 | ||
| 762 | /* Allocator code */ | ||
| 763 | |||
| 764 | static void alloc_region(yescrypt_region_t *region, size_t size) | ||
| 765 | { | ||
| 766 | int flags = | ||
| 767 | # ifdef MAP_NOCORE /* huh? */ | ||
| 768 | MAP_NOCORE | | ||
| 769 | # endif | ||
| 770 | MAP_ANON | MAP_PRIVATE; | ||
| 771 | uint8_t *base = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, -1, 0); | ||
| 772 | if (base == MAP_FAILED) | ||
| 773 | bb_die_memory_exhausted(); | ||
| 774 | //region->base = base; | ||
| 775 | //region->base_size = size; | ||
| 776 | region->aligned = base; | ||
| 777 | region->aligned_size = size; | ||
| 778 | } | ||
| 779 | |||
| 780 | static void free_region(yescrypt_region_t *region) | ||
| 781 | { | ||
| 782 | if (region->aligned) | ||
| 783 | munmap(region->aligned, region->aligned_size); | ||
| 784 | //region->base = NULL; | ||
| 785 | //region->base_size = 0; | ||
| 786 | region->aligned = NULL; | ||
| 787 | region->aligned_size = 0; | ||
| 788 | } | ||
| 762 | /** | 789 | /** |
| 763 | * yescrypt_kdf_body(shared, local, passwd, passwdlen, salt, saltlen, | 790 | * yescrypt_kdf_body(shared, local, passwd, passwdlen, salt, saltlen, |
| 764 | * flags, N, r, p, t, NROM, buf, buflen): | 791 | * flags, N, r, p, t, NROM, buf, buflen): |
diff --git a/libbb/yescrypt/alg-yescrypt-platform.c b/libbb/yescrypt/alg-yescrypt-platform.c deleted file mode 100644 index 8dd5feb55..000000000 --- a/libbb/yescrypt/alg-yescrypt-platform.c +++ /dev/null | |||
| @@ -1,45 +0,0 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright 2013-2018,2022 Alexander Peslyak | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted. | ||
| 7 | * | ||
| 8 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | ||
| 9 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 10 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 11 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 12 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 13 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 14 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 15 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 16 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 17 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 18 | * SUCH DAMAGE. | ||
| 19 | */ | ||
| 20 | |||
| 21 | static void alloc_region(yescrypt_region_t *region, size_t size) | ||
| 22 | { | ||
| 23 | int flags = | ||
| 24 | # ifdef MAP_NOCORE /* huh? */ | ||
| 25 | MAP_NOCORE | | ||
| 26 | # endif | ||
| 27 | MAP_ANON | MAP_PRIVATE; | ||
| 28 | uint8_t *base = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, -1, 0); | ||
| 29 | if (base == MAP_FAILED) | ||
| 30 | bb_die_memory_exhausted(); | ||
| 31 | //region->base = base; | ||
| 32 | //region->base_size = size; | ||
| 33 | region->aligned = base; | ||
| 34 | region->aligned_size = size; | ||
| 35 | } | ||
| 36 | |||
| 37 | static void free_region(yescrypt_region_t *region) | ||
| 38 | { | ||
| 39 | if (region->aligned) | ||
| 40 | munmap(region->aligned, region->aligned_size); | ||
| 41 | //region->base = NULL; | ||
| 42 | //region->base_size = 0; | ||
| 43 | region->aligned = NULL; | ||
| 44 | region->aligned_size = 0; | ||
| 45 | } | ||
diff --git a/libbb/yescrypt/alg-yescrypt.h b/libbb/yescrypt/alg-yescrypt.h index edabbc222..5b442c2c9 100644 --- a/libbb/yescrypt/alg-yescrypt.h +++ b/libbb/yescrypt/alg-yescrypt.h | |||
| @@ -64,6 +64,7 @@ typedef uint32_t yescrypt_flags_t; | |||
| 64 | #define YESCRYPT_SBOX_192K 0x280 | 64 | #define YESCRYPT_SBOX_192K 0x280 |
| 65 | #define YESCRYPT_SBOX_384K 0x300 | 65 | #define YESCRYPT_SBOX_384K 0x300 |
| 66 | #define YESCRYPT_SBOX_768K 0x380 | 66 | #define YESCRYPT_SBOX_768K 0x380 |
| 67 | |||
| 67 | #ifdef YESCRYPT_INTERNAL | 68 | #ifdef YESCRYPT_INTERNAL |
| 68 | /* Private */ | 69 | /* Private */ |
| 69 | #define YESCRYPT_MODE_MASK 0x003 | 70 | #define YESCRYPT_MODE_MASK 0x003 |
| @@ -123,59 +124,17 @@ typedef struct { | |||
| 123 | } yescrypt_ctx_t; | 124 | } yescrypt_ctx_t; |
| 124 | 125 | ||
| 125 | /* How many chars base-64 encoded bytes require? */ | 126 | /* How many chars base-64 encoded bytes require? */ |
| 126 | #define BYTES2CHARS(bytes) ((((bytes) * 8) + 5) / 6) | 127 | #define YESCRYPT_BYTES2CHARS(bytes) ((((bytes) * 8) + 5) / 6) |
| 127 | /* The /etc/passwd-style hash is "<prefix>$<hash><NUL>" */ | 128 | /* The /etc/passwd-style hash is "<prefix>$<hash><NUL>" */ |
| 128 | /* | 129 | /* |
| 129 | * "$y$", up to 8 params of up to 6 chars each, '$', salt | 130 | * "$y$", up to 8 params of up to 6 chars each, '$', salt |
| 130 | * Alternatively, but that's smaller: | 131 | * Alternatively, but that's smaller: |
| 131 | * "$7$", 3 params encoded as 1+5+5 chars, salt | 132 | * "$7$", 3 params encoded as 1+5+5 chars, salt |
| 132 | */ | 133 | */ |
| 133 | #define PREFIX_LEN (3 + 8 * 6 + 1 + BYTES2CHARS(32)) | 134 | #define YESCRYPT_PREFIX_LEN (3 + 8 * 6 + 1 + YESCRYPT_BYTES2CHARS(32)) |
| 134 | |||
| 135 | #define HASH_SIZE 32 | ||
| 136 | #define HASH_LEN BYTES2CHARS(HASH_SIZE) | ||
| 137 | 135 | ||
| 138 | /** | 136 | #define YESCRYPT_HASH_SIZE 32 |
| 139 | * yescrypt_kdf(shared, local, passwd, passwdlen, salt, saltlen, params, | 137 | #define YESCRYPT_HASH_LEN YESCRYPT_BYTES2CHARS(YESCRYPT_HASH_SIZE) |
| 140 | * buf, buflen): | ||
| 141 | * Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r, | ||
| 142 | * p, buflen), or a revision of scrypt as requested by flags and shared, and | ||
| 143 | * write the result into buf. The parameters N, r, p, and buflen must satisfy | ||
| 144 | * the same conditions as with crypto_scrypt(). t controls computation time | ||
| 145 | * while not affecting peak memory usage (t = 0 is optimal unless higher N*r | ||
| 146 | * is not affordable while higher t is). g controls hash upgrades (g = 0 for | ||
| 147 | * no upgrades so far). shared and flags may request special modes. local is | ||
| 148 | * the thread-local data structure, allowing to preserve and reuse a memory | ||
| 149 | * allocation across calls, thereby reducing processing overhead. | ||
| 150 | * | ||
| 151 | * Return 0 on success; or -1 on error. | ||
| 152 | * | ||
| 153 | * Classic scrypt is available by setting shared = NULL, flags = 0, and t = 0. | ||
| 154 | * | ||
| 155 | * Setting YESCRYPT_WORM enables only minimal deviations from classic scrypt: | ||
| 156 | * support for the t parameter, and pre- and post-hashing. | ||
| 157 | * | ||
| 158 | * Setting YESCRYPT_RW fully enables yescrypt. As a side effect of differences | ||
| 159 | * between the algorithms, it also prevents p > 1 from growing the threads' | ||
| 160 | * combined processing time and memory allocation (like it did with classic | ||
| 161 | * scrypt and YESCRYPT_WORM), treating p as a divider rather than a multiplier. | ||
| 162 | * | ||
| 163 | * Passing a shared structure, with ROM contents previously computed by | ||
| 164 | * yescrypt_init_shared(), enables the use of ROM and requires YESCRYPT_RW. | ||
| 165 | * | ||
| 166 | * In order to allow for initialization of the ROM to be split into a separate | ||
| 167 | * program (or separate invocation of the same program), the shared->aligned | ||
| 168 | * and shared->aligned_size fields may optionally be set by the caller directly | ||
| 169 | * (e.g., to a mapped SysV shm segment), without using yescrypt_init_shared(). | ||
| 170 | * | ||
| 171 | * MT-safe as long as local and buf are local to the thread. | ||
| 172 | */ | ||
| 173 | #ifdef YESCRYPT_INTERNAL | ||
| 174 | static int yescrypt_kdf32( | ||
| 175 | yescrypt_ctx_t *yctx, | ||
| 176 | const uint8_t *passwd, size_t passwdlen, | ||
| 177 | uint8_t *buf32); | ||
| 178 | #endif | ||
| 179 | 138 | ||
| 180 | /** | 139 | /** |
| 181 | * yescrypt_r(shared, local, passwd, passwdlen, setting, key, buf, buflen): | 140 | * yescrypt_r(shared, local, passwd, passwdlen, setting, key, buf, buflen): |
diff --git a/libbb/yescrypt/y.c b/libbb/yescrypt/y.c index 92c6eb7a8..e7d447531 100644 --- a/libbb/yescrypt/y.c +++ b/libbb/yescrypt/y.c | |||
| @@ -1,11 +1,17 @@ | |||
| 1 | /* | ||
| 2 | * The compilation unit for yescrypt-related code. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2025 by Denys Vlasenko <vda.linux@googlemail.com> | ||
| 5 | * | ||
| 6 | * Licensed under GPLv2, see file LICENSE in this source tree. | ||
| 7 | */ | ||
| 1 | //kbuild:lib-$(CONFIG_USE_BB_CRYPT_YES) += y.o | 8 | //kbuild:lib-$(CONFIG_USE_BB_CRYPT_YES) += y.o |
| 2 | 9 | ||
| 3 | #include <libbb.h> | 10 | #include "libbb.h" |
| 4 | 11 | ||
| 5 | #define YESCRYPT_INTERNAL | 12 | #define YESCRYPT_INTERNAL |
| 6 | #include "alg-sha256.h" | 13 | #include "alg-sha256.h" |
| 7 | #include "alg-yescrypt.h" | 14 | #include "alg-yescrypt.h" |
| 8 | #include "alg-sha256.c" | 15 | #include "alg-sha256.c" |
| 9 | #include "alg-yescrypt-platform.c" | ||
| 10 | #include "alg-yescrypt-kdf.c" | 16 | #include "alg-yescrypt-kdf.c" |
| 11 | #include "alg-yescrypt-common.c" | 17 | #include "alg-yescrypt-common.c" |
