diff options
Diffstat (limited to '')
| -rw-r--r-- | libbb/yescrypt/alg-yescrypt.h | 247 |
1 files changed, 247 insertions, 0 deletions
diff --git a/libbb/yescrypt/alg-yescrypt.h b/libbb/yescrypt/alg-yescrypt.h new file mode 100644 index 000000000..b69843f5d --- /dev/null +++ b/libbb/yescrypt/alg-yescrypt.h | |||
| @@ -0,0 +1,247 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright 2009 Colin Percival | ||
| 3 | * Copyright 2013-2018 Alexander Peslyak | ||
| 4 | * All rights reserved. | ||
| 5 | * | ||
| 6 | * Redistribution and use in source and binary forms, with or without | ||
| 7 | * modification, are permitted provided that the following conditions | ||
| 8 | * are met: | ||
| 9 | * 1. Redistributions of source code must retain the above copyright | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer in the | ||
| 13 | * documentation and/or other materials provided with the distribution. | ||
| 14 | * | ||
| 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | ||
| 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 25 | * SUCH DAMAGE. | ||
| 26 | * | ||
| 27 | * This file was originally written by Colin Percival as part of the Tarsnap | ||
| 28 | * online backup system. | ||
| 29 | */ | ||
| 30 | |||
| 31 | // busybox debug and size-reduction configuration | ||
| 32 | |||
| 33 | #ifdef YESCRYPT_INTERNAL | ||
| 34 | # if 1 | ||
| 35 | # define dbg(...) ((void)0) | ||
| 36 | # else | ||
| 37 | # define dbg(...) bb_error_msg(__VA_ARGS__) | ||
| 38 | # endif | ||
| 39 | # if 1 | ||
| 40 | # define dbg_dec64(...) ((void)0) | ||
| 41 | # else | ||
| 42 | # define dbg_dec64(...) bb_error_msg(__VA_ARGS__) | ||
| 43 | # endif | ||
| 44 | # define TEST_DECODE64 0 | ||
| 45 | #endif | ||
| 46 | |||
| 47 | // Only accept one-char parameters in salt, and only first three? | ||
| 48 | // Almost any reasonable yescrypt hashes in /etc/shadow should | ||
| 49 | // only ever use "jXY" parameters which set N and r. | ||
| 50 | // Fancy multi-byte-encoded wide integers are not needed for that. | ||
| 51 | #define RESTRICTED_PARAMS 1 | ||
| 52 | // Note: if you enable the above, please also enable | ||
| 53 | // YCTX_param_p, YCTX_param_t, YCTX_param_g, YCTX_param_NROM | ||
| 54 | // optimizations, and DISABLE_NROM_CODE. | ||
| 55 | |||
| 56 | #define DISABLE_NROM_CODE 1 | ||
| 57 | |||
| 58 | // How much we save by forcing "standard" value by commenting the next line: | ||
| 59 | // 160 bytes | ||
| 60 | //#define YCTX_param_flags yctx->param.flags | ||
| 61 | // 260 bytes | ||
| 62 | //#define flags___YESCRYPT_RW (flags & YESCRYPT_RW) | ||
| 63 | // 140 bytes | ||
| 64 | //#define flags___YESCRYPT_MODE_MASK (flags & YESCRYPT_MODE_MASK) | ||
| 65 | // ^^^^ forcing the above since the code already requires (checks for) this | ||
| 66 | // 50 bytes | ||
| 67 | #define YCTX_param_N yctx->param.N | ||
| 68 | // -100 bytes (negative!!!) | ||
| 69 | #define YCTX_param_r yctx->param.r | ||
| 70 | // 400 bytes | ||
| 71 | //#define YCTX_param_p yctx->param.p | ||
| 72 | // 130 bytes | ||
| 73 | //#define YCTX_param_t yctx->param.t | ||
| 74 | // 2 bytes | ||
| 75 | //#define YCTX_param_g yctx->param.g | ||
| 76 | // 1 bytes | ||
| 77 | // ^^^^ this looks wrong, compiler should be able to constant-propagate the fact that NROM code is dead | ||
| 78 | //#define YCTX_param_NROM yctx->param.NROM | ||
| 79 | |||
| 80 | #ifndef YCTX_param_flags | ||
| 81 | #define YCTX_param_flags (YESCRYPT_RW | YESCRYPT_ROUNDS_6 | YESCRYPT_GATHER_4 | YESCRYPT_SIMPLE_2 | YESCRYPT_SBOX_12K) | ||
| 82 | #endif | ||
| 83 | #ifndef flags___YESCRYPT_RW | ||
| 84 | #define flags___YESCRYPT_RW ((void)flags, YESCRYPT_RW) | ||
| 85 | #endif | ||
| 86 | #ifndef flags___YESCRYPT_MODE_MASK | ||
| 87 | #define flags___YESCRYPT_MODE_MASK ((void)flags, YESCRYPT_RW) | ||
| 88 | #endif | ||
| 89 | // standard ("j9T") values: | ||
| 90 | #ifndef YCTX_param_N | ||
| 91 | #define YCTX_param_N 4096 | ||
| 92 | #endif | ||
| 93 | #ifndef YCTX_param_r | ||
| 94 | #define YCTX_param_r 32 | ||
| 95 | #endif | ||
| 96 | #ifndef YCTX_param_p | ||
| 97 | #define YCTX_param_p 1 | ||
| 98 | #endif | ||
| 99 | #ifndef YCTX_param_t | ||
| 100 | #define YCTX_param_t 0 | ||
| 101 | #endif | ||
| 102 | #ifndef YCTX_param_g | ||
| 103 | #define YCTX_param_g 0 | ||
| 104 | #endif | ||
| 105 | #ifndef YCTX_param_NROM | ||
| 106 | #define YCTX_param_NROM 0 | ||
| 107 | #endif | ||
| 108 | |||
| 109 | // "Faster/smaller code" knobs: | ||
| 110 | // -941 bytes: | ||
| 111 | #define KDF_UNROLL_COPY 0 | ||
| 112 | // -5324 bytes if 0: | ||
| 113 | #define KDF_UNROLL_PWXFORM_ROUND 0 | ||
| 114 | // -4864 bytes if 0: | ||
| 115 | #define KDF_UNROLL_PWXFORM 0 | ||
| 116 | // if both this ^^^^^^^^^^ and PWXFORM_ROUND set to 0: -7666 bytes | ||
| 117 | // -464 bytes: | ||
| 118 | #define KDF_UNROLL_SALSA20 0 | ||
| 119 | |||
| 120 | /** | ||
| 121 | * Type and possible values for the flags argument of yescrypt_kdf(), | ||
| 122 | * yescrypt_encode_params_r(), yescrypt_encode_params(). Most of these may be | ||
| 123 | * OR'ed together, except that YESCRYPT_WORM stands on its own. | ||
| 124 | * Please refer to the description of yescrypt_kdf() below for the meaning of | ||
| 125 | * these flags. | ||
| 126 | */ | ||
| 127 | /* yescrypt flags: | ||
| 128 | * bits pos: 7654321076543210 | ||
| 129 | * ss r w | ||
| 130 | * sbox gg y | ||
| 131 | */ | ||
| 132 | /* Public */ | ||
| 133 | #define YESCRYPT_WORM 1 | ||
| 134 | #define YESCRYPT_RW 0x002 | ||
| 135 | #define YESCRYPT_ROUNDS_3 0x000 //r=0 | ||
| 136 | #define YESCRYPT_ROUNDS_6 0x004 //r=1 | ||
| 137 | #define YESCRYPT_GATHER_1 0x000 //gg=00 | ||
| 138 | #define YESCRYPT_GATHER_2 0x008 //gg=01 | ||
| 139 | #define YESCRYPT_GATHER_4 0x010 //gg=10 | ||
| 140 | #define YESCRYPT_GATHER_8 0x018 //gg=11 | ||
| 141 | #define YESCRYPT_SIMPLE_1 0x000 //ss=00 | ||
| 142 | #define YESCRYPT_SIMPLE_2 0x020 //ss=01 | ||
| 143 | #define YESCRYPT_SIMPLE_4 0x040 //ss=10 | ||
| 144 | #define YESCRYPT_SIMPLE_8 0x060 //ss=11 | ||
| 145 | #define YESCRYPT_SBOX_6K 0x000 //sbox=0000 | ||
| 146 | #define YESCRYPT_SBOX_12K 0x080 //sbox=0001 | ||
| 147 | #define YESCRYPT_SBOX_24K 0x100 //sbox=0010 | ||
| 148 | #define YESCRYPT_SBOX_48K 0x180 //sbox=0011 | ||
| 149 | #define YESCRYPT_SBOX_96K 0x200 //sbox=0100 | ||
| 150 | #define YESCRYPT_SBOX_192K 0x280 //sbox=0101 | ||
| 151 | #define YESCRYPT_SBOX_384K 0x300 //sbox=0110 | ||
| 152 | #define YESCRYPT_SBOX_768K 0x380 //sbox=0111 | ||
| 153 | |||
| 154 | #ifdef YESCRYPT_INTERNAL | ||
| 155 | /* Private */ | ||
| 156 | #define YESCRYPT_MODE_MASK 0x003 | ||
| 157 | #define YESCRYPT_RW_FLAVOR_MASK 0x3fc | ||
| 158 | #define YESCRYPT_ALLOC_ONLY 0x08000000 | ||
| 159 | #define YESCRYPT_PREHASH 0x10000000 | ||
| 160 | #endif | ||
| 161 | |||
| 162 | #define YESCRYPT_RW_DEFAULTS \ | ||
| 163 | (YESCRYPT_RW | \ | ||
| 164 | YESCRYPT_ROUNDS_6 | YESCRYPT_GATHER_4 | YESCRYPT_SIMPLE_2 | \ | ||
| 165 | YESCRYPT_SBOX_12K) | ||
| 166 | |||
| 167 | #define YESCRYPT_DEFAULTS YESCRYPT_RW_DEFAULTS | ||
| 168 | |||
| 169 | #ifdef YESCRYPT_INTERNAL | ||
| 170 | #define YESCRYPT_KNOWN_FLAGS \ | ||
| 171 | (YESCRYPT_MODE_MASK | YESCRYPT_RW_FLAVOR_MASK | \ | ||
| 172 | YESCRYPT_ALLOC_ONLY | YESCRYPT_PREHASH) | ||
| 173 | #endif | ||
| 174 | |||
| 175 | /* How many chars base-64 encoded bytes require? */ | ||
| 176 | #define YESCRYPT_BYTES2CHARS(bytes) ((((bytes) * 8) + 5) / 6) | ||
| 177 | /* The /etc/passwd-style hash is "<prefix>$<hash><NUL>" */ | ||
| 178 | /* | ||
| 179 | * "$y$", up to 8 params of up to 6 chars each, '$', salt | ||
| 180 | * Alternatively, but that's smaller: | ||
| 181 | * "$7$", 3 params encoded as 1+5+5 chars, salt | ||
| 182 | */ | ||
| 183 | #define YESCRYPT_PREFIX_LEN (3 + 8 * 6 + 1 + YESCRYPT_BYTES2CHARS(32)) | ||
| 184 | |||
| 185 | #define YESCRYPT_HASH_SIZE 32 | ||
| 186 | #define YESCRYPT_HASH_LEN YESCRYPT_BYTES2CHARS(YESCRYPT_HASH_SIZE) | ||
| 187 | |||
| 188 | /** | ||
| 189 | * Internal type used by the memory allocator. Please do not use it directly. | ||
| 190 | * Use yescrypt_shared_t and yescrypt_local_t as appropriate instead, since | ||
| 191 | * they might differ from each other in a future version. | ||
| 192 | */ | ||
| 193 | typedef struct { | ||
| 194 | // void *base; | ||
| 195 | void *aligned; | ||
| 196 | // size_t base_size; | ||
| 197 | size_t aligned_size; | ||
| 198 | } yescrypt_region_t; | ||
| 199 | |||
| 200 | /** | ||
| 201 | * yescrypt parameters combined into one struct. N, r, p are the same as in | ||
| 202 | * classic scrypt, except that the meaning of p changes when YESCRYPT_RW is | ||
| 203 | * set. flags, t, g, NROM are special to yescrypt. | ||
| 204 | */ | ||
| 205 | typedef struct { | ||
| 206 | uint32_t flags; | ||
| 207 | uint32_t r; | ||
| 208 | uint64_t N; | ||
| 209 | #if !RESTRICTED_PARAMS | ||
| 210 | uint32_t p, t, g; | ||
| 211 | uint64_t NROM; | ||
| 212 | #endif | ||
| 213 | } yescrypt_params_t; | ||
| 214 | |||
| 215 | typedef struct { | ||
| 216 | yescrypt_params_t param; | ||
| 217 | |||
| 218 | /* salt in binary form */ | ||
| 219 | /* stored here to cut down on the amount of function paramaters */ | ||
| 220 | unsigned char salt[64]; | ||
| 221 | size_t saltlen; | ||
| 222 | |||
| 223 | /* used by the memory allocator */ | ||
| 224 | //yescrypt_region_t shared[1]; | ||
| 225 | yescrypt_region_t local[1]; | ||
| 226 | } yescrypt_ctx_t; | ||
| 227 | |||
| 228 | /** | ||
| 229 | * yescrypt_r(shared, local, passwd, passwdlen, setting, key, buf, buflen): | ||
| 230 | * Compute and encode an scrypt or enhanced scrypt hash of passwd given the | ||
| 231 | * parameters and salt value encoded in setting. If shared is not NULL, a ROM | ||
| 232 | * is used and YESCRYPT_RW is required. Otherwise, whether to compute classic | ||
| 233 | * scrypt, YESCRYPT_WORM (a slight deviation from classic scrypt), or | ||
| 234 | * YESCRYPT_RW (time-memory tradeoff discouraging modification) is determined | ||
| 235 | * by the setting string. shared (if not NULL) and local must be initialized | ||
| 236 | * as described above for yescrypt_kdf(). buf must be large enough (as | ||
| 237 | * indicated by buflen) to hold the encoded hash string. | ||
| 238 | * | ||
| 239 | * Return the encoded hash string on success; or NULL on error. | ||
| 240 | * | ||
| 241 | * MT-safe as long as local and buf are local to the thread. | ||
| 242 | */ | ||
| 243 | extern char *yescrypt_r( | ||
| 244 | const uint8_t *passwd, size_t passwdlen, | ||
| 245 | const uint8_t *setting, | ||
| 246 | char *buf, size_t buflen | ||
| 247 | ); | ||
