rbnacl-libsodium 1.0.8 → 1.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +23 -0
- data/CHANGES.md +5 -0
- data/Gemfile +5 -2
- data/Rakefile +5 -0
- data/ext/rbnacl/extconf.rb +2 -1
- data/lib/rbnacl/libsodium.rb +8 -2
- data/lib/rbnacl/libsodium/version.rb +1 -1
- data/vendor/libsodium/AUTHORS +14 -0
- data/vendor/libsodium/ChangeLog +26 -0
- data/vendor/libsodium/LICENSE +1 -1
- data/vendor/libsodium/Makefile.am +1 -0
- data/vendor/libsodium/Makefile.in +9 -0
- data/vendor/libsodium/README.markdown +7 -0
- data/vendor/libsodium/aclocal.m4 +1 -0
- data/vendor/libsodium/appveyor.yml +25 -0
- data/vendor/libsodium/autom4te.cache/output.1 +640 -126
- data/vendor/libsodium/autom4te.cache/output.6 +19049 -0
- data/vendor/libsodium/autom4te.cache/requests +1151 -914
- data/vendor/libsodium/autom4te.cache/traces.1 +472 -426
- data/vendor/libsodium/autom4te.cache/traces.6 +3193 -0
- data/vendor/libsodium/builds/msvc/version.h +2 -2
- data/vendor/libsodium/builds/msvc/vs2010/libsodium.sln +50 -79
- data/vendor/libsodium/builds/msvc/vs2010/libsodium/libsodium.vcxproj +20 -8
- data/vendor/libsodium/builds/msvc/vs2010/libsodium/libsodium.vcxproj.filters +208 -166
- data/vendor/libsodium/builds/msvc/vs2012/libsodium.sln +50 -79
- data/vendor/libsodium/builds/msvc/vs2012/libsodium/libsodium.vcxproj +20 -8
- data/vendor/libsodium/builds/msvc/vs2012/libsodium/libsodium.vcxproj.filters +206 -164
- data/vendor/libsodium/builds/msvc/vs2013/libsodium.sln +52 -81
- data/vendor/libsodium/builds/msvc/vs2013/libsodium/libsodium.vcxproj +20 -8
- data/vendor/libsodium/builds/msvc/vs2013/libsodium/libsodium.vcxproj.filters +206 -164
- data/vendor/libsodium/builds/msvc/vs2015/libsodium.sln +52 -81
- data/vendor/libsodium/builds/msvc/vs2015/libsodium/libsodium.vcxproj +20 -8
- data/vendor/libsodium/builds/msvc/vs2015/libsodium/libsodium.vcxproj.filters +206 -164
- data/vendor/libsodium/configure +639 -125
- data/vendor/libsodium/configure.ac +94 -16
- data/vendor/libsodium/dist-build/Makefile.in +9 -0
- data/vendor/libsodium/dist-build/emscripten-symbols.def +370 -0
- data/vendor/libsodium/dist-build/emscripten.sh +9 -3
- data/vendor/libsodium/dist-build/generate-emscripten-symbols.sh +43 -0
- data/vendor/libsodium/libsodium-uninstalled.pc.in +1 -1
- data/vendor/libsodium/libsodium.pc.in +1 -1
- data/vendor/libsodium/libsodium.vcxproj +70 -66
- data/vendor/libsodium/libsodium.vcxproj.filters +204 -192
- data/vendor/libsodium/m4/ax_valgrind_check.m4 +190 -0
- data/vendor/libsodium/msvc-scripts/Makefile.in +9 -0
- data/vendor/libsodium/msvc-scripts/process.bat +2 -2
- data/vendor/libsodium/src/Makefile.in +9 -0
- data/vendor/libsodium/src/libsodium/Makefile.am +31 -6
- data/vendor/libsodium/src/libsodium/Makefile.in +238 -42
- data/vendor/libsodium/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c +234 -38
- data/vendor/libsodium/src/libsodium/crypto_aead/chacha20poly1305/sodium/aead_chacha20poly1305.c +208 -118
- data/vendor/libsodium/src/libsodium/crypto_box/crypto_box_seal.c +2 -2
- data/vendor/libsodium/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/before_curve25519xsalsa20poly1305.c +1 -4
- data/vendor/libsodium/src/libsodium/crypto_core/curve25519/ref10/curve25519_ref10.c +1799 -1790
- data/vendor/libsodium/src/libsodium/crypto_core/curve25519/ref10/curve25519_ref10.h +39 -39
- data/vendor/libsodium/src/libsodium/crypto_core/hchacha20/core_hchacha20.c +86 -0
- data/vendor/libsodium/src/libsodium/crypto_core/hchacha20/core_hchacha20.h +28 -0
- data/vendor/libsodium/src/libsodium/crypto_core/hsalsa20/ref2/core_hsalsa20.c +38 -46
- data/vendor/libsodium/src/libsodium/crypto_core/salsa20/ref/core_salsa20.c +47 -55
- data/vendor/libsodium/src/libsodium/crypto_core/salsa2012/ref/core_salsa2012.c +47 -55
- data/vendor/libsodium/src/libsodium/crypto_core/salsa208/ref/core_salsa208.c +47 -55
- data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/generichash_blake2_api.c +7 -0
- data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/blake2-impl.h +0 -89
- data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/blake2.h +50 -141
- data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/blake2b-compress-avx2.c +45 -0
- data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/blake2b-compress-avx2.h +123 -0
- data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/blake2b-compress-ref.c +3 -2
- data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/blake2b-compress-sse41.c +2 -2
- data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/{blake2b-round.h → blake2b-compress-sse41.h} +2 -28
- data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/blake2b-compress-ssse3.c +2 -4
- data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/blake2b-compress-ssse3.h +97 -0
- data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/blake2b-load-avx2.h +339 -0
- data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/blake2b-load-sse2.h +0 -2
- data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/blake2b-load-sse41.h +0 -2
- data/vendor/libsodium/src/libsodium/crypto_generichash/blake2/ref/blake2b-ref.c +29 -18
- data/vendor/libsodium/src/libsodium/crypto_hash/sha256/cp/hash_sha256.c +4 -43
- data/vendor/libsodium/src/libsodium/crypto_hash/sha512/cp/hash_sha512.c +3 -32
- data/vendor/libsodium/src/libsodium/crypto_onetimeauth/poly1305/donna/poly1305_donna.h +1 -20
- data/vendor/libsodium/src/libsodium/crypto_onetimeauth/poly1305/donna/poly1305_donna32.h +22 -41
- data/vendor/libsodium/src/libsodium/crypto_onetimeauth/poly1305/donna/poly1305_donna64.h +12 -39
- data/vendor/libsodium/src/libsodium/crypto_onetimeauth/poly1305/sse2/poly1305_sse2.c +2 -4
- data/vendor/libsodium/src/libsodium/crypto_onetimeauth/poly1305/sse2/poly1305_sse2.h +1 -20
- data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-core.c +570 -0
- data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-core.h +198 -0
- data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-encoding.c +444 -0
- data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-encoding.h +32 -0
- data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-ref.c +229 -0
- data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-ssse3.c +222 -0
- data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-impl.h +40 -0
- data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2.c +238 -0
- data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2.h +251 -0
- data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/blake2b-long.c +80 -0
- data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/blake2b-long.h +8 -0
- data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/blamka-round-ref.h +38 -0
- data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/blamka-round-ssse3.h +117 -0
- data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/pwhash_argon2i.c +164 -0
- data/vendor/libsodium/src/libsodium/crypto_pwhash/crypto_pwhash.c +106 -0
- data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/crypto_scrypt-common.c +1 -1
- data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/crypto_scrypt.h +4 -4
- data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/nosse/pwhash_scryptsalsa208sha256_nosse.c +186 -186
- data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/pbkdf2-sha256.c +2 -2
- data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/pwhash_scryptsalsa208sha256.c +3 -2
- data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/scrypt_platform.c +33 -33
- data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/sse/pwhash_scryptsalsa208sha256_sse.c +253 -254
- data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/donna_c64/curve25519_donna_c64.c +16 -17
- data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/donna_c64/curve25519_donna_c64.h +1 -0
- data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/ref10/x25519_ref10.c +11 -11
- data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/ref10/x25519_ref10.h +1 -0
- data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/consts_namespace.h +1 -1
- data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/fe.h +3 -2
- data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/fe51.h +5 -3
- data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/fe51_invert.c +41 -41
- data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/fe51_mul.S +10 -2
- data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/fe51_namespace.h +1 -1
- data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/fe51_nsquare.S +4 -0
- data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/fe51_pack.S +4 -0
- data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/fe_frombytes_sandy2x.c +31 -32
- data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/ladder.S +4 -0
- data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/ladder.h +1 -1
- data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/ladder_base.S +4 -0
- data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/ladder_base.h +1 -1
- data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/ladder_base_namespace.h +1 -1
- data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/ladder_namespace.h +1 -1
- data/vendor/libsodium/src/libsodium/crypto_secretbox/crypto_secretbox_easy.c +2 -6
- data/vendor/libsodium/src/libsodium/crypto_shorthash/siphash24/ref/shorthash_siphash24.c +8 -28
- data/vendor/libsodium/src/libsodium/crypto_sign/ed25519/ref10/open.c +75 -0
- data/vendor/libsodium/src/libsodium/crypto_stream/aes128ctr/portable/afternm_aes128ctr.c +6 -6
- data/vendor/libsodium/src/libsodium/crypto_stream/aes128ctr/portable/common.h +1 -18
- data/vendor/libsodium/src/libsodium/crypto_stream/aes128ctr/portable/int128_aes128ctr.c +20 -20
- data/vendor/libsodium/src/libsodium/crypto_stream/aes128ctr/portable/types.h +4 -4
- data/vendor/libsodium/src/libsodium/crypto_stream/aes128ctr/portable/xor_afternm_aes128ctr.c +6 -6
- data/vendor/libsodium/src/libsodium/crypto_stream/chacha20/ref/stream_chacha20_ref.c +56 -77
- data/vendor/libsodium/src/libsodium/crypto_stream/chacha20/ref/stream_chacha20_ref.h +1 -0
- data/vendor/libsodium/src/libsodium/crypto_stream/chacha20/vec/stream_chacha20_vec.h +1 -0
- data/vendor/libsodium/src/libsodium/crypto_stream/salsa20/ref/stream_salsa20_ref.c +2 -8
- data/vendor/libsodium/src/libsodium/crypto_stream/salsa20/ref/xor_salsa20_ref.c +2 -8
- data/vendor/libsodium/src/libsodium/crypto_stream/salsa2012/ref/stream_salsa2012.c +2 -8
- data/vendor/libsodium/src/libsodium/crypto_stream/salsa2012/ref/xor_salsa2012.c +2 -8
- data/vendor/libsodium/src/libsodium/crypto_stream/salsa208/ref/stream_salsa208.c +2 -8
- data/vendor/libsodium/src/libsodium/crypto_stream/salsa208/ref/xor_salsa208.c +2 -8
- data/vendor/libsodium/src/libsodium/crypto_stream/xsalsa20/ref/stream_xsalsa20.c +1 -5
- data/vendor/libsodium/src/libsodium/crypto_stream/xsalsa20/ref/xor_xsalsa20.c +1 -5
- data/vendor/libsodium/src/libsodium/include/Makefile.am +3 -0
- data/vendor/libsodium/src/libsodium/include/Makefile.in +19 -8
- data/vendor/libsodium/src/libsodium/include/sodium.h +3 -0
- data/vendor/libsodium/src/libsodium/include/sodium/crypto_aead_aes256gcm.h +50 -0
- data/vendor/libsodium/src/libsodium/include/sodium/crypto_aead_chacha20poly1305.h +94 -22
- data/vendor/libsodium/src/libsodium/include/sodium/crypto_box_curve25519xsalsa20poly1305.h +6 -6
- data/vendor/libsodium/src/libsodium/include/sodium/crypto_core_hchacha20.h +35 -0
- data/vendor/libsodium/src/libsodium/include/sodium/crypto_generichash_blake2b.h +3 -0
- data/vendor/libsodium/src/libsodium/include/sodium/crypto_pwhash.h +89 -0
- data/vendor/libsodium/src/libsodium/include/sodium/crypto_pwhash_argon2i.h +86 -0
- data/vendor/libsodium/src/libsodium/include/sodium/crypto_secretbox_xsalsa20poly1305.h +6 -6
- data/vendor/libsodium/src/libsodium/include/sodium/crypto_sign_edwards25519sha512batch.h +0 -11
- data/vendor/libsodium/src/libsodium/include/sodium/runtime.h +3 -0
- data/vendor/libsodium/src/libsodium/randombytes/randombytes.c +3 -0
- data/vendor/libsodium/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c +5 -1
- data/vendor/libsodium/src/libsodium/sodium/common.h +150 -0
- data/vendor/libsodium/src/libsodium/sodium/core.c +3 -1
- data/vendor/libsodium/src/libsodium/sodium/runtime.c +37 -19
- data/vendor/libsodium/src/libsodium/sodium/utils.c +18 -9
- data/vendor/libsodium/test/Makefile.in +9 -0
- data/vendor/libsodium/test/default/Makefile.am +10 -0
- data/vendor/libsodium/test/default/Makefile.in +53 -20
- data/vendor/libsodium/test/default/aead_aes256gcm.c +43 -17
- data/vendor/libsodium/test/default/aead_chacha20poly1305.c +179 -86
- data/vendor/libsodium/test/default/auth7.c +5 -5
- data/vendor/libsodium/test/default/box.c +4 -4
- data/vendor/libsodium/test/default/box2.c +1 -1
- data/vendor/libsodium/test/default/core6.c +1 -1
- data/vendor/libsodium/test/default/generichash.c +12 -1
- data/vendor/libsodium/test/default/generichash2.c +2 -2
- data/vendor/libsodium/test/default/generichash3.c +21 -0
- data/vendor/libsodium/test/default/pwhash.c +186 -168
- data/vendor/libsodium/test/default/pwhash.exp +11 -30
- data/vendor/libsodium/test/default/pwhash_scrypt.c +349 -0
- data/vendor/libsodium/test/default/pwhash_scrypt.exp +31 -0
- data/vendor/libsodium/test/default/secretbox.c +1 -1
- data/vendor/libsodium/test/default/secretbox2.c +1 -1
- data/vendor/libsodium/test/default/sign.c +15 -0
- data/vendor/libsodium/test/default/sodium_utils2.c +8 -3
- data/vendor/libsodium/test/default/sodium_utils3.c +4 -2
- data/vendor/libsodium/test/default/verify1.c +0 -4
- data/vendor/libsodium/test/quirks/quirks.h +3 -0
- metadata +37 -22
- data/vendor/libsodium/builds/msvc/vs2010/test/test.props +0 -43
- data/vendor/libsodium/builds/msvc/vs2010/test/test.runner.bat +0 -78
- data/vendor/libsodium/builds/msvc/vs2010/test/test.vcxproj +0 -244
- data/vendor/libsodium/builds/msvc/vs2010/test/test.vcxproj.filters +0 -192
- data/vendor/libsodium/builds/msvc/vs2012/test/test.props +0 -43
- data/vendor/libsodium/builds/msvc/vs2012/test/test.runner.bat +0 -78
- data/vendor/libsodium/builds/msvc/vs2012/test/test.vcxproj +0 -244
- data/vendor/libsodium/builds/msvc/vs2012/test/test.vcxproj.filters +0 -192
- data/vendor/libsodium/builds/msvc/vs2013/test/test.props +0 -43
- data/vendor/libsodium/builds/msvc/vs2013/test/test.runner.bat +0 -78
- data/vendor/libsodium/builds/msvc/vs2013/test/test.vcxproj +0 -244
- data/vendor/libsodium/builds/msvc/vs2013/test/test.vcxproj.filters +0 -192
- data/vendor/libsodium/builds/msvc/vs2015/test/test.props +0 -43
- data/vendor/libsodium/builds/msvc/vs2015/test/test.runner.bat +0 -78
- data/vendor/libsodium/builds/msvc/vs2015/test/test.vcxproj +0 -244
- data/vendor/libsodium/builds/msvc/vs2015/test/test.vcxproj.filters +0 -192
- data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/sysendian.h +0 -146
- data/vendor/libsodium/src/libsodium/crypto_stream/aes128ctr/portable/common_aes128ctr.c +0 -64
@@ -0,0 +1,198 @@
|
|
1
|
+
/*
|
2
|
+
* Argon2 source code package
|
3
|
+
*
|
4
|
+
* Written by Daniel Dinu and Dmitry Khovratovich, 2015
|
5
|
+
*
|
6
|
+
* This work is licensed under a Creative Commons CC0 1.0 License/Waiver.
|
7
|
+
*
|
8
|
+
* You should have received a copy of the CC0 Public Domain Dedication along
|
9
|
+
* with
|
10
|
+
* this software. If not, see
|
11
|
+
* <http://creativecommons.org/publicdomain/zero/1.0/>.
|
12
|
+
*/
|
13
|
+
|
14
|
+
#ifndef argon2_core_H
|
15
|
+
#define argon2_core_H
|
16
|
+
|
17
|
+
#include "argon2.h"
|
18
|
+
|
19
|
+
/*************************Argon2 internal
|
20
|
+
* constants**************************************************/
|
21
|
+
|
22
|
+
enum argon2_ctx_constants {
|
23
|
+
/* Version of the algorithm */
|
24
|
+
ARGON2_VERSION_NUMBER = 0x13,
|
25
|
+
|
26
|
+
/* Memory block size in bytes */
|
27
|
+
ARGON2_BLOCK_SIZE = 1024,
|
28
|
+
ARGON2_QWORDS_IN_BLOCK = ARGON2_BLOCK_SIZE / 8,
|
29
|
+
ARGON2_OWORDS_IN_BLOCK = ARGON2_BLOCK_SIZE / 16,
|
30
|
+
|
31
|
+
/* Number of pseudo-random values generated by one call to Blake in Argon2i
|
32
|
+
to
|
33
|
+
generate reference block positions */
|
34
|
+
ARGON2_ADDRESSES_IN_BLOCK = 128,
|
35
|
+
|
36
|
+
/* Pre-hashing digest length and its extension*/
|
37
|
+
ARGON2_PREHASH_DIGEST_LENGTH = 64,
|
38
|
+
ARGON2_PREHASH_SEED_LENGTH = 72
|
39
|
+
};
|
40
|
+
|
41
|
+
/*************************Argon2 internal data
|
42
|
+
* types**************************************************/
|
43
|
+
|
44
|
+
/*
|
45
|
+
* Structure for the (1KB) memory block implemented as 128 64-bit words.
|
46
|
+
* Memory blocks can be copied, XORed. Internal words can be accessed by [] (no
|
47
|
+
* bounds checking).
|
48
|
+
*/
|
49
|
+
typedef struct block_ { uint64_t v[ARGON2_QWORDS_IN_BLOCK]; } block;
|
50
|
+
|
51
|
+
typedef struct block_region_ {
|
52
|
+
void *base;
|
53
|
+
block *memory;
|
54
|
+
size_t size;
|
55
|
+
} block_region;
|
56
|
+
|
57
|
+
/*****************Functions that work with the block******************/
|
58
|
+
|
59
|
+
/* Initialize each byte of the block with @in */
|
60
|
+
void init_block_value(block *b, uint8_t in);
|
61
|
+
|
62
|
+
/* Copy block @src to block @dst */
|
63
|
+
void copy_block(block *dst, const block *src);
|
64
|
+
|
65
|
+
/* XOR @src onto @dst bytewise */
|
66
|
+
void xor_block(block *dst, const block *src);
|
67
|
+
|
68
|
+
/*
|
69
|
+
* Argon2 instance: memory pointer, number of passes, amount of memory, type,
|
70
|
+
* and derived values.
|
71
|
+
* Used to evaluate the number and location of blocks to construct in each
|
72
|
+
* thread
|
73
|
+
*/
|
74
|
+
typedef struct Argon2_instance_t {
|
75
|
+
block_region *region; /* Memory region pointer */
|
76
|
+
uint32_t passes; /* Number of passes */
|
77
|
+
uint32_t memory_blocks; /* Number of blocks in memory */
|
78
|
+
uint32_t segment_length;
|
79
|
+
uint32_t lane_length;
|
80
|
+
uint32_t lanes;
|
81
|
+
uint32_t threads;
|
82
|
+
argon2_type type;
|
83
|
+
int print_internals; /* whether to print the memory blocks */
|
84
|
+
} argon2_instance_t;
|
85
|
+
|
86
|
+
/*
|
87
|
+
* Argon2 position: where we construct the block right now. Used to distribute
|
88
|
+
* work between threads.
|
89
|
+
*/
|
90
|
+
typedef struct Argon2_position_t {
|
91
|
+
uint32_t pass;
|
92
|
+
uint32_t lane;
|
93
|
+
uint8_t slice;
|
94
|
+
uint32_t index;
|
95
|
+
} argon2_position_t;
|
96
|
+
|
97
|
+
/*Struct that holds the inputs for thread handling FillSegment*/
|
98
|
+
typedef struct Argon2_thread_data {
|
99
|
+
argon2_instance_t *instance_ptr;
|
100
|
+
argon2_position_t pos;
|
101
|
+
} argon2_thread_data;
|
102
|
+
|
103
|
+
/*************************Argon2 core
|
104
|
+
* functions**************************************************/
|
105
|
+
|
106
|
+
/*
|
107
|
+
* Computes absolute position of reference block in the lane following a skewed
|
108
|
+
* distribution and using a pseudo-random value as input
|
109
|
+
* @param instance Pointer to the current instance
|
110
|
+
* @param position Pointer to the current position
|
111
|
+
* @param pseudo_rand 32-bit pseudo-random value used to determine the position
|
112
|
+
* @param same_lane Indicates if the block will be taken from the current lane.
|
113
|
+
* If so we can reference the current segment
|
114
|
+
* @pre All pointers must be valid
|
115
|
+
*/
|
116
|
+
uint32_t index_alpha(const argon2_instance_t *instance,
|
117
|
+
const argon2_position_t *position, uint32_t pseudo_rand,
|
118
|
+
int same_lane);
|
119
|
+
|
120
|
+
/*
|
121
|
+
* Function that validates all inputs against predefined restrictions and return
|
122
|
+
* an error code
|
123
|
+
* @param context Pointer to current Argon2 context
|
124
|
+
* @return ARGON2_OK if everything is all right, otherwise one of error codes
|
125
|
+
* (all defined in <argon2.h>
|
126
|
+
*/
|
127
|
+
int validate_inputs(const argon2_context *context);
|
128
|
+
|
129
|
+
/*
|
130
|
+
* Hashes all the inputs into @a blockhash[PREHASH_DIGEST_LENGTH], clears
|
131
|
+
* password and secret if needed
|
132
|
+
* @param context Pointer to the Argon2 internal structure containing memory
|
133
|
+
* pointer, and parameters for time and space requirements.
|
134
|
+
* @param blockhash Buffer for pre-hashing digest
|
135
|
+
* @param type Argon2 type
|
136
|
+
* @pre @a blockhash must have at least @a PREHASH_DIGEST_LENGTH bytes
|
137
|
+
* allocated
|
138
|
+
*/
|
139
|
+
void initial_hash(uint8_t *blockhash, argon2_context *context,
|
140
|
+
argon2_type type);
|
141
|
+
|
142
|
+
/*
|
143
|
+
* Function creates first 2 blocks per lane
|
144
|
+
* @param instance Pointer to the current instance
|
145
|
+
* @param blockhash Pointer to the pre-hashing digest
|
146
|
+
* @pre blockhash must point to @a PREHASH_SEED_LENGTH allocated values
|
147
|
+
*/
|
148
|
+
void fill_first_blocks(uint8_t *blockhash, const argon2_instance_t *instance);
|
149
|
+
|
150
|
+
/*
|
151
|
+
* Function allocates memory, hashes the inputs with Blake, and creates first
|
152
|
+
* two blocks. Returns the pointer to the main memory with 2 blocks per lane
|
153
|
+
* initialized
|
154
|
+
* @param context Pointer to the Argon2 internal structure containing memory
|
155
|
+
* pointer, and parameters for time and space requirements.
|
156
|
+
* @param instance Current Argon2 instance
|
157
|
+
* @return Zero if successful, -1 if memory failed to allocate. @context->state
|
158
|
+
* will be modified if successful.
|
159
|
+
*/
|
160
|
+
int initialize(argon2_instance_t *instance, argon2_context *context);
|
161
|
+
|
162
|
+
/*
|
163
|
+
* XORing the last block of each lane, hashing it, making the tag. Deallocates
|
164
|
+
* the memory.
|
165
|
+
* @param context Pointer to current Argon2 context (use only the out parameters
|
166
|
+
* from it)
|
167
|
+
* @param instance Pointer to current instance of Argon2
|
168
|
+
* @pre instance->state must point to necessary amount of memory
|
169
|
+
* @pre context->out must point to outlen bytes of memory
|
170
|
+
* @pre if context->free_cbk is not NULL, it should point to a function that
|
171
|
+
* deallocates memory
|
172
|
+
*/
|
173
|
+
void finalize(const argon2_context *context, argon2_instance_t *instance);
|
174
|
+
|
175
|
+
/*
|
176
|
+
* Function that fills the segment using previous segments also from other
|
177
|
+
* threads
|
178
|
+
* @param instance Pointer to the current instance
|
179
|
+
* @param position Current position
|
180
|
+
* @pre all block pointers must be valid
|
181
|
+
*/
|
182
|
+
typedef int (*fill_segment_fn)(const argon2_instance_t *instance,
|
183
|
+
argon2_position_t position);
|
184
|
+
int argon2_pick_best_implementation(void);
|
185
|
+
int fill_segment_ssse3(const argon2_instance_t *instance,
|
186
|
+
argon2_position_t position);
|
187
|
+
int fill_segment_ref(const argon2_instance_t *instance,
|
188
|
+
argon2_position_t position);
|
189
|
+
|
190
|
+
/*
|
191
|
+
* Function that fills the entire memory t_cost times based on the first two
|
192
|
+
* blocks in each lane
|
193
|
+
* @param instance Pointer to the current instance
|
194
|
+
* @return Zero if successful, -1 if memory failed to allocate
|
195
|
+
*/
|
196
|
+
int fill_memory_blocks(argon2_instance_t *instance);
|
197
|
+
|
198
|
+
#endif
|
@@ -0,0 +1,444 @@
|
|
1
|
+
#include <stdio.h>
|
2
|
+
#include <stdlib.h>
|
3
|
+
#include <string.h>
|
4
|
+
#include <limits.h>
|
5
|
+
#include "argon2-core.h"
|
6
|
+
#include "argon2-encoding.h"
|
7
|
+
|
8
|
+
/*
|
9
|
+
* Example code for a decoder and encoder of "hash strings", with Argon2
|
10
|
+
* parameters.
|
11
|
+
*
|
12
|
+
* This code comprises three sections:
|
13
|
+
*
|
14
|
+
* -- The first section contains generic Base64 encoding and decoding
|
15
|
+
* functions. It is conceptually applicable to any hash function
|
16
|
+
* implementation that uses Base64 to encode and decode parameters,
|
17
|
+
* salts and outputs. It could be made into a library, provided that
|
18
|
+
* the relevant functions are made public (non-static) and be given
|
19
|
+
* reasonable names to avoid collisions with other functions.
|
20
|
+
*
|
21
|
+
* -- The second section is specific to Argon2. It encodes and decodes
|
22
|
+
* the parameters, salts and outputs. It does not compute the hash
|
23
|
+
* itself.
|
24
|
+
*
|
25
|
+
* -- The third section is test code, with a main() function. With
|
26
|
+
* this section, the whole file compiles as a stand-alone program
|
27
|
+
* that exercises the encoding and decoding functions with some
|
28
|
+
* test vectors.
|
29
|
+
*
|
30
|
+
* The code was originally written by Thomas Pornin <pornin@bolet.org>,
|
31
|
+
* to whom comments and remarks may be sent. It is released under what
|
32
|
+
* should amount to Public Domain or its closest equivalent; the
|
33
|
+
* following mantra is supposed to incarnate that fact with all the
|
34
|
+
* proper legal rituals:
|
35
|
+
*
|
36
|
+
* ---------------------------------------------------------------------
|
37
|
+
* This file is provided under the terms of Creative Commons CC0 1.0
|
38
|
+
* Public Domain Dedication. To the extent possible under law, the
|
39
|
+
* author (Thomas Pornin) has waived all copyright and related or
|
40
|
+
* neighboring rights to this file. This work is published from: Canada.
|
41
|
+
* ---------------------------------------------------------------------
|
42
|
+
*
|
43
|
+
* Copyright (c) 2015 Thomas Pornin
|
44
|
+
*/
|
45
|
+
|
46
|
+
/* ==================================================================== */
|
47
|
+
/*
|
48
|
+
* Common code; could be shared between different hash functions.
|
49
|
+
*
|
50
|
+
* Note: the Base64 functions below assume that uppercase letters (resp.
|
51
|
+
* lowercase letters) have consecutive numerical codes, that fit on 8
|
52
|
+
* bits. All modern systems use ASCII-compatible charsets, where these
|
53
|
+
* properties are true. If you are stuck with a dinosaur of a system
|
54
|
+
* that still defaults to EBCDIC then you already have much bigger
|
55
|
+
* interoperability issues to deal with.
|
56
|
+
*/
|
57
|
+
|
58
|
+
/*
|
59
|
+
* Some macros for constant-time comparisons. These work over values in
|
60
|
+
* the 0..255 range. Returned value is 0x00 on "false", 0xFF on "true".
|
61
|
+
*/
|
62
|
+
#define EQ(x, y) ((((0U - ((unsigned)(x) ^ (unsigned)(y))) >> 8) & 0xFF) ^ 0xFF)
|
63
|
+
#define GT(x, y) ((((unsigned)(y) - (unsigned)(x)) >> 8) & 0xFF)
|
64
|
+
#define GE(x, y) (GT(y, x) ^ 0xFF)
|
65
|
+
#define LT(x, y) GT(y, x)
|
66
|
+
#define LE(x, y) GE(y, x)
|
67
|
+
|
68
|
+
/*
|
69
|
+
* Convert value x (0..63) to corresponding Base64 character.
|
70
|
+
*/
|
71
|
+
static int b64_byte_to_char(unsigned x) {
|
72
|
+
return (LT(x, 26) & (x + 'A')) |
|
73
|
+
(GE(x, 26) & LT(x, 52) & (x + ('a' - 26))) |
|
74
|
+
(GE(x, 52) & LT(x, 62) & (x + ('0' - 52))) | (EQ(x, 62) & '+') |
|
75
|
+
(EQ(x, 63) & '/');
|
76
|
+
}
|
77
|
+
|
78
|
+
/*
|
79
|
+
* Convert character c to the corresponding 6-bit value. If character c
|
80
|
+
* is not a Base64 character, then 0xFF (255) is returned.
|
81
|
+
*/
|
82
|
+
static unsigned b64_char_to_byte(int c) {
|
83
|
+
unsigned x;
|
84
|
+
|
85
|
+
x = (GE(c, 'A') & LE(c, 'Z') & (c - 'A')) |
|
86
|
+
(GE(c, 'a') & LE(c, 'z') & (c - ('a' - 26))) |
|
87
|
+
(GE(c, '0') & LE(c, '9') & (c - ('0' - 52))) | (EQ(c, '+') & 62) |
|
88
|
+
(EQ(c, '/') & 63);
|
89
|
+
return x | (EQ(x, 0) & (EQ(c, 'A') ^ 0xFF));
|
90
|
+
}
|
91
|
+
|
92
|
+
/*
|
93
|
+
* Convert some bytes to Base64. 'dst_len' is the length (in characters)
|
94
|
+
* of the output buffer 'dst'; if that buffer is not large enough to
|
95
|
+
* receive the result (including the terminating 0), then (size_t)-1
|
96
|
+
* is returned. Otherwise, the zero-terminated Base64 string is written
|
97
|
+
* in the buffer, and the output length (counted WITHOUT the terminating
|
98
|
+
* zero) is returned.
|
99
|
+
*/
|
100
|
+
static size_t to_base64(char *dst, size_t dst_len, const void *src,
|
101
|
+
size_t src_len) {
|
102
|
+
size_t olen;
|
103
|
+
const unsigned char *buf;
|
104
|
+
unsigned acc, acc_len;
|
105
|
+
|
106
|
+
olen = (src_len / 3) << 2;
|
107
|
+
switch (src_len % 3) {
|
108
|
+
case 2:
|
109
|
+
olen++;
|
110
|
+
/* fall through */
|
111
|
+
case 1:
|
112
|
+
olen += 2;
|
113
|
+
break;
|
114
|
+
}
|
115
|
+
if (dst_len <= olen) {
|
116
|
+
return (size_t)-1;
|
117
|
+
}
|
118
|
+
acc = 0;
|
119
|
+
acc_len = 0;
|
120
|
+
buf = (const unsigned char *)src;
|
121
|
+
while (src_len-- > 0) {
|
122
|
+
acc = (acc << 8) + (*buf++);
|
123
|
+
acc_len += 8;
|
124
|
+
while (acc_len >= 6) {
|
125
|
+
acc_len -= 6;
|
126
|
+
*dst++ = (char)b64_byte_to_char((acc >> acc_len) & 0x3F);
|
127
|
+
}
|
128
|
+
}
|
129
|
+
if (acc_len > 0) {
|
130
|
+
*dst++ = (char)b64_byte_to_char((acc << (6 - acc_len)) & 0x3F);
|
131
|
+
}
|
132
|
+
*dst++ = 0;
|
133
|
+
return olen;
|
134
|
+
}
|
135
|
+
|
136
|
+
/*
|
137
|
+
* Decode Base64 chars into bytes. The '*dst_len' value must initially
|
138
|
+
* contain the length of the output buffer '*dst'; when the decoding
|
139
|
+
* ends, the actual number of decoded bytes is written back in
|
140
|
+
* '*dst_len'.
|
141
|
+
*
|
142
|
+
* Decoding stops when a non-Base64 character is encountered, or when
|
143
|
+
* the output buffer capacity is exceeded. If an error occurred (output
|
144
|
+
* buffer is too small, invalid last characters leading to unprocessed
|
145
|
+
* buffered bits), then NULL is returned; otherwise, the returned value
|
146
|
+
* points to the first non-Base64 character in the source stream, which
|
147
|
+
* may be the terminating zero.
|
148
|
+
*/
|
149
|
+
static const char *from_base64(void *dst, size_t *dst_len, const char *src) {
|
150
|
+
size_t len;
|
151
|
+
unsigned char *buf;
|
152
|
+
unsigned acc, acc_len;
|
153
|
+
|
154
|
+
buf = (unsigned char *)dst;
|
155
|
+
len = 0;
|
156
|
+
acc = 0;
|
157
|
+
acc_len = 0;
|
158
|
+
for (;;) {
|
159
|
+
unsigned d;
|
160
|
+
|
161
|
+
d = b64_char_to_byte(*src);
|
162
|
+
if (d == 0xFF) {
|
163
|
+
break;
|
164
|
+
}
|
165
|
+
src++;
|
166
|
+
acc = (acc << 6) + d;
|
167
|
+
acc_len += 6;
|
168
|
+
if (acc_len >= 8) {
|
169
|
+
acc_len -= 8;
|
170
|
+
if ((len++) >= *dst_len) {
|
171
|
+
return NULL;
|
172
|
+
}
|
173
|
+
*buf++ = (acc >> acc_len) & 0xFF;
|
174
|
+
}
|
175
|
+
}
|
176
|
+
|
177
|
+
/*
|
178
|
+
* If the input length is equal to 1 modulo 4 (which is
|
179
|
+
* invalid), then there will remain 6 unprocessed bits;
|
180
|
+
* otherwise, only 0, 2 or 4 bits are buffered. The buffered
|
181
|
+
* bits must also all be zero.
|
182
|
+
*/
|
183
|
+
if (acc_len > 4 || (acc & ((1U << acc_len) - 1)) != 0) {
|
184
|
+
return NULL;
|
185
|
+
}
|
186
|
+
*dst_len = len;
|
187
|
+
return src;
|
188
|
+
}
|
189
|
+
|
190
|
+
/*
|
191
|
+
* Decode decimal integer from 'str'; the value is written in '*v'.
|
192
|
+
* Returned value is a pointer to the next non-decimal character in the
|
193
|
+
* string. If there is no digit at all, or the value encoding is not
|
194
|
+
* minimal (extra leading zeros), or the value does not fit in an
|
195
|
+
* 'unsigned long', then NULL is returned.
|
196
|
+
*/
|
197
|
+
static const char *decode_decimal(const char *str, unsigned long *v) {
|
198
|
+
const char *orig;
|
199
|
+
unsigned long acc;
|
200
|
+
|
201
|
+
acc = 0;
|
202
|
+
for (orig = str;; str++) {
|
203
|
+
int c;
|
204
|
+
|
205
|
+
c = *str;
|
206
|
+
if (c < '0' || c > '9') {
|
207
|
+
break;
|
208
|
+
}
|
209
|
+
c -= '0';
|
210
|
+
if (acc > (ULONG_MAX / 10)) {
|
211
|
+
return NULL;
|
212
|
+
}
|
213
|
+
acc *= 10;
|
214
|
+
if ((unsigned long)c > (ULONG_MAX - acc)) {
|
215
|
+
return NULL;
|
216
|
+
}
|
217
|
+
acc += (unsigned long)c;
|
218
|
+
}
|
219
|
+
if (str == orig || (*orig == '0' && str != (orig + 1))) {
|
220
|
+
return NULL;
|
221
|
+
}
|
222
|
+
*v = acc;
|
223
|
+
return str;
|
224
|
+
}
|
225
|
+
|
226
|
+
/* ==================================================================== */
|
227
|
+
/*
|
228
|
+
* Code specific to Argon2.
|
229
|
+
*
|
230
|
+
* The code below applies the following format:
|
231
|
+
*
|
232
|
+
* $argon2<T>$v=<num>$m=<num>,t=<num>,p=<num>[,keyid=<bin>][,data=<bin>][$<bin>[$<bin>]]
|
233
|
+
*
|
234
|
+
* where <T> is either 'd' or 'i', <num> is a decimal integer (positive, fits in an 'unsigned long')
|
235
|
+
* and <bin> is Base64-encoded data (no '=' padding characters, no newline
|
236
|
+
* or whitespace). The "keyid" is a binary identifier for a key (up to 8
|
237
|
+
* bytes); "data" is associated data (up to 32 bytes). When the 'keyid'
|
238
|
+
* (resp. the 'data') is empty, then it is ommitted from the output.
|
239
|
+
*
|
240
|
+
* The last two binary chunks (encoded in Base64) are, in that order,
|
241
|
+
* the salt and the output. Both are optional, but you cannot have an
|
242
|
+
* output without a salt. The binary salt length is between 8 and 48 bytes.
|
243
|
+
* The output length is always exactly 32 bytes.
|
244
|
+
*/
|
245
|
+
|
246
|
+
/*
|
247
|
+
* Decode an Argon2i hash string into the provided structure 'ctx'.
|
248
|
+
* Returned value is ARGON2_OK on success.
|
249
|
+
*/
|
250
|
+
int decode_string(argon2_context *ctx, const char *str, argon2_type type) {
|
251
|
+
/* Prefix checking */
|
252
|
+
#define CC(prefix) \
|
253
|
+
do { \
|
254
|
+
size_t cc_len = strlen(prefix); \
|
255
|
+
if (strncmp(str, prefix, cc_len) != 0) { \
|
256
|
+
return ARGON2_DECODING_FAIL; \
|
257
|
+
} \
|
258
|
+
str += cc_len; \
|
259
|
+
} while ((void)0, 0)
|
260
|
+
|
261
|
+
/* Prefix checking with supplied code */
|
262
|
+
#define CC_opt(prefix, code) \
|
263
|
+
do { \
|
264
|
+
size_t cc_len = strlen(prefix); \
|
265
|
+
if (strncmp(str, prefix, cc_len) == 0) { \
|
266
|
+
str += cc_len; \
|
267
|
+
{ code; } \
|
268
|
+
} \
|
269
|
+
} while ((void)0, 0)
|
270
|
+
|
271
|
+
/* Decoding prefix into decimal */
|
272
|
+
#define DECIMAL(x) \
|
273
|
+
do { \
|
274
|
+
unsigned long dec_x; \
|
275
|
+
str = decode_decimal(str, &dec_x); \
|
276
|
+
if (str == NULL) { \
|
277
|
+
return ARGON2_DECODING_FAIL; \
|
278
|
+
} \
|
279
|
+
(x) = dec_x; \
|
280
|
+
} while ((void)0, 0)
|
281
|
+
|
282
|
+
/* Decoding prefix into binary */
|
283
|
+
#define BIN(buf, max_len, len) \
|
284
|
+
do { \
|
285
|
+
size_t bin_len = (max_len); \
|
286
|
+
str = from_base64(buf, &bin_len, str); \
|
287
|
+
if (str == NULL || bin_len > UINT32_MAX) { \
|
288
|
+
return ARGON2_DECODING_FAIL; \
|
289
|
+
} \
|
290
|
+
(len) = (uint32_t)bin_len; \
|
291
|
+
} while ((void)0, 0)
|
292
|
+
|
293
|
+
size_t maxadlen = ctx->adlen;
|
294
|
+
size_t maxsaltlen = ctx->saltlen;
|
295
|
+
size_t maxoutlen = ctx->outlen;
|
296
|
+
unsigned long version = 0;
|
297
|
+
int validation_result;
|
298
|
+
|
299
|
+
ctx->adlen = 0;
|
300
|
+
ctx->saltlen = 0;
|
301
|
+
ctx->outlen = 0;
|
302
|
+
ctx->pwdlen = 0;
|
303
|
+
if (type == Argon2_i) {
|
304
|
+
CC("$argon2i");
|
305
|
+
} else {
|
306
|
+
return ARGON2_INCORRECT_TYPE;
|
307
|
+
}
|
308
|
+
CC("$v=");
|
309
|
+
DECIMAL(version);
|
310
|
+
if (version != ARGON2_VERSION_NUMBER) {
|
311
|
+
return ARGON2_INCORRECT_TYPE;
|
312
|
+
}
|
313
|
+
CC("$m=");
|
314
|
+
DECIMAL(ctx->m_cost);
|
315
|
+
CC(",t=");
|
316
|
+
DECIMAL(ctx->t_cost);
|
317
|
+
CC(",p=");
|
318
|
+
DECIMAL(ctx->lanes);
|
319
|
+
ctx->threads = ctx->lanes;
|
320
|
+
|
321
|
+
CC_opt(",data=", BIN(ctx->ad, maxadlen, ctx->adlen));
|
322
|
+
if (*str == 0) {
|
323
|
+
return ARGON2_OK;
|
324
|
+
}
|
325
|
+
CC("$");
|
326
|
+
BIN(ctx->salt, maxsaltlen, ctx->saltlen);
|
327
|
+
if (*str == 0) {
|
328
|
+
return ARGON2_OK;
|
329
|
+
}
|
330
|
+
CC("$");
|
331
|
+
BIN(ctx->out, maxoutlen, ctx->outlen);
|
332
|
+
validation_result = validate_inputs(ctx);
|
333
|
+
if (validation_result != ARGON2_OK) {
|
334
|
+
return validation_result;
|
335
|
+
}
|
336
|
+
if (*str == 0) {
|
337
|
+
return ARGON2_OK;
|
338
|
+
}
|
339
|
+
return ARGON2_DECODING_FAIL;
|
340
|
+
|
341
|
+
#undef CC
|
342
|
+
#undef CC_opt
|
343
|
+
#undef DECIMAL
|
344
|
+
#undef BIN
|
345
|
+
}
|
346
|
+
|
347
|
+
#define U32_STR_MAXSIZE 11U
|
348
|
+
|
349
|
+
static void u32_to_string(char *str, uint32_t x) {
|
350
|
+
char tmp[U32_STR_MAXSIZE - 1U];
|
351
|
+
size_t i;
|
352
|
+
|
353
|
+
i = sizeof tmp;
|
354
|
+
do {
|
355
|
+
tmp[--i] = (x % (uint32_t) 10U) + '0';
|
356
|
+
x /= (uint32_t) 10U;
|
357
|
+
} while (x != 0U && i != 0U);
|
358
|
+
memcpy(str, &tmp[i], (sizeof tmp) - i);
|
359
|
+
str[(sizeof tmp) - i] = 0;
|
360
|
+
}
|
361
|
+
|
362
|
+
/*
|
363
|
+
* Encode an argon2i hash string into the provided buffer. 'dst_len'
|
364
|
+
* contains the size, in characters, of the 'dst' buffer; if 'dst_len'
|
365
|
+
* is less than the number of required characters (including the
|
366
|
+
* terminating 0), then this function returns 0.
|
367
|
+
*
|
368
|
+
* If pp->output_len is 0, then the hash string will be a salt string
|
369
|
+
* (no output). if pp->salt_len is also 0, then the string will be a
|
370
|
+
* parameter-only string (no salt and no output).
|
371
|
+
*
|
372
|
+
* On success, ARGON2_OK is returned.
|
373
|
+
*/
|
374
|
+
int encode_string(char *dst, size_t dst_len, argon2_context *ctx,
|
375
|
+
argon2_type type) {
|
376
|
+
#define SS(str) \
|
377
|
+
do { \
|
378
|
+
size_t pp_len = strlen(str); \
|
379
|
+
if (pp_len >= dst_len) { \
|
380
|
+
return ARGON2_ENCODING_FAIL; \
|
381
|
+
} \
|
382
|
+
memcpy(dst, str, pp_len + 1); \
|
383
|
+
dst += pp_len; \
|
384
|
+
dst_len -= pp_len; \
|
385
|
+
} while ((void)0, 0)
|
386
|
+
|
387
|
+
#define SX(x) \
|
388
|
+
do { \
|
389
|
+
char tmp[U32_STR_MAXSIZE]; \
|
390
|
+
u32_to_string(tmp, x); \
|
391
|
+
SS(tmp); \
|
392
|
+
} while ((void)0, 0)
|
393
|
+
|
394
|
+
#define SB(buf, len) \
|
395
|
+
do { \
|
396
|
+
size_t sb_len = to_base64(dst, dst_len, buf, len); \
|
397
|
+
if (sb_len == (size_t)-1) { \
|
398
|
+
return ARGON2_ENCODING_FAIL; \
|
399
|
+
} \
|
400
|
+
dst += sb_len; \
|
401
|
+
dst_len -= sb_len; \
|
402
|
+
} while ((void)0, 0)
|
403
|
+
|
404
|
+
int validation_result;
|
405
|
+
|
406
|
+
if (type == Argon2_i) {
|
407
|
+
SS("$argon2i$v=");
|
408
|
+
} else {
|
409
|
+
return ARGON2_ENCODING_FAIL;
|
410
|
+
}
|
411
|
+
validation_result = validate_inputs(ctx);
|
412
|
+
if (validation_result != ARGON2_OK) {
|
413
|
+
return validation_result;
|
414
|
+
}
|
415
|
+
SX(ARGON2_VERSION_NUMBER);
|
416
|
+
SS("$m=");
|
417
|
+
SX(ctx->m_cost);
|
418
|
+
SS(",t=");
|
419
|
+
SX(ctx->t_cost);
|
420
|
+
SS(",p=");
|
421
|
+
SX(ctx->lanes);
|
422
|
+
|
423
|
+
if (ctx->adlen > 0) {
|
424
|
+
SS(",data=");
|
425
|
+
SB(ctx->ad, ctx->adlen);
|
426
|
+
}
|
427
|
+
|
428
|
+
if (ctx->saltlen == 0) {
|
429
|
+
return ARGON2_OK;
|
430
|
+
}
|
431
|
+
SS("$");
|
432
|
+
SB(ctx->salt, ctx->saltlen);
|
433
|
+
|
434
|
+
if (ctx->outlen == 0) {
|
435
|
+
return ARGON2_OK;
|
436
|
+
}
|
437
|
+
SS("$");
|
438
|
+
SB(ctx->out, ctx->outlen);
|
439
|
+
return ARGON2_OK;
|
440
|
+
|
441
|
+
#undef SS
|
442
|
+
#undef SX
|
443
|
+
#undef SB
|
444
|
+
}
|