argon2 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +4 -0
  3. data/.travis.yml +2 -0
  4. data/README.md +25 -9
  5. data/argon2.gemspec +10 -2
  6. data/bin/console +1 -1
  7. data/bin/setup +3 -0
  8. data/ext/argon2_wrap/Makefile +72 -0
  9. data/ext/argon2_wrap/argon_wrap.c +65 -0
  10. data/ext/argon2_wrap/extconf.rb +1 -0
  11. data/ext/argon2_wrap/test.c +67 -0
  12. data/ext/phc-winner-argon2/.gitignore +7 -0
  13. data/ext/phc-winner-argon2/LICENSE +31 -0
  14. data/ext/phc-winner-argon2/Makefile +102 -0
  15. data/ext/phc-winner-argon2/README.md +193 -0
  16. data/ext/phc-winner-argon2/argon2-specs.pdf +0 -0
  17. data/ext/phc-winner-argon2/kats/argon2d +12302 -0
  18. data/ext/phc-winner-argon2/kats/argon2d.shasum +1 -0
  19. data/ext/phc-winner-argon2/kats/argon2i +12302 -0
  20. data/ext/phc-winner-argon2/kats/argon2i.shasum +1 -0
  21. data/ext/phc-winner-argon2/kats/check-sums.sh +13 -0
  22. data/ext/phc-winner-argon2/kats/test.sh +47 -0
  23. data/ext/phc-winner-argon2/src/argon2.c +360 -0
  24. data/ext/phc-winner-argon2/src/argon2.h +298 -0
  25. data/ext/phc-winner-argon2/src/bench.c +111 -0
  26. data/ext/phc-winner-argon2/src/blake2/blake2-impl.h +143 -0
  27. data/ext/phc-winner-argon2/src/blake2/blake2.h +74 -0
  28. data/ext/phc-winner-argon2/src/blake2/blake2b.c +372 -0
  29. data/ext/phc-winner-argon2/src/blake2/blamka-round-opt.h +162 -0
  30. data/ext/phc-winner-argon2/src/blake2/blamka-round-ref.h +39 -0
  31. data/ext/phc-winner-argon2/src/core.c +662 -0
  32. data/ext/phc-winner-argon2/src/core.h +226 -0
  33. data/ext/phc-winner-argon2/src/genkat.c +194 -0
  34. data/ext/phc-winner-argon2/src/genkat.h +45 -0
  35. data/ext/phc-winner-argon2/src/opt.c +173 -0
  36. data/ext/phc-winner-argon2/src/opt.h +49 -0
  37. data/ext/phc-winner-argon2/src/ref.c +175 -0
  38. data/ext/phc-winner-argon2/src/ref.h +49 -0
  39. data/ext/phc-winner-argon2/src/run.c +223 -0
  40. data/ext/phc-winner-argon2/src/thread.c +36 -0
  41. data/ext/phc-winner-argon2/src/thread.h +46 -0
  42. data/lib/argon2.rb +15 -32
  43. data/lib/argon2/constants.rb +6 -0
  44. data/lib/argon2/engine.rb +10 -0
  45. data/lib/argon2/errors.rb +36 -0
  46. data/lib/argon2/ffi_engine.rb +47 -0
  47. data/lib/argon2/version.rb +1 -1
  48. metadata +75 -11
@@ -0,0 +1,111 @@
1
+ #include <stdio.h>
2
+ #include <stdint.h>
3
+ #include <stdlib.h>
4
+ #include <string.h>
5
+ #include <time.h>
6
+ #ifdef _MSC_VER
7
+ #include <intrin.h>
8
+ #endif
9
+
10
+ #include "argon2.h"
11
+
12
+ static uint64_t rdtsc(void) {
13
+ #ifdef _MSC_VER
14
+ return __rdtsc();
15
+ #else
16
+ #if defined(__amd64__) || defined(__x86_64__)
17
+ uint64_t rax, rdx;
18
+ __asm__ __volatile__("rdtsc" : "=a"(rax), "=d"(rdx) : :);
19
+ return (rdx << 32) | rax;
20
+ #elif defined(__i386__) || defined(__i386) || defined(__X86__)
21
+ uint64_t rax;
22
+ __asm__ __volatile__("rdtsc" : "=A"(rax) : :);
23
+ return rax;
24
+ #else
25
+ #error "Not implemented!"
26
+ #endif
27
+ #endif
28
+ }
29
+
30
+ /*
31
+ * Benchmarks Argon2 with salt length 16, password length 16, t_cost 1,
32
+ and different m_cost and threads
33
+ */
34
+ static void benchmark() {
35
+ #define BENCH_OUTLEN 16
36
+ #define BENCH_INLEN 16
37
+ const uint32_t inlen = BENCH_INLEN;
38
+ const unsigned outlen = BENCH_OUTLEN;
39
+ unsigned char out[BENCH_OUTLEN];
40
+ unsigned char pwd_array[BENCH_INLEN];
41
+ unsigned char salt_array[BENCH_INLEN];
42
+ #undef BENCH_INLEN
43
+ #undef BENCH_OUTLEN
44
+
45
+ uint32_t t_cost = 1;
46
+ uint32_t m_cost;
47
+ uint32_t thread_test[6] = {1, 2, 4, 6, 8, 16};
48
+
49
+ memset(pwd_array, 0, inlen);
50
+ memset(salt_array, 1, inlen);
51
+
52
+ for (m_cost = (uint32_t)1 << 10; m_cost <= (uint32_t)1 << 22; m_cost *= 2) {
53
+ unsigned i;
54
+ for (i = 0; i < 6; ++i) {
55
+ argon2_context context;
56
+ uint32_t thread_n = thread_test[i];
57
+ uint64_t stop_cycles, stop_cycles_i;
58
+ clock_t stop_time;
59
+ uint64_t delta_d, delta_i;
60
+ double mcycles_d, mcycles_i, run_time;
61
+
62
+ clock_t start_time = clock();
63
+ uint64_t start_cycles = rdtsc();
64
+
65
+ context.out = out;
66
+ context.outlen = outlen;
67
+ context.pwd = pwd_array;
68
+ context.pwdlen = inlen;
69
+ context.salt = salt_array;
70
+ context.saltlen = inlen;
71
+ context.secret = NULL;
72
+ context.secretlen = 0;
73
+ context.ad = NULL;
74
+ context.adlen = 0;
75
+ context.t_cost = t_cost;
76
+ context.m_cost = m_cost;
77
+ context.lanes = thread_n;
78
+ context.threads = thread_n;
79
+ context.allocate_cbk = NULL;
80
+ context.free_cbk = NULL;
81
+ context.flags = 0;
82
+
83
+ argon2d(&context);
84
+ stop_cycles = rdtsc();
85
+ argon2i(&context);
86
+ stop_cycles_i = rdtsc();
87
+ stop_time = clock();
88
+
89
+ delta_d = (stop_cycles - start_cycles) / (m_cost);
90
+ delta_i = (stop_cycles_i - stop_cycles) / (m_cost);
91
+ mcycles_d = (double)(stop_cycles - start_cycles) / (1UL << 20);
92
+ mcycles_i = (double)(stop_cycles_i - stop_cycles) / (1UL << 20);
93
+ printf("Argon2d %d iterations %d MiB %d threads: %2.2f cpb %2.2f "
94
+ "Mcycles \n",
95
+ t_cost, m_cost >> 10, thread_n, (float)delta_d / 1024,
96
+ mcycles_d);
97
+ printf("Argon2i %d iterations %d MiB %d threads: %2.2f cpb %2.2f "
98
+ "Mcycles \n",
99
+ t_cost, m_cost >> 10, thread_n, (float)delta_i / 1024,
100
+ mcycles_i);
101
+
102
+ run_time = ((double)stop_time - start_time) / (CLOCKS_PER_SEC);
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,143 @@
1
+ #ifndef PORTABLE_BLAKE2_IMPL_H
2
+ #define PORTABLE_BLAKE2_IMPL_H
3
+
4
+ #include <stdint.h>
5
+ #include <string.h>
6
+
7
+ #if defined(_MSC_VER)
8
+ #define BLAKE2_INLINE __inline
9
+ #elif defined(__GNUC__) || defined(__clang__)
10
+ #define BLAKE2_INLINE __inline__
11
+ #else
12
+ #define BLAKE2_INLINE
13
+ #endif
14
+
15
+ /* Argon2 Team - Begin Code */
16
+ /*
17
+ Not an exhaustive list, but should cover the majority of modern platforms
18
+ Additionally, the code will always be correct---this is only a performance
19
+ tweak.
20
+ */
21
+ #if (defined(__BYTE_ORDER__) && \
22
+ (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) || \
23
+ defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || defined(__MIPSEL__) || \
24
+ defined(__AARCH64EL__) || defined(__amd64__) || defined(__i386__) || \
25
+ defined(_M_IX86) || defined(_M_X64) || defined(_M_AMD64) || \
26
+ defined(_M_ARM)
27
+ #define NATIVE_LITTLE_ENDIAN
28
+ #endif
29
+ /* Argon2 Team - End Code */
30
+
31
+ static BLAKE2_INLINE uint32_t load32(const void *src) {
32
+ #if defined(NATIVE_LITTLE_ENDIAN)
33
+ uint32_t w;
34
+ memcpy(&w, src, sizeof w);
35
+ return w;
36
+ #else
37
+ const uint8_t *p = (const uint8_t *)src;
38
+ uint32_t w = *p++;
39
+ w |= (uint32_t)(*p++) << 8;
40
+ w |= (uint32_t)(*p++) << 16;
41
+ w |= (uint32_t)(*p++) << 24;
42
+ return w;
43
+ #endif
44
+ }
45
+
46
+ static BLAKE2_INLINE uint64_t load64(const void *src) {
47
+ #if defined(NATIVE_LITTLE_ENDIAN)
48
+ uint64_t w;
49
+ memcpy(&w, src, sizeof w);
50
+ return w;
51
+ #else
52
+ const uint8_t *p = (const uint8_t *)src;
53
+ uint64_t w = *p++;
54
+ w |= (uint64_t)(*p++) << 8;
55
+ w |= (uint64_t)(*p++) << 16;
56
+ w |= (uint64_t)(*p++) << 24;
57
+ w |= (uint64_t)(*p++) << 32;
58
+ w |= (uint64_t)(*p++) << 40;
59
+ w |= (uint64_t)(*p++) << 48;
60
+ w |= (uint64_t)(*p++) << 56;
61
+ return w;
62
+ #endif
63
+ }
64
+
65
+ static BLAKE2_INLINE void store32(void *dst, uint32_t w) {
66
+ #if defined(NATIVE_LITTLE_ENDIAN)
67
+ memcpy(dst, &w, sizeof w);
68
+ #else
69
+ uint8_t *p = (uint8_t *)dst;
70
+ *p++ = (uint8_t)w;
71
+ w >>= 8;
72
+ *p++ = (uint8_t)w;
73
+ w >>= 8;
74
+ *p++ = (uint8_t)w;
75
+ w >>= 8;
76
+ *p++ = (uint8_t)w;
77
+ #endif
78
+ }
79
+
80
+ static BLAKE2_INLINE void store64(void *dst, uint64_t w) {
81
+ #if defined(NATIVE_LITTLE_ENDIAN)
82
+ memcpy(dst, &w, sizeof w);
83
+ #else
84
+ uint8_t *p = (uint8_t *)dst;
85
+ *p++ = (uint8_t)w;
86
+ w >>= 8;
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
+ w >>= 8;
95
+ *p++ = (uint8_t)w;
96
+ w >>= 8;
97
+ *p++ = (uint8_t)w;
98
+ w >>= 8;
99
+ *p++ = (uint8_t)w;
100
+ #endif
101
+ }
102
+
103
+ static BLAKE2_INLINE uint64_t load48(const void *src) {
104
+ const uint8_t *p = (const uint8_t *)src;
105
+ uint64_t w = *p++;
106
+ w |= (uint64_t)(*p++) << 8;
107
+ w |= (uint64_t)(*p++) << 16;
108
+ w |= (uint64_t)(*p++) << 24;
109
+ w |= (uint64_t)(*p++) << 32;
110
+ w |= (uint64_t)(*p++) << 40;
111
+ return w;
112
+ }
113
+
114
+ static BLAKE2_INLINE void store48(void *dst, uint64_t w) {
115
+ uint8_t *p = (uint8_t *)dst;
116
+ *p++ = (uint8_t)w;
117
+ w >>= 8;
118
+ *p++ = (uint8_t)w;
119
+ w >>= 8;
120
+ *p++ = (uint8_t)w;
121
+ w >>= 8;
122
+ *p++ = (uint8_t)w;
123
+ w >>= 8;
124
+ *p++ = (uint8_t)w;
125
+ w >>= 8;
126
+ *p++ = (uint8_t)w;
127
+ }
128
+
129
+ static BLAKE2_INLINE uint32_t rotr32(const uint32_t w, const unsigned c) {
130
+ return (w >> c) | (w << (32 - c));
131
+ }
132
+
133
+ static BLAKE2_INLINE uint64_t rotr64(const uint64_t w, const unsigned c) {
134
+ return (w >> c) | (w << (64 - c));
135
+ }
136
+
137
+ /* prevents compiler optimizing out memset() */
138
+ static BLAKE2_INLINE void burn(void *v, size_t n) {
139
+ static void *(*const volatile memset_v)(void *, int, size_t) = &memset;
140
+ memset_v(v, 0, n);
141
+ }
142
+
143
+ #endif
@@ -0,0 +1,74 @@
1
+ #ifndef PORTABLE_BLAKE2_H
2
+ #define PORTABLE_BLAKE2_H
3
+
4
+ #include <stddef.h>
5
+ #include <stdint.h>
6
+ #include <limits.h>
7
+
8
+ #if defined(__cplusplus)
9
+ extern "C" {
10
+ #endif
11
+
12
+ enum blake2b_constant {
13
+ BLAKE2B_BLOCKBYTES = 128,
14
+ BLAKE2B_OUTBYTES = 64,
15
+ BLAKE2B_KEYBYTES = 64,
16
+ BLAKE2B_SALTBYTES = 16,
17
+ BLAKE2B_PERSONALBYTES = 16
18
+ };
19
+
20
+ #pragma pack(push, 1)
21
+ typedef struct __blake2b_param {
22
+ uint8_t digest_length; /* 1 */
23
+ uint8_t key_length; /* 2 */
24
+ uint8_t fanout; /* 3 */
25
+ uint8_t depth; /* 4 */
26
+ uint32_t leaf_length; /* 8 */
27
+ uint64_t node_offset; /* 16 */
28
+ uint8_t node_depth; /* 17 */
29
+ uint8_t inner_length; /* 18 */
30
+ uint8_t reserved[14]; /* 32 */
31
+ uint8_t salt[BLAKE2B_SALTBYTES]; /* 48 */
32
+ uint8_t personal[BLAKE2B_PERSONALBYTES]; /* 64 */
33
+ } blake2b_param;
34
+ #pragma pack(pop)
35
+
36
+ typedef struct __blake2b_state {
37
+ uint64_t h[8];
38
+ uint64_t t[2];
39
+ uint64_t f[2];
40
+ uint8_t buf[BLAKE2B_BLOCKBYTES];
41
+ unsigned buflen;
42
+ unsigned outlen;
43
+ uint8_t last_node;
44
+ } blake2b_state;
45
+
46
+ /* Ensure param structs have not been wrongly padded */
47
+ /* Poor man's static_assert */
48
+ enum {
49
+ blake2_size_check_0 = 1 / !!(CHAR_BIT == 8),
50
+ blake2_size_check_2 =
51
+ 1 / !!(sizeof(blake2b_param) == sizeof(uint64_t) * CHAR_BIT)
52
+ };
53
+
54
+ /* Streaming API */
55
+ int blake2b_init(blake2b_state *S, size_t outlen);
56
+ int blake2b_init_key(blake2b_state *S, size_t outlen, const void *key,
57
+ size_t keylen);
58
+ int blake2b_init_param(blake2b_state *S, const blake2b_param *P);
59
+ int blake2b_update(blake2b_state *S, const void *in, size_t inlen);
60
+ int blake2b_final(blake2b_state *S, void *out, size_t outlen);
61
+
62
+ /* Simple API */
63
+ int blake2b(void *out, size_t outlen, const void *in, size_t inlen,
64
+ const void *key, size_t keylen);
65
+
66
+ /* Argon2 Team - Begin Code */
67
+ int blake2b_long(void *out, size_t outlen, const void *in, size_t inlen);
68
+ /* Argon2 Team - End Code */
69
+
70
+ #if defined(__cplusplus)
71
+ }
72
+ #endif
73
+
74
+ #endif
@@ -0,0 +1,372 @@
1
+ #include <stdint.h>
2
+ #include <string.h>
3
+ #include <stdio.h>
4
+
5
+ #include "blake2.h"
6
+ #include "blake2-impl.h"
7
+
8
+ static const uint64_t blake2b_IV[8] = {
9
+ UINT64_C(0x6a09e667f3bcc908), UINT64_C(0xbb67ae8584caa73b),
10
+ UINT64_C(0x3c6ef372fe94f82b), UINT64_C(0xa54ff53a5f1d36f1),
11
+ UINT64_C(0x510e527fade682d1), UINT64_C(0x9b05688c2b3e6c1f),
12
+ UINT64_C(0x1f83d9abfb41bd6b), UINT64_C(0x5be0cd19137e2179)};
13
+
14
+ static const unsigned int blake2b_sigma[12][16] = {
15
+ {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
16
+ {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3},
17
+ {11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4},
18
+ {7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8},
19
+ {9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13},
20
+ {2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9},
21
+ {12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11},
22
+ {13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10},
23
+ {6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5},
24
+ {10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0},
25
+ {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
26
+ {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3},
27
+ };
28
+
29
+ static BLAKE2_INLINE void blake2b_set_lastnode(blake2b_state *S) {
30
+ S->f[1] = (uint64_t)-1;
31
+ }
32
+
33
+ static BLAKE2_INLINE void blake2b_set_lastblock(blake2b_state *S) {
34
+ if (S->last_node) {
35
+ blake2b_set_lastnode(S);
36
+ }
37
+ S->f[0] = (uint64_t)-1;
38
+ }
39
+
40
+ static BLAKE2_INLINE void blake2b_increment_counter(blake2b_state *S,
41
+ uint64_t inc) {
42
+ S->t[0] += inc;
43
+ S->t[1] += (S->t[0] < inc);
44
+ }
45
+
46
+ static BLAKE2_INLINE void blake2b_invalidate_state(blake2b_state *S) {
47
+ burn(S, sizeof(*S)); /* wipe */
48
+ blake2b_set_lastblock(S); /* invalidate for further use */
49
+ }
50
+
51
+ static BLAKE2_INLINE void blake2b_init0(blake2b_state *S) {
52
+ memset(S, 0, sizeof(*S));
53
+ memcpy(S->h, blake2b_IV, sizeof(S->h));
54
+ }
55
+
56
+ int blake2b_init_param(blake2b_state *S, const blake2b_param *P) {
57
+ const unsigned char *p = (const unsigned char *)P;
58
+ unsigned int i;
59
+
60
+ if (NULL == P || NULL == S) {
61
+ return -1;
62
+ }
63
+
64
+ blake2b_init0(S);
65
+ /* IV XOR Parameter Block */
66
+ for (i = 0; i < 8; ++i) {
67
+ S->h[i] ^= load64(&p[i * sizeof(S->h[i])]);
68
+ }
69
+ S->outlen = P->digest_length;
70
+ return 0;
71
+ }
72
+
73
+ /* Sequential blake2b initialization */
74
+ int blake2b_init(blake2b_state *S, size_t outlen) {
75
+ blake2b_param P;
76
+
77
+ if (S == NULL) {
78
+ return -1;
79
+ }
80
+
81
+ if ((outlen == 0) || (outlen > BLAKE2B_OUTBYTES)) {
82
+ blake2b_invalidate_state(S);
83
+ return -1;
84
+ }
85
+
86
+ /* Setup Parameter Block for unkeyed BLAKE2 */
87
+ P.digest_length = (uint8_t)outlen;
88
+ P.key_length = 0;
89
+ P.fanout = 1;
90
+ P.depth = 1;
91
+ P.leaf_length = 0;
92
+ P.node_offset = 0;
93
+ P.node_depth = 0;
94
+ P.inner_length = 0;
95
+ memset(P.reserved, 0, sizeof(P.reserved));
96
+ memset(P.salt, 0, sizeof(P.salt));
97
+ memset(P.personal, 0, sizeof(P.personal));
98
+
99
+ return blake2b_init_param(S, &P);
100
+ }
101
+
102
+ int blake2b_init_key(blake2b_state *S, size_t outlen, const void *key,
103
+ size_t keylen) {
104
+ blake2b_param P;
105
+
106
+ if (S == NULL) {
107
+ return -1;
108
+ }
109
+
110
+ if ((outlen == 0) || (outlen > BLAKE2B_OUTBYTES)) {
111
+ blake2b_invalidate_state(S);
112
+ return -1;
113
+ }
114
+
115
+ if ((key == 0) || (keylen == 0) || (keylen > BLAKE2B_KEYBYTES)) {
116
+ blake2b_invalidate_state(S);
117
+ return -1;
118
+ }
119
+
120
+ /* Setup Parameter Block for keyed BLAKE2 */
121
+ P.digest_length = (uint8_t)outlen;
122
+ P.key_length = (uint8_t)keylen;
123
+ P.fanout = 1;
124
+ P.depth = 1;
125
+ P.leaf_length = 0;
126
+ P.node_offset = 0;
127
+ P.node_depth = 0;
128
+ P.inner_length = 0;
129
+ memset(P.reserved, 0, sizeof(P.reserved));
130
+ memset(P.salt, 0, sizeof(P.salt));
131
+ memset(P.personal, 0, sizeof(P.personal));
132
+
133
+ if (blake2b_init_param(S, &P) < 0) {
134
+ blake2b_invalidate_state(S);
135
+ return -1;
136
+ }
137
+
138
+ {
139
+ uint8_t block[BLAKE2B_BLOCKBYTES];
140
+ memset(block, 0, BLAKE2B_BLOCKBYTES);
141
+ memcpy(block, key, keylen);
142
+ blake2b_update(S, block, BLAKE2B_BLOCKBYTES);
143
+ burn(block, BLAKE2B_BLOCKBYTES); /* Burn the key from stack */
144
+ }
145
+ return 0;
146
+ }
147
+
148
+ static void blake2b_compress(blake2b_state *S, const uint8_t *block) {
149
+ uint64_t m[16];
150
+ uint64_t v[16];
151
+ unsigned int i, r;
152
+
153
+ for (i = 0; i < 16; ++i) {
154
+ m[i] = load64(block + i * sizeof(m[i]));
155
+ }
156
+
157
+ for (i = 0; i < 8; ++i) {
158
+ v[i] = S->h[i];
159
+ }
160
+
161
+ v[8] = blake2b_IV[0];
162
+ v[9] = blake2b_IV[1];
163
+ v[10] = blake2b_IV[2];
164
+ v[11] = blake2b_IV[3];
165
+ v[12] = blake2b_IV[4] ^ S->t[0];
166
+ v[13] = blake2b_IV[5] ^ S->t[1];
167
+ v[14] = blake2b_IV[6] ^ S->f[0];
168
+ v[15] = blake2b_IV[7] ^ S->f[1];
169
+
170
+ #define G(r, i, a, b, c, d) \
171
+ do { \
172
+ a = a + b + m[blake2b_sigma[r][2 * i + 0]]; \
173
+ d = rotr64(d ^ a, 32); \
174
+ c = c + d; \
175
+ b = rotr64(b ^ c, 24); \
176
+ a = a + b + m[blake2b_sigma[r][2 * i + 1]]; \
177
+ d = rotr64(d ^ a, 16); \
178
+ c = c + d; \
179
+ b = rotr64(b ^ c, 63); \
180
+ } while ((void)0, 0)
181
+
182
+ #define ROUND(r) \
183
+ do { \
184
+ G(r, 0, v[0], v[4], v[8], v[12]); \
185
+ G(r, 1, v[1], v[5], v[9], v[13]); \
186
+ G(r, 2, v[2], v[6], v[10], v[14]); \
187
+ G(r, 3, v[3], v[7], v[11], v[15]); \
188
+ G(r, 4, v[0], v[5], v[10], v[15]); \
189
+ G(r, 5, v[1], v[6], v[11], v[12]); \
190
+ G(r, 6, v[2], v[7], v[8], v[13]); \
191
+ G(r, 7, v[3], v[4], v[9], v[14]); \
192
+ } while ((void)0, 0)
193
+
194
+ for (r = 0; r < 12; ++r) {
195
+ ROUND(r);
196
+ }
197
+
198
+ for (i = 0; i < 8; ++i) {
199
+ S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
200
+ }
201
+
202
+ #undef G
203
+ #undef ROUND
204
+ }
205
+
206
+ int blake2b_update(blake2b_state *S, const void *in, size_t inlen) {
207
+ const uint8_t *pin = (const uint8_t *)in;
208
+
209
+ if (inlen == 0) {
210
+ return 0;
211
+ }
212
+
213
+ /* Sanity check */
214
+ if (S == NULL || in == NULL) {
215
+ return -1;
216
+ }
217
+
218
+ /* Is this a reused state? */
219
+ if (S->f[0] != 0) {
220
+ return -1;
221
+ }
222
+
223
+ if (S->buflen + inlen > BLAKE2B_BLOCKBYTES) {
224
+ /* Complete current block */
225
+ size_t left = S->buflen;
226
+ size_t fill = BLAKE2B_BLOCKBYTES - left;
227
+ memcpy(&S->buf[left], pin, fill);
228
+ blake2b_increment_counter(S, BLAKE2B_BLOCKBYTES);
229
+ blake2b_compress(S, S->buf);
230
+ S->buflen = 0;
231
+ inlen -= fill;
232
+ pin += fill;
233
+ /* Avoid buffer copies when possible */
234
+ while (inlen > BLAKE2B_BLOCKBYTES) {
235
+ blake2b_increment_counter(S, BLAKE2B_BLOCKBYTES);
236
+ blake2b_compress(S, pin);
237
+ inlen -= BLAKE2B_BLOCKBYTES;
238
+ pin += BLAKE2B_BLOCKBYTES;
239
+ }
240
+ }
241
+ memcpy(&S->buf[S->buflen], pin, inlen);
242
+ S->buflen += (unsigned int)inlen;
243
+ return 0;
244
+ }
245
+
246
+ int blake2b_final(blake2b_state *S, void *out, size_t outlen) {
247
+ uint8_t buffer[BLAKE2B_OUTBYTES] = {0};
248
+ unsigned int i;
249
+
250
+ /* Sanity checks */
251
+ if (S == NULL || out == NULL || outlen < S->outlen) {
252
+ return -1;
253
+ }
254
+
255
+ /* Is this a reused state? */
256
+ if (S->f[0] != 0) {
257
+ return -1;
258
+ }
259
+
260
+ blake2b_increment_counter(S, S->buflen);
261
+ blake2b_set_lastblock(S);
262
+ memset(&S->buf[S->buflen], 0, BLAKE2B_BLOCKBYTES - S->buflen); /* Padding */
263
+ blake2b_compress(S, S->buf);
264
+
265
+ for (i = 0; i < 8; ++i) { /* Output full hash to temp buffer */
266
+ store64(buffer + sizeof(S->h[i]) * i, S->h[i]);
267
+ }
268
+
269
+ memcpy(out, buffer, S->outlen);
270
+ burn(buffer, sizeof(buffer));
271
+ burn(S->buf, sizeof(S->buf));
272
+ burn(S->h, sizeof(S->h));
273
+ return 0;
274
+ }
275
+
276
+ int blake2b(void *out, size_t outlen, const void *in, size_t inlen,
277
+ const void *key, size_t keylen) {
278
+ blake2b_state S;
279
+ int ret = -1;
280
+
281
+ /* Verify parameters */
282
+ if (NULL == in && inlen > 0) {
283
+ goto fail;
284
+ }
285
+
286
+ if (NULL == out || outlen == 0 || outlen > BLAKE2B_OUTBYTES) {
287
+ goto fail;
288
+ }
289
+
290
+ if ((NULL == key && keylen > 0) || keylen > BLAKE2B_KEYBYTES) {
291
+ goto fail;
292
+ }
293
+
294
+ if (keylen > 0) {
295
+ if (blake2b_init_key(&S, outlen, key, keylen) < 0) {
296
+ goto fail;
297
+ }
298
+ } else {
299
+ if (blake2b_init(&S, outlen) < 0) {
300
+ goto fail;
301
+ }
302
+ }
303
+
304
+ if (blake2b_update(&S, in, inlen) < 0) {
305
+ goto fail;
306
+ }
307
+ ret = blake2b_final(&S, out, outlen);
308
+
309
+ fail:
310
+ burn(&S, sizeof(S));
311
+ return ret;
312
+ }
313
+
314
+ /* Argon2 Team - Begin Code */
315
+ int blake2b_long(void *pout, size_t outlen, const void *in, size_t inlen) {
316
+ uint8_t *out = (uint8_t *)pout;
317
+ blake2b_state blake_state;
318
+ uint8_t outlen_bytes[sizeof(uint32_t)] = {0};
319
+ int ret = -1;
320
+
321
+ if (outlen > UINT32_MAX) {
322
+ goto fail;
323
+ }
324
+
325
+ /* Ensure little-endian byte order! */
326
+ store32(outlen_bytes, (uint32_t)outlen);
327
+
328
+ #define TRY(statement) \
329
+ do { \
330
+ ret = statement; \
331
+ if (ret < 0) { \
332
+ goto fail; \
333
+ } \
334
+ } while ((void)0, 0)
335
+
336
+ if (outlen <= BLAKE2B_OUTBYTES) {
337
+ TRY(blake2b_init(&blake_state, outlen));
338
+ TRY(blake2b_update(&blake_state, outlen_bytes, sizeof(outlen_bytes)));
339
+ TRY(blake2b_update(&blake_state, in, inlen));
340
+ TRY(blake2b_final(&blake_state, out, outlen));
341
+ } else {
342
+ uint32_t toproduce;
343
+ uint8_t out_buffer[BLAKE2B_OUTBYTES];
344
+ uint8_t in_buffer[BLAKE2B_OUTBYTES];
345
+ TRY(blake2b_init(&blake_state, BLAKE2B_OUTBYTES));
346
+ TRY(blake2b_update(&blake_state, outlen_bytes, sizeof(outlen_bytes)));
347
+ TRY(blake2b_update(&blake_state, in, inlen));
348
+ TRY(blake2b_final(&blake_state, out_buffer, BLAKE2B_OUTBYTES));
349
+ memcpy(out, out_buffer, BLAKE2B_OUTBYTES / 2);
350
+ out += BLAKE2B_OUTBYTES / 2;
351
+ toproduce = (uint32_t)outlen - BLAKE2B_OUTBYTES / 2;
352
+
353
+ while (toproduce > BLAKE2B_OUTBYTES) {
354
+ memcpy(in_buffer, out_buffer, BLAKE2B_OUTBYTES);
355
+ TRY(blake2b(out_buffer, BLAKE2B_OUTBYTES, in_buffer,
356
+ BLAKE2B_OUTBYTES, NULL, 0));
357
+ memcpy(out, out_buffer, BLAKE2B_OUTBYTES / 2);
358
+ out += BLAKE2B_OUTBYTES / 2;
359
+ toproduce -= BLAKE2B_OUTBYTES / 2;
360
+ }
361
+
362
+ memcpy(in_buffer, out_buffer, BLAKE2B_OUTBYTES);
363
+ TRY(blake2b(out_buffer, toproduce, in_buffer, BLAKE2B_OUTBYTES, NULL,
364
+ 0));
365
+ memcpy(out, out_buffer, toproduce);
366
+ }
367
+ fail:
368
+ burn(&blake_state, sizeof(blake_state));
369
+ return ret;
370
+ #undef TRY
371
+ }
372
+ /* Argon2 Team - End Code */