sorcery-argon2 1.0.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (96) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/codeql.yml +74 -0
  3. data/.github/workflows/ruby.yml +13 -3
  4. data/.rubocop.yml +112 -2
  5. data/CHANGELOG.md +8 -0
  6. data/MAINTAINING.md +8 -3
  7. data/README.md +155 -14
  8. data/bin/setup +4 -0
  9. data/ext/argon2_wrap/{Makefile → Makefile.real} +1 -0
  10. data/ext/argon2_wrap/extconf.rb +4 -1
  11. data/ext/argon2_wrap/libargon2_wrap.so +0 -0
  12. data/ext/argon2_wrap/tests +0 -0
  13. data/ext/phc-winner-argon2/.git +1 -0
  14. data/ext/phc-winner-argon2/.gitattributes +10 -0
  15. data/ext/phc-winner-argon2/.gitignore +22 -0
  16. data/ext/phc-winner-argon2/.travis.yml +25 -0
  17. data/ext/phc-winner-argon2/Argon2.sln +158 -0
  18. data/ext/phc-winner-argon2/CHANGELOG.md +32 -0
  19. data/ext/phc-winner-argon2/LICENSE +314 -0
  20. data/ext/phc-winner-argon2/Makefile +255 -0
  21. data/ext/phc-winner-argon2/Package.swift +46 -0
  22. data/ext/phc-winner-argon2/README.md +303 -0
  23. data/ext/phc-winner-argon2/appveyor.yml +25 -0
  24. data/ext/phc-winner-argon2/argon2-specs.pdf +0 -0
  25. data/ext/phc-winner-argon2/export.sh +7 -0
  26. data/ext/phc-winner-argon2/include/argon2.h +437 -0
  27. data/ext/phc-winner-argon2/kats/argon2d +12304 -0
  28. data/ext/phc-winner-argon2/kats/argon2d.shasum +1 -0
  29. data/ext/phc-winner-argon2/kats/argon2d_v16 +12304 -0
  30. data/ext/phc-winner-argon2/kats/argon2d_v16.shasum +1 -0
  31. data/ext/phc-winner-argon2/kats/argon2i +12304 -0
  32. data/ext/phc-winner-argon2/kats/argon2i.shasum +1 -0
  33. data/ext/phc-winner-argon2/kats/argon2i_v16 +12304 -0
  34. data/ext/phc-winner-argon2/kats/argon2i_v16.shasum +1 -0
  35. data/ext/phc-winner-argon2/kats/argon2id +12304 -0
  36. data/ext/phc-winner-argon2/kats/argon2id.shasum +1 -0
  37. data/ext/phc-winner-argon2/kats/argon2id_v16 +12304 -0
  38. data/ext/phc-winner-argon2/kats/argon2id_v16.shasum +1 -0
  39. data/ext/phc-winner-argon2/kats/check-sums.ps1 +42 -0
  40. data/ext/phc-winner-argon2/kats/check-sums.sh +13 -0
  41. data/ext/phc-winner-argon2/kats/test.ps1 +50 -0
  42. data/ext/phc-winner-argon2/kats/test.sh +49 -0
  43. data/ext/phc-winner-argon2/latex/IEEEtran.cls +6347 -0
  44. data/ext/phc-winner-argon2/latex/Makefile +18 -0
  45. data/ext/phc-winner-argon2/latex/argon2-specs.tex +920 -0
  46. data/ext/phc-winner-argon2/latex/pics/argon2-par.pdf +0 -0
  47. data/ext/phc-winner-argon2/latex/pics/compression.pdf +0 -0
  48. data/ext/phc-winner-argon2/latex/pics/generic.pdf +0 -0
  49. data/ext/phc-winner-argon2/latex/pics/power-distribution.jpg +0 -0
  50. data/ext/phc-winner-argon2/latex/tradeoff.bib +822 -0
  51. data/ext/phc-winner-argon2/libargon2.pc.in +18 -0
  52. data/ext/phc-winner-argon2/man/argon2.1 +57 -0
  53. data/ext/phc-winner-argon2/src/argon2.c +452 -0
  54. data/ext/phc-winner-argon2/src/bench.c +111 -0
  55. data/ext/phc-winner-argon2/src/blake2/blake2-impl.h +156 -0
  56. data/ext/phc-winner-argon2/src/blake2/blake2.h +89 -0
  57. data/ext/phc-winner-argon2/src/blake2/blake2b.c +390 -0
  58. data/ext/phc-winner-argon2/src/blake2/blamka-round-opt.h +471 -0
  59. data/ext/phc-winner-argon2/src/blake2/blamka-round-ref.h +56 -0
  60. data/ext/phc-winner-argon2/src/core.c +648 -0
  61. data/ext/phc-winner-argon2/src/core.h +228 -0
  62. data/ext/phc-winner-argon2/src/encoding.c +463 -0
  63. data/ext/phc-winner-argon2/src/encoding.h +57 -0
  64. data/ext/phc-winner-argon2/src/genkat.c +213 -0
  65. data/ext/phc-winner-argon2/src/genkat.h +51 -0
  66. data/ext/phc-winner-argon2/src/opt.c +283 -0
  67. data/ext/phc-winner-argon2/src/ref.c +194 -0
  68. data/ext/phc-winner-argon2/src/run.c +337 -0
  69. data/ext/phc-winner-argon2/src/test.c +289 -0
  70. data/ext/phc-winner-argon2/src/thread.c +57 -0
  71. data/ext/phc-winner-argon2/src/thread.h +67 -0
  72. data/ext/phc-winner-argon2/vs2015/Argon2Opt/Argon2Opt.vcxproj +231 -0
  73. data/ext/phc-winner-argon2/vs2015/Argon2Opt/Argon2Opt.vcxproj.filters +69 -0
  74. data/ext/phc-winner-argon2/vs2015/Argon2OptBench/Argon2OptBench.vcxproj +231 -0
  75. data/ext/phc-winner-argon2/vs2015/Argon2OptBench/Argon2OptBench.vcxproj.filters +69 -0
  76. data/ext/phc-winner-argon2/vs2015/Argon2OptDll/Argon2OptDll.vcxproj +230 -0
  77. data/ext/phc-winner-argon2/vs2015/Argon2OptDll/Argon2OptDll.vcxproj.filters +66 -0
  78. data/ext/phc-winner-argon2/vs2015/Argon2OptGenKAT/Argon2OptGenKAT.vcxproj +244 -0
  79. data/ext/phc-winner-argon2/vs2015/Argon2OptGenKAT/Argon2OptGenKAT.vcxproj.filters +72 -0
  80. data/ext/phc-winner-argon2/vs2015/Argon2OptTestCI/Argon2OptTestCI.vcxproj +235 -0
  81. data/ext/phc-winner-argon2/vs2015/Argon2OptTestCI/Argon2OptTestCI.vcxproj.filters +69 -0
  82. data/ext/phc-winner-argon2/vs2015/Argon2Ref/Argon2Ref.vcxproj +243 -0
  83. data/ext/phc-winner-argon2/vs2015/Argon2Ref/Argon2Ref.vcxproj.filters +69 -0
  84. data/ext/phc-winner-argon2/vs2015/Argon2RefBench/Argon2RefBench.vcxproj +231 -0
  85. data/ext/phc-winner-argon2/vs2015/Argon2RefBench/Argon2RefBench.vcxproj.filters +69 -0
  86. data/ext/phc-winner-argon2/vs2015/Argon2RefDll/Argon2RefDll.vcxproj +230 -0
  87. data/ext/phc-winner-argon2/vs2015/Argon2RefDll/Argon2RefDll.vcxproj.filters +66 -0
  88. data/ext/phc-winner-argon2/vs2015/Argon2RefGenKAT/Argon2RefGenKAT.vcxproj +232 -0
  89. data/ext/phc-winner-argon2/vs2015/Argon2RefGenKAT/Argon2RefGenKAT.vcxproj.filters +72 -0
  90. data/ext/phc-winner-argon2/vs2015/Argon2RefTestCI/Argon2RefTestCI.vcxproj +231 -0
  91. data/ext/phc-winner-argon2/vs2015/Argon2RefTestCI/Argon2RefTestCI.vcxproj.filters +69 -0
  92. data/lib/argon2/ffi_engine.rb +4 -4
  93. data/lib/argon2/password.rb +28 -5
  94. data/lib/argon2/version.rb +1 -1
  95. data/sorcery-argon2.gemspec +3 -2
  96. metadata +91 -8
@@ -0,0 +1,156 @@
1
+ /*
2
+ * Argon2 reference source code package - reference C implementations
3
+ *
4
+ * Copyright 2015
5
+ * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
6
+ *
7
+ * You may use this work under the terms of a Creative Commons CC0 1.0
8
+ * License/Waiver or the Apache Public License 2.0, at your option. The terms of
9
+ * these licenses can be found at:
10
+ *
11
+ * - CC0 1.0 Universal : https://creativecommons.org/publicdomain/zero/1.0
12
+ * - Apache 2.0 : https://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * You should have received a copy of both of these licenses along with this
15
+ * software. If not, they may be obtained at the above URLs.
16
+ */
17
+
18
+ #ifndef PORTABLE_BLAKE2_IMPL_H
19
+ #define PORTABLE_BLAKE2_IMPL_H
20
+
21
+ #include <stdint.h>
22
+ #include <string.h>
23
+
24
+ #ifdef _WIN32
25
+ #define BLAKE2_INLINE __inline
26
+ #elif defined(__GNUC__) || defined(__clang__)
27
+ #define BLAKE2_INLINE __inline__
28
+ #else
29
+ #define BLAKE2_INLINE
30
+ #endif
31
+
32
+ /* Argon2 Team - Begin Code */
33
+ /*
34
+ Not an exhaustive list, but should cover the majority of modern platforms
35
+ Additionally, the code will always be correct---this is only a performance
36
+ tweak.
37
+ */
38
+ #if (defined(__BYTE_ORDER__) && \
39
+ (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) || \
40
+ defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || defined(__MIPSEL__) || \
41
+ defined(__AARCH64EL__) || defined(__amd64__) || defined(__i386__) || \
42
+ defined(_M_IX86) || defined(_M_X64) || defined(_M_AMD64) || \
43
+ defined(_M_ARM)
44
+ #define NATIVE_LITTLE_ENDIAN
45
+ #endif
46
+ /* Argon2 Team - End Code */
47
+
48
+ static BLAKE2_INLINE uint32_t load32(const void *src) {
49
+ #if defined(NATIVE_LITTLE_ENDIAN)
50
+ uint32_t w;
51
+ memcpy(&w, src, sizeof w);
52
+ return w;
53
+ #else
54
+ const uint8_t *p = (const uint8_t *)src;
55
+ uint32_t w = *p++;
56
+ w |= (uint32_t)(*p++) << 8;
57
+ w |= (uint32_t)(*p++) << 16;
58
+ w |= (uint32_t)(*p++) << 24;
59
+ return w;
60
+ #endif
61
+ }
62
+
63
+ static BLAKE2_INLINE uint64_t load64(const void *src) {
64
+ #if defined(NATIVE_LITTLE_ENDIAN)
65
+ uint64_t w;
66
+ memcpy(&w, src, sizeof w);
67
+ return w;
68
+ #else
69
+ const uint8_t *p = (const uint8_t *)src;
70
+ uint64_t w = *p++;
71
+ w |= (uint64_t)(*p++) << 8;
72
+ w |= (uint64_t)(*p++) << 16;
73
+ w |= (uint64_t)(*p++) << 24;
74
+ w |= (uint64_t)(*p++) << 32;
75
+ w |= (uint64_t)(*p++) << 40;
76
+ w |= (uint64_t)(*p++) << 48;
77
+ w |= (uint64_t)(*p++) << 56;
78
+ return w;
79
+ #endif
80
+ }
81
+
82
+ static BLAKE2_INLINE void store32(void *dst, uint32_t w) {
83
+ #if defined(NATIVE_LITTLE_ENDIAN)
84
+ memcpy(dst, &w, sizeof w);
85
+ #else
86
+ uint8_t *p = (uint8_t *)dst;
87
+ *p++ = (uint8_t)w;
88
+ w >>= 8;
89
+ *p++ = (uint8_t)w;
90
+ w >>= 8;
91
+ *p++ = (uint8_t)w;
92
+ w >>= 8;
93
+ *p++ = (uint8_t)w;
94
+ #endif
95
+ }
96
+
97
+ static BLAKE2_INLINE void store64(void *dst, uint64_t w) {
98
+ #if defined(NATIVE_LITTLE_ENDIAN)
99
+ memcpy(dst, &w, sizeof w);
100
+ #else
101
+ uint8_t *p = (uint8_t *)dst;
102
+ *p++ = (uint8_t)w;
103
+ w >>= 8;
104
+ *p++ = (uint8_t)w;
105
+ w >>= 8;
106
+ *p++ = (uint8_t)w;
107
+ w >>= 8;
108
+ *p++ = (uint8_t)w;
109
+ w >>= 8;
110
+ *p++ = (uint8_t)w;
111
+ w >>= 8;
112
+ *p++ = (uint8_t)w;
113
+ w >>= 8;
114
+ *p++ = (uint8_t)w;
115
+ w >>= 8;
116
+ *p++ = (uint8_t)w;
117
+ #endif
118
+ }
119
+
120
+ static BLAKE2_INLINE uint64_t load48(const void *src) {
121
+ const uint8_t *p = (const uint8_t *)src;
122
+ uint64_t w = *p++;
123
+ w |= (uint64_t)(*p++) << 8;
124
+ w |= (uint64_t)(*p++) << 16;
125
+ w |= (uint64_t)(*p++) << 24;
126
+ w |= (uint64_t)(*p++) << 32;
127
+ w |= (uint64_t)(*p++) << 40;
128
+ return w;
129
+ }
130
+
131
+ static BLAKE2_INLINE void store48(void *dst, uint64_t w) {
132
+ uint8_t *p = (uint8_t *)dst;
133
+ *p++ = (uint8_t)w;
134
+ w >>= 8;
135
+ *p++ = (uint8_t)w;
136
+ w >>= 8;
137
+ *p++ = (uint8_t)w;
138
+ w >>= 8;
139
+ *p++ = (uint8_t)w;
140
+ w >>= 8;
141
+ *p++ = (uint8_t)w;
142
+ w >>= 8;
143
+ *p++ = (uint8_t)w;
144
+ }
145
+
146
+ static BLAKE2_INLINE uint32_t rotr32(const uint32_t w, const unsigned c) {
147
+ return (w >> c) | (w << (32 - c));
148
+ }
149
+
150
+ static BLAKE2_INLINE uint64_t rotr64(const uint64_t w, const unsigned c) {
151
+ return (w >> c) | (w << (64 - c));
152
+ }
153
+
154
+ void clear_internal_memory(void *v, size_t n);
155
+
156
+ #endif
@@ -0,0 +1,89 @@
1
+ /*
2
+ * Argon2 reference source code package - reference C implementations
3
+ *
4
+ * Copyright 2015
5
+ * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
6
+ *
7
+ * You may use this work under the terms of a Creative Commons CC0 1.0
8
+ * License/Waiver or the Apache Public License 2.0, at your option. The terms of
9
+ * these licenses can be found at:
10
+ *
11
+ * - CC0 1.0 Universal : https://creativecommons.org/publicdomain/zero/1.0
12
+ * - Apache 2.0 : https://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * You should have received a copy of both of these licenses along with this
15
+ * software. If not, they may be obtained at the above URLs.
16
+ */
17
+
18
+ #ifndef PORTABLE_BLAKE2_H
19
+ #define PORTABLE_BLAKE2_H
20
+
21
+ #include <argon2.h>
22
+
23
+ #if defined(__cplusplus)
24
+ extern "C" {
25
+ #endif
26
+
27
+ enum blake2b_constant {
28
+ BLAKE2B_BLOCKBYTES = 128,
29
+ BLAKE2B_OUTBYTES = 64,
30
+ BLAKE2B_KEYBYTES = 64,
31
+ BLAKE2B_SALTBYTES = 16,
32
+ BLAKE2B_PERSONALBYTES = 16
33
+ };
34
+
35
+ #pragma pack(push, 1)
36
+ typedef struct __blake2b_param {
37
+ uint8_t digest_length; /* 1 */
38
+ uint8_t key_length; /* 2 */
39
+ uint8_t fanout; /* 3 */
40
+ uint8_t depth; /* 4 */
41
+ uint32_t leaf_length; /* 8 */
42
+ uint64_t node_offset; /* 16 */
43
+ uint8_t node_depth; /* 17 */
44
+ uint8_t inner_length; /* 18 */
45
+ uint8_t reserved[14]; /* 32 */
46
+ uint8_t salt[BLAKE2B_SALTBYTES]; /* 48 */
47
+ uint8_t personal[BLAKE2B_PERSONALBYTES]; /* 64 */
48
+ } blake2b_param;
49
+ #pragma pack(pop)
50
+
51
+ typedef struct __blake2b_state {
52
+ uint64_t h[8];
53
+ uint64_t t[2];
54
+ uint64_t f[2];
55
+ uint8_t buf[BLAKE2B_BLOCKBYTES];
56
+ unsigned buflen;
57
+ unsigned outlen;
58
+ uint8_t last_node;
59
+ } blake2b_state;
60
+
61
+ /* Ensure param structs have not been wrongly padded */
62
+ /* Poor man's static_assert */
63
+ enum {
64
+ blake2_size_check_0 = 1 / !!(CHAR_BIT == 8),
65
+ blake2_size_check_2 =
66
+ 1 / !!(sizeof(blake2b_param) == sizeof(uint64_t) * CHAR_BIT)
67
+ };
68
+
69
+ /* Streaming API */
70
+ ARGON2_LOCAL int blake2b_init(blake2b_state *S, size_t outlen);
71
+ ARGON2_LOCAL int blake2b_init_key(blake2b_state *S, size_t outlen, const void *key,
72
+ size_t keylen);
73
+ ARGON2_LOCAL int blake2b_init_param(blake2b_state *S, const blake2b_param *P);
74
+ ARGON2_LOCAL int blake2b_update(blake2b_state *S, const void *in, size_t inlen);
75
+ ARGON2_LOCAL int blake2b_final(blake2b_state *S, void *out, size_t outlen);
76
+
77
+ /* Simple API */
78
+ ARGON2_LOCAL int blake2b(void *out, size_t outlen, const void *in, size_t inlen,
79
+ const void *key, size_t keylen);
80
+
81
+ /* Argon2 Team - Begin Code */
82
+ ARGON2_LOCAL int blake2b_long(void *out, size_t outlen, const void *in, size_t inlen);
83
+ /* Argon2 Team - End Code */
84
+
85
+ #if defined(__cplusplus)
86
+ }
87
+ #endif
88
+
89
+ #endif
@@ -0,0 +1,390 @@
1
+ /*
2
+ * Argon2 reference source code package - reference C implementations
3
+ *
4
+ * Copyright 2015
5
+ * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
6
+ *
7
+ * You may use this work under the terms of a Creative Commons CC0 1.0
8
+ * License/Waiver or the Apache Public License 2.0, at your option. The terms of
9
+ * these licenses can be found at:
10
+ *
11
+ * - CC0 1.0 Universal : https://creativecommons.org/publicdomain/zero/1.0
12
+ * - Apache 2.0 : https://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * You should have received a copy of both of these licenses along with this
15
+ * software. If not, they may be obtained at the above URLs.
16
+ */
17
+
18
+ #include <stdint.h>
19
+ #include <string.h>
20
+ #include <stdio.h>
21
+
22
+ #include "blake2.h"
23
+ #include "blake2-impl.h"
24
+
25
+ static const uint64_t blake2b_IV[8] = {
26
+ UINT64_C(0x6a09e667f3bcc908), UINT64_C(0xbb67ae8584caa73b),
27
+ UINT64_C(0x3c6ef372fe94f82b), UINT64_C(0xa54ff53a5f1d36f1),
28
+ UINT64_C(0x510e527fade682d1), UINT64_C(0x9b05688c2b3e6c1f),
29
+ UINT64_C(0x1f83d9abfb41bd6b), UINT64_C(0x5be0cd19137e2179)};
30
+
31
+ static const unsigned int blake2b_sigma[12][16] = {
32
+ {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
33
+ {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3},
34
+ {11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4},
35
+ {7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8},
36
+ {9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13},
37
+ {2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9},
38
+ {12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11},
39
+ {13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10},
40
+ {6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5},
41
+ {10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0},
42
+ {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
43
+ {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3},
44
+ };
45
+
46
+ static BLAKE2_INLINE void blake2b_set_lastnode(blake2b_state *S) {
47
+ S->f[1] = (uint64_t)-1;
48
+ }
49
+
50
+ static BLAKE2_INLINE void blake2b_set_lastblock(blake2b_state *S) {
51
+ if (S->last_node) {
52
+ blake2b_set_lastnode(S);
53
+ }
54
+ S->f[0] = (uint64_t)-1;
55
+ }
56
+
57
+ static BLAKE2_INLINE void blake2b_increment_counter(blake2b_state *S,
58
+ uint64_t inc) {
59
+ S->t[0] += inc;
60
+ S->t[1] += (S->t[0] < inc);
61
+ }
62
+
63
+ static BLAKE2_INLINE void blake2b_invalidate_state(blake2b_state *S) {
64
+ clear_internal_memory(S, sizeof(*S)); /* wipe */
65
+ blake2b_set_lastblock(S); /* invalidate for further use */
66
+ }
67
+
68
+ static BLAKE2_INLINE void blake2b_init0(blake2b_state *S) {
69
+ memset(S, 0, sizeof(*S));
70
+ memcpy(S->h, blake2b_IV, sizeof(S->h));
71
+ }
72
+
73
+ int blake2b_init_param(blake2b_state *S, const blake2b_param *P) {
74
+ const unsigned char *p = (const unsigned char *)P;
75
+ unsigned int i;
76
+
77
+ if (NULL == P || NULL == S) {
78
+ return -1;
79
+ }
80
+
81
+ blake2b_init0(S);
82
+ /* IV XOR Parameter Block */
83
+ for (i = 0; i < 8; ++i) {
84
+ S->h[i] ^= load64(&p[i * sizeof(S->h[i])]);
85
+ }
86
+ S->outlen = P->digest_length;
87
+ return 0;
88
+ }
89
+
90
+ /* Sequential blake2b initialization */
91
+ int blake2b_init(blake2b_state *S, size_t outlen) {
92
+ blake2b_param P;
93
+
94
+ if (S == NULL) {
95
+ return -1;
96
+ }
97
+
98
+ if ((outlen == 0) || (outlen > BLAKE2B_OUTBYTES)) {
99
+ blake2b_invalidate_state(S);
100
+ return -1;
101
+ }
102
+
103
+ /* Setup Parameter Block for unkeyed BLAKE2 */
104
+ P.digest_length = (uint8_t)outlen;
105
+ P.key_length = 0;
106
+ P.fanout = 1;
107
+ P.depth = 1;
108
+ P.leaf_length = 0;
109
+ P.node_offset = 0;
110
+ P.node_depth = 0;
111
+ P.inner_length = 0;
112
+ memset(P.reserved, 0, sizeof(P.reserved));
113
+ memset(P.salt, 0, sizeof(P.salt));
114
+ memset(P.personal, 0, sizeof(P.personal));
115
+
116
+ return blake2b_init_param(S, &P);
117
+ }
118
+
119
+ int blake2b_init_key(blake2b_state *S, size_t outlen, const void *key,
120
+ size_t keylen) {
121
+ blake2b_param P;
122
+
123
+ if (S == NULL) {
124
+ return -1;
125
+ }
126
+
127
+ if ((outlen == 0) || (outlen > BLAKE2B_OUTBYTES)) {
128
+ blake2b_invalidate_state(S);
129
+ return -1;
130
+ }
131
+
132
+ if ((key == 0) || (keylen == 0) || (keylen > BLAKE2B_KEYBYTES)) {
133
+ blake2b_invalidate_state(S);
134
+ return -1;
135
+ }
136
+
137
+ /* Setup Parameter Block for keyed BLAKE2 */
138
+ P.digest_length = (uint8_t)outlen;
139
+ P.key_length = (uint8_t)keylen;
140
+ P.fanout = 1;
141
+ P.depth = 1;
142
+ P.leaf_length = 0;
143
+ P.node_offset = 0;
144
+ P.node_depth = 0;
145
+ P.inner_length = 0;
146
+ memset(P.reserved, 0, sizeof(P.reserved));
147
+ memset(P.salt, 0, sizeof(P.salt));
148
+ memset(P.personal, 0, sizeof(P.personal));
149
+
150
+ if (blake2b_init_param(S, &P) < 0) {
151
+ blake2b_invalidate_state(S);
152
+ return -1;
153
+ }
154
+
155
+ {
156
+ uint8_t block[BLAKE2B_BLOCKBYTES];
157
+ memset(block, 0, BLAKE2B_BLOCKBYTES);
158
+ memcpy(block, key, keylen);
159
+ blake2b_update(S, block, BLAKE2B_BLOCKBYTES);
160
+ /* Burn the key from stack */
161
+ clear_internal_memory(block, BLAKE2B_BLOCKBYTES);
162
+ }
163
+ return 0;
164
+ }
165
+
166
+ static void blake2b_compress(blake2b_state *S, const uint8_t *block) {
167
+ uint64_t m[16];
168
+ uint64_t v[16];
169
+ unsigned int i, r;
170
+
171
+ for (i = 0; i < 16; ++i) {
172
+ m[i] = load64(block + i * sizeof(m[i]));
173
+ }
174
+
175
+ for (i = 0; i < 8; ++i) {
176
+ v[i] = S->h[i];
177
+ }
178
+
179
+ v[8] = blake2b_IV[0];
180
+ v[9] = blake2b_IV[1];
181
+ v[10] = blake2b_IV[2];
182
+ v[11] = blake2b_IV[3];
183
+ v[12] = blake2b_IV[4] ^ S->t[0];
184
+ v[13] = blake2b_IV[5] ^ S->t[1];
185
+ v[14] = blake2b_IV[6] ^ S->f[0];
186
+ v[15] = blake2b_IV[7] ^ S->f[1];
187
+
188
+ #define G(r, i, a, b, c, d) \
189
+ do { \
190
+ a = a + b + m[blake2b_sigma[r][2 * i + 0]]; \
191
+ d = rotr64(d ^ a, 32); \
192
+ c = c + d; \
193
+ b = rotr64(b ^ c, 24); \
194
+ a = a + b + m[blake2b_sigma[r][2 * i + 1]]; \
195
+ d = rotr64(d ^ a, 16); \
196
+ c = c + d; \
197
+ b = rotr64(b ^ c, 63); \
198
+ } while ((void)0, 0)
199
+
200
+ #define ROUND(r) \
201
+ do { \
202
+ G(r, 0, v[0], v[4], v[8], v[12]); \
203
+ G(r, 1, v[1], v[5], v[9], v[13]); \
204
+ G(r, 2, v[2], v[6], v[10], v[14]); \
205
+ G(r, 3, v[3], v[7], v[11], v[15]); \
206
+ G(r, 4, v[0], v[5], v[10], v[15]); \
207
+ G(r, 5, v[1], v[6], v[11], v[12]); \
208
+ G(r, 6, v[2], v[7], v[8], v[13]); \
209
+ G(r, 7, v[3], v[4], v[9], v[14]); \
210
+ } while ((void)0, 0)
211
+
212
+ for (r = 0; r < 12; ++r) {
213
+ ROUND(r);
214
+ }
215
+
216
+ for (i = 0; i < 8; ++i) {
217
+ S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
218
+ }
219
+
220
+ #undef G
221
+ #undef ROUND
222
+ }
223
+
224
+ int blake2b_update(blake2b_state *S, const void *in, size_t inlen) {
225
+ const uint8_t *pin = (const uint8_t *)in;
226
+
227
+ if (inlen == 0) {
228
+ return 0;
229
+ }
230
+
231
+ /* Sanity check */
232
+ if (S == NULL || in == NULL) {
233
+ return -1;
234
+ }
235
+
236
+ /* Is this a reused state? */
237
+ if (S->f[0] != 0) {
238
+ return -1;
239
+ }
240
+
241
+ if (S->buflen + inlen > BLAKE2B_BLOCKBYTES) {
242
+ /* Complete current block */
243
+ size_t left = S->buflen;
244
+ size_t fill = BLAKE2B_BLOCKBYTES - left;
245
+ memcpy(&S->buf[left], pin, fill);
246
+ blake2b_increment_counter(S, BLAKE2B_BLOCKBYTES);
247
+ blake2b_compress(S, S->buf);
248
+ S->buflen = 0;
249
+ inlen -= fill;
250
+ pin += fill;
251
+ /* Avoid buffer copies when possible */
252
+ while (inlen > BLAKE2B_BLOCKBYTES) {
253
+ blake2b_increment_counter(S, BLAKE2B_BLOCKBYTES);
254
+ blake2b_compress(S, pin);
255
+ inlen -= BLAKE2B_BLOCKBYTES;
256
+ pin += BLAKE2B_BLOCKBYTES;
257
+ }
258
+ }
259
+ memcpy(&S->buf[S->buflen], pin, inlen);
260
+ S->buflen += (unsigned int)inlen;
261
+ return 0;
262
+ }
263
+
264
+ int blake2b_final(blake2b_state *S, void *out, size_t outlen) {
265
+ uint8_t buffer[BLAKE2B_OUTBYTES] = {0};
266
+ unsigned int i;
267
+
268
+ /* Sanity checks */
269
+ if (S == NULL || out == NULL || outlen < S->outlen) {
270
+ return -1;
271
+ }
272
+
273
+ /* Is this a reused state? */
274
+ if (S->f[0] != 0) {
275
+ return -1;
276
+ }
277
+
278
+ blake2b_increment_counter(S, S->buflen);
279
+ blake2b_set_lastblock(S);
280
+ memset(&S->buf[S->buflen], 0, BLAKE2B_BLOCKBYTES - S->buflen); /* Padding */
281
+ blake2b_compress(S, S->buf);
282
+
283
+ for (i = 0; i < 8; ++i) { /* Output full hash to temp buffer */
284
+ store64(buffer + sizeof(S->h[i]) * i, S->h[i]);
285
+ }
286
+
287
+ memcpy(out, buffer, S->outlen);
288
+ clear_internal_memory(buffer, sizeof(buffer));
289
+ clear_internal_memory(S->buf, sizeof(S->buf));
290
+ clear_internal_memory(S->h, sizeof(S->h));
291
+ return 0;
292
+ }
293
+
294
+ int blake2b(void *out, size_t outlen, const void *in, size_t inlen,
295
+ const void *key, size_t keylen) {
296
+ blake2b_state S;
297
+ int ret = -1;
298
+
299
+ /* Verify parameters */
300
+ if (NULL == in && inlen > 0) {
301
+ goto fail;
302
+ }
303
+
304
+ if (NULL == out || outlen == 0 || outlen > BLAKE2B_OUTBYTES) {
305
+ goto fail;
306
+ }
307
+
308
+ if ((NULL == key && keylen > 0) || keylen > BLAKE2B_KEYBYTES) {
309
+ goto fail;
310
+ }
311
+
312
+ if (keylen > 0) {
313
+ if (blake2b_init_key(&S, outlen, key, keylen) < 0) {
314
+ goto fail;
315
+ }
316
+ } else {
317
+ if (blake2b_init(&S, outlen) < 0) {
318
+ goto fail;
319
+ }
320
+ }
321
+
322
+ if (blake2b_update(&S, in, inlen) < 0) {
323
+ goto fail;
324
+ }
325
+ ret = blake2b_final(&S, out, outlen);
326
+
327
+ fail:
328
+ clear_internal_memory(&S, sizeof(S));
329
+ return ret;
330
+ }
331
+
332
+ /* Argon2 Team - Begin Code */
333
+ int blake2b_long(void *pout, size_t outlen, const void *in, size_t inlen) {
334
+ uint8_t *out = (uint8_t *)pout;
335
+ blake2b_state blake_state;
336
+ uint8_t outlen_bytes[sizeof(uint32_t)] = {0};
337
+ int ret = -1;
338
+
339
+ if (outlen > UINT32_MAX) {
340
+ goto fail;
341
+ }
342
+
343
+ /* Ensure little-endian byte order! */
344
+ store32(outlen_bytes, (uint32_t)outlen);
345
+
346
+ #define TRY(statement) \
347
+ do { \
348
+ ret = statement; \
349
+ if (ret < 0) { \
350
+ goto fail; \
351
+ } \
352
+ } while ((void)0, 0)
353
+
354
+ if (outlen <= BLAKE2B_OUTBYTES) {
355
+ TRY(blake2b_init(&blake_state, outlen));
356
+ TRY(blake2b_update(&blake_state, outlen_bytes, sizeof(outlen_bytes)));
357
+ TRY(blake2b_update(&blake_state, in, inlen));
358
+ TRY(blake2b_final(&blake_state, out, outlen));
359
+ } else {
360
+ uint32_t toproduce;
361
+ uint8_t out_buffer[BLAKE2B_OUTBYTES];
362
+ uint8_t in_buffer[BLAKE2B_OUTBYTES];
363
+ TRY(blake2b_init(&blake_state, BLAKE2B_OUTBYTES));
364
+ TRY(blake2b_update(&blake_state, outlen_bytes, sizeof(outlen_bytes)));
365
+ TRY(blake2b_update(&blake_state, in, inlen));
366
+ TRY(blake2b_final(&blake_state, out_buffer, BLAKE2B_OUTBYTES));
367
+ memcpy(out, out_buffer, BLAKE2B_OUTBYTES / 2);
368
+ out += BLAKE2B_OUTBYTES / 2;
369
+ toproduce = (uint32_t)outlen - BLAKE2B_OUTBYTES / 2;
370
+
371
+ while (toproduce > BLAKE2B_OUTBYTES) {
372
+ memcpy(in_buffer, out_buffer, BLAKE2B_OUTBYTES);
373
+ TRY(blake2b(out_buffer, BLAKE2B_OUTBYTES, in_buffer,
374
+ BLAKE2B_OUTBYTES, NULL, 0));
375
+ memcpy(out, out_buffer, BLAKE2B_OUTBYTES / 2);
376
+ out += BLAKE2B_OUTBYTES / 2;
377
+ toproduce -= BLAKE2B_OUTBYTES / 2;
378
+ }
379
+
380
+ memcpy(in_buffer, out_buffer, BLAKE2B_OUTBYTES);
381
+ TRY(blake2b(out_buffer, toproduce, in_buffer, BLAKE2B_OUTBYTES, NULL,
382
+ 0));
383
+ memcpy(out, out_buffer, toproduce);
384
+ }
385
+ fail:
386
+ clear_internal_memory(&blake_state, sizeof(blake_state));
387
+ return ret;
388
+ #undef TRY
389
+ }
390
+ /* Argon2 Team - End Code */