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,123 @@
|
|
|
1
|
+
/*-
|
|
2
|
+
* Copyright 2005-2016 Colin Percival
|
|
3
|
+
* All rights reserved.
|
|
4
|
+
*
|
|
5
|
+
* Redistribution and use in source and binary forms, with or without
|
|
6
|
+
* modification, are permitted provided that the following conditions
|
|
7
|
+
* are met:
|
|
8
|
+
* 1. Redistributions of source code must retain the above copyright
|
|
9
|
+
* notice, this list of conditions and the following disclaimer.
|
|
10
|
+
* 2. Redistributions in binary form must reproduce the above copyright
|
|
11
|
+
* notice, this list of conditions and the following disclaimer in the
|
|
12
|
+
* documentation and/or other materials provided with the distribution.
|
|
13
|
+
*
|
|
14
|
+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
15
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
16
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
17
|
+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
18
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
19
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
20
|
+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
21
|
+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
22
|
+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
23
|
+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
24
|
+
* SUCH DAMAGE.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
#ifndef _SHA256_H_
|
|
28
|
+
#define _SHA256_H_
|
|
29
|
+
|
|
30
|
+
#include "crypt-port.h"
|
|
31
|
+
|
|
32
|
+
#include <stddef.h>
|
|
33
|
+
#include <stdint.h>
|
|
34
|
+
|
|
35
|
+
/*
|
|
36
|
+
* Use #defines in order to avoid namespace collisions with anyone else's
|
|
37
|
+
* SHA256 code (e.g., the code in OpenSSL).
|
|
38
|
+
*/
|
|
39
|
+
#define SHA256_Init libcperciva_SHA256_Init
|
|
40
|
+
#define SHA256_Update libcperciva_SHA256_Update
|
|
41
|
+
#define SHA256_Final libcperciva_SHA256_Final
|
|
42
|
+
#define SHA256_Buf libcperciva_SHA256_Buf
|
|
43
|
+
#define SHA256_CTX libcperciva_SHA256_CTX
|
|
44
|
+
#define HMAC_SHA256_Init libcperciva_HMAC_SHA256_Init
|
|
45
|
+
#define HMAC_SHA256_Update libcperciva_HMAC_SHA256_Update
|
|
46
|
+
#define HMAC_SHA256_Final libcperciva_HMAC_SHA256_Final
|
|
47
|
+
#define HMAC_SHA256_Buf libcperciva_HMAC_SHA256_Buf
|
|
48
|
+
#define HMAC_SHA256_CTX libcperciva_HMAC_SHA256_CTX
|
|
49
|
+
|
|
50
|
+
/* Context structure for SHA256 operations. */
|
|
51
|
+
typedef struct {
|
|
52
|
+
uint32_t state[8];
|
|
53
|
+
uint64_t count;
|
|
54
|
+
uint8_t buf[64];
|
|
55
|
+
} SHA256_CTX;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* SHA256_Init(ctx):
|
|
59
|
+
* Initialize the SHA256 context ${ctx}.
|
|
60
|
+
*/
|
|
61
|
+
extern void SHA256_Init(SHA256_CTX *);
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* SHA256_Update(ctx, in, len):
|
|
65
|
+
* Input ${len} bytes from ${in} into the SHA256 context ${ctx}.
|
|
66
|
+
*/
|
|
67
|
+
extern void SHA256_Update(SHA256_CTX *, const void *, size_t);
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* SHA256_Final(digest, ctx):
|
|
71
|
+
* Output the SHA256 hash of the data input to the context ${ctx} into the
|
|
72
|
+
* buffer ${digest}.
|
|
73
|
+
*/
|
|
74
|
+
extern void SHA256_Final(uint8_t[32], SHA256_CTX *);
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* SHA256_Buf(in, len, digest):
|
|
78
|
+
* Compute the SHA256 hash of ${len} bytes from ${in} and write it to ${digest}.
|
|
79
|
+
*/
|
|
80
|
+
extern void SHA256_Buf(const void *, size_t, uint8_t[32]);
|
|
81
|
+
|
|
82
|
+
/* Context structure for HMAC-SHA256 operations. */
|
|
83
|
+
typedef struct {
|
|
84
|
+
SHA256_CTX ictx;
|
|
85
|
+
SHA256_CTX octx;
|
|
86
|
+
} HMAC_SHA256_CTX;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* HMAC_SHA256_Init(ctx, K, Klen):
|
|
90
|
+
* Initialize the HMAC-SHA256 context ${ctx} with ${Klen} bytes of key from
|
|
91
|
+
* ${K}.
|
|
92
|
+
*/
|
|
93
|
+
extern void HMAC_SHA256_Init(HMAC_SHA256_CTX *, const void *, size_t);
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* HMAC_SHA256_Update(ctx, in, len):
|
|
97
|
+
* Input ${len} bytes from ${in} into the HMAC-SHA256 context ${ctx}.
|
|
98
|
+
*/
|
|
99
|
+
extern void HMAC_SHA256_Update(HMAC_SHA256_CTX *, const void *, size_t);
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* HMAC_SHA256_Final(digest, ctx):
|
|
103
|
+
* Output the HMAC-SHA256 of the data input to the context ${ctx} into the
|
|
104
|
+
* buffer ${digest}.
|
|
105
|
+
*/
|
|
106
|
+
extern void HMAC_SHA256_Final(uint8_t[32], HMAC_SHA256_CTX *);
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* HMAC_SHA256_Buf(K, Klen, in, len, digest):
|
|
110
|
+
* Compute the HMAC-SHA256 of ${len} bytes from ${in} using the key ${K} of
|
|
111
|
+
* length ${Klen}, and write the result to ${digest}.
|
|
112
|
+
*/
|
|
113
|
+
extern void HMAC_SHA256_Buf(const void *, size_t, const void *, size_t, uint8_t[32]);
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
|
|
117
|
+
* Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and
|
|
118
|
+
* write the output to buf. The value dkLen must be at most 32 * (2^32 - 1).
|
|
119
|
+
*/
|
|
120
|
+
extern void PBKDF2_SHA256(const uint8_t *, size_t, const uint8_t *, size_t,
|
|
121
|
+
uint64_t, uint8_t *, size_t);
|
|
122
|
+
|
|
123
|
+
#endif /* !_SHA256_H_ */
|
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
/*-
|
|
2
|
+
* Copyright 2005 Colin Percival
|
|
3
|
+
* Copyright (c) 2015 Allan Jude <allanjude@FreeBSD.org>
|
|
4
|
+
* Copyright 2021, 2022 Alexander Peslyak
|
|
5
|
+
* All rights reserved.
|
|
6
|
+
*
|
|
7
|
+
* Redistribution and use in source and binary forms, with or without
|
|
8
|
+
* modification, are permitted provided that the following conditions
|
|
9
|
+
* are met:
|
|
10
|
+
* 1. Redistributions of source code must retain the above copyright
|
|
11
|
+
* notice, this list of conditions and the following disclaimer.
|
|
12
|
+
* 2. Redistributions in binary form must reproduce the above copyright
|
|
13
|
+
* notice, this list of conditions and the following disclaimer in the
|
|
14
|
+
* documentation and/or other materials provided with the distribution.
|
|
15
|
+
*
|
|
16
|
+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
17
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
18
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
19
|
+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
20
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
21
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
22
|
+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
23
|
+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
24
|
+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
25
|
+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
26
|
+
* SUCH DAMAGE.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
#include "crypt-port.h"
|
|
30
|
+
|
|
31
|
+
#if INCLUDE_sha512crypt
|
|
32
|
+
|
|
33
|
+
#include "alg-sha512.h"
|
|
34
|
+
#include "byteorder.h"
|
|
35
|
+
|
|
36
|
+
/* SHA512 round constants. */
|
|
37
|
+
static const uint64_t K[80] = {
|
|
38
|
+
0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
|
|
39
|
+
0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
|
|
40
|
+
0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
|
|
41
|
+
0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
|
|
42
|
+
0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
|
|
43
|
+
0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
|
|
44
|
+
0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
|
|
45
|
+
0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
|
|
46
|
+
0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
|
|
47
|
+
0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
|
|
48
|
+
0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
|
|
49
|
+
0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
|
|
50
|
+
0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
|
|
51
|
+
0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
|
|
52
|
+
0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
|
|
53
|
+
0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
|
|
54
|
+
0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
|
|
55
|
+
0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
|
|
56
|
+
0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
|
|
57
|
+
0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
|
|
58
|
+
0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
|
|
59
|
+
0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
|
|
60
|
+
0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
|
|
61
|
+
0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
|
|
62
|
+
0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
|
|
63
|
+
0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
|
|
64
|
+
0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
|
|
65
|
+
0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
|
|
66
|
+
0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
|
|
67
|
+
0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
|
|
68
|
+
0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
|
|
69
|
+
0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
|
|
70
|
+
0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
|
|
71
|
+
0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
|
|
72
|
+
0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
|
|
73
|
+
0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
|
|
74
|
+
0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
|
|
75
|
+
0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
|
|
76
|
+
0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
|
|
77
|
+
0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/* Elementary functions used by SHA512 */
|
|
81
|
+
#define Ch(x, y, z) ((x & (y ^ z)) ^ z)
|
|
82
|
+
#if 1 /* Explicit caching/reuse of common subexpression between rounds */
|
|
83
|
+
#define Maj(x, y, z) (y ^ ((x_xor_y = x ^ y) & y_xor_z))
|
|
84
|
+
#else /* Let the compiler cache/reuse or not */
|
|
85
|
+
#define Maj(x, y, z) (y ^ ((x ^ y) & (y ^ z)))
|
|
86
|
+
#endif
|
|
87
|
+
#define SHR(x, n) (x >> n)
|
|
88
|
+
#define ROTR(x, n) ((x >> n) | (x << (64 - n)))
|
|
89
|
+
#define S0(x) (ROTR(x, 28) ^ ROTR(x, 34) ^ ROTR(x, 39))
|
|
90
|
+
#define S1(x) (ROTR(x, 14) ^ ROTR(x, 18) ^ ROTR(x, 41))
|
|
91
|
+
#define s0(x) (ROTR(x, 1) ^ ROTR(x, 8) ^ SHR(x, 7))
|
|
92
|
+
#define s1(x) (ROTR(x, 19) ^ ROTR(x, 61) ^ SHR(x, 6))
|
|
93
|
+
|
|
94
|
+
/* SHA512 round function */
|
|
95
|
+
#define RND(a, b, c, d, e, f, g, h, k) \
|
|
96
|
+
h += S1(e) + Ch(e, f, g) + k; \
|
|
97
|
+
d += h; \
|
|
98
|
+
h += S0(a) + Maj(a, b, c); \
|
|
99
|
+
y_xor_z = x_xor_y;
|
|
100
|
+
|
|
101
|
+
/* Adjusted round function for rotating state */
|
|
102
|
+
#define RNDr(S, W, i, ii) \
|
|
103
|
+
RND(S[(80 - i) % 8], S[(81 - i) % 8], \
|
|
104
|
+
S[(82 - i) % 8], S[(83 - i) % 8], \
|
|
105
|
+
S[(84 - i) % 8], S[(85 - i) % 8], \
|
|
106
|
+
S[(86 - i) % 8], S[(87 - i) % 8], \
|
|
107
|
+
W[i + ii] + K[i + ii])
|
|
108
|
+
|
|
109
|
+
/* Message schedule computation */
|
|
110
|
+
#define MSCH(W, ii, i) \
|
|
111
|
+
W[i + ii + 16] = s1(W[i + ii + 14]) + W[i + ii + 9] + s0(W[i + ii + 1]) + W[i + ii]
|
|
112
|
+
|
|
113
|
+
/*
|
|
114
|
+
* SHA512 block compression function. The 512-bit state is transformed via
|
|
115
|
+
* the 512-bit input block to produce a new state.
|
|
116
|
+
*/
|
|
117
|
+
static void
|
|
118
|
+
SHA512_Transform(uint64_t * state, const unsigned char block[SHA512_BLOCK_LENGTH])
|
|
119
|
+
{
|
|
120
|
+
uint64_t W[80];
|
|
121
|
+
uint64_t S[8];
|
|
122
|
+
int i;
|
|
123
|
+
|
|
124
|
+
/* 1. Prepare the first part of the message schedule W. */
|
|
125
|
+
be64dec_vect(W, block, SHA512_BLOCK_LENGTH/8);
|
|
126
|
+
|
|
127
|
+
/* 2. Initialize working variables. */
|
|
128
|
+
memcpy(S, state, SHA512_DIGEST_LENGTH);
|
|
129
|
+
|
|
130
|
+
/* 3. Mix. */
|
|
131
|
+
for (i = 0; i < 80; i += 16) {
|
|
132
|
+
uint64_t x_xor_y, y_xor_z = S[(65 - i) % 8] ^ S[(66 - i) % 8];
|
|
133
|
+
RNDr(S, W, 0, i);
|
|
134
|
+
RNDr(S, W, 1, i);
|
|
135
|
+
RNDr(S, W, 2, i);
|
|
136
|
+
RNDr(S, W, 3, i);
|
|
137
|
+
RNDr(S, W, 4, i);
|
|
138
|
+
RNDr(S, W, 5, i);
|
|
139
|
+
RNDr(S, W, 6, i);
|
|
140
|
+
RNDr(S, W, 7, i);
|
|
141
|
+
RNDr(S, W, 8, i);
|
|
142
|
+
RNDr(S, W, 9, i);
|
|
143
|
+
RNDr(S, W, 10, i);
|
|
144
|
+
RNDr(S, W, 11, i);
|
|
145
|
+
RNDr(S, W, 12, i);
|
|
146
|
+
RNDr(S, W, 13, i);
|
|
147
|
+
RNDr(S, W, 14, i);
|
|
148
|
+
RNDr(S, W, 15, i);
|
|
149
|
+
|
|
150
|
+
if (i == 64)
|
|
151
|
+
break;
|
|
152
|
+
MSCH(W, 0, i);
|
|
153
|
+
MSCH(W, 1, i);
|
|
154
|
+
MSCH(W, 2, i);
|
|
155
|
+
MSCH(W, 3, i);
|
|
156
|
+
MSCH(W, 4, i);
|
|
157
|
+
MSCH(W, 5, i);
|
|
158
|
+
MSCH(W, 6, i);
|
|
159
|
+
MSCH(W, 7, i);
|
|
160
|
+
MSCH(W, 8, i);
|
|
161
|
+
MSCH(W, 9, i);
|
|
162
|
+
MSCH(W, 10, i);
|
|
163
|
+
MSCH(W, 11, i);
|
|
164
|
+
MSCH(W, 12, i);
|
|
165
|
+
MSCH(W, 13, i);
|
|
166
|
+
MSCH(W, 14, i);
|
|
167
|
+
MSCH(W, 15, i);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/* 4. Mix local working variables into global state */
|
|
171
|
+
for (i = 0; i < 8; i++)
|
|
172
|
+
state[i] += S[i];
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
static const unsigned char PAD[SHA512_BLOCK_LENGTH] = {
|
|
176
|
+
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
177
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
178
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
179
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
180
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
181
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
182
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
183
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
/* Add padding and terminating bit-count. */
|
|
187
|
+
static void
|
|
188
|
+
SHA512_Pad(SHA512_CTX * ctx)
|
|
189
|
+
{
|
|
190
|
+
size_t r;
|
|
191
|
+
|
|
192
|
+
/* Figure out how many bytes we have buffered. */
|
|
193
|
+
r = (ctx->count[1] >> 3) & 0x7f;
|
|
194
|
+
|
|
195
|
+
/* Pad to 112 mod 128, transforming if we finish a block en route. */
|
|
196
|
+
if (r < 112) {
|
|
197
|
+
/* Pad to 112 mod 128. */
|
|
198
|
+
memcpy(&ctx->buf[r], PAD, 112 - r);
|
|
199
|
+
} else {
|
|
200
|
+
/* Finish the current block and mix. */
|
|
201
|
+
memcpy(&ctx->buf[r], PAD, 128 - r);
|
|
202
|
+
SHA512_Transform(ctx->state, ctx->buf);
|
|
203
|
+
|
|
204
|
+
/* The start of the final block is all zeroes. */
|
|
205
|
+
memset(&ctx->buf[0], 0, 112);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/* Add the terminating bit-count. */
|
|
209
|
+
be64enc_vect(&ctx->buf[112], ctx->count, 2);
|
|
210
|
+
|
|
211
|
+
/* Mix in the final block. */
|
|
212
|
+
SHA512_Transform(ctx->state, ctx->buf);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/* SHA-512 initialization. Begins a SHA-512 operation. */
|
|
216
|
+
void
|
|
217
|
+
SHA512_Init(SHA512_CTX * ctx)
|
|
218
|
+
{
|
|
219
|
+
|
|
220
|
+
/* Zero bits processed so far */
|
|
221
|
+
ctx->count[0] = ctx->count[1] = 0;
|
|
222
|
+
|
|
223
|
+
/* Magic initialization constants */
|
|
224
|
+
ctx->state[0] = 0x6a09e667f3bcc908ULL;
|
|
225
|
+
ctx->state[1] = 0xbb67ae8584caa73bULL;
|
|
226
|
+
ctx->state[2] = 0x3c6ef372fe94f82bULL;
|
|
227
|
+
ctx->state[3] = 0xa54ff53a5f1d36f1ULL;
|
|
228
|
+
ctx->state[4] = 0x510e527fade682d1ULL;
|
|
229
|
+
ctx->state[5] = 0x9b05688c2b3e6c1fULL;
|
|
230
|
+
ctx->state[6] = 0x1f83d9abfb41bd6bULL;
|
|
231
|
+
ctx->state[7] = 0x5be0cd19137e2179ULL;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/* Add bytes into the hash */
|
|
235
|
+
void
|
|
236
|
+
SHA512_Update(SHA512_CTX * ctx, const void *in, size_t len)
|
|
237
|
+
{
|
|
238
|
+
uint64_t bitlen[2];
|
|
239
|
+
size_t r;
|
|
240
|
+
const unsigned char *src = in;
|
|
241
|
+
|
|
242
|
+
/* Number of bytes left in the buffer from previous updates */
|
|
243
|
+
r = (ctx->count[1] >> 3) & 0x7f;
|
|
244
|
+
|
|
245
|
+
/* Convert the length into a number of bits */
|
|
246
|
+
bitlen[1] = ((uint64_t)len) << 3;
|
|
247
|
+
bitlen[0] = ((uint64_t)len) >> 61;
|
|
248
|
+
|
|
249
|
+
/* Update number of bits */
|
|
250
|
+
if ((ctx->count[1] += bitlen[1]) < bitlen[1])
|
|
251
|
+
ctx->count[0]++;
|
|
252
|
+
ctx->count[0] += bitlen[0];
|
|
253
|
+
|
|
254
|
+
/* Handle the case where we don't need to perform any transforms */
|
|
255
|
+
if (len < SHA512_BLOCK_LENGTH - r) {
|
|
256
|
+
memcpy(&ctx->buf[r], src, len);
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/* Finish the current block */
|
|
261
|
+
memcpy(&ctx->buf[r], src, SHA512_BLOCK_LENGTH - r);
|
|
262
|
+
SHA512_Transform(ctx->state, ctx->buf);
|
|
263
|
+
src += SHA512_BLOCK_LENGTH - r;
|
|
264
|
+
len -= SHA512_BLOCK_LENGTH - r;
|
|
265
|
+
|
|
266
|
+
/* Perform complete blocks */
|
|
267
|
+
while (len >= SHA512_BLOCK_LENGTH) {
|
|
268
|
+
SHA512_Transform(ctx->state, src);
|
|
269
|
+
src += SHA512_BLOCK_LENGTH;
|
|
270
|
+
len -= SHA512_BLOCK_LENGTH;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/* Copy left over data into buffer */
|
|
274
|
+
memcpy(ctx->buf, src, len);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/*
|
|
278
|
+
* SHA-512 finalization. Pads the input data, exports the hash value,
|
|
279
|
+
* and clears the context state.
|
|
280
|
+
*/
|
|
281
|
+
void
|
|
282
|
+
SHA512_Final(unsigned char digest[MIN_SIZE(SHA512_DIGEST_LENGTH)],
|
|
283
|
+
SHA512_CTX *ctx)
|
|
284
|
+
{
|
|
285
|
+
|
|
286
|
+
/* Add padding */
|
|
287
|
+
SHA512_Pad(ctx);
|
|
288
|
+
|
|
289
|
+
/* Write the hash */
|
|
290
|
+
be64enc_vect(digest, ctx->state, SHA512_DIGEST_LENGTH/8);
|
|
291
|
+
|
|
292
|
+
/* Clear the context state */
|
|
293
|
+
explicit_bzero(ctx, sizeof(*ctx));
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* SHA512_Buf(in, len, digest):
|
|
298
|
+
* Compute the SHA512 hash of ${len} bytes from ${in} and write it to ${digest}.
|
|
299
|
+
*/
|
|
300
|
+
void
|
|
301
|
+
SHA512_Buf(const void * in, size_t len,
|
|
302
|
+
unsigned char digest[MIN_SIZE(SHA512_DIGEST_LENGTH)])
|
|
303
|
+
{
|
|
304
|
+
SHA512_CTX ctx;
|
|
305
|
+
|
|
306
|
+
SHA512_Init(&ctx);
|
|
307
|
+
SHA512_Update(&ctx, in, len);
|
|
308
|
+
SHA512_Final(digest, &ctx);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
#endif
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/*-
|
|
2
|
+
* Copyright 2005 Colin Percival
|
|
3
|
+
* All rights reserved.
|
|
4
|
+
*
|
|
5
|
+
* Redistribution and use in source and binary forms, with or without
|
|
6
|
+
* modification, are permitted provided that the following conditions
|
|
7
|
+
* are met:
|
|
8
|
+
* 1. Redistributions of source code must retain the above copyright
|
|
9
|
+
* notice, this list of conditions and the following disclaimer.
|
|
10
|
+
* 2. Redistributions in binary form must reproduce the above copyright
|
|
11
|
+
* notice, this list of conditions and the following disclaimer in the
|
|
12
|
+
* documentation and/or other materials provided with the distribution.
|
|
13
|
+
*
|
|
14
|
+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
15
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
16
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
17
|
+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
18
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
19
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
20
|
+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
21
|
+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
22
|
+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
23
|
+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
24
|
+
* SUCH DAMAGE.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
#ifndef _SHA512_H_
|
|
28
|
+
#define _SHA512_H_
|
|
29
|
+
|
|
30
|
+
#include <stddef.h>
|
|
31
|
+
#include <stdint.h>
|
|
32
|
+
|
|
33
|
+
/*
|
|
34
|
+
* Use #defines in order to avoid namespace collisions with anyone else's
|
|
35
|
+
* SHA512 code (e.g., the code in OpenSSL).
|
|
36
|
+
*/
|
|
37
|
+
#define SHA512_Init libcperciva_SHA512_Init
|
|
38
|
+
#define SHA512_Update libcperciva_SHA512_Update
|
|
39
|
+
#define SHA512_Final libcperciva_SHA512_Final
|
|
40
|
+
#define SHA512_Buf libcperciva_SHA512_Buf
|
|
41
|
+
#define SHA512_CTX libcperciva_SHA512_CTX
|
|
42
|
+
|
|
43
|
+
/* Common constants. */
|
|
44
|
+
#define SHA512_BLOCK_LENGTH 128
|
|
45
|
+
#define SHA512_DIGEST_LENGTH 64
|
|
46
|
+
|
|
47
|
+
/* Context structure for SHA512 operations. */
|
|
48
|
+
typedef struct {
|
|
49
|
+
uint64_t state[8];
|
|
50
|
+
uint64_t count[2];
|
|
51
|
+
uint8_t buf[SHA512_BLOCK_LENGTH];
|
|
52
|
+
} SHA512_CTX;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* SHA512_Init(ctx):
|
|
56
|
+
* Initialize the SHA512 context ${ctx}.
|
|
57
|
+
*/
|
|
58
|
+
extern void SHA512_Init(SHA512_CTX *);
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* SHA512_Update(ctx, in, len):
|
|
62
|
+
* Input ${len} bytes from ${in} into the SHA512 context ${ctx}.
|
|
63
|
+
*/
|
|
64
|
+
extern void SHA512_Update(SHA512_CTX *, const void *, size_t);
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* SHA512_Final(digest, ctx):
|
|
68
|
+
* Output the SHA512 hash of the data input to the context ${ctx} into the
|
|
69
|
+
* buffer ${digest}.
|
|
70
|
+
*/
|
|
71
|
+
extern void SHA512_Final(unsigned char[MIN_SIZE(SHA512_DIGEST_LENGTH)],
|
|
72
|
+
SHA512_CTX *);
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* SHA512_Buf(in, len, digest):
|
|
76
|
+
* Compute the SHA512 hash of ${len} bytes from ${in} and write it to ${digest}.
|
|
77
|
+
*/
|
|
78
|
+
extern void SHA512_Buf(const void *, size_t,
|
|
79
|
+
unsigned char[MIN_SIZE(SHA512_DIGEST_LENGTH)]);
|
|
80
|
+
|
|
81
|
+
#endif /* !_SHA512_H_ */
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/* Copyright (C) 2024 Björn Esser <besser82@fedoraproject.org>
|
|
2
|
+
*
|
|
3
|
+
* Redistribution and use in source and binary forms, with or without
|
|
4
|
+
* modification, are permitted.
|
|
5
|
+
*
|
|
6
|
+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
7
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
8
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
9
|
+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
10
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
11
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
12
|
+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
13
|
+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
14
|
+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
15
|
+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
16
|
+
* SUCH DAMAGE.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
#include "crypt-port.h"
|
|
20
|
+
|
|
21
|
+
#if INCLUDE_sm3_yescrypt
|
|
22
|
+
|
|
23
|
+
#include <string.h>
|
|
24
|
+
#include "alg-sm3-hmac.h"
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* HMAC_k(m) = H((k ^ opad), H((k ^ ipad), m))
|
|
28
|
+
* pseudo-code:
|
|
29
|
+
* function hmac(key, message)
|
|
30
|
+
* opad = [0x5c * blocksize]
|
|
31
|
+
* ipad = [0x36 * blocksize]
|
|
32
|
+
* if (length(key) > blocksize) then
|
|
33
|
+
* key = hash(key)
|
|
34
|
+
* end if
|
|
35
|
+
* for i from 0 to length(key) - 1 step 1
|
|
36
|
+
* ipad[i] = ipad[i] XOR key[i]
|
|
37
|
+
* opad[i] = opad[i] XOR key[i]
|
|
38
|
+
* end for
|
|
39
|
+
* return hash(opad || hash(ipad || message))
|
|
40
|
+
* end function
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
#define IPAD 0x36
|
|
44
|
+
#define OPAD 0x5C
|
|
45
|
+
|
|
46
|
+
void
|
|
47
|
+
sm3_hmac_init (sm3_hmac_ctx_t *ctx, const uint8_t *key, size_t key_len)
|
|
48
|
+
{
|
|
49
|
+
/* Initialize */
|
|
50
|
+
memset (ctx, 0, sizeof (sm3_hmac_ctx_t));
|
|
51
|
+
|
|
52
|
+
if (key_len > 64)
|
|
53
|
+
{
|
|
54
|
+
sm3_init (&ctx->sm3_ctx);
|
|
55
|
+
sm3_update (&ctx->sm3_ctx, key, key_len);
|
|
56
|
+
sm3_final (ctx->key, &ctx->sm3_ctx);
|
|
57
|
+
goto end;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
memcpy (ctx->key, key, key_len);
|
|
61
|
+
|
|
62
|
+
end:
|
|
63
|
+
for (int i = 0; i < 64; i++)
|
|
64
|
+
{
|
|
65
|
+
ctx->key[i] ^= IPAD;
|
|
66
|
+
}
|
|
67
|
+
sm3_init (&ctx->sm3_ctx);
|
|
68
|
+
sm3_update (&ctx->sm3_ctx, ctx->key, 64);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
void
|
|
72
|
+
sm3_hmac_update (sm3_hmac_ctx_t *ctx, const uint8_t *data, size_t data_len)
|
|
73
|
+
{
|
|
74
|
+
sm3_update (&ctx->sm3_ctx, data, data_len);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
void
|
|
78
|
+
sm3_hmac_final (sm3_hmac_ctx_t *ctx, uint8_t mac[32])
|
|
79
|
+
{
|
|
80
|
+
for (int i = 0; i < 64; i++)
|
|
81
|
+
{
|
|
82
|
+
ctx->key[i] ^= (IPAD ^ OPAD);
|
|
83
|
+
}
|
|
84
|
+
sm3_final (mac, &ctx->sm3_ctx);
|
|
85
|
+
sm3_init (&ctx->sm3_ctx);
|
|
86
|
+
sm3_update (&ctx->sm3_ctx, ctx->key, 64);
|
|
87
|
+
sm3_update (&ctx->sm3_ctx, mac, 32);
|
|
88
|
+
sm3_final (mac, &ctx->sm3_ctx);
|
|
89
|
+
|
|
90
|
+
/* Zeroize sensitive information. */
|
|
91
|
+
explicit_bzero (ctx, sizeof (sm3_hmac_ctx_t));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
void
|
|
95
|
+
sm3_hmac (const unsigned char *data, size_t data_len,
|
|
96
|
+
const uint8_t *key, size_t key_len,
|
|
97
|
+
uint8_t mac[32], sm3_hmac_ctx_t *ctx)
|
|
98
|
+
{
|
|
99
|
+
sm3_hmac_init (ctx, key, key_len);
|
|
100
|
+
sm3_hmac_update (ctx, data, data_len);
|
|
101
|
+
sm3_hmac_final (ctx, mac);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
void
|
|
105
|
+
sm3_hmac_buf (const unsigned char *data, size_t data_len,
|
|
106
|
+
const uint8_t *key, size_t key_len,
|
|
107
|
+
uint8_t mac[32])
|
|
108
|
+
{
|
|
109
|
+
sm3_hmac_ctx_t ctx;
|
|
110
|
+
sm3_hmac (data, data_len, key, key_len, mac, &ctx);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
#endif /* INCLUDE_sm3_yescrypt */
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/* Copyright (C) 2024 Björn Esser <besser82@fedoraproject.org>
|
|
2
|
+
*
|
|
3
|
+
* Redistribution and use in source and binary forms, with or without
|
|
4
|
+
* modification, are permitted.
|
|
5
|
+
*
|
|
6
|
+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
7
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
8
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
9
|
+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
10
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
11
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
12
|
+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
13
|
+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
14
|
+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
15
|
+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
16
|
+
* SUCH DAMAGE.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
#ifndef _CRYPT_ALG_SM3_HMAC_H
|
|
20
|
+
#define _CRYPT_ALG_SM3_HMAC_H
|
|
21
|
+
|
|
22
|
+
#include "alg-sm3.h"
|
|
23
|
+
|
|
24
|
+
typedef struct
|
|
25
|
+
{
|
|
26
|
+
sm3_ctx sm3_ctx;
|
|
27
|
+
uint8_t key[64];
|
|
28
|
+
} sm3_hmac_ctx_t;
|
|
29
|
+
|
|
30
|
+
void sm3_hmac_init (sm3_hmac_ctx_t * ctx, const uint8_t * key,
|
|
31
|
+
size_t key_len);
|
|
32
|
+
void sm3_hmac_update (sm3_hmac_ctx_t * ctx, const uint8_t * data,
|
|
33
|
+
size_t data_len);
|
|
34
|
+
void sm3_hmac_final (sm3_hmac_ctx_t * ctx, uint8_t mac[32]);
|
|
35
|
+
void sm3_hmac (const uint8_t * data, size_t data_len,
|
|
36
|
+
const uint8_t * key, size_t key_len,
|
|
37
|
+
uint8_t mac[32], sm3_hmac_ctx_t * ctx);
|
|
38
|
+
void sm3_hmac_buf (const uint8_t * data, size_t data_len,
|
|
39
|
+
const uint8_t * key, size_t key_len,
|
|
40
|
+
uint8_t mac[32]);
|
|
41
|
+
|
|
42
|
+
#endif /* _CRYPT_ALG_SM3_HMAC_H */
|