Small code clean-up + improved the Makefile + detect GNU/Hurd operating system.
[slunkcrypt.git] / frontend / src / blake2.c
blobd3cd530644e4ec7f86dd5ac31492062cf836ed18
1 /*
2 This is a stripped-down and simplified "64-Bit only" version of BLAKE2s
3 BLAKE2 reference source code package - reference C implementations
4 Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under the
5 terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at
6 your option. The terms of these licenses can be found at:
7 - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
8 - OpenSSL license : https://www.openssl.org/source/license.html
9 - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
10 More information about the BLAKE2 hash function can be found at
11 https://blake2.net.
14 #include <stdint.h>
15 #include <string.h>
16 #include <stdio.h>
18 #include "blake2.h"
20 /* -------------------------------------------------------------------------- */
21 /* blake2-impl.h */
22 /* -------------------------------------------------------------------------- */
24 #if defined(_MSC_VER)
25 # define BLAKE2_PACKED(X) __pragma(pack(push, 1)) X __pragma(pack(pop))
26 #else
27 # define BLAKE2_PACKED(X) X __attribute__((packed))
28 #endif
30 #if !defined(__cplusplus) && (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L)
31 # if defined(_MSC_VER)
32 # define BLAKE2_INLINE __inline
33 # elif defined(__GNUC__)
34 # define BLAKE2_INLINE __inline__
35 # else
36 # define BLAKE2_INLINE
37 # endif
38 #else
39 # define BLAKE2_INLINE inline
40 #endif
42 static const uint32_t blake2s_IV[8] =
44 0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL, 0xA54FF53AUL,
45 0x510E527FUL, 0x9B05688CUL, 0x1F83D9ABUL, 0x5BE0CD19UL
48 static const uint8_t blake2s_sigma[10][16] =
50 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
51 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
52 { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
53 { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
54 { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
55 { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 },
56 { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 },
57 { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 },
58 { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 },
59 { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 },
62 static BLAKE2_INLINE uint32_t load32(const void *const src)
64 const uint8_t *const p = (const uint8_t*)src;
65 return
66 ((uint32_t)(p[0]) << 0) |
67 ((uint32_t)(p[1]) << 8) |
68 ((uint32_t)(p[2]) << 16) |
69 ((uint32_t)(p[3]) << 24);
72 static BLAKE2_INLINE uint64_t make64(const uint32_t a, const uint32_t b)
74 return
75 ((uint64_t)(a) << 0) |
76 ((uint64_t)(b) << 32);
79 static BLAKE2_INLINE uint32_t rotr32(const uint32_t w, const unsigned c)
81 return (w >> c) | (w << (32 - c));
84 /* -------------------------------------------------------------------------- */
85 /* blake2s-ref.c */
86 /* -------------------------------------------------------------------------- */
88 static void blake2s_set_lastnode(blake2s_t *const S)
90 S->f[1] = (uint32_t)-1;
93 /* Some helper functions, not necessarily useful */
94 static int blake2s_is_lastblock(const blake2s_t *const S)
96 return S->f[0] != 0;
99 static void blake2s_set_lastblock(blake2s_t *const S)
101 if (S->last_node)
102 blake2s_set_lastnode(S);
103 S->f[0] = (uint32_t)-1;
106 static void blake2s_increment_counter(blake2s_t *const S, const uint32_t inc)
108 S->t[0] += inc;
109 S->t[1] += (S->t[0] < inc);
112 /* Sequential blake2s initialization */
113 void blake2s_init(blake2s_t *const S)
115 size_t i;
116 memset(S, 0, sizeof(blake2s_t));
118 for (i = 0; i < 8; ++i)
119 S->h[i] = blake2s_IV[i];
121 /* IV XOR ParamBlock, assuming P.digest_length = sizeof(uint64_t) */
122 S->h[0] ^= 0x01010008UL;
125 #define G(r,i,a,b,c,d) do \
127 a = a + b + m[blake2s_sigma[r][2*i+0]]; \
128 d = rotr32(d ^ a, 16); \
129 c = c + d; \
130 b = rotr32(b ^ c, 12); \
131 a = a + b + m[blake2s_sigma[r][2*i+1]]; \
132 d = rotr32(d ^ a, 8); \
133 c = c + d; \
134 b = rotr32(b ^ c, 7); \
136 while(0)
138 #define ROUND(r) do \
140 G(r, 0, v[0], v[4], v[ 8], v[12]); \
141 G(r, 1, v[1], v[5], v[ 9], v[13]); \
142 G(r, 2, v[2], v[6], v[10], v[14]); \
143 G(r, 3, v[3], v[7], v[11], v[15]); \
144 G(r, 4, v[0], v[5], v[10], v[15]); \
145 G(r, 5, v[1], v[6], v[11], v[12]); \
146 G(r, 6, v[2], v[7], v[ 8], v[13]); \
147 G(r, 7, v[3], v[4], v[ 9], v[14]); \
149 while(0)
151 static void blake2s_compress(blake2s_t *const S, const uint8_t in[BLAKE2S_BLOCKBYTES])
153 uint32_t m[16];
154 uint32_t v[16];
155 size_t i;
157 for (i = 0; i < 16; ++i)
159 m[i] = load32(in + i * sizeof(m[i]));
162 for (i = 0; i < 8; ++i)
164 v[i] = S->h[i];
167 v[ 8] = blake2s_IV[0];
168 v[ 9] = blake2s_IV[1];
169 v[10] = blake2s_IV[2];
170 v[11] = blake2s_IV[3];
171 v[12] = S->t[0] ^ blake2s_IV[4];
172 v[13] = S->t[1] ^ blake2s_IV[5];
173 v[14] = S->f[0] ^ blake2s_IV[6];
174 v[15] = S->f[1] ^ blake2s_IV[7];
176 ROUND(0);
177 ROUND(1);
178 ROUND(2);
179 ROUND(3);
180 ROUND(4);
181 ROUND(5);
182 ROUND(6);
183 ROUND(7);
184 ROUND(8);
185 ROUND(9);
187 for (i = 0; i < 8; ++i)
189 S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
193 #undef G
194 #undef ROUND
196 void blake2s_update(blake2s_t *const S, const void *const pin, size_t inlen)
198 const unsigned char* in = (const unsigned char*)pin;
199 if (inlen > 0)
201 size_t left = S->buflen;
202 size_t fill = BLAKE2S_BLOCKBYTES - left;
203 if (inlen > fill)
205 S->buflen = 0;
206 memcpy(S->buf + left, in, fill); /* Fill buffer */
207 blake2s_increment_counter(S, BLAKE2S_BLOCKBYTES);
208 blake2s_compress(S, S->buf); /* Compress */
209 in += fill; inlen -= fill;
210 while (inlen > BLAKE2S_BLOCKBYTES)
212 blake2s_increment_counter(S, BLAKE2S_BLOCKBYTES);
213 blake2s_compress(S, in);
214 in += BLAKE2S_BLOCKBYTES;
215 inlen -= BLAKE2S_BLOCKBYTES;
218 memcpy(S->buf + S->buflen, in, inlen);
219 S->buflen += inlen;
223 uint64_t blake2s_final(blake2s_t *const S)
225 if (blake2s_is_lastblock(S))
226 return 0U;
228 blake2s_increment_counter(S, (uint32_t)S->buflen);
229 blake2s_set_lastblock(S);
230 memset(S->buf + S->buflen, 0, BLAKE2S_BLOCKBYTES - S->buflen); /* Padding */
231 blake2s_compress(S, S->buf);
233 return make64(S->h[0], S->h[1]);
236 uint64_t blake2s_compute(const void *const pin, const size_t inlen)
238 blake2s_t state;
240 if ((pin != NULL) && (inlen > 0U))
242 blake2s_init(&state);
243 blake2s_update(&state, pin, inlen);
244 return blake2s_final(&state);
247 return 0xCCF2B16074DF93CEull;