xcrypt 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Rakefile +106 -0
- data/ext/libxcrypt/AUTHORS +38 -0
- data/ext/libxcrypt/COPYING.LIB +502 -0
- data/ext/libxcrypt/ChangeLog +239 -0
- data/ext/libxcrypt/INSTALL +380 -0
- data/ext/libxcrypt/LICENSING +152 -0
- data/ext/libxcrypt/Makefile.am +704 -0
- data/ext/libxcrypt/Makefile.in +4110 -0
- data/ext/libxcrypt/NEWS +630 -0
- data/ext/libxcrypt/README +1 -0
- data/ext/libxcrypt/README.md +179 -0
- data/ext/libxcrypt/THANKS +13 -0
- data/ext/libxcrypt/TODO +1 -0
- data/ext/libxcrypt/TODO.md +100 -0
- data/ext/libxcrypt/aclocal.m4 +2617 -0
- data/ext/libxcrypt/autogen.sh +33 -0
- data/ext/libxcrypt/autom4te.cache/output.0 +19884 -0
- data/ext/libxcrypt/autom4te.cache/output.1 +19884 -0
- data/ext/libxcrypt/autom4te.cache/output.2 +19884 -0
- data/ext/libxcrypt/autom4te.cache/output.3 +19885 -0
- data/ext/libxcrypt/autom4te.cache/requests +714 -0
- data/ext/libxcrypt/autom4te.cache/traces.0 +4088 -0
- data/ext/libxcrypt/autom4te.cache/traces.1 +1060 -0
- data/ext/libxcrypt/autom4te.cache/traces.2 +4088 -0
- data/ext/libxcrypt/autom4te.cache/traces.3 +1060 -0
- data/ext/libxcrypt/build-aux/ci/ci-log-dependency-versions +79 -0
- data/ext/libxcrypt/build-aux/ci/ci-log-logfiles +22 -0
- data/ext/libxcrypt/build-aux/ci/clang-gcov-wrapper +2 -0
- data/ext/libxcrypt/build-aux/ci/configure-wrapper +10 -0
- data/ext/libxcrypt/build-aux/ci/summarize-coverage +24 -0
- data/ext/libxcrypt/build-aux/m4/ax_append_compile_flags.m4 +46 -0
- data/ext/libxcrypt/build-aux/m4/ax_append_flag.m4 +50 -0
- data/ext/libxcrypt/build-aux/m4/ax_check_compile_flag.m4 +53 -0
- data/ext/libxcrypt/build-aux/m4/ax_check_vscript.m4 +142 -0
- data/ext/libxcrypt/build-aux/m4/ax_gcc_func_attribute.m4 +246 -0
- data/ext/libxcrypt/build-aux/m4/ax_require_defined.m4 +37 -0
- data/ext/libxcrypt/build-aux/m4/ax_valgrind_check.m4 +239 -0
- data/ext/libxcrypt/build-aux/m4/libtool.m4 +8488 -0
- data/ext/libxcrypt/build-aux/m4/ltoptions.m4 +467 -0
- data/ext/libxcrypt/build-aux/m4/ltsugar.m4 +124 -0
- data/ext/libxcrypt/build-aux/m4/ltversion.m4 +24 -0
- data/ext/libxcrypt/build-aux/m4/lt~obsolete.m4 +99 -0
- data/ext/libxcrypt/build-aux/m4/pkg_compat.m4 +88 -0
- data/ext/libxcrypt/build-aux/m4/zw_alignment.m4 +90 -0
- data/ext/libxcrypt/build-aux/m4/zw_automodern.m4 +307 -0
- data/ext/libxcrypt/build-aux/m4/zw_detect_asan.m4 +24 -0
- data/ext/libxcrypt/build-aux/m4/zw_endianness.m4 +152 -0
- data/ext/libxcrypt/build-aux/m4/zw_ld_wrap.m4 +47 -0
- data/ext/libxcrypt/build-aux/m4/zw_prog_perl.m4 +40 -0
- data/ext/libxcrypt/build-aux/m4/zw_simple_warnings.m4 +150 -0
- data/ext/libxcrypt/build-aux/m4/zw_static_assert.m4 +68 -0
- data/ext/libxcrypt/build-aux/m4-autogen/compile +364 -0
- data/ext/libxcrypt/build-aux/m4-autogen/config.guess +1815 -0
- data/ext/libxcrypt/build-aux/m4-autogen/config.sub +2354 -0
- data/ext/libxcrypt/build-aux/m4-autogen/depcomp +792 -0
- data/ext/libxcrypt/build-aux/m4-autogen/install-sh +541 -0
- data/ext/libxcrypt/build-aux/m4-autogen/ltmain.sh +11524 -0
- data/ext/libxcrypt/build-aux/m4-autogen/missing +236 -0
- data/ext/libxcrypt/build-aux/m4-autogen/test-driver +160 -0
- data/ext/libxcrypt/build-aux/scripts/BuildCommon.pm +712 -0
- data/ext/libxcrypt/build-aux/scripts/check-perlcritic-config +76 -0
- data/ext/libxcrypt/build-aux/scripts/compute-symver-floor +116 -0
- data/ext/libxcrypt/build-aux/scripts/expand-selected-hashes +80 -0
- data/ext/libxcrypt/build-aux/scripts/gen-crypt-h +131 -0
- data/ext/libxcrypt/build-aux/scripts/gen-crypt-hashes-h +141 -0
- data/ext/libxcrypt/build-aux/scripts/gen-crypt-symbol-vers-h +150 -0
- data/ext/libxcrypt/build-aux/scripts/gen-libcrypt-map +67 -0
- data/ext/libxcrypt/build-aux/scripts/move-if-change +84 -0
- data/ext/libxcrypt/build-aux/scripts/skip-if-exec-format-error +78 -0
- data/ext/libxcrypt/codecov.yml +4 -0
- data/ext/libxcrypt/config.h.in +303 -0
- data/ext/libxcrypt/configure +19885 -0
- data/ext/libxcrypt/configure.ac +549 -0
- data/ext/libxcrypt/doc/crypt.3 +512 -0
- data/ext/libxcrypt/doc/crypt.5 +343 -0
- data/ext/libxcrypt/doc/crypt_checksalt.3 +106 -0
- data/ext/libxcrypt/doc/crypt_gensalt.3 +285 -0
- data/ext/libxcrypt/doc/crypt_gensalt_ra.3 +1 -0
- data/ext/libxcrypt/doc/crypt_gensalt_rn.3 +1 -0
- data/ext/libxcrypt/doc/crypt_preferred_method.3 +68 -0
- data/ext/libxcrypt/doc/crypt_r.3 +1 -0
- data/ext/libxcrypt/doc/crypt_ra.3 +1 -0
- data/ext/libxcrypt/doc/crypt_rn.3 +1 -0
- data/ext/libxcrypt/lib/alg-des-tables.c +3858 -0
- data/ext/libxcrypt/lib/alg-des.c +269 -0
- data/ext/libxcrypt/lib/alg-des.h +74 -0
- data/ext/libxcrypt/lib/alg-gost3411-2012-const.h +313 -0
- data/ext/libxcrypt/lib/alg-gost3411-2012-core.c +238 -0
- data/ext/libxcrypt/lib/alg-gost3411-2012-core.h +51 -0
- data/ext/libxcrypt/lib/alg-gost3411-2012-hmac.c +78 -0
- data/ext/libxcrypt/lib/alg-gost3411-2012-hmac.h +46 -0
- data/ext/libxcrypt/lib/alg-gost3411-2012-precalc.h +1426 -0
- data/ext/libxcrypt/lib/alg-gost3411-2012-ref.h +67 -0
- data/ext/libxcrypt/lib/alg-hmac-sha1.c +140 -0
- data/ext/libxcrypt/lib/alg-hmac-sha1.h +35 -0
- data/ext/libxcrypt/lib/alg-md4.c +270 -0
- data/ext/libxcrypt/lib/alg-md4.h +43 -0
- data/ext/libxcrypt/lib/alg-md5.c +291 -0
- data/ext/libxcrypt/lib/alg-md5.h +43 -0
- data/ext/libxcrypt/lib/alg-sha1.c +288 -0
- data/ext/libxcrypt/lib/alg-sha1.h +34 -0
- data/ext/libxcrypt/lib/alg-sha256.c +630 -0
- data/ext/libxcrypt/lib/alg-sha256.h +123 -0
- data/ext/libxcrypt/lib/alg-sha512.c +311 -0
- data/ext/libxcrypt/lib/alg-sha512.h +81 -0
- data/ext/libxcrypt/lib/alg-sm3-hmac.c +113 -0
- data/ext/libxcrypt/lib/alg-sm3-hmac.h +42 -0
- data/ext/libxcrypt/lib/alg-sm3.c +449 -0
- data/ext/libxcrypt/lib/alg-sm3.h +63 -0
- data/ext/libxcrypt/lib/alg-yescrypt-common.c +713 -0
- data/ext/libxcrypt/lib/alg-yescrypt-opt.c +1568 -0
- data/ext/libxcrypt/lib/alg-yescrypt-platform.c +106 -0
- data/ext/libxcrypt/lib/alg-yescrypt.h +360 -0
- data/ext/libxcrypt/lib/byteorder.h +164 -0
- data/ext/libxcrypt/lib/crypt-bcrypt.c +1061 -0
- data/ext/libxcrypt/lib/crypt-des-obsolete.c +215 -0
- data/ext/libxcrypt/lib/crypt-des.c +491 -0
- data/ext/libxcrypt/lib/crypt-gensalt-static.c +40 -0
- data/ext/libxcrypt/lib/crypt-gost-yescrypt.c +182 -0
- data/ext/libxcrypt/lib/crypt-md5.c +232 -0
- data/ext/libxcrypt/lib/crypt-nthash.c +134 -0
- data/ext/libxcrypt/lib/crypt-obsolete.h +40 -0
- data/ext/libxcrypt/lib/crypt-pbkdf1-sha1.c +260 -0
- data/ext/libxcrypt/lib/crypt-port.h +514 -0
- data/ext/libxcrypt/lib/crypt-scrypt.c +247 -0
- data/ext/libxcrypt/lib/crypt-sha256.c +308 -0
- data/ext/libxcrypt/lib/crypt-sha512.c +323 -0
- data/ext/libxcrypt/lib/crypt-sm3-yescrypt.c +189 -0
- data/ext/libxcrypt/lib/crypt-sm3.c +308 -0
- data/ext/libxcrypt/lib/crypt-static.c +44 -0
- data/ext/libxcrypt/lib/crypt-sunmd5.c +314 -0
- data/ext/libxcrypt/lib/crypt-yescrypt.c +177 -0
- data/ext/libxcrypt/lib/crypt.c +421 -0
- data/ext/libxcrypt/lib/crypt.h.in +249 -0
- data/ext/libxcrypt/lib/gen-des-tables.c +363 -0
- data/ext/libxcrypt/lib/hashes.conf +59 -0
- data/ext/libxcrypt/lib/libcrypt.map.in +48 -0
- data/ext/libxcrypt/lib/libcrypt.minver +97 -0
- data/ext/libxcrypt/lib/libxcrypt.pc.in +15 -0
- data/ext/libxcrypt/lib/util-base64.c +26 -0
- data/ext/libxcrypt/lib/util-gensalt-sha.c +88 -0
- data/ext/libxcrypt/lib/util-get-random-bytes.c +154 -0
- data/ext/libxcrypt/lib/util-make-failure-token.c +48 -0
- data/ext/libxcrypt/lib/util-xbzero.c +43 -0
- data/ext/libxcrypt/lib/util-xstrcpy.c +42 -0
- data/ext/libxcrypt/lib/xcrypt.h.in +58 -0
- data/ext/libxcrypt/libxcrypt.spec.rpkg +481 -0
- data/ext/libxcrypt/rpkg.conf +2 -0
- data/ext/libxcrypt/rpkg.macros +86 -0
- data/ext/libxcrypt/test/TestCommon.pm +326 -0
- data/ext/libxcrypt/test/alg-des.c +80 -0
- data/ext/libxcrypt/test/alg-gost3411-2012-hmac.c +90 -0
- data/ext/libxcrypt/test/alg-gost3411-2012.c +191 -0
- data/ext/libxcrypt/test/alg-hmac-sha1.c +187 -0
- data/ext/libxcrypt/test/alg-md4.c +111 -0
- data/ext/libxcrypt/test/alg-md5.c +134 -0
- data/ext/libxcrypt/test/alg-pbkdf-hmac-sha256.c +269 -0
- data/ext/libxcrypt/test/alg-sha1.c +111 -0
- data/ext/libxcrypt/test/alg-sha256.c +141 -0
- data/ext/libxcrypt/test/alg-sha512.c +170 -0
- data/ext/libxcrypt/test/alg-sm3-hmac.c +149 -0
- data/ext/libxcrypt/test/alg-sm3.c +168 -0
- data/ext/libxcrypt/test/alg-yescrypt.c +466 -0
- data/ext/libxcrypt/test/badsalt.c +726 -0
- data/ext/libxcrypt/test/badsetting.c +350 -0
- data/ext/libxcrypt/test/byteorder.c +254 -0
- data/ext/libxcrypt/test/checksalt.c +265 -0
- data/ext/libxcrypt/test/compile-strong-alias.c +43 -0
- data/ext/libxcrypt/test/crypt-badargs.c +392 -0
- data/ext/libxcrypt/test/crypt-gost-yescrypt.c +149 -0
- data/ext/libxcrypt/test/crypt-nested-call.c +180 -0
- data/ext/libxcrypt/test/crypt-sm3-yescrypt.c +149 -0
- data/ext/libxcrypt/test/crypt-too-long-phrase.c +157 -0
- data/ext/libxcrypt/test/des-cases.h +196 -0
- data/ext/libxcrypt/test/des-obsolete.c +206 -0
- data/ext/libxcrypt/test/des-obsolete_r.c +207 -0
- data/ext/libxcrypt/test/explicit-bzero.c +334 -0
- data/ext/libxcrypt/test/gensalt-bcrypt_x.c +54 -0
- data/ext/libxcrypt/test/gensalt-extradata.c +246 -0
- data/ext/libxcrypt/test/gensalt-nested-call.c +126 -0
- data/ext/libxcrypt/test/gensalt-nthash.c +65 -0
- data/ext/libxcrypt/test/gensalt.c +599 -0
- data/ext/libxcrypt/test/getrandom-fallbacks.c +295 -0
- data/ext/libxcrypt/test/getrandom-interface.c +211 -0
- data/ext/libxcrypt/test/ka-table-gen.py +945 -0
- data/ext/libxcrypt/test/ka-table.inc +5849 -0
- data/ext/libxcrypt/test/ka-tester.c +240 -0
- data/ext/libxcrypt/test/preferred-method.c +133 -0
- data/ext/libxcrypt/test/short-outbuf.c +119 -0
- data/ext/libxcrypt/test/special-char-salt.c +1160 -0
- data/ext/libxcrypt/test/symbols-compat.pl +137 -0
- data/ext/libxcrypt/test/symbols-renames.pl +107 -0
- data/ext/libxcrypt/test/symbols-static.pl +87 -0
- data/ext/xcrypt/xcrypt.c +9 -0
- data/lib/xcrypt/ffi.rb +76 -0
- data/lib/xcrypt/version.rb +5 -0
- data/lib/xcrypt.rb +89 -0
- metadata +267 -0
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/* Copyright (C) 2013 Alexander Peslyak
|
|
2
|
+
* Copyright (C) 2018 Björn Esser <besser82@fedoraproject.org>
|
|
3
|
+
*
|
|
4
|
+
* Redistribution and use in source and binary forms, with or without
|
|
5
|
+
* modification, are permitted.
|
|
6
|
+
*
|
|
7
|
+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
8
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
9
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
10
|
+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
11
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
12
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
13
|
+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
14
|
+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
15
|
+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
16
|
+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
17
|
+
* SUCH DAMAGE.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
#include "crypt-port.h"
|
|
21
|
+
#include "crypt-hashes.h"
|
|
22
|
+
|
|
23
|
+
#include <errno.h>
|
|
24
|
+
|
|
25
|
+
#if INCLUDE_scrypt
|
|
26
|
+
|
|
27
|
+
static int
|
|
28
|
+
check_salt_char (char ch)
|
|
29
|
+
{
|
|
30
|
+
if (ch > 'z')
|
|
31
|
+
return 0;
|
|
32
|
+
if (ch >= 'a')
|
|
33
|
+
return 1;
|
|
34
|
+
if (ch > 'Z')
|
|
35
|
+
return 0;
|
|
36
|
+
if (ch >= 'A')
|
|
37
|
+
return 1;
|
|
38
|
+
if (ch > '9')
|
|
39
|
+
return 0;
|
|
40
|
+
if (ch >= '.' || ch == '$')
|
|
41
|
+
return 1;
|
|
42
|
+
return 0;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static int
|
|
46
|
+
verify_salt (const char *setting, size_t set_size)
|
|
47
|
+
{
|
|
48
|
+
for (size_t i = 3 + 1 + 5 * 2; i < set_size; i++)
|
|
49
|
+
{
|
|
50
|
+
if (!check_salt_char (setting[i]))
|
|
51
|
+
{
|
|
52
|
+
/* Salt is terminated properly.
|
|
53
|
+
Following characters don't matter. */
|
|
54
|
+
if (setting[i - 1] == '$')
|
|
55
|
+
break;
|
|
56
|
+
|
|
57
|
+
/* Salt has an invalid character. */
|
|
58
|
+
return 0;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return 1;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
static uint8_t *
|
|
65
|
+
encode64_uint32 (uint8_t * dst, ssize_t dstlen,
|
|
66
|
+
uint32_t src, uint32_t srcbits)
|
|
67
|
+
{
|
|
68
|
+
uint32_t bit;
|
|
69
|
+
|
|
70
|
+
for (bit = 0; bit < srcbits; bit += 6)
|
|
71
|
+
{
|
|
72
|
+
if (dstlen < 1)
|
|
73
|
+
{
|
|
74
|
+
errno = ERANGE;
|
|
75
|
+
return NULL;
|
|
76
|
+
}
|
|
77
|
+
*dst++ = ascii64[src & 0x3f];
|
|
78
|
+
dstlen--;
|
|
79
|
+
src >>= 6;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
*dst = '\0';
|
|
83
|
+
return dst;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
static uint8_t *
|
|
87
|
+
encode64 (uint8_t * dst, ssize_t dstlen,
|
|
88
|
+
const uint8_t * src, size_t srclen)
|
|
89
|
+
{
|
|
90
|
+
size_t i;
|
|
91
|
+
|
|
92
|
+
for (i = 0; i < srclen; )
|
|
93
|
+
{
|
|
94
|
+
uint8_t * dnext;
|
|
95
|
+
uint32_t value = 0, bits = 0;
|
|
96
|
+
do
|
|
97
|
+
{
|
|
98
|
+
value |= (uint32_t) src[i++] << bits;
|
|
99
|
+
bits += 8;
|
|
100
|
+
}
|
|
101
|
+
while (bits < 24 && i < srclen);
|
|
102
|
+
dnext = encode64_uint32 (dst, dstlen, value, bits);
|
|
103
|
+
if (!dnext)
|
|
104
|
+
{
|
|
105
|
+
errno = ERANGE;
|
|
106
|
+
return NULL;
|
|
107
|
+
}
|
|
108
|
+
dstlen -= (dnext - dst);
|
|
109
|
+
dst = dnext;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
*dst = '\0';
|
|
113
|
+
return dst;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
static uint32_t
|
|
117
|
+
N2log2 (uint64_t N)
|
|
118
|
+
{
|
|
119
|
+
uint32_t N_log2;
|
|
120
|
+
|
|
121
|
+
if (N < 2)
|
|
122
|
+
return 0;
|
|
123
|
+
|
|
124
|
+
N_log2 = 2;
|
|
125
|
+
while (N >> N_log2 != 0)
|
|
126
|
+
N_log2++;
|
|
127
|
+
N_log2--;
|
|
128
|
+
|
|
129
|
+
if (N >> N_log2 != 1)
|
|
130
|
+
return 0;
|
|
131
|
+
|
|
132
|
+
return N_log2;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/*
|
|
136
|
+
* Wrapper for crypt_yescrypt_rn to compute the hash.
|
|
137
|
+
*/
|
|
138
|
+
void
|
|
139
|
+
crypt_scrypt_rn (const char *phrase, size_t phr_size,
|
|
140
|
+
const char *setting, size_t set_size,
|
|
141
|
+
uint8_t *output, size_t o_size,
|
|
142
|
+
void *scratch, size_t s_size)
|
|
143
|
+
{
|
|
144
|
+
if (o_size < set_size + 1 + 43 + 1 ||
|
|
145
|
+
CRYPT_OUTPUT_SIZE < set_size + 1 + 43 + 1)
|
|
146
|
+
{
|
|
147
|
+
errno = ERANGE;
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/* Setting is invalid. */
|
|
152
|
+
if (strncmp (setting, "$7$", 3) || !verify_salt (setting, set_size))
|
|
153
|
+
{
|
|
154
|
+
errno = EINVAL;
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
crypt_yescrypt_rn (phrase, phr_size, setting, set_size,
|
|
159
|
+
output, o_size, scratch, s_size);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
void
|
|
164
|
+
gensalt_scrypt_rn (unsigned long count,
|
|
165
|
+
const uint8_t *rbytes, size_t nrbytes,
|
|
166
|
+
uint8_t *output, size_t o_size)
|
|
167
|
+
{
|
|
168
|
+
/* Up to 512 bits (64 bytes) of entropy for computing the salt portion
|
|
169
|
+
of the MCF-setting are supported. */
|
|
170
|
+
nrbytes = (nrbytes > 64 ? 64 : nrbytes);
|
|
171
|
+
|
|
172
|
+
if (o_size < 3 + 1 + 5 * 2 + BASE64_LEN (nrbytes) + 1 ||
|
|
173
|
+
CRYPT_GENSALT_OUTPUT_SIZE < 3 + 1 + 5 * 2 + BASE64_LEN (nrbytes) + 1)
|
|
174
|
+
{
|
|
175
|
+
errno = ERANGE;
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if ((count > 0 && count < 6) || count > 11 || nrbytes < 16)
|
|
180
|
+
{
|
|
181
|
+
errno = EINVAL;
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/* Temporary buffer for operation. The buffer is guaranteed to be
|
|
186
|
+
large enough to hold the maximum size of the generated salt. */
|
|
187
|
+
uint8_t outbuf[CRYPT_GENSALT_OUTPUT_SIZE];
|
|
188
|
+
uint8_t *out_p = outbuf + 4;
|
|
189
|
+
ssize_t out_s = CRYPT_GENSALT_OUTPUT_SIZE - (out_p - outbuf);
|
|
190
|
+
|
|
191
|
+
/* Valid cost parameters are from 6 to 11. The default is 7.
|
|
192
|
+
Any cost parameter below 6 is not to be considered strong
|
|
193
|
+
enough anymore, because using less than 32 MiBytes of RAM
|
|
194
|
+
when computing a hash is even weaker than bcrypt ($2y$).
|
|
195
|
+
These are used to set scrypt's 'N' and 'r' parameters as
|
|
196
|
+
follows:
|
|
197
|
+
N (block count) is specified in units of r (block size,
|
|
198
|
+
adjustable in steps of 128 bytes).
|
|
199
|
+
|
|
200
|
+
128 bytes * r = size of each memory block
|
|
201
|
+
|
|
202
|
+
128 bytes * r * N = total amount of memory used for hashing
|
|
203
|
+
in N blocks of r * 128 bytes.
|
|
204
|
+
|
|
205
|
+
The author of yescrypt recommends in the documentation to use
|
|
206
|
+
r=8 (a block size of 1 KiB) for total sizes of 2 MiB and less,
|
|
207
|
+
and r=32 (a block size of 4KiB) above that.
|
|
208
|
+
This has to do with the typical per-core last-level cache sizes
|
|
209
|
+
of current CPUs. */
|
|
210
|
+
if (count == 0)
|
|
211
|
+
count = 7;
|
|
212
|
+
|
|
213
|
+
uint32_t p = 1;
|
|
214
|
+
uint32_t r = 32;
|
|
215
|
+
uint64_t N = 1ULL << (count + 7); // 6 -> 8192, 7 -> 16384, ... 11 -> 262144
|
|
216
|
+
|
|
217
|
+
if (out_s > (ssize_t) BASE64_LEN (30))
|
|
218
|
+
{
|
|
219
|
+
outbuf[0] = '$';
|
|
220
|
+
outbuf[1] = '7';
|
|
221
|
+
outbuf[2] = '$';
|
|
222
|
+
outbuf[3] = ascii64[N2log2 (N)];
|
|
223
|
+
|
|
224
|
+
out_p = encode64_uint32 (out_p, out_s, r, 30);
|
|
225
|
+
out_s -= (out_p - outbuf);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (out_p && out_s > (ssize_t) BASE64_LEN (30))
|
|
229
|
+
{
|
|
230
|
+
out_p = encode64_uint32 (out_p, out_s, p, 30);
|
|
231
|
+
out_s -= (out_p - outbuf);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (out_p && out_s > (ssize_t) BASE64_LEN (nrbytes))
|
|
235
|
+
{
|
|
236
|
+
out_p = encode64 (out_p, out_s, rbytes, nrbytes);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (out_p)
|
|
240
|
+
{
|
|
241
|
+
strcpy_or_abort (output, o_size, outbuf);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
#endif /* INCLUDE_scrypt */
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
/* One way encryption based on the SHA256-based Unix crypt implementation.
|
|
2
|
+
*
|
|
3
|
+
* Written by Ulrich Drepper <drepper at redhat.com> in 2007 [1].
|
|
4
|
+
* Modified by Zack Weinberg <zackw at panix.com> in 2017, 2018.
|
|
5
|
+
* Composed by Björn Esser <besser82 at fedoraproject.org> in 2018.
|
|
6
|
+
* Modified by Björn Esser <besser82 at fedoraproject.org> in 2020.
|
|
7
|
+
* To the extent possible under law, the named authors have waived all
|
|
8
|
+
* copyright and related or neighboring rights to this work.
|
|
9
|
+
*
|
|
10
|
+
* See https://creativecommons.org/publicdomain/zero/1.0/ for further
|
|
11
|
+
* details.
|
|
12
|
+
*
|
|
13
|
+
* This file is a modified except from [2], lines 648 up to 909.
|
|
14
|
+
*
|
|
15
|
+
* [1] https://www.akkadia.org/drepper/sha-crypt.html
|
|
16
|
+
* [2] https://www.akkadia.org/drepper/SHA-crypt.txt
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
#include "crypt-port.h"
|
|
20
|
+
#include "alg-sha256.h"
|
|
21
|
+
|
|
22
|
+
#include <errno.h>
|
|
23
|
+
#include <stdio.h>
|
|
24
|
+
#include <stdlib.h>
|
|
25
|
+
|
|
26
|
+
#if INCLUDE_sha256crypt
|
|
27
|
+
|
|
28
|
+
/* Define our magic string to mark salt for SHA256 "encryption"
|
|
29
|
+
replacement. */
|
|
30
|
+
static const char sha256_salt_prefix[] = "$5$";
|
|
31
|
+
|
|
32
|
+
/* Prefix for optional rounds specification. */
|
|
33
|
+
static const char sha256_rounds_prefix[] = "rounds=";
|
|
34
|
+
|
|
35
|
+
/* Maximum salt string length. */
|
|
36
|
+
#define SALT_LEN_MAX 16
|
|
37
|
+
/* Default number of rounds if not explicitly specified. */
|
|
38
|
+
#define ROUNDS_DEFAULT 5000
|
|
39
|
+
/* Minimum number of rounds. */
|
|
40
|
+
#define ROUNDS_MIN 1000
|
|
41
|
+
/* Maximum number of rounds. */
|
|
42
|
+
#define ROUNDS_MAX 999999999
|
|
43
|
+
|
|
44
|
+
/* The maximum possible length of a SHA256-hashed password string,
|
|
45
|
+
including the terminating NUL character. Prefix (including its NUL)
|
|
46
|
+
+ rounds tag ("rounds=$" = "rounds=\0") + strlen(ROUNDS_MAX)
|
|
47
|
+
+ salt (up to SALT_LEN_MAX chars) + '$' + hash (43 chars). */
|
|
48
|
+
|
|
49
|
+
#define LENGTH_OF_NUMBER(n) (sizeof #n - 1)
|
|
50
|
+
|
|
51
|
+
#define SHA256_HASH_LENGTH \
|
|
52
|
+
(sizeof (sha256_salt_prefix) + sizeof (sha256_rounds_prefix) + \
|
|
53
|
+
LENGTH_OF_NUMBER (ROUNDS_MAX) + SALT_LEN_MAX + 1 + 43)
|
|
54
|
+
|
|
55
|
+
static_assert (SHA256_HASH_LENGTH <= CRYPT_OUTPUT_SIZE,
|
|
56
|
+
"CRYPT_OUTPUT_SIZE is too small for SHA256");
|
|
57
|
+
|
|
58
|
+
/* A sha256_buffer holds all of the sensitive intermediate data. */
|
|
59
|
+
struct sha256_buffer
|
|
60
|
+
{
|
|
61
|
+
SHA256_CTX ctx;
|
|
62
|
+
uint8_t result[32];
|
|
63
|
+
uint8_t p_bytes[32];
|
|
64
|
+
uint8_t s_bytes[32];
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
static_assert (sizeof (struct sha256_buffer) <= ALG_SPECIFIC_SIZE,
|
|
68
|
+
"ALG_SPECIFIC_SIZE is too small for SHA256");
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
/* Feed CTX with LEN bytes of a virtual byte sequence consisting of
|
|
72
|
+
BLOCK repeated over and over indefinitely. */
|
|
73
|
+
static void
|
|
74
|
+
SHA256_Update_recycled (SHA256_CTX *ctx,
|
|
75
|
+
unsigned char block[32], size_t len)
|
|
76
|
+
{
|
|
77
|
+
size_t cnt;
|
|
78
|
+
for (cnt = len; cnt >= 32; cnt -= 32)
|
|
79
|
+
SHA256_Update (ctx, block, 32);
|
|
80
|
+
SHA256_Update (ctx, block, cnt);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
void
|
|
84
|
+
crypt_sha256crypt_rn (const char *phrase, size_t phr_size,
|
|
85
|
+
const char *setting, size_t ARG_UNUSED (set_size),
|
|
86
|
+
uint8_t *output, size_t out_size,
|
|
87
|
+
void *scratch, size_t scr_size)
|
|
88
|
+
{
|
|
89
|
+
/* This shouldn't ever happen, but... */
|
|
90
|
+
if (out_size < SHA256_HASH_LENGTH
|
|
91
|
+
|| scr_size < sizeof (struct sha256_buffer))
|
|
92
|
+
{
|
|
93
|
+
errno = ERANGE;
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
struct sha256_buffer *buf = scratch;
|
|
98
|
+
SHA256_CTX *ctx = &buf->ctx;
|
|
99
|
+
uint8_t *result = buf->result;
|
|
100
|
+
uint8_t *p_bytes = buf->p_bytes;
|
|
101
|
+
uint8_t *s_bytes = buf->s_bytes;
|
|
102
|
+
char *cp = (char *)output;
|
|
103
|
+
const char *salt = setting;
|
|
104
|
+
|
|
105
|
+
size_t salt_size;
|
|
106
|
+
size_t cnt;
|
|
107
|
+
/* Default number of rounds. */
|
|
108
|
+
size_t rounds = ROUNDS_DEFAULT;
|
|
109
|
+
bool rounds_custom = false;
|
|
110
|
+
|
|
111
|
+
/* Find beginning of salt string. The prefix should normally always
|
|
112
|
+
be present. Just in case it is not. */
|
|
113
|
+
if (strncmp (sha256_salt_prefix, salt, sizeof (sha256_salt_prefix) - 1) == 0)
|
|
114
|
+
/* Skip salt prefix. */
|
|
115
|
+
salt += sizeof (sha256_salt_prefix) - 1;
|
|
116
|
+
|
|
117
|
+
if (strncmp (salt, sha256_rounds_prefix, sizeof (sha256_rounds_prefix) - 1)
|
|
118
|
+
== 0)
|
|
119
|
+
{
|
|
120
|
+
const char *num = salt + sizeof (sha256_rounds_prefix) - 1;
|
|
121
|
+
/* Do not allow an explicit setting of zero rounds, nor of the
|
|
122
|
+
default number of rounds, nor leading zeroes on the rounds. */
|
|
123
|
+
if (!(*num >= '1' && *num <= '9'))
|
|
124
|
+
{
|
|
125
|
+
errno = EINVAL;
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
errno = 0;
|
|
130
|
+
char *endp;
|
|
131
|
+
rounds = strtoul (num, &endp, 10);
|
|
132
|
+
if (endp == num || *endp != '$'
|
|
133
|
+
|| rounds < ROUNDS_MIN
|
|
134
|
+
|| rounds > ROUNDS_MAX
|
|
135
|
+
|| errno)
|
|
136
|
+
{
|
|
137
|
+
errno = EINVAL;
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
salt = endp + 1;
|
|
141
|
+
rounds_custom = true;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/* The salt ends at the next '$' or the end of the string.
|
|
145
|
+
Ensure ':' does not appear in the salt (it is used as a separator in /etc/passwd).
|
|
146
|
+
Also check for '\n', as in /etc/passwd the whole parameters of the user data must
|
|
147
|
+
be on a single line. */
|
|
148
|
+
salt_size = strcspn (salt, "$:\n");
|
|
149
|
+
if (!(salt[salt_size] == '$' || !salt[salt_size]))
|
|
150
|
+
{
|
|
151
|
+
errno = EINVAL;
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/* Ensure we do not use more salt than SALT_LEN_MAX. */
|
|
156
|
+
if (salt_size > SALT_LEN_MAX)
|
|
157
|
+
salt_size = SALT_LEN_MAX;
|
|
158
|
+
|
|
159
|
+
/* Compute alternate SHA256 sum with input PHRASE, SALT, and PHRASE. The
|
|
160
|
+
final result will be added to the first context. */
|
|
161
|
+
SHA256_Init (ctx);
|
|
162
|
+
|
|
163
|
+
/* Add phrase. */
|
|
164
|
+
SHA256_Update (ctx, phrase, phr_size);
|
|
165
|
+
|
|
166
|
+
/* Add salt. */
|
|
167
|
+
SHA256_Update (ctx, salt, salt_size);
|
|
168
|
+
|
|
169
|
+
/* Add phrase again. */
|
|
170
|
+
SHA256_Update (ctx, phrase, phr_size);
|
|
171
|
+
|
|
172
|
+
/* Now get result of this (32 bytes). */
|
|
173
|
+
SHA256_Final (result, ctx);
|
|
174
|
+
|
|
175
|
+
/* Prepare for the real work. */
|
|
176
|
+
SHA256_Init (ctx);
|
|
177
|
+
|
|
178
|
+
/* Add the phrase string. */
|
|
179
|
+
SHA256_Update (ctx, phrase, phr_size);
|
|
180
|
+
|
|
181
|
+
/* The last part is the salt string. This must be at most 8
|
|
182
|
+
characters and it ends at the first `$' character (for
|
|
183
|
+
compatibility with existing implementations). */
|
|
184
|
+
SHA256_Update (ctx, salt, salt_size);
|
|
185
|
+
|
|
186
|
+
/* Add for any character in the phrase one byte of the alternate sum. */
|
|
187
|
+
for (cnt = phr_size; cnt > 32; cnt -= 32)
|
|
188
|
+
SHA256_Update (ctx, result, 32);
|
|
189
|
+
SHA256_Update (ctx, result, cnt);
|
|
190
|
+
|
|
191
|
+
/* Take the binary representation of the length of the phrase and for every
|
|
192
|
+
1 add the alternate sum, for every 0 the phrase. */
|
|
193
|
+
for (cnt = phr_size; cnt > 0; cnt >>= 1)
|
|
194
|
+
if ((cnt & 1) != 0)
|
|
195
|
+
SHA256_Update (ctx, result, 32);
|
|
196
|
+
else
|
|
197
|
+
SHA256_Update (ctx, phrase, phr_size);
|
|
198
|
+
|
|
199
|
+
/* Create intermediate result. */
|
|
200
|
+
SHA256_Final (result, ctx);
|
|
201
|
+
|
|
202
|
+
/* Start computation of P byte sequence. */
|
|
203
|
+
SHA256_Init (ctx);
|
|
204
|
+
|
|
205
|
+
/* For every character in the password add the entire password. */
|
|
206
|
+
for (cnt = 0; cnt < phr_size; ++cnt)
|
|
207
|
+
SHA256_Update (ctx, phrase, phr_size);
|
|
208
|
+
|
|
209
|
+
/* Finish the digest. */
|
|
210
|
+
SHA256_Final (p_bytes, ctx);
|
|
211
|
+
|
|
212
|
+
/* Start computation of S byte sequence. */
|
|
213
|
+
SHA256_Init (ctx);
|
|
214
|
+
|
|
215
|
+
/* For every character in the password add the entire password. */
|
|
216
|
+
for (cnt = 0; cnt < (size_t) 16 + (size_t) result[0]; ++cnt)
|
|
217
|
+
SHA256_Update (ctx, salt, salt_size);
|
|
218
|
+
|
|
219
|
+
/* Finish the digest. */
|
|
220
|
+
SHA256_Final (s_bytes, ctx);
|
|
221
|
+
|
|
222
|
+
/* Repeatedly run the collected hash value through SHA256 to burn
|
|
223
|
+
CPU cycles. */
|
|
224
|
+
for (cnt = 0; cnt < rounds; ++cnt)
|
|
225
|
+
{
|
|
226
|
+
/* New context. */
|
|
227
|
+
SHA256_Init (ctx);
|
|
228
|
+
|
|
229
|
+
/* Add phrase or last result. */
|
|
230
|
+
if ((cnt & 1) != 0)
|
|
231
|
+
SHA256_Update_recycled (ctx, p_bytes, phr_size);
|
|
232
|
+
else
|
|
233
|
+
SHA256_Update (ctx, result, 32);
|
|
234
|
+
|
|
235
|
+
/* Add salt for numbers not divisible by 3. */
|
|
236
|
+
if (cnt % 3 != 0)
|
|
237
|
+
SHA256_Update_recycled (ctx, s_bytes, salt_size);
|
|
238
|
+
|
|
239
|
+
/* Add phrase for numbers not divisible by 7. */
|
|
240
|
+
if (cnt % 7 != 0)
|
|
241
|
+
SHA256_Update_recycled (ctx, p_bytes, phr_size);
|
|
242
|
+
|
|
243
|
+
/* Add phrase or last result. */
|
|
244
|
+
if ((cnt & 1) != 0)
|
|
245
|
+
SHA256_Update (ctx, result, 32);
|
|
246
|
+
else
|
|
247
|
+
SHA256_Update_recycled (ctx, p_bytes, phr_size);
|
|
248
|
+
|
|
249
|
+
/* Create intermediate result. */
|
|
250
|
+
SHA256_Final (result, ctx);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/* Now we can construct the result string. It consists of four
|
|
254
|
+
parts, one of which is optional. We already know that there
|
|
255
|
+
is sufficient space at CP for the longest possible result string. */
|
|
256
|
+
memcpy (cp, sha256_salt_prefix, sizeof (sha256_salt_prefix) - 1);
|
|
257
|
+
cp += sizeof (sha256_salt_prefix) - 1;
|
|
258
|
+
|
|
259
|
+
if (rounds_custom)
|
|
260
|
+
{
|
|
261
|
+
int n = snprintf (cp,
|
|
262
|
+
SHA256_HASH_LENGTH - (sizeof (sha256_salt_prefix) - 1),
|
|
263
|
+
"%s%zu$", sha256_rounds_prefix, rounds);
|
|
264
|
+
cp += n;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
memcpy (cp, salt, salt_size);
|
|
268
|
+
cp += salt_size;
|
|
269
|
+
*cp++ = '$';
|
|
270
|
+
|
|
271
|
+
#define b64_from_24bit(B2, B1, B0, N) \
|
|
272
|
+
do { \
|
|
273
|
+
unsigned int w = ((((unsigned int)(B2)) << 16) | \
|
|
274
|
+
(((unsigned int)(B1)) << 8) | \
|
|
275
|
+
((unsigned int)(B0))); \
|
|
276
|
+
int n = (N); \
|
|
277
|
+
while (n-- > 0) \
|
|
278
|
+
{ \
|
|
279
|
+
*cp++ = b64t[w & 0x3f]; \
|
|
280
|
+
w >>= 6; \
|
|
281
|
+
} \
|
|
282
|
+
} while (0)
|
|
283
|
+
|
|
284
|
+
b64_from_24bit (result[0], result[10], result[20], 4);
|
|
285
|
+
b64_from_24bit (result[21], result[1], result[11], 4);
|
|
286
|
+
b64_from_24bit (result[12], result[22], result[2], 4);
|
|
287
|
+
b64_from_24bit (result[3], result[13], result[23], 4);
|
|
288
|
+
b64_from_24bit (result[24], result[4], result[14], 4);
|
|
289
|
+
b64_from_24bit (result[15], result[25], result[5], 4);
|
|
290
|
+
b64_from_24bit (result[6], result[16], result[26], 4);
|
|
291
|
+
b64_from_24bit (result[27], result[7], result[17], 4);
|
|
292
|
+
b64_from_24bit (result[18], result[28], result[8], 4);
|
|
293
|
+
b64_from_24bit (result[9], result[19], result[29], 4);
|
|
294
|
+
b64_from_24bit (0, result[31], result[30], 3);
|
|
295
|
+
|
|
296
|
+
*cp = '\0';
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
void
|
|
300
|
+
gensalt_sha256crypt_rn (unsigned long count,
|
|
301
|
+
const uint8_t *rbytes, size_t nrbytes,
|
|
302
|
+
uint8_t *output, size_t output_size)
|
|
303
|
+
{
|
|
304
|
+
gensalt_sha_rn ("5", SALT_LEN_MAX, ROUNDS_DEFAULT, ROUNDS_MIN, ROUNDS_MAX,
|
|
305
|
+
count, rbytes, nrbytes, output, output_size);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
#endif
|