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,630 @@
|
|
|
1
|
+
/*-
|
|
2
|
+
* Copyright 2005-2016 Colin Percival
|
|
3
|
+
* Copyright 2016-2018,2021 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
|
+
|
|
28
|
+
#include "crypt-port.h"
|
|
29
|
+
|
|
30
|
+
#if INCLUDE_gost_yescrypt || INCLUDE_yescrypt || INCLUDE_scrypt || \
|
|
31
|
+
INCLUDE_sha256crypt || INCLUDE_sm3_yescrypt
|
|
32
|
+
|
|
33
|
+
#include "alg-sha256.h"
|
|
34
|
+
#include "byteorder.h"
|
|
35
|
+
|
|
36
|
+
#ifdef __ICC
|
|
37
|
+
/* Miscompile with icc 14.0.0 (at least), so don't use restrict there */
|
|
38
|
+
#define restrict
|
|
39
|
+
#elif __STDC_VERSION__ >= 199901L
|
|
40
|
+
/* Have restrict */
|
|
41
|
+
#elif defined(__GNUC__)
|
|
42
|
+
#define restrict __restrict
|
|
43
|
+
#else
|
|
44
|
+
#define restrict
|
|
45
|
+
#endif
|
|
46
|
+
|
|
47
|
+
/* SHA256 round constants. */
|
|
48
|
+
static const uint32_t Krnd[64] = {
|
|
49
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
|
|
50
|
+
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
51
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
|
52
|
+
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
53
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
|
|
54
|
+
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
55
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
|
|
56
|
+
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
57
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
|
58
|
+
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
59
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
|
|
60
|
+
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
61
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
|
|
62
|
+
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
63
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
|
64
|
+
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/* Elementary functions used by SHA256 */
|
|
68
|
+
#define Ch(x, y, z) ((x & (y ^ z)) ^ z)
|
|
69
|
+
#if 1 /* Explicit caching/reuse of common subexpression between rounds */
|
|
70
|
+
#define Maj(x, y, z) (y ^ ((x_xor_y = x ^ y) & y_xor_z))
|
|
71
|
+
#else /* Let the compiler cache/reuse or not */
|
|
72
|
+
#define Maj(x, y, z) (y ^ ((x ^ y) & (y ^ z)))
|
|
73
|
+
#endif
|
|
74
|
+
#define SHR(x, n) (x >> n)
|
|
75
|
+
#define ROTR(x, n) ((x >> n) | (x << (32 - n)))
|
|
76
|
+
#define S0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
|
|
77
|
+
#define S1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
|
|
78
|
+
#define s0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3))
|
|
79
|
+
#define s1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10))
|
|
80
|
+
|
|
81
|
+
/* SHA256 round function */
|
|
82
|
+
#define RND(a, b, c, d, e, f, g, h, k) \
|
|
83
|
+
h += S1(e) + Ch(e, f, g) + k; \
|
|
84
|
+
d += h; \
|
|
85
|
+
h += S0(a) + Maj(a, b, c); \
|
|
86
|
+
y_xor_z = x_xor_y;
|
|
87
|
+
|
|
88
|
+
/* Adjusted round function for rotating state */
|
|
89
|
+
#define RNDr(S, W, i, ii) \
|
|
90
|
+
RND(S[(64 - i) % 8], S[(65 - i) % 8], \
|
|
91
|
+
S[(66 - i) % 8], S[(67 - i) % 8], \
|
|
92
|
+
S[(68 - i) % 8], S[(69 - i) % 8], \
|
|
93
|
+
S[(70 - i) % 8], S[(71 - i) % 8], \
|
|
94
|
+
W[i + ii] + Krnd[i + ii])
|
|
95
|
+
|
|
96
|
+
/* Message schedule computation */
|
|
97
|
+
#define MSCH(W, ii, i) \
|
|
98
|
+
W[i + ii + 16] = s1(W[i + ii + 14]) + W[i + ii + 9] + s0(W[i + ii + 1]) + W[i + ii]
|
|
99
|
+
|
|
100
|
+
/*
|
|
101
|
+
* SHA256 block compression function. The 256-bit state is transformed via
|
|
102
|
+
* the 512-bit input block to produce a new state.
|
|
103
|
+
*/
|
|
104
|
+
static void
|
|
105
|
+
SHA256_Transform(uint32_t state[static restrict 8],
|
|
106
|
+
const uint8_t block[static restrict 64],
|
|
107
|
+
uint32_t W[static restrict 64], uint32_t S[static restrict 8])
|
|
108
|
+
{
|
|
109
|
+
int i;
|
|
110
|
+
|
|
111
|
+
/* 1. Prepare the first part of the message schedule W. */
|
|
112
|
+
be32dec_vect(W, block, 16);
|
|
113
|
+
|
|
114
|
+
/* 2. Initialize working variables. */
|
|
115
|
+
memcpy(S, state, 32);
|
|
116
|
+
|
|
117
|
+
/* 3. Mix. */
|
|
118
|
+
for (i = 0; i <= 48; i += 16) {
|
|
119
|
+
uint32_t x_xor_y, y_xor_z = S[(65 - i) % 8] ^ S[(66 - i) % 8];
|
|
120
|
+
RNDr(S, W, 0, i);
|
|
121
|
+
RNDr(S, W, 1, i);
|
|
122
|
+
RNDr(S, W, 2, i);
|
|
123
|
+
RNDr(S, W, 3, i);
|
|
124
|
+
RNDr(S, W, 4, i);
|
|
125
|
+
RNDr(S, W, 5, i);
|
|
126
|
+
RNDr(S, W, 6, i);
|
|
127
|
+
RNDr(S, W, 7, i);
|
|
128
|
+
RNDr(S, W, 8, i);
|
|
129
|
+
RNDr(S, W, 9, i);
|
|
130
|
+
RNDr(S, W, 10, i);
|
|
131
|
+
RNDr(S, W, 11, i);
|
|
132
|
+
RNDr(S, W, 12, i);
|
|
133
|
+
RNDr(S, W, 13, i);
|
|
134
|
+
RNDr(S, W, 14, i);
|
|
135
|
+
RNDr(S, W, 15, i);
|
|
136
|
+
|
|
137
|
+
if (i == 48)
|
|
138
|
+
break;
|
|
139
|
+
|
|
140
|
+
MSCH(W, 0, i);
|
|
141
|
+
MSCH(W, 1, i);
|
|
142
|
+
MSCH(W, 2, i);
|
|
143
|
+
MSCH(W, 3, i);
|
|
144
|
+
MSCH(W, 4, i);
|
|
145
|
+
MSCH(W, 5, i);
|
|
146
|
+
MSCH(W, 6, i);
|
|
147
|
+
MSCH(W, 7, i);
|
|
148
|
+
MSCH(W, 8, i);
|
|
149
|
+
MSCH(W, 9, i);
|
|
150
|
+
MSCH(W, 10, i);
|
|
151
|
+
MSCH(W, 11, i);
|
|
152
|
+
MSCH(W, 12, i);
|
|
153
|
+
MSCH(W, 13, i);
|
|
154
|
+
MSCH(W, 14, i);
|
|
155
|
+
MSCH(W, 15, i);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/* 4. Mix local working variables into global state. */
|
|
159
|
+
state[0] += S[0];
|
|
160
|
+
state[1] += S[1];
|
|
161
|
+
state[2] += S[2];
|
|
162
|
+
state[3] += S[3];
|
|
163
|
+
state[4] += S[4];
|
|
164
|
+
state[5] += S[5];
|
|
165
|
+
state[6] += S[6];
|
|
166
|
+
state[7] += S[7];
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
static const uint8_t PAD[64] = {
|
|
170
|
+
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
171
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
172
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
173
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
/* Add padding and terminating bit-count. */
|
|
177
|
+
static void
|
|
178
|
+
SHA256_Pad(SHA256_CTX * ctx, uint32_t tmp32[static restrict 72])
|
|
179
|
+
{
|
|
180
|
+
size_t r;
|
|
181
|
+
|
|
182
|
+
/* Figure out how many bytes we have buffered. */
|
|
183
|
+
r = (ctx->count >> 3) & 0x3f;
|
|
184
|
+
|
|
185
|
+
/* Pad to 56 mod 64, transforming if we finish a block en route. */
|
|
186
|
+
if (r < 56) {
|
|
187
|
+
/* Pad to 56 mod 64. */
|
|
188
|
+
memcpy(&ctx->buf[r], PAD, 56 - r);
|
|
189
|
+
} else {
|
|
190
|
+
/* Finish the current block and mix. */
|
|
191
|
+
memcpy(&ctx->buf[r], PAD, 64 - r);
|
|
192
|
+
SHA256_Transform(ctx->state, ctx->buf, &tmp32[0], &tmp32[64]);
|
|
193
|
+
|
|
194
|
+
/* The start of the final block is all zeroes. */
|
|
195
|
+
memset(&ctx->buf[0], 0, 56);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/* Add the terminating bit-count. */
|
|
199
|
+
be64enc(&ctx->buf[56], ctx->count);
|
|
200
|
+
|
|
201
|
+
/* Mix in the final block. */
|
|
202
|
+
SHA256_Transform(ctx->state, ctx->buf, &tmp32[0], &tmp32[64]);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/* Magic initialization constants. */
|
|
206
|
+
static const uint32_t initial_state[8] = {
|
|
207
|
+
0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
|
|
208
|
+
0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* SHA256_Init(ctx):
|
|
213
|
+
* Initialize the SHA256 context ${ctx}.
|
|
214
|
+
*/
|
|
215
|
+
void
|
|
216
|
+
SHA256_Init(SHA256_CTX * ctx)
|
|
217
|
+
{
|
|
218
|
+
|
|
219
|
+
/* Zero bits processed so far. */
|
|
220
|
+
ctx->count = 0;
|
|
221
|
+
|
|
222
|
+
/* Initialize state. */
|
|
223
|
+
memcpy(ctx->state, initial_state, sizeof(initial_state));
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* SHA256_Update(ctx, in, len):
|
|
228
|
+
* Input ${len} bytes from ${in} into the SHA256 context ${ctx}.
|
|
229
|
+
*/
|
|
230
|
+
static void
|
|
231
|
+
_SHA256_Update(SHA256_CTX * ctx, const void * in, size_t len,
|
|
232
|
+
uint32_t tmp32[static restrict 72])
|
|
233
|
+
{
|
|
234
|
+
uint32_t r;
|
|
235
|
+
const uint8_t * src = in;
|
|
236
|
+
|
|
237
|
+
/* Return immediately if we have nothing to do. */
|
|
238
|
+
if (len == 0)
|
|
239
|
+
return;
|
|
240
|
+
|
|
241
|
+
/* Number of bytes left in the buffer from previous updates. */
|
|
242
|
+
r = (ctx->count >> 3) & 0x3f;
|
|
243
|
+
|
|
244
|
+
/* Update number of bits. */
|
|
245
|
+
ctx->count += (uint64_t)(len) << 3;
|
|
246
|
+
|
|
247
|
+
/* Handle the case where we don't need to perform any transforms. */
|
|
248
|
+
if (len < 64 - r) {
|
|
249
|
+
memcpy(&ctx->buf[r], src, len);
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/* Finish the current block. */
|
|
254
|
+
memcpy(&ctx->buf[r], src, 64 - r);
|
|
255
|
+
SHA256_Transform(ctx->state, ctx->buf, &tmp32[0], &tmp32[64]);
|
|
256
|
+
src += 64 - r;
|
|
257
|
+
len -= 64 - r;
|
|
258
|
+
|
|
259
|
+
/* Perform complete blocks. */
|
|
260
|
+
while (len >= 64) {
|
|
261
|
+
SHA256_Transform(ctx->state, src, &tmp32[0], &tmp32[64]);
|
|
262
|
+
src += 64;
|
|
263
|
+
len -= 64;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/* Copy left over data into buffer. */
|
|
267
|
+
memcpy(ctx->buf, src, len);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/* Wrapper function for intermediate-values sanitization. */
|
|
271
|
+
void
|
|
272
|
+
SHA256_Update(SHA256_CTX * ctx, const void * in, size_t len)
|
|
273
|
+
{
|
|
274
|
+
uint32_t tmp32[72];
|
|
275
|
+
|
|
276
|
+
/* Call the real function. */
|
|
277
|
+
_SHA256_Update(ctx, in, len, tmp32);
|
|
278
|
+
|
|
279
|
+
/* Clean the stack. */
|
|
280
|
+
explicit_bzero(tmp32, 288);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* SHA256_Final(digest, ctx):
|
|
285
|
+
* Output the SHA256 hash of the data input to the context ${ctx} into the
|
|
286
|
+
* buffer ${digest}.
|
|
287
|
+
*/
|
|
288
|
+
static void
|
|
289
|
+
_SHA256_Final(uint8_t digest[32], SHA256_CTX * ctx,
|
|
290
|
+
uint32_t tmp32[static restrict 72])
|
|
291
|
+
{
|
|
292
|
+
|
|
293
|
+
/* Add padding. */
|
|
294
|
+
SHA256_Pad(ctx, tmp32);
|
|
295
|
+
|
|
296
|
+
/* Write the hash. */
|
|
297
|
+
be32enc_vect(digest, ctx->state, 8);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/* Wrapper function for intermediate-values sanitization. */
|
|
301
|
+
void
|
|
302
|
+
SHA256_Final(uint8_t digest[32], SHA256_CTX * ctx)
|
|
303
|
+
{
|
|
304
|
+
uint32_t tmp32[72];
|
|
305
|
+
|
|
306
|
+
/* Call the real function. */
|
|
307
|
+
_SHA256_Final(digest, ctx, tmp32);
|
|
308
|
+
|
|
309
|
+
/* Clear the context state. */
|
|
310
|
+
explicit_bzero(ctx, sizeof(SHA256_CTX));
|
|
311
|
+
|
|
312
|
+
/* Clean the stack. */
|
|
313
|
+
explicit_bzero(tmp32, 288);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* SHA256_Buf(in, len, digest):
|
|
318
|
+
* Compute the SHA256 hash of ${len} bytes from ${in} and write it to ${digest}.
|
|
319
|
+
*/
|
|
320
|
+
void
|
|
321
|
+
SHA256_Buf(const void * in, size_t len, uint8_t digest[32])
|
|
322
|
+
{
|
|
323
|
+
SHA256_CTX ctx;
|
|
324
|
+
uint32_t tmp32[72];
|
|
325
|
+
|
|
326
|
+
SHA256_Init(&ctx);
|
|
327
|
+
_SHA256_Update(&ctx, in, len, tmp32);
|
|
328
|
+
_SHA256_Final(digest, &ctx, tmp32);
|
|
329
|
+
|
|
330
|
+
/* Clean the stack. */
|
|
331
|
+
explicit_bzero(&ctx, sizeof(SHA256_CTX));
|
|
332
|
+
explicit_bzero(tmp32, 288);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
#endif /* INCLUDE_gost_yescrypt || INCLUDE_yescrypt || INCLUDE_scrypt ||
|
|
336
|
+
INCLUDE_sha256crypt || INCLUDE_sm3_yescrypt */
|
|
337
|
+
|
|
338
|
+
#if INCLUDE_gost_yescrypt || INCLUDE_yescrypt || INCLUDE_scrypt || \
|
|
339
|
+
INCLUDE_sm3_yescrypt
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* HMAC_SHA256_Init(ctx, K, Klen):
|
|
343
|
+
* Initialize the HMAC-SHA256 context ${ctx} with ${Klen} bytes of key from
|
|
344
|
+
* ${K}.
|
|
345
|
+
*/
|
|
346
|
+
static void
|
|
347
|
+
_HMAC_SHA256_Init(HMAC_SHA256_CTX * ctx, const void * _K, size_t Klen,
|
|
348
|
+
uint32_t tmp32[static restrict 72], uint8_t pad[static restrict 64],
|
|
349
|
+
uint8_t khash[static restrict 32])
|
|
350
|
+
{
|
|
351
|
+
const uint8_t * K = _K;
|
|
352
|
+
size_t i;
|
|
353
|
+
|
|
354
|
+
/* If Klen > 64, the key is really SHA256(K). */
|
|
355
|
+
if (Klen > 64) {
|
|
356
|
+
SHA256_Init(&ctx->ictx);
|
|
357
|
+
_SHA256_Update(&ctx->ictx, K, Klen, tmp32);
|
|
358
|
+
_SHA256_Final(khash, &ctx->ictx, tmp32);
|
|
359
|
+
K = khash;
|
|
360
|
+
Klen = 32;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/* Inner SHA256 operation is SHA256(K xor [block of 0x36] || data). */
|
|
364
|
+
SHA256_Init(&ctx->ictx);
|
|
365
|
+
memset(pad, 0x36, 64);
|
|
366
|
+
for (i = 0; i < Klen; i++)
|
|
367
|
+
pad[i] ^= K[i];
|
|
368
|
+
_SHA256_Update(&ctx->ictx, pad, 64, tmp32);
|
|
369
|
+
|
|
370
|
+
/* Outer SHA256 operation is SHA256(K xor [block of 0x5c] || hash). */
|
|
371
|
+
SHA256_Init(&ctx->octx);
|
|
372
|
+
memset(pad, 0x5c, 64);
|
|
373
|
+
for (i = 0; i < Klen; i++)
|
|
374
|
+
pad[i] ^= K[i];
|
|
375
|
+
_SHA256_Update(&ctx->octx, pad, 64, tmp32);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/* Wrapper function for intermediate-values sanitization. */
|
|
379
|
+
void
|
|
380
|
+
HMAC_SHA256_Init(HMAC_SHA256_CTX * ctx, const void * _K, size_t Klen)
|
|
381
|
+
{
|
|
382
|
+
uint32_t tmp32[72];
|
|
383
|
+
uint8_t pad[64];
|
|
384
|
+
uint8_t khash[32];
|
|
385
|
+
|
|
386
|
+
/* Call the real function. */
|
|
387
|
+
_HMAC_SHA256_Init(ctx, _K, Klen, tmp32, pad, khash);
|
|
388
|
+
|
|
389
|
+
/* Clean the stack. */
|
|
390
|
+
explicit_bzero(tmp32, 288);
|
|
391
|
+
explicit_bzero(khash, 32);
|
|
392
|
+
explicit_bzero(pad, 64);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* HMAC_SHA256_Update(ctx, in, len):
|
|
397
|
+
* Input ${len} bytes from ${in} into the HMAC-SHA256 context ${ctx}.
|
|
398
|
+
*/
|
|
399
|
+
static void
|
|
400
|
+
_HMAC_SHA256_Update(HMAC_SHA256_CTX * ctx, const void * in, size_t len,
|
|
401
|
+
uint32_t tmp32[static restrict 72])
|
|
402
|
+
{
|
|
403
|
+
|
|
404
|
+
/* Feed data to the inner SHA256 operation. */
|
|
405
|
+
_SHA256_Update(&ctx->ictx, in, len, tmp32);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/* Wrapper function for intermediate-values sanitization. */
|
|
409
|
+
void
|
|
410
|
+
HMAC_SHA256_Update(HMAC_SHA256_CTX * ctx, const void * in, size_t len)
|
|
411
|
+
{
|
|
412
|
+
uint32_t tmp32[72];
|
|
413
|
+
|
|
414
|
+
/* Call the real function. */
|
|
415
|
+
_HMAC_SHA256_Update(ctx, in, len, tmp32);
|
|
416
|
+
|
|
417
|
+
/* Clean the stack. */
|
|
418
|
+
explicit_bzero(tmp32, 288);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* HMAC_SHA256_Final(digest, ctx):
|
|
423
|
+
* Output the HMAC-SHA256 of the data input to the context ${ctx} into the
|
|
424
|
+
* buffer ${digest}.
|
|
425
|
+
*/
|
|
426
|
+
static void
|
|
427
|
+
_HMAC_SHA256_Final(uint8_t digest[32], HMAC_SHA256_CTX * ctx,
|
|
428
|
+
uint32_t tmp32[static restrict 72], uint8_t ihash[static restrict 32])
|
|
429
|
+
{
|
|
430
|
+
|
|
431
|
+
/* Finish the inner SHA256 operation. */
|
|
432
|
+
_SHA256_Final(ihash, &ctx->ictx, tmp32);
|
|
433
|
+
|
|
434
|
+
/* Feed the inner hash to the outer SHA256 operation. */
|
|
435
|
+
_SHA256_Update(&ctx->octx, ihash, 32, tmp32);
|
|
436
|
+
|
|
437
|
+
/* Finish the outer SHA256 operation. */
|
|
438
|
+
_SHA256_Final(digest, &ctx->octx, tmp32);
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/* Wrapper function for intermediate-values sanitization. */
|
|
442
|
+
void
|
|
443
|
+
HMAC_SHA256_Final(uint8_t digest[32], HMAC_SHA256_CTX * ctx)
|
|
444
|
+
{
|
|
445
|
+
uint32_t tmp32[72];
|
|
446
|
+
uint8_t ihash[32];
|
|
447
|
+
|
|
448
|
+
/* Call the real function. */
|
|
449
|
+
_HMAC_SHA256_Final(digest, ctx, tmp32, ihash);
|
|
450
|
+
|
|
451
|
+
/* Clear the context state. */
|
|
452
|
+
explicit_bzero(ctx, sizeof(HMAC_SHA256_CTX));
|
|
453
|
+
|
|
454
|
+
/* Clean the stack. */
|
|
455
|
+
explicit_bzero(tmp32, 288);
|
|
456
|
+
explicit_bzero(ihash, 32);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* HMAC_SHA256_Buf(K, Klen, in, len, digest):
|
|
461
|
+
* Compute the HMAC-SHA256 of ${len} bytes from ${in} using the key ${K} of
|
|
462
|
+
* length ${Klen}, and write the result to ${digest}.
|
|
463
|
+
*/
|
|
464
|
+
void
|
|
465
|
+
HMAC_SHA256_Buf(const void * K, size_t Klen, const void * in, size_t len,
|
|
466
|
+
uint8_t digest[32])
|
|
467
|
+
{
|
|
468
|
+
HMAC_SHA256_CTX ctx;
|
|
469
|
+
uint32_t tmp32[72];
|
|
470
|
+
uint8_t tmp8[96];
|
|
471
|
+
|
|
472
|
+
_HMAC_SHA256_Init(&ctx, K, Klen, tmp32, &tmp8[0], &tmp8[64]);
|
|
473
|
+
_HMAC_SHA256_Update(&ctx, in, len, tmp32);
|
|
474
|
+
_HMAC_SHA256_Final(digest, &ctx, tmp32, &tmp8[0]);
|
|
475
|
+
|
|
476
|
+
/* Clean the stack. */
|
|
477
|
+
explicit_bzero(&ctx, sizeof(HMAC_SHA256_CTX));
|
|
478
|
+
explicit_bzero(tmp32, 288);
|
|
479
|
+
explicit_bzero(tmp8, 96);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/* Add padding and terminating bit-count, but don't invoke Transform yet. */
|
|
483
|
+
static int
|
|
484
|
+
SHA256_Pad_Almost(SHA256_CTX * ctx, uint8_t len[static restrict 8],
|
|
485
|
+
uint32_t tmp32[static restrict 72])
|
|
486
|
+
{
|
|
487
|
+
uint32_t r;
|
|
488
|
+
|
|
489
|
+
r = (ctx->count >> 3) & 0x3f;
|
|
490
|
+
if (r >= 56)
|
|
491
|
+
return -1;
|
|
492
|
+
|
|
493
|
+
/*
|
|
494
|
+
* Convert length to a vector of bytes -- we do this now rather
|
|
495
|
+
* than later because the length will change after we pad.
|
|
496
|
+
*/
|
|
497
|
+
be64enc(len, ctx->count);
|
|
498
|
+
|
|
499
|
+
/* Add 1--56 bytes so that the resulting length is 56 mod 64. */
|
|
500
|
+
_SHA256_Update(ctx, PAD, 56 - r, tmp32);
|
|
501
|
+
|
|
502
|
+
/* Add the terminating bit-count. */
|
|
503
|
+
ctx->buf[63] = len[7];
|
|
504
|
+
_SHA256_Update(ctx, len, 7, tmp32);
|
|
505
|
+
|
|
506
|
+
return 0;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
|
|
511
|
+
* Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and
|
|
512
|
+
* write the output to buf. The value dkLen must be at most 32 * (2^32 - 1).
|
|
513
|
+
*/
|
|
514
|
+
void
|
|
515
|
+
PBKDF2_SHA256(const uint8_t * passwd, size_t passwdlen, const uint8_t * salt,
|
|
516
|
+
size_t saltlen, uint64_t c, uint8_t * buf, size_t dkLen)
|
|
517
|
+
{
|
|
518
|
+
HMAC_SHA256_CTX Phctx, PShctx, hctx;
|
|
519
|
+
uint32_t tmp32[72];
|
|
520
|
+
union {
|
|
521
|
+
uint8_t tmp8[96];
|
|
522
|
+
uint32_t state[8];
|
|
523
|
+
} u;
|
|
524
|
+
size_t i;
|
|
525
|
+
uint8_t ivec[4];
|
|
526
|
+
uint8_t U[32];
|
|
527
|
+
uint8_t T[32];
|
|
528
|
+
uint64_t j;
|
|
529
|
+
int k;
|
|
530
|
+
size_t clen;
|
|
531
|
+
|
|
532
|
+
/* Sanity-check. */
|
|
533
|
+
assert(dkLen <= 32 * (size_t)(UINT32_MAX));
|
|
534
|
+
|
|
535
|
+
if (c == 1 && (dkLen & 31) == 0 && (saltlen & 63) <= 51) {
|
|
536
|
+
uint32_t oldcount;
|
|
537
|
+
uint8_t * ivecp;
|
|
538
|
+
|
|
539
|
+
/* Compute HMAC state after processing P and S. */
|
|
540
|
+
_HMAC_SHA256_Init(&hctx, passwd, passwdlen,
|
|
541
|
+
tmp32, &u.tmp8[0], &u.tmp8[64]);
|
|
542
|
+
_HMAC_SHA256_Update(&hctx, salt, saltlen, tmp32);
|
|
543
|
+
|
|
544
|
+
/* Prepare ictx padding. */
|
|
545
|
+
oldcount = hctx.ictx.count & (0x3f << 3);
|
|
546
|
+
_HMAC_SHA256_Update(&hctx, "\0\0\0", 4, tmp32);
|
|
547
|
+
if ((hctx.ictx.count & (0x3f << 3)) < oldcount ||
|
|
548
|
+
SHA256_Pad_Almost(&hctx.ictx, u.tmp8, tmp32))
|
|
549
|
+
goto generic; /* Can't happen due to saltlen check */
|
|
550
|
+
ivecp = hctx.ictx.buf + (oldcount >> 3);
|
|
551
|
+
|
|
552
|
+
/* Prepare octx padding. */
|
|
553
|
+
hctx.octx.count += 32 << 3;
|
|
554
|
+
SHA256_Pad_Almost(&hctx.octx, u.tmp8, tmp32);
|
|
555
|
+
|
|
556
|
+
/* Iterate through the blocks. */
|
|
557
|
+
for (i = 0; i * 32 < dkLen; i++) {
|
|
558
|
+
/* Generate INT(i + 1). */
|
|
559
|
+
be32enc(ivecp, (uint32_t)(i + 1));
|
|
560
|
+
|
|
561
|
+
/* Compute U_1 = PRF(P, S || INT(i)). */
|
|
562
|
+
memcpy(u.state, hctx.ictx.state, sizeof(u.state));
|
|
563
|
+
SHA256_Transform(u.state, hctx.ictx.buf,
|
|
564
|
+
&tmp32[0], &tmp32[64]);
|
|
565
|
+
be32enc_vect(hctx.octx.buf, u.state, 8);
|
|
566
|
+
memcpy(u.state, hctx.octx.state, sizeof(u.state));
|
|
567
|
+
SHA256_Transform(u.state, hctx.octx.buf,
|
|
568
|
+
&tmp32[0], &tmp32[64]);
|
|
569
|
+
be32enc_vect(&buf[i * 32], u.state, 8);
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
goto cleanup;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
generic:
|
|
576
|
+
/* Compute HMAC state after processing P. */
|
|
577
|
+
_HMAC_SHA256_Init(&Phctx, passwd, passwdlen,
|
|
578
|
+
tmp32, &u.tmp8[0], &u.tmp8[64]);
|
|
579
|
+
|
|
580
|
+
/* Compute HMAC state after processing P and S. */
|
|
581
|
+
memcpy(&PShctx, &Phctx, sizeof(HMAC_SHA256_CTX));
|
|
582
|
+
_HMAC_SHA256_Update(&PShctx, salt, saltlen, tmp32);
|
|
583
|
+
|
|
584
|
+
/* Iterate through the blocks. */
|
|
585
|
+
for (i = 0; i * 32 < dkLen; i++) {
|
|
586
|
+
/* Generate INT(i + 1). */
|
|
587
|
+
be32enc(ivec, (uint32_t)(i + 1));
|
|
588
|
+
|
|
589
|
+
/* Compute U_1 = PRF(P, S || INT(i)). */
|
|
590
|
+
memcpy(&hctx, &PShctx, sizeof(HMAC_SHA256_CTX));
|
|
591
|
+
_HMAC_SHA256_Update(&hctx, ivec, 4, tmp32);
|
|
592
|
+
_HMAC_SHA256_Final(T, &hctx, tmp32, u.tmp8);
|
|
593
|
+
|
|
594
|
+
if (c > 1) {
|
|
595
|
+
/* T_i = U_1 ... */
|
|
596
|
+
memcpy(U, T, 32);
|
|
597
|
+
|
|
598
|
+
for (j = 2; j <= c; j++) {
|
|
599
|
+
/* Compute U_j. */
|
|
600
|
+
memcpy(&hctx, &Phctx, sizeof(HMAC_SHA256_CTX));
|
|
601
|
+
_HMAC_SHA256_Update(&hctx, U, 32, tmp32);
|
|
602
|
+
_HMAC_SHA256_Final(U, &hctx, tmp32, u.tmp8);
|
|
603
|
+
|
|
604
|
+
/* ... xor U_j ... */
|
|
605
|
+
for (k = 0; k < 32; k++)
|
|
606
|
+
T[k] ^= U[k];
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/* Copy as many bytes as necessary into buf. */
|
|
611
|
+
clen = dkLen - i * 32;
|
|
612
|
+
if (clen > 32)
|
|
613
|
+
clen = 32;
|
|
614
|
+
memcpy(&buf[i * 32], T, clen);
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
/* Clean the stack. */
|
|
618
|
+
explicit_bzero(&Phctx, sizeof(HMAC_SHA256_CTX));
|
|
619
|
+
explicit_bzero(&PShctx, sizeof(HMAC_SHA256_CTX));
|
|
620
|
+
explicit_bzero(U, 32);
|
|
621
|
+
explicit_bzero(T, 32);
|
|
622
|
+
|
|
623
|
+
cleanup:
|
|
624
|
+
explicit_bzero(&hctx, sizeof(HMAC_SHA256_CTX));
|
|
625
|
+
explicit_bzero(tmp32, 288);
|
|
626
|
+
explicit_bzero(&u, sizeof(u));
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
#endif /* INCLUDE_gost_yescrypt || INCLUDE_yescrypt || INCLUDE_scrypt ||
|
|
630
|
+
INCLUDE_sm3_yescrypt */
|