enzoic 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.gitmodules +3 -0
  4. data/.travis.yml +3 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE +5 -0
  7. data/README.md +90 -0
  8. data/Rakefile +28 -0
  9. data/enzoic.gemspec +36 -0
  10. data/ext/.DS_Store +0 -0
  11. data/ext/argon2-wrapper/Makefile +74 -0
  12. data/ext/argon2-wrapper/argon2-wrapper.c +165 -0
  13. data/ext/argon2-wrapper/extconf.rb +1 -0
  14. data/ext/argon2_import/.DS_Store +0 -0
  15. data/ext/digest/whirlpool/extconf.rb +10 -0
  16. data/ext/digest/whirlpool/whirlpool-algorithm.c +476 -0
  17. data/ext/digest/whirlpool/whirlpool-algorithm.h +86 -0
  18. data/ext/digest/whirlpool/whirlpool-constants.h +1116 -0
  19. data/ext/digest/whirlpool/whirlpool-portability.h +142 -0
  20. data/ext/digest/whirlpool/whirlpool.c +51 -0
  21. data/ext/phc-winner-argon2/.gitattributes +10 -0
  22. data/ext/phc-winner-argon2/.gitignore +21 -0
  23. data/ext/phc-winner-argon2/.travis.yml +14 -0
  24. data/ext/phc-winner-argon2/Argon2.sln +160 -0
  25. data/ext/phc-winner-argon2/CHANGELOG.md +25 -0
  26. data/ext/phc-winner-argon2/LICENSE +314 -0
  27. data/ext/phc-winner-argon2/Makefile +187 -0
  28. data/ext/phc-winner-argon2/README.md +290 -0
  29. data/ext/phc-winner-argon2/appveyor.yml +25 -0
  30. data/ext/phc-winner-argon2/argon2-specs.pdf +0 -0
  31. data/ext/phc-winner-argon2/export.sh +7 -0
  32. data/ext/phc-winner-argon2/include/argon2.h +435 -0
  33. data/ext/phc-winner-argon2/kats/argon2d +12304 -0
  34. data/ext/phc-winner-argon2/kats/argon2d.shasum +1 -0
  35. data/ext/phc-winner-argon2/kats/argon2d_v16 +12304 -0
  36. data/ext/phc-winner-argon2/kats/argon2d_v16.shasum +1 -0
  37. data/ext/phc-winner-argon2/kats/argon2i +12304 -0
  38. data/ext/phc-winner-argon2/kats/argon2i.shasum +1 -0
  39. data/ext/phc-winner-argon2/kats/argon2i_v16 +12304 -0
  40. data/ext/phc-winner-argon2/kats/argon2i_v16.shasum +1 -0
  41. data/ext/phc-winner-argon2/kats/argon2id +12304 -0
  42. data/ext/phc-winner-argon2/kats/argon2id.shasum +1 -0
  43. data/ext/phc-winner-argon2/kats/argon2id_v16 +12304 -0
  44. data/ext/phc-winner-argon2/kats/argon2id_v16.shasum +1 -0
  45. data/ext/phc-winner-argon2/kats/check-sums.ps1 +42 -0
  46. data/ext/phc-winner-argon2/kats/check-sums.sh +13 -0
  47. data/ext/phc-winner-argon2/kats/test.ps1 +50 -0
  48. data/ext/phc-winner-argon2/kats/test.sh +49 -0
  49. data/ext/phc-winner-argon2/latex/IEEEtran.cls +6347 -0
  50. data/ext/phc-winner-argon2/latex/Makefile +18 -0
  51. data/ext/phc-winner-argon2/latex/argon2-specs.tex +920 -0
  52. data/ext/phc-winner-argon2/latex/pics/argon2-par.pdf +0 -0
  53. data/ext/phc-winner-argon2/latex/pics/compression.pdf +0 -0
  54. data/ext/phc-winner-argon2/latex/pics/generic.pdf +0 -0
  55. data/ext/phc-winner-argon2/latex/pics/power-distribution.jpg +0 -0
  56. data/ext/phc-winner-argon2/latex/tradeoff.bib +822 -0
  57. data/ext/phc-winner-argon2/libargon2.pc +16 -0
  58. data/ext/phc-winner-argon2/man/argon2.1 +57 -0
  59. data/ext/phc-winner-argon2/src/argon2.c +452 -0
  60. data/ext/phc-winner-argon2/src/bench.c +111 -0
  61. data/ext/phc-winner-argon2/src/blake2/blake2-impl.h +156 -0
  62. data/ext/phc-winner-argon2/src/blake2/blake2.h +91 -0
  63. data/ext/phc-winner-argon2/src/blake2/blake2b.c +390 -0
  64. data/ext/phc-winner-argon2/src/blake2/blamka-round-opt.h +328 -0
  65. data/ext/phc-winner-argon2/src/blake2/blamka-round-ref.h +56 -0
  66. data/ext/phc-winner-argon2/src/core.c +635 -0
  67. data/ext/phc-winner-argon2/src/core.h +227 -0
  68. data/ext/phc-winner-argon2/src/encoding.c +463 -0
  69. data/ext/phc-winner-argon2/src/encoding.h +57 -0
  70. data/ext/phc-winner-argon2/src/genkat.c +208 -0
  71. data/ext/phc-winner-argon2/src/genkat.h +49 -0
  72. data/ext/phc-winner-argon2/src/opt.c +241 -0
  73. data/ext/phc-winner-argon2/src/ref.c +194 -0
  74. data/ext/phc-winner-argon2/src/run.c +317 -0
  75. data/ext/phc-winner-argon2/src/test.c +254 -0
  76. data/ext/phc-winner-argon2/src/thread.c +57 -0
  77. data/ext/phc-winner-argon2/src/thread.h +67 -0
  78. data/ext/phc-winner-argon2/vs2015/Argon2Opt/Argon2Opt.vcxproj +226 -0
  79. data/ext/phc-winner-argon2/vs2015/Argon2Opt/Argon2Opt.vcxproj.filters +69 -0
  80. data/ext/phc-winner-argon2/vs2015/Argon2OptBench/Argon2OptBench.vcxproj +226 -0
  81. data/ext/phc-winner-argon2/vs2015/Argon2OptBench/Argon2OptBench.vcxproj.filters +69 -0
  82. data/ext/phc-winner-argon2/vs2015/Argon2OptDll/Argon2OptDll.vcxproj +225 -0
  83. data/ext/phc-winner-argon2/vs2015/Argon2OptDll/Argon2OptDll.vcxproj.filters +66 -0
  84. data/ext/phc-winner-argon2/vs2015/Argon2OptGenKAT/Argon2OptGenKAT.vcxproj +239 -0
  85. data/ext/phc-winner-argon2/vs2015/Argon2OptGenKAT/Argon2OptGenKAT.vcxproj.filters +72 -0
  86. data/ext/phc-winner-argon2/vs2015/Argon2OptTestCI/Argon2OptTestCI.vcxproj +227 -0
  87. data/ext/phc-winner-argon2/vs2015/Argon2OptTestCI/Argon2OptTestCI.vcxproj.filters +69 -0
  88. data/ext/phc-winner-argon2/vs2015/Argon2Ref/Argon2Ref.vcxproj +226 -0
  89. data/ext/phc-winner-argon2/vs2015/Argon2Ref/Argon2Ref.vcxproj.filters +69 -0
  90. data/ext/phc-winner-argon2/vs2015/Argon2RefBench/Argon2RefBench.vcxproj +226 -0
  91. data/ext/phc-winner-argon2/vs2015/Argon2RefBench/Argon2RefBench.vcxproj.filters +69 -0
  92. data/ext/phc-winner-argon2/vs2015/Argon2RefDll/Argon2RefDll.vcxproj +225 -0
  93. data/ext/phc-winner-argon2/vs2015/Argon2RefDll/Argon2RefDll.vcxproj.filters +66 -0
  94. data/ext/phc-winner-argon2/vs2015/Argon2RefGenKAT/Argon2RefGenKAT.vcxproj +227 -0
  95. data/ext/phc-winner-argon2/vs2015/Argon2RefGenKAT/Argon2RefGenKAT.vcxproj.filters +72 -0
  96. data/ext/phc-winner-argon2/vs2015/Argon2RefTestCI/Argon2RefTestCI.vcxproj +226 -0
  97. data/ext/phc-winner-argon2/vs2015/Argon2RefTestCI/Argon2RefTestCI.vcxproj.filters +69 -0
  98. data/lib/enzoic.rb +189 -0
  99. data/lib/enzoic/argon2_errors.rb +39 -0
  100. data/lib/enzoic/argon2_wrapper_ffi.rb +89 -0
  101. data/lib/enzoic/constants.rb +10 -0
  102. data/lib/enzoic/errors.rb +3 -0
  103. data/lib/enzoic/hashing.rb +258 -0
  104. data/lib/enzoic/password_type.rb +25 -0
  105. data/lib/enzoic/version.rb +5 -0
  106. metadata +354 -0
@@ -0,0 +1,328 @@
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 : http://creativecommons.org/publicdomain/zero/1.0
12
+ * - Apache 2.0 : http://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 BLAKE_ROUND_MKA_OPT_H
19
+ #define BLAKE_ROUND_MKA_OPT_H
20
+
21
+ #include "blake2-impl.h"
22
+
23
+ #include <emmintrin.h>
24
+ #if defined(__SSSE3__)
25
+ #include <tmmintrin.h> /* for _mm_shuffle_epi8 and _mm_alignr_epi8 */
26
+ #endif
27
+
28
+ #if defined(__XOP__) && (defined(__GNUC__) || defined(__clang__))
29
+ #include <x86intrin.h>
30
+ #endif
31
+
32
+ #if !defined(__AVX2__)
33
+ #if !defined(__XOP__)
34
+ #if defined(__SSSE3__)
35
+ #define r16 \
36
+ (_mm_setr_epi8(2, 3, 4, 5, 6, 7, 0, 1, 10, 11, 12, 13, 14, 15, 8, 9))
37
+ #define r24 \
38
+ (_mm_setr_epi8(3, 4, 5, 6, 7, 0, 1, 2, 11, 12, 13, 14, 15, 8, 9, 10))
39
+ #define _mm_roti_epi64(x, c) \
40
+ (-(c) == 32) \
41
+ ? _mm_shuffle_epi32((x), _MM_SHUFFLE(2, 3, 0, 1)) \
42
+ : (-(c) == 24) \
43
+ ? _mm_shuffle_epi8((x), r24) \
44
+ : (-(c) == 16) \
45
+ ? _mm_shuffle_epi8((x), r16) \
46
+ : (-(c) == 63) \
47
+ ? _mm_xor_si128(_mm_srli_epi64((x), -(c)), \
48
+ _mm_add_epi64((x), (x))) \
49
+ : _mm_xor_si128(_mm_srli_epi64((x), -(c)), \
50
+ _mm_slli_epi64((x), 64 - (-(c))))
51
+ #else /* defined(__SSE2__) */
52
+ #define _mm_roti_epi64(r, c) \
53
+ _mm_xor_si128(_mm_srli_epi64((r), -(c)), _mm_slli_epi64((r), 64 - (-(c))))
54
+ #endif
55
+ #else
56
+ #endif
57
+
58
+ static BLAKE2_INLINE __m128i fBlaMka(__m128i x, __m128i y) {
59
+ const __m128i z = _mm_mul_epu32(x, y);
60
+ return _mm_add_epi64(_mm_add_epi64(x, y), _mm_add_epi64(z, z));
61
+ }
62
+
63
+ #define G1(A0, B0, C0, D0, A1, B1, C1, D1) \
64
+ do { \
65
+ A0 = fBlaMka(A0, B0); \
66
+ A1 = fBlaMka(A1, B1); \
67
+ \
68
+ D0 = _mm_xor_si128(D0, A0); \
69
+ D1 = _mm_xor_si128(D1, A1); \
70
+ \
71
+ D0 = _mm_roti_epi64(D0, -32); \
72
+ D1 = _mm_roti_epi64(D1, -32); \
73
+ \
74
+ C0 = fBlaMka(C0, D0); \
75
+ C1 = fBlaMka(C1, D1); \
76
+ \
77
+ B0 = _mm_xor_si128(B0, C0); \
78
+ B1 = _mm_xor_si128(B1, C1); \
79
+ \
80
+ B0 = _mm_roti_epi64(B0, -24); \
81
+ B1 = _mm_roti_epi64(B1, -24); \
82
+ } while ((void)0, 0)
83
+
84
+ #define G2(A0, B0, C0, D0, A1, B1, C1, D1) \
85
+ do { \
86
+ A0 = fBlaMka(A0, B0); \
87
+ A1 = fBlaMka(A1, B1); \
88
+ \
89
+ D0 = _mm_xor_si128(D0, A0); \
90
+ D1 = _mm_xor_si128(D1, A1); \
91
+ \
92
+ D0 = _mm_roti_epi64(D0, -16); \
93
+ D1 = _mm_roti_epi64(D1, -16); \
94
+ \
95
+ C0 = fBlaMka(C0, D0); \
96
+ C1 = fBlaMka(C1, D1); \
97
+ \
98
+ B0 = _mm_xor_si128(B0, C0); \
99
+ B1 = _mm_xor_si128(B1, C1); \
100
+ \
101
+ B0 = _mm_roti_epi64(B0, -63); \
102
+ B1 = _mm_roti_epi64(B1, -63); \
103
+ } while ((void)0, 0)
104
+
105
+ #if defined(__SSSE3__)
106
+ #define DIAGONALIZE(A0, B0, C0, D0, A1, B1, C1, D1) \
107
+ do { \
108
+ __m128i t0 = _mm_alignr_epi8(B1, B0, 8); \
109
+ __m128i t1 = _mm_alignr_epi8(B0, B1, 8); \
110
+ B0 = t0; \
111
+ B1 = t1; \
112
+ \
113
+ t0 = C0; \
114
+ C0 = C1; \
115
+ C1 = t0; \
116
+ \
117
+ t0 = _mm_alignr_epi8(D1, D0, 8); \
118
+ t1 = _mm_alignr_epi8(D0, D1, 8); \
119
+ D0 = t1; \
120
+ D1 = t0; \
121
+ } while ((void)0, 0)
122
+
123
+ #define UNDIAGONALIZE(A0, B0, C0, D0, A1, B1, C1, D1) \
124
+ do { \
125
+ __m128i t0 = _mm_alignr_epi8(B0, B1, 8); \
126
+ __m128i t1 = _mm_alignr_epi8(B1, B0, 8); \
127
+ B0 = t0; \
128
+ B1 = t1; \
129
+ \
130
+ t0 = C0; \
131
+ C0 = C1; \
132
+ C1 = t0; \
133
+ \
134
+ t0 = _mm_alignr_epi8(D0, D1, 8); \
135
+ t1 = _mm_alignr_epi8(D1, D0, 8); \
136
+ D0 = t1; \
137
+ D1 = t0; \
138
+ } while ((void)0, 0)
139
+ #else /* SSE2 */
140
+ #define DIAGONALIZE(A0, B0, C0, D0, A1, B1, C1, D1) \
141
+ do { \
142
+ __m128i t0 = D0; \
143
+ __m128i t1 = B0; \
144
+ D0 = C0; \
145
+ C0 = C1; \
146
+ C1 = D0; \
147
+ D0 = _mm_unpackhi_epi64(D1, _mm_unpacklo_epi64(t0, t0)); \
148
+ D1 = _mm_unpackhi_epi64(t0, _mm_unpacklo_epi64(D1, D1)); \
149
+ B0 = _mm_unpackhi_epi64(B0, _mm_unpacklo_epi64(B1, B1)); \
150
+ B1 = _mm_unpackhi_epi64(B1, _mm_unpacklo_epi64(t1, t1)); \
151
+ } while ((void)0, 0)
152
+
153
+ #define UNDIAGONALIZE(A0, B0, C0, D0, A1, B1, C1, D1) \
154
+ do { \
155
+ __m128i t0, t1; \
156
+ t0 = C0; \
157
+ C0 = C1; \
158
+ C1 = t0; \
159
+ t0 = B0; \
160
+ t1 = D0; \
161
+ B0 = _mm_unpackhi_epi64(B1, _mm_unpacklo_epi64(B0, B0)); \
162
+ B1 = _mm_unpackhi_epi64(t0, _mm_unpacklo_epi64(B1, B1)); \
163
+ D0 = _mm_unpackhi_epi64(D0, _mm_unpacklo_epi64(D1, D1)); \
164
+ D1 = _mm_unpackhi_epi64(D1, _mm_unpacklo_epi64(t1, t1)); \
165
+ } while ((void)0, 0)
166
+ #endif
167
+
168
+ #define BLAKE2_ROUND(A0, A1, B0, B1, C0, C1, D0, D1) \
169
+ do { \
170
+ G1(A0, B0, C0, D0, A1, B1, C1, D1); \
171
+ G2(A0, B0, C0, D0, A1, B1, C1, D1); \
172
+ \
173
+ DIAGONALIZE(A0, B0, C0, D0, A1, B1, C1, D1); \
174
+ \
175
+ G1(A0, B0, C0, D0, A1, B1, C1, D1); \
176
+ G2(A0, B0, C0, D0, A1, B1, C1, D1); \
177
+ \
178
+ UNDIAGONALIZE(A0, B0, C0, D0, A1, B1, C1, D1); \
179
+ } while ((void)0, 0)
180
+ #else /* __AVX2__ */
181
+
182
+ #include <immintrin.h>
183
+
184
+ #define rotr32(x) _mm256_shuffle_epi32(x, _MM_SHUFFLE(2, 3, 0, 1))
185
+ #define rotr24(x) _mm256_shuffle_epi8(x, _mm256_setr_epi8(3, 4, 5, 6, 7, 0, 1, 2, 11, 12, 13, 14, 15, 8, 9, 10, 3, 4, 5, 6, 7, 0, 1, 2, 11, 12, 13, 14, 15, 8, 9, 10))
186
+ #define rotr16(x) _mm256_shuffle_epi8(x, _mm256_setr_epi8(2, 3, 4, 5, 6, 7, 0, 1, 10, 11, 12, 13, 14, 15, 8, 9, 2, 3, 4, 5, 6, 7, 0, 1, 10, 11, 12, 13, 14, 15, 8, 9))
187
+ #define rotr63(x) _mm256_xor_si256(_mm256_srli_epi64((x), 63), _mm256_add_epi64((x), (x)))
188
+
189
+ #define G1_AVX2(A0, A1, B0, B1, C0, C1, D0, D1) \
190
+ do { \
191
+ __m256i ml = _mm256_mul_epu32(A0, B0); \
192
+ ml = _mm256_add_epi64(ml, ml); \
193
+ A0 = _mm256_add_epi64(A0, _mm256_add_epi64(B0, ml)); \
194
+ D0 = _mm256_xor_si256(D0, A0); \
195
+ D0 = rotr32(D0); \
196
+ \
197
+ ml = _mm256_mul_epu32(C0, D0); \
198
+ ml = _mm256_add_epi64(ml, ml); \
199
+ C0 = _mm256_add_epi64(C0, _mm256_add_epi64(D0, ml)); \
200
+ \
201
+ B0 = _mm256_xor_si256(B0, C0); \
202
+ B0 = rotr24(B0); \
203
+ \
204
+ ml = _mm256_mul_epu32(A1, B1); \
205
+ ml = _mm256_add_epi64(ml, ml); \
206
+ A1 = _mm256_add_epi64(A1, _mm256_add_epi64(B1, ml)); \
207
+ D1 = _mm256_xor_si256(D1, A1); \
208
+ D1 = rotr32(D1); \
209
+ \
210
+ ml = _mm256_mul_epu32(C1, D1); \
211
+ ml = _mm256_add_epi64(ml, ml); \
212
+ C1 = _mm256_add_epi64(C1, _mm256_add_epi64(D1, ml)); \
213
+ \
214
+ B1 = _mm256_xor_si256(B1, C1); \
215
+ B1 = rotr24(B1); \
216
+ } while((void)0, 0);
217
+
218
+ #define G2_AVX2(A0, A1, B0, B1, C0, C1, D0, D1) \
219
+ do { \
220
+ __m256i ml = _mm256_mul_epu32(A0, B0); \
221
+ ml = _mm256_add_epi64(ml, ml); \
222
+ A0 = _mm256_add_epi64(A0, _mm256_add_epi64(B0, ml)); \
223
+ D0 = _mm256_xor_si256(D0, A0); \
224
+ D0 = rotr16(D0); \
225
+ \
226
+ ml = _mm256_mul_epu32(C0, D0); \
227
+ ml = _mm256_add_epi64(ml, ml); \
228
+ C0 = _mm256_add_epi64(C0, _mm256_add_epi64(D0, ml)); \
229
+ B0 = _mm256_xor_si256(B0, C0); \
230
+ B0 = rotr63(B0); \
231
+ \
232
+ ml = _mm256_mul_epu32(A1, B1); \
233
+ ml = _mm256_add_epi64(ml, ml); \
234
+ A1 = _mm256_add_epi64(A1, _mm256_add_epi64(B1, ml)); \
235
+ D1 = _mm256_xor_si256(D1, A1); \
236
+ D1 = rotr16(D1); \
237
+ \
238
+ ml = _mm256_mul_epu32(C1, D1); \
239
+ ml = _mm256_add_epi64(ml, ml); \
240
+ C1 = _mm256_add_epi64(C1, _mm256_add_epi64(D1, ml)); \
241
+ B1 = _mm256_xor_si256(B1, C1); \
242
+ B1 = rotr63(B1); \
243
+ } while((void)0, 0);
244
+
245
+ #define DIAGONALIZE_1(A0, B0, C0, D0, A1, B1, C1, D1) \
246
+ do { \
247
+ B0 = _mm256_permute4x64_epi64(B0, _MM_SHUFFLE(0, 3, 2, 1)); \
248
+ C0 = _mm256_permute4x64_epi64(C0, _MM_SHUFFLE(1, 0, 3, 2)); \
249
+ D0 = _mm256_permute4x64_epi64(D0, _MM_SHUFFLE(2, 1, 0, 3)); \
250
+ \
251
+ B1 = _mm256_permute4x64_epi64(B1, _MM_SHUFFLE(0, 3, 2, 1)); \
252
+ C1 = _mm256_permute4x64_epi64(C1, _MM_SHUFFLE(1, 0, 3, 2)); \
253
+ D1 = _mm256_permute4x64_epi64(D1, _MM_SHUFFLE(2, 1, 0, 3)); \
254
+ } while((void)0, 0);
255
+
256
+ #define DIAGONALIZE_2(A0, A1, B0, B1, C0, C1, D0, D1) \
257
+ do { \
258
+ __m256i tmp1 = _mm256_blend_epi32(B0, B1, 0xCC); \
259
+ __m256i tmp2 = _mm256_blend_epi32(B0, B1, 0x33); \
260
+ B1 = _mm256_permute4x64_epi64(tmp1, _MM_SHUFFLE(2,3,0,1)); \
261
+ B0 = _mm256_permute4x64_epi64(tmp2, _MM_SHUFFLE(2,3,0,1)); \
262
+ \
263
+ tmp1 = C0; \
264
+ C0 = C1; \
265
+ C1 = tmp1; \
266
+ \
267
+ tmp1 = _mm256_blend_epi32(D0, D1, 0xCC); \
268
+ tmp2 = _mm256_blend_epi32(D0, D1, 0x33); \
269
+ D0 = _mm256_permute4x64_epi64(tmp1, _MM_SHUFFLE(2,3,0,1)); \
270
+ D1 = _mm256_permute4x64_epi64(tmp2, _MM_SHUFFLE(2,3,0,1)); \
271
+ } while(0);
272
+
273
+ #define UNDIAGONALIZE_1(A0, B0, C0, D0, A1, B1, C1, D1) \
274
+ do { \
275
+ B0 = _mm256_permute4x64_epi64(B0, _MM_SHUFFLE(2, 1, 0, 3)); \
276
+ C0 = _mm256_permute4x64_epi64(C0, _MM_SHUFFLE(1, 0, 3, 2)); \
277
+ D0 = _mm256_permute4x64_epi64(D0, _MM_SHUFFLE(0, 3, 2, 1)); \
278
+ \
279
+ B1 = _mm256_permute4x64_epi64(B1, _MM_SHUFFLE(2, 1, 0, 3)); \
280
+ C1 = _mm256_permute4x64_epi64(C1, _MM_SHUFFLE(1, 0, 3, 2)); \
281
+ D1 = _mm256_permute4x64_epi64(D1, _MM_SHUFFLE(0, 3, 2, 1)); \
282
+ } while((void)0, 0);
283
+
284
+ #define UNDIAGONALIZE_2(A0, A1, B0, B1, C0, C1, D0, D1) \
285
+ do { \
286
+ __m256i tmp1 = _mm256_blend_epi32(B0, B1, 0xCC); \
287
+ __m256i tmp2 = _mm256_blend_epi32(B0, B1, 0x33); \
288
+ B0 = _mm256_permute4x64_epi64(tmp1, _MM_SHUFFLE(2,3,0,1)); \
289
+ B1 = _mm256_permute4x64_epi64(tmp2, _MM_SHUFFLE(2,3,0,1)); \
290
+ \
291
+ tmp1 = C0; \
292
+ C0 = C1; \
293
+ C1 = tmp1; \
294
+ \
295
+ tmp1 = _mm256_blend_epi32(D0, D1, 0x33); \
296
+ tmp2 = _mm256_blend_epi32(D0, D1, 0xCC); \
297
+ D0 = _mm256_permute4x64_epi64(tmp1, _MM_SHUFFLE(2,3,0,1)); \
298
+ D1 = _mm256_permute4x64_epi64(tmp2, _MM_SHUFFLE(2,3,0,1)); \
299
+ } while((void)0, 0);
300
+
301
+ #define BLAKE2_ROUND_1(A0, A1, B0, B1, C0, C1, D0, D1) \
302
+ do{ \
303
+ G1_AVX2(A0, A1, B0, B1, C0, C1, D0, D1) \
304
+ G2_AVX2(A0, A1, B0, B1, C0, C1, D0, D1) \
305
+ \
306
+ DIAGONALIZE_1(A0, B0, C0, D0, A1, B1, C1, D1) \
307
+ \
308
+ G1_AVX2(A0, A1, B0, B1, C0, C1, D0, D1) \
309
+ G2_AVX2(A0, A1, B0, B1, C0, C1, D0, D1) \
310
+ \
311
+ UNDIAGONALIZE_1(A0, B0, C0, D0, A1, B1, C1, D1) \
312
+ } while((void)0, 0);
313
+
314
+ #define BLAKE2_ROUND_2(A0, A1, B0, B1, C0, C1, D0, D1) \
315
+ do{ \
316
+ G1_AVX2(A0, A1, B0, B1, C0, C1, D0, D1) \
317
+ G2_AVX2(A0, A1, B0, B1, C0, C1, D0, D1) \
318
+ \
319
+ DIAGONALIZE_2(A0, A1, B0, B1, C0, C1, D0, D1) \
320
+ \
321
+ G1_AVX2(A0, A1, B0, B1, C0, C1, D0, D1) \
322
+ G2_AVX2(A0, A1, B0, B1, C0, C1, D0, D1) \
323
+ \
324
+ UNDIAGONALIZE_2(A0, A1, B0, B1, C0, C1, D0, D1) \
325
+ } while((void)0, 0);
326
+
327
+ #endif /* __AVX2__ */
328
+ #endif /* BLAKE_ROUND_MKA_OPT_H */
@@ -0,0 +1,56 @@
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 : http://creativecommons.org/publicdomain/zero/1.0
12
+ * - Apache 2.0 : http://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 BLAKE_ROUND_MKA_H
19
+ #define BLAKE_ROUND_MKA_H
20
+
21
+ #include "blake2.h"
22
+ #include "blake2-impl.h"
23
+
24
+ /*designed by the Lyra PHC team */
25
+ static BLAKE2_INLINE uint64_t fBlaMka(uint64_t x, uint64_t y) {
26
+ const uint64_t m = UINT64_C(0xFFFFFFFF);
27
+ const uint64_t xy = (x & m) * (y & m);
28
+ return x + y + 2 * xy;
29
+ }
30
+
31
+ #define G(a, b, c, d) \
32
+ do { \
33
+ a = fBlaMka(a, b); \
34
+ d = rotr64(d ^ a, 32); \
35
+ c = fBlaMka(c, d); \
36
+ b = rotr64(b ^ c, 24); \
37
+ a = fBlaMka(a, b); \
38
+ d = rotr64(d ^ a, 16); \
39
+ c = fBlaMka(c, d); \
40
+ b = rotr64(b ^ c, 63); \
41
+ } while ((void)0, 0)
42
+
43
+ #define BLAKE2_ROUND_NOMSG(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, \
44
+ v12, v13, v14, v15) \
45
+ do { \
46
+ G(v0, v4, v8, v12); \
47
+ G(v1, v5, v9, v13); \
48
+ G(v2, v6, v10, v14); \
49
+ G(v3, v7, v11, v15); \
50
+ G(v0, v5, v10, v15); \
51
+ G(v1, v6, v11, v12); \
52
+ G(v2, v7, v8, v13); \
53
+ G(v3, v4, v9, v14); \
54
+ } while ((void)0, 0)
55
+
56
+ #endif
@@ -0,0 +1,635 @@
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 : http://creativecommons.org/publicdomain/zero/1.0
12
+ * - Apache 2.0 : http://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
+ /*For memory wiping*/
19
+ #ifdef _MSC_VER
20
+ #include <windows.h>
21
+ #include <winbase.h> /* For SecureZeroMemory */
22
+ #endif
23
+ #if defined __STDC_LIB_EXT1__
24
+ #define __STDC_WANT_LIB_EXT1__ 1
25
+ #endif
26
+ #define VC_GE_2005(version) (version >= 1400)
27
+
28
+ #include <inttypes.h>
29
+ #include <stdio.h>
30
+ #include <stdlib.h>
31
+ #include <string.h>
32
+
33
+ #include "core.h"
34
+ #include "thread.h"
35
+ #include "blake2/blake2.h"
36
+ #include "blake2/blake2-impl.h"
37
+
38
+ #ifdef GENKAT
39
+ #include "genkat.h"
40
+ #endif
41
+
42
+ #if defined(__clang__)
43
+ #if __has_attribute(optnone)
44
+ #define NOT_OPTIMIZED __attribute__((optnone))
45
+ #endif
46
+ #elif defined(__GNUC__)
47
+ #define GCC_VERSION \
48
+ (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
49
+ #if GCC_VERSION >= 40400
50
+ #define NOT_OPTIMIZED __attribute__((optimize("O0")))
51
+ #endif
52
+ #endif
53
+ #ifndef NOT_OPTIMIZED
54
+ #define NOT_OPTIMIZED
55
+ #endif
56
+
57
+ /***************Instance and Position constructors**********/
58
+ void init_block_value(block *b, uint8_t in) { memset(b->v, in, sizeof(b->v)); }
59
+
60
+ void copy_block(block *dst, const block *src) {
61
+ memcpy(dst->v, src->v, sizeof(uint64_t) * ARGON2_QWORDS_IN_BLOCK);
62
+ }
63
+
64
+ void xor_block(block *dst, const block *src) {
65
+ int i;
66
+ for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i) {
67
+ dst->v[i] ^= src->v[i];
68
+ }
69
+ }
70
+
71
+ static void load_block(block *dst, const void *input) {
72
+ unsigned i;
73
+ for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i) {
74
+ dst->v[i] = load64((const uint8_t *)input + i * sizeof(dst->v[i]));
75
+ }
76
+ }
77
+
78
+ static void store_block(void *output, const block *src) {
79
+ unsigned i;
80
+ for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i) {
81
+ store64((uint8_t *)output + i * sizeof(src->v[i]), src->v[i]);
82
+ }
83
+ }
84
+
85
+ /***************Memory functions*****************/
86
+
87
+ int allocate_memory(const argon2_context *context, uint8_t **memory,
88
+ size_t num, size_t size) {
89
+ size_t memory_size = num*size;
90
+ if (memory == NULL) {
91
+ return ARGON2_MEMORY_ALLOCATION_ERROR;
92
+ }
93
+
94
+ /* 1. Check for multiplication overflow */
95
+ if (size != 0 && memory_size / size != num) {
96
+ return ARGON2_MEMORY_ALLOCATION_ERROR;
97
+ }
98
+
99
+ /* 2. Try to allocate with appropriate allocator */
100
+ if (context->allocate_cbk) {
101
+ (context->allocate_cbk)(memory, memory_size);
102
+ } else {
103
+ *memory = malloc(memory_size);
104
+ }
105
+
106
+ if (*memory == NULL) {
107
+ return ARGON2_MEMORY_ALLOCATION_ERROR;
108
+ }
109
+
110
+ return ARGON2_OK;
111
+ }
112
+
113
+ void free_memory(const argon2_context *context, uint8_t *memory,
114
+ size_t num, size_t size) {
115
+ size_t memory_size = num*size;
116
+ clear_internal_memory(memory, memory_size);
117
+ if (context->free_cbk) {
118
+ (context->free_cbk)(memory, memory_size);
119
+ } else {
120
+ free(memory);
121
+ }
122
+ }
123
+
124
+ void NOT_OPTIMIZED secure_wipe_memory(void *v, size_t n) {
125
+ #if defined(_MSC_VER) && VC_GE_2005(_MSC_VER)
126
+ SecureZeroMemory(v, n);
127
+ #elif defined memset_s
128
+ memset_s(v, n, 0, n);
129
+ #elif defined(__OpenBSD__)
130
+ explicit_bzero(v, n);
131
+ #else
132
+ static void *(*const volatile memset_sec)(void *, int, size_t) = &memset;
133
+ memset_sec(v, 0, n);
134
+ #endif
135
+ }
136
+
137
+ /* Memory clear flag defaults to true. */
138
+ int FLAG_clear_internal_memory = 1;
139
+ void clear_internal_memory(void *v, size_t n) {
140
+ if (FLAG_clear_internal_memory && v) {
141
+ secure_wipe_memory(v, n);
142
+ }
143
+ }
144
+
145
+ void finalize(const argon2_context *context, argon2_instance_t *instance) {
146
+ if (context != NULL && instance != NULL) {
147
+ block blockhash;
148
+ uint32_t l;
149
+
150
+ copy_block(&blockhash, instance->memory + instance->lane_length - 1);
151
+
152
+ /* XOR the last blocks */
153
+ for (l = 1; l < instance->lanes; ++l) {
154
+ uint32_t last_block_in_lane =
155
+ l * instance->lane_length + (instance->lane_length - 1);
156
+ xor_block(&blockhash, instance->memory + last_block_in_lane);
157
+ }
158
+
159
+ /* Hash the result */
160
+ {
161
+ uint8_t blockhash_bytes[ARGON2_BLOCK_SIZE];
162
+ store_block(blockhash_bytes, &blockhash);
163
+ blake2b_long(context->out, context->outlen, blockhash_bytes,
164
+ ARGON2_BLOCK_SIZE);
165
+ /* clear blockhash and blockhash_bytes */
166
+ clear_internal_memory(blockhash.v, ARGON2_BLOCK_SIZE);
167
+ clear_internal_memory(blockhash_bytes, ARGON2_BLOCK_SIZE);
168
+ }
169
+
170
+ #ifdef GENKAT
171
+ print_tag(context->out, context->outlen);
172
+ #endif
173
+
174
+ free_memory(context, (uint8_t *)instance->memory,
175
+ instance->memory_blocks, sizeof(block));
176
+ }
177
+ }
178
+
179
+ uint32_t index_alpha(const argon2_instance_t *instance,
180
+ const argon2_position_t *position, uint32_t pseudo_rand,
181
+ int same_lane) {
182
+ /*
183
+ * Pass 0:
184
+ * This lane : all already finished segments plus already constructed
185
+ * blocks in this segment
186
+ * Other lanes : all already finished segments
187
+ * Pass 1+:
188
+ * This lane : (SYNC_POINTS - 1) last segments plus already constructed
189
+ * blocks in this segment
190
+ * Other lanes : (SYNC_POINTS - 1) last segments
191
+ */
192
+ uint32_t reference_area_size;
193
+ uint64_t relative_position;
194
+ uint32_t start_position, absolute_position;
195
+
196
+ if (0 == position->pass) {
197
+ /* First pass */
198
+ if (0 == position->slice) {
199
+ /* First slice */
200
+ reference_area_size =
201
+ position->index - 1; /* all but the previous */
202
+ } else {
203
+ if (same_lane) {
204
+ /* The same lane => add current segment */
205
+ reference_area_size =
206
+ position->slice * instance->segment_length +
207
+ position->index - 1;
208
+ } else {
209
+ reference_area_size =
210
+ position->slice * instance->segment_length +
211
+ ((position->index == 0) ? (-1) : 0);
212
+ }
213
+ }
214
+ } else {
215
+ /* Second pass */
216
+ if (same_lane) {
217
+ reference_area_size = instance->lane_length -
218
+ instance->segment_length + position->index -
219
+ 1;
220
+ } else {
221
+ reference_area_size = instance->lane_length -
222
+ instance->segment_length +
223
+ ((position->index == 0) ? (-1) : 0);
224
+ }
225
+ }
226
+
227
+ /* 1.2.4. Mapping pseudo_rand to 0..<reference_area_size-1> and produce
228
+ * relative position */
229
+ relative_position = pseudo_rand;
230
+ relative_position = relative_position * relative_position >> 32;
231
+ relative_position = reference_area_size - 1 -
232
+ (reference_area_size * relative_position >> 32);
233
+
234
+ /* 1.2.5 Computing starting position */
235
+ start_position = 0;
236
+
237
+ if (0 != position->pass) {
238
+ start_position = (position->slice == ARGON2_SYNC_POINTS - 1)
239
+ ? 0
240
+ : (position->slice + 1) * instance->segment_length;
241
+ }
242
+
243
+ /* 1.2.6. Computing absolute position */
244
+ absolute_position = (start_position + relative_position) %
245
+ instance->lane_length; /* absolute position */
246
+ return absolute_position;
247
+ }
248
+
249
+ /* Single-threaded version for p=1 case */
250
+ static int fill_memory_blocks_st(argon2_instance_t *instance) {
251
+ uint32_t r, s, l;
252
+
253
+ for (r = 0; r < instance->passes; ++r) {
254
+ for (s = 0; s < ARGON2_SYNC_POINTS; ++s) {
255
+ for (l = 0; l < instance->lanes; ++l) {
256
+ argon2_position_t position = {r, l, (uint8_t)s, 0};
257
+ fill_segment(instance, position);
258
+ }
259
+ }
260
+ #ifdef GENKAT
261
+ internal_kat(instance, r); /* Print all memory blocks */
262
+ #endif
263
+ }
264
+ return ARGON2_OK;
265
+ }
266
+
267
+ #if !defined(ARGON2_NO_THREADS)
268
+
269
+ #ifdef _WIN32
270
+ static unsigned __stdcall fill_segment_thr(void *thread_data)
271
+ #else
272
+ static void *fill_segment_thr(void *thread_data)
273
+ #endif
274
+ {
275
+ argon2_thread_data *my_data = thread_data;
276
+ fill_segment(my_data->instance_ptr, my_data->pos);
277
+ argon2_thread_exit();
278
+ return 0;
279
+ }
280
+
281
+ /* Multi-threaded version for p > 1 case */
282
+ static int fill_memory_blocks_mt(argon2_instance_t *instance) {
283
+ uint32_t r, s;
284
+ argon2_thread_handle_t *thread = NULL;
285
+ argon2_thread_data *thr_data = NULL;
286
+ int rc = ARGON2_OK;
287
+
288
+ /* 1. Allocating space for threads */
289
+ thread = calloc(instance->lanes, sizeof(argon2_thread_handle_t));
290
+ if (thread == NULL) {
291
+ rc = ARGON2_MEMORY_ALLOCATION_ERROR;
292
+ goto fail;
293
+ }
294
+
295
+ thr_data = calloc(instance->lanes, sizeof(argon2_thread_data));
296
+ if (thr_data == NULL) {
297
+ rc = ARGON2_MEMORY_ALLOCATION_ERROR;
298
+ goto fail;
299
+ }
300
+
301
+ for (r = 0; r < instance->passes; ++r) {
302
+ for (s = 0; s < ARGON2_SYNC_POINTS; ++s) {
303
+ uint32_t l;
304
+
305
+ /* 2. Calling threads */
306
+ for (l = 0; l < instance->lanes; ++l) {
307
+ argon2_position_t position;
308
+
309
+ /* 2.1 Join a thread if limit is exceeded */
310
+ if (l >= instance->threads) {
311
+ if (argon2_thread_join(thread[l - instance->threads])) {
312
+ rc = ARGON2_THREAD_FAIL;
313
+ goto fail;
314
+ }
315
+ }
316
+
317
+ /* 2.2 Create thread */
318
+ position.pass = r;
319
+ position.lane = l;
320
+ position.slice = (uint8_t)s;
321
+ position.index = 0;
322
+ thr_data[l].instance_ptr =
323
+ instance; /* preparing the thread input */
324
+ memcpy(&(thr_data[l].pos), &position,
325
+ sizeof(argon2_position_t));
326
+ if (argon2_thread_create(&thread[l], &fill_segment_thr,
327
+ (void *)&thr_data[l])) {
328
+ rc = ARGON2_THREAD_FAIL;
329
+ goto fail;
330
+ }
331
+
332
+ /* fill_segment(instance, position); */
333
+ /*Non-thread equivalent of the lines above */
334
+ }
335
+
336
+ /* 3. Joining remaining threads */
337
+ for (l = instance->lanes - instance->threads; l < instance->lanes;
338
+ ++l) {
339
+ if (argon2_thread_join(thread[l])) {
340
+ rc = ARGON2_THREAD_FAIL;
341
+ goto fail;
342
+ }
343
+ }
344
+ }
345
+
346
+ #ifdef GENKAT
347
+ internal_kat(instance, r); /* Print all memory blocks */
348
+ #endif
349
+ }
350
+
351
+ fail:
352
+ if (thread != NULL) {
353
+ free(thread);
354
+ }
355
+ if (thr_data != NULL) {
356
+ free(thr_data);
357
+ }
358
+ return rc;
359
+ }
360
+
361
+ #endif /* ARGON2_NO_THREADS */
362
+
363
+ int fill_memory_blocks(argon2_instance_t *instance) {
364
+ if (instance == NULL || instance->lanes == 0) {
365
+ return ARGON2_INCORRECT_PARAMETER;
366
+ }
367
+ #if defined(ARGON2_NO_THREADS)
368
+ return fill_memory_blocks_st(instance);
369
+ #else
370
+ return instance->threads == 1 ?
371
+ fill_memory_blocks_st(instance) : fill_memory_blocks_mt(instance);
372
+ #endif
373
+ }
374
+
375
+ int validate_inputs(const argon2_context *context) {
376
+ if (NULL == context) {
377
+ return ARGON2_INCORRECT_PARAMETER;
378
+ }
379
+
380
+ if (NULL == context->out) {
381
+ return ARGON2_OUTPUT_PTR_NULL;
382
+ }
383
+
384
+ /* Validate output length */
385
+ if (ARGON2_MIN_OUTLEN > context->outlen) {
386
+ return ARGON2_OUTPUT_TOO_SHORT;
387
+ }
388
+
389
+ if (ARGON2_MAX_OUTLEN < context->outlen) {
390
+ return ARGON2_OUTPUT_TOO_LONG;
391
+ }
392
+
393
+ /* Validate password (required param) */
394
+ if (NULL == context->pwd) {
395
+ if (0 != context->pwdlen) {
396
+ return ARGON2_PWD_PTR_MISMATCH;
397
+ }
398
+ }
399
+
400
+ if (ARGON2_MIN_PWD_LENGTH > context->pwdlen) {
401
+ return ARGON2_PWD_TOO_SHORT;
402
+ }
403
+
404
+ if (ARGON2_MAX_PWD_LENGTH < context->pwdlen) {
405
+ return ARGON2_PWD_TOO_LONG;
406
+ }
407
+
408
+ /* Validate salt (required param) */
409
+ if (NULL == context->salt) {
410
+ if (0 != context->saltlen) {
411
+ return ARGON2_SALT_PTR_MISMATCH;
412
+ }
413
+ }
414
+
415
+ if (ARGON2_MIN_SALT_LENGTH > context->saltlen) {
416
+ return ARGON2_SALT_TOO_SHORT;
417
+ }
418
+
419
+ if (ARGON2_MAX_SALT_LENGTH < context->saltlen) {
420
+ return ARGON2_SALT_TOO_LONG;
421
+ }
422
+
423
+ /* Validate secret (optional param) */
424
+ if (NULL == context->secret) {
425
+ if (0 != context->secretlen) {
426
+ return ARGON2_SECRET_PTR_MISMATCH;
427
+ }
428
+ } else {
429
+ if (ARGON2_MIN_SECRET > context->secretlen) {
430
+ return ARGON2_SECRET_TOO_SHORT;
431
+ }
432
+ if (ARGON2_MAX_SECRET < context->secretlen) {
433
+ return ARGON2_SECRET_TOO_LONG;
434
+ }
435
+ }
436
+
437
+ /* Validate associated data (optional param) */
438
+ if (NULL == context->ad) {
439
+ if (0 != context->adlen) {
440
+ return ARGON2_AD_PTR_MISMATCH;
441
+ }
442
+ } else {
443
+ if (ARGON2_MIN_AD_LENGTH > context->adlen) {
444
+ return ARGON2_AD_TOO_SHORT;
445
+ }
446
+ if (ARGON2_MAX_AD_LENGTH < context->adlen) {
447
+ return ARGON2_AD_TOO_LONG;
448
+ }
449
+ }
450
+
451
+ /* Validate memory cost */
452
+ if (ARGON2_MIN_MEMORY > context->m_cost) {
453
+ return ARGON2_MEMORY_TOO_LITTLE;
454
+ }
455
+
456
+ if (ARGON2_MAX_MEMORY < context->m_cost) {
457
+ return ARGON2_MEMORY_TOO_MUCH;
458
+ }
459
+
460
+ if (context->m_cost < 8 * context->lanes) {
461
+ return ARGON2_MEMORY_TOO_LITTLE;
462
+ }
463
+
464
+ /* Validate time cost */
465
+ if (ARGON2_MIN_TIME > context->t_cost) {
466
+ return ARGON2_TIME_TOO_SMALL;
467
+ }
468
+
469
+ if (ARGON2_MAX_TIME < context->t_cost) {
470
+ return ARGON2_TIME_TOO_LARGE;
471
+ }
472
+
473
+ /* Validate lanes */
474
+ if (ARGON2_MIN_LANES > context->lanes) {
475
+ return ARGON2_LANES_TOO_FEW;
476
+ }
477
+
478
+ if (ARGON2_MAX_LANES < context->lanes) {
479
+ return ARGON2_LANES_TOO_MANY;
480
+ }
481
+
482
+ /* Validate threads */
483
+ if (ARGON2_MIN_THREADS > context->threads) {
484
+ return ARGON2_THREADS_TOO_FEW;
485
+ }
486
+
487
+ if (ARGON2_MAX_THREADS < context->threads) {
488
+ return ARGON2_THREADS_TOO_MANY;
489
+ }
490
+
491
+ if (NULL != context->allocate_cbk && NULL == context->free_cbk) {
492
+ return ARGON2_FREE_MEMORY_CBK_NULL;
493
+ }
494
+
495
+ if (NULL == context->allocate_cbk && NULL != context->free_cbk) {
496
+ return ARGON2_ALLOCATE_MEMORY_CBK_NULL;
497
+ }
498
+
499
+ return ARGON2_OK;
500
+ }
501
+
502
+ void fill_first_blocks(uint8_t *blockhash, const argon2_instance_t *instance) {
503
+ uint32_t l;
504
+ /* Make the first and second block in each lane as G(H0||0||i) or
505
+ G(H0||1||i) */
506
+ uint8_t blockhash_bytes[ARGON2_BLOCK_SIZE];
507
+ for (l = 0; l < instance->lanes; ++l) {
508
+
509
+ store32(blockhash + ARGON2_PREHASH_DIGEST_LENGTH, 0);
510
+ store32(blockhash + ARGON2_PREHASH_DIGEST_LENGTH + 4, l);
511
+ blake2b_long(blockhash_bytes, ARGON2_BLOCK_SIZE, blockhash,
512
+ ARGON2_PREHASH_SEED_LENGTH);
513
+ load_block(&instance->memory[l * instance->lane_length + 0],
514
+ blockhash_bytes);
515
+
516
+ store32(blockhash + ARGON2_PREHASH_DIGEST_LENGTH, 1);
517
+ blake2b_long(blockhash_bytes, ARGON2_BLOCK_SIZE, blockhash,
518
+ ARGON2_PREHASH_SEED_LENGTH);
519
+ load_block(&instance->memory[l * instance->lane_length + 1],
520
+ blockhash_bytes);
521
+ }
522
+ clear_internal_memory(blockhash_bytes, ARGON2_BLOCK_SIZE);
523
+ }
524
+
525
+ void initial_hash(uint8_t *blockhash, argon2_context *context,
526
+ argon2_type type) {
527
+ blake2b_state BlakeHash;
528
+ uint8_t value[sizeof(uint32_t)];
529
+
530
+ if (NULL == context || NULL == blockhash) {
531
+ return;
532
+ }
533
+
534
+ blake2b_init(&BlakeHash, ARGON2_PREHASH_DIGEST_LENGTH);
535
+
536
+ store32(&value, context->lanes);
537
+ blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
538
+
539
+ store32(&value, context->outlen);
540
+ blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
541
+
542
+ store32(&value, context->m_cost);
543
+ blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
544
+
545
+ store32(&value, context->t_cost);
546
+ blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
547
+
548
+ store32(&value, context->version);
549
+ blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
550
+
551
+ store32(&value, (uint32_t)type);
552
+ blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
553
+
554
+ store32(&value, context->pwdlen);
555
+ blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
556
+
557
+ if (context->pwd != NULL) {
558
+ blake2b_update(&BlakeHash, (const uint8_t *)context->pwd,
559
+ context->pwdlen);
560
+
561
+ if (context->flags & ARGON2_FLAG_CLEAR_PASSWORD) {
562
+ secure_wipe_memory(context->pwd, context->pwdlen);
563
+ context->pwdlen = 0;
564
+ }
565
+ }
566
+
567
+ store32(&value, context->saltlen);
568
+ blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
569
+
570
+ if (context->salt != NULL) {
571
+ blake2b_update(&BlakeHash, (const uint8_t *)context->salt,
572
+ context->saltlen);
573
+ }
574
+
575
+ store32(&value, context->secretlen);
576
+ blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
577
+
578
+ if (context->secret != NULL) {
579
+ blake2b_update(&BlakeHash, (const uint8_t *)context->secret,
580
+ context->secretlen);
581
+
582
+ if (context->flags & ARGON2_FLAG_CLEAR_SECRET) {
583
+ secure_wipe_memory(context->secret, context->secretlen);
584
+ context->secretlen = 0;
585
+ }
586
+ }
587
+
588
+ store32(&value, context->adlen);
589
+ blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
590
+
591
+ if (context->ad != NULL) {
592
+ blake2b_update(&BlakeHash, (const uint8_t *)context->ad,
593
+ context->adlen);
594
+ }
595
+
596
+ blake2b_final(&BlakeHash, blockhash, ARGON2_PREHASH_DIGEST_LENGTH);
597
+ }
598
+
599
+ int initialize(argon2_instance_t *instance, argon2_context *context) {
600
+ uint8_t blockhash[ARGON2_PREHASH_SEED_LENGTH];
601
+ int result = ARGON2_OK;
602
+
603
+ if (instance == NULL || context == NULL)
604
+ return ARGON2_INCORRECT_PARAMETER;
605
+ instance->context_ptr = context;
606
+
607
+ /* 1. Memory allocation */
608
+ result = allocate_memory(context, (uint8_t **)&(instance->memory),
609
+ instance->memory_blocks, sizeof(block));
610
+ if (result != ARGON2_OK) {
611
+ return result;
612
+ }
613
+
614
+ /* 2. Initial hashing */
615
+ /* H_0 + 8 extra bytes to produce the first blocks */
616
+ /* uint8_t blockhash[ARGON2_PREHASH_SEED_LENGTH]; */
617
+ /* Hashing all inputs */
618
+ initial_hash(blockhash, context, instance->type);
619
+ /* Zeroing 8 extra bytes */
620
+ clear_internal_memory(blockhash + ARGON2_PREHASH_DIGEST_LENGTH,
621
+ ARGON2_PREHASH_SEED_LENGTH -
622
+ ARGON2_PREHASH_DIGEST_LENGTH);
623
+
624
+ #ifdef GENKAT
625
+ initial_kat(blockhash, context, instance->type);
626
+ #endif
627
+
628
+ /* 3. Creating first blocks, we always have at least two blocks in a slice
629
+ */
630
+ fill_first_blocks(blockhash, instance);
631
+ /* Clearing the hash */
632
+ clear_internal_memory(blockhash, ARGON2_PREHASH_SEED_LENGTH);
633
+
634
+ return ARGON2_OK;
635
+ }