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,111 @@
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
+ #include <stdio.h>
19
+ #include <stdint.h>
20
+ #include <stdlib.h>
21
+ #include <string.h>
22
+ #include <time.h>
23
+ #ifdef _MSC_VER
24
+ #include <intrin.h>
25
+ #endif
26
+
27
+ #include "argon2.h"
28
+
29
+ static uint64_t rdtsc(void) {
30
+ #ifdef _MSC_VER
31
+ return __rdtsc();
32
+ #else
33
+ #if defined(__amd64__) || defined(__x86_64__)
34
+ uint64_t rax, rdx;
35
+ __asm__ __volatile__("rdtsc" : "=a"(rax), "=d"(rdx) : :);
36
+ return (rdx << 32) | rax;
37
+ #elif defined(__i386__) || defined(__i386) || defined(__X86__)
38
+ uint64_t rax;
39
+ __asm__ __volatile__("rdtsc" : "=A"(rax) : :);
40
+ return rax;
41
+ #else
42
+ #error "Not implemented!"
43
+ #endif
44
+ #endif
45
+ }
46
+
47
+ /*
48
+ * Benchmarks Argon2 with salt length 16, password length 16, t_cost 1,
49
+ and different m_cost and threads
50
+ */
51
+ static void benchmark() {
52
+ #define BENCH_OUTLEN 16
53
+ #define BENCH_INLEN 16
54
+ const uint32_t inlen = BENCH_INLEN;
55
+ const unsigned outlen = BENCH_OUTLEN;
56
+ unsigned char out[BENCH_OUTLEN];
57
+ unsigned char pwd_array[BENCH_INLEN];
58
+ unsigned char salt_array[BENCH_INLEN];
59
+ #undef BENCH_INLEN
60
+ #undef BENCH_OUTLEN
61
+
62
+ uint32_t t_cost = 3;
63
+ uint32_t m_cost;
64
+ uint32_t thread_test[4] = {1, 2, 4, 8};
65
+ argon2_type types[3] = {Argon2_i, Argon2_d, Argon2_id};
66
+
67
+ memset(pwd_array, 0, inlen);
68
+ memset(salt_array, 1, inlen);
69
+
70
+ for (m_cost = (uint32_t)1 << 10; m_cost <= (uint32_t)1 << 22; m_cost *= 2) {
71
+ unsigned i;
72
+ for (i = 0; i < 4; ++i) {
73
+ double run_time = 0;
74
+ uint32_t thread_n = thread_test[i];
75
+
76
+ unsigned j;
77
+ for (j = 0; j < 3; ++j) {
78
+ clock_t start_time, stop_time;
79
+ uint64_t start_cycles, stop_cycles;
80
+ uint64_t delta;
81
+ double mcycles;
82
+
83
+ argon2_type type = types[j];
84
+ start_time = clock();
85
+ start_cycles = rdtsc();
86
+
87
+ argon2_hash(t_cost, m_cost, thread_n, pwd_array, inlen,
88
+ salt_array, inlen, out, outlen, NULL, 0, type,
89
+ ARGON2_VERSION_NUMBER);
90
+
91
+ stop_cycles = rdtsc();
92
+ stop_time = clock();
93
+
94
+ delta = (stop_cycles - start_cycles) / (m_cost);
95
+ mcycles = (double)(stop_cycles - start_cycles) / (1UL << 20);
96
+ run_time += ((double)stop_time - start_time) / (CLOCKS_PER_SEC);
97
+
98
+ printf("%s %d iterations %d MiB %d threads: %2.2f cpb %2.2f "
99
+ "Mcycles \n", argon2_type2string(type, 1), t_cost,
100
+ m_cost >> 10, thread_n, (float)delta / 1024, mcycles);
101
+ }
102
+
103
+ printf("%2.4f seconds\n\n", run_time);
104
+ }
105
+ }
106
+ }
107
+
108
+ int main() {
109
+ benchmark();
110
+ return ARGON2_OK;
111
+ }
@@ -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 : 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 PORTABLE_BLAKE2_IMPL_H
19
+ #define PORTABLE_BLAKE2_IMPL_H
20
+
21
+ #include <stdint.h>
22
+ #include <string.h>
23
+
24
+ #if defined(_MSC_VER)
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,91 @@
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 PORTABLE_BLAKE2_H
19
+ #define PORTABLE_BLAKE2_H
20
+
21
+ #include <stddef.h>
22
+ #include <stdint.h>
23
+ #include <limits.h>
24
+
25
+ #if defined(__cplusplus)
26
+ extern "C" {
27
+ #endif
28
+
29
+ enum blake2b_constant {
30
+ BLAKE2B_BLOCKBYTES = 128,
31
+ BLAKE2B_OUTBYTES = 64,
32
+ BLAKE2B_KEYBYTES = 64,
33
+ BLAKE2B_SALTBYTES = 16,
34
+ BLAKE2B_PERSONALBYTES = 16
35
+ };
36
+
37
+ #pragma pack(push, 1)
38
+ typedef struct __blake2b_param {
39
+ uint8_t digest_length; /* 1 */
40
+ uint8_t key_length; /* 2 */
41
+ uint8_t fanout; /* 3 */
42
+ uint8_t depth; /* 4 */
43
+ uint32_t leaf_length; /* 8 */
44
+ uint64_t node_offset; /* 16 */
45
+ uint8_t node_depth; /* 17 */
46
+ uint8_t inner_length; /* 18 */
47
+ uint8_t reserved[14]; /* 32 */
48
+ uint8_t salt[BLAKE2B_SALTBYTES]; /* 48 */
49
+ uint8_t personal[BLAKE2B_PERSONALBYTES]; /* 64 */
50
+ } blake2b_param;
51
+ #pragma pack(pop)
52
+
53
+ typedef struct __blake2b_state {
54
+ uint64_t h[8];
55
+ uint64_t t[2];
56
+ uint64_t f[2];
57
+ uint8_t buf[BLAKE2B_BLOCKBYTES];
58
+ unsigned buflen;
59
+ unsigned outlen;
60
+ uint8_t last_node;
61
+ } blake2b_state;
62
+
63
+ /* Ensure param structs have not been wrongly padded */
64
+ /* Poor man's static_assert */
65
+ enum {
66
+ blake2_size_check_0 = 1 / !!(CHAR_BIT == 8),
67
+ blake2_size_check_2 =
68
+ 1 / !!(sizeof(blake2b_param) == sizeof(uint64_t) * CHAR_BIT)
69
+ };
70
+
71
+ /* Streaming API */
72
+ int blake2b_init(blake2b_state *S, size_t outlen);
73
+ int blake2b_init_key(blake2b_state *S, size_t outlen, const void *key,
74
+ size_t keylen);
75
+ int blake2b_init_param(blake2b_state *S, const blake2b_param *P);
76
+ int blake2b_update(blake2b_state *S, const void *in, size_t inlen);
77
+ int blake2b_final(blake2b_state *S, void *out, size_t outlen);
78
+
79
+ /* Simple API */
80
+ int blake2b(void *out, size_t outlen, const void *in, size_t inlen,
81
+ const void *key, size_t keylen);
82
+
83
+ /* Argon2 Team - Begin Code */
84
+ int blake2b_long(void *out, size_t outlen, const void *in, size_t inlen);
85
+ /* Argon2 Team - End Code */
86
+
87
+ #if defined(__cplusplus)
88
+ }
89
+ #endif
90
+
91
+ #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 : 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
+ #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 */