aboutsummaryrefslogtreecommitdiffstats
path: root/src/libcryptobox/blake2/blake2.c
blob: 594459bf717c41a6dea8f3518b12d918057ea515 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * Copyright (c) 2015, Vsevolod Stakhov
 * Copyright (c) 2015, Andrew Moon
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *	 * Redistributions of source code must retain the above copyright
 *	   notice, this list of conditions and the following disclaimer.
 *	 * Redistributions in binary form must reproduce the above copyright
 *	   notice, this list of conditions and the following disclaimer in the
 *	   documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY AUTHOR ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "cryptobox.h"
#include "platform_config.h"
#include "blake2.h"
#include "blake2-internal.h"

extern unsigned long cpu_config;

typedef struct blake2b_impl_t {
	unsigned long cpu_flags;
	const char *desc;

	void (*blake2b_blocks) (blake2b_state_internal *state,
			const unsigned char *in,
			size_t bytes,
			size_t stride);
} blake2b_impl_t;

#define BLAKE2B_STRIDE BLAKE2B_BLOCKBYTES
#define BLAKE2B_STRIDE_NONE 0

#define BLAKE2B_DECLARE(ext) \
    void blake2b_blocks_##ext(blake2b_state_internal *state, const unsigned char *in, size_t bytes, size_t stride);

#define BLAKE2B_IMPL(cpuflags, desc, ext) \
    {(cpuflags), desc, blake2b_blocks_##ext}

#if defined(HAVE_AVX)
BLAKE2B_DECLARE(avx)
#define BLAKE2B_AVX BLAKE2B_IMPL(CPUID_AVX, "avx", avx)
#endif

#if defined(CMAKE_ARCH_x86_64) || defined(CMAKE_ARCH_i386)
BLAKE2B_DECLARE(x86)
#define BLAKE2B_X86 BLAKE2B_IMPL(0, "x86", x86)
#endif

/* the "always runs" version */
BLAKE2B_DECLARE(ref)
#define BLAKE2B_GENERIC BLAKE2B_IMPL(0, "generic", ref)

/* list implemenations from most optimized to least, with generic as the last entry */
static const blake2b_impl_t blake2b_list[] = {
		BLAKE2B_GENERIC,
#if defined(BLAKE2B_AVX)
		BLAKE2B_AVX,
#endif
#if defined(BLAKE2B_X86)
		BLAKE2B_X86,
#endif
};

static const blake2b_impl_t *blake2b_opt = &blake2b_list[0];


/* is the pointer not aligned on a word boundary? */
static int
blake2b_not_aligned (const void *p)
{
#if !defined(CPU_8BITS)
	return ((size_t) p & (sizeof (size_t) - 1)) != 0;
#else
	return 0;
#endif
}

static const union endian_test_t {
	unsigned char b[2];
	unsigned short s;
} blake2b_endian_test = {{1, 0}};

/* copy the hash from the internal state */
static void
blake2b_store_hash (blake2b_state_internal *state, unsigned char *hash)
{
	if (blake2b_endian_test.s == 0x0001) {
		memcpy (hash, state->h, 64);
	}
	else {
		size_t i, j;
		for (i = 0; i < 8; i++, hash += 8) {
			for (j = 0; j < 8; j++)
				hash[7 - j] = state->h[(i * 8) + j];
		}
	}
}

static const unsigned char blake2b_init_le[64] = {
		0x08 ^ 0x40, 0xc9 ^ 0x00, 0xbc ^ 0x01, 0xf3 ^ 0x01, 0x67 ^ 0x00,
		0xe6 ^ 0x00, 0x09 ^ 0x00, 0x6a ^ 0x00,
		0x3b, 0xa7, 0xca, 0x84, 0x85, 0xae, 0x67, 0xbb,
		0x2b, 0xf8, 0x94, 0xfe, 0x72, 0xf3, 0x6e, 0x3c,
		0xf1, 0x36, 0x1d, 0x5f, 0x3a, 0xf5, 0x4f, 0xa5,
		0xd1, 0x82, 0xe6, 0xad, 0x7f, 0x52, 0x0e, 0x51,
		0x1f, 0x6c, 0x3e, 0x2b, 0x8c, 0x68, 0x05, 0x9b,
		0x6b, 0xbd, 0x41, 0xfb, 0xab, 0xd9, 0x83, 0x1f,
		0x79, 0x21, 0x7e, 0x13, 0x19, 0xcd, 0xe0, 0x5b,
};

/* initialize the state in serial mode */
void
blake2b_init (blake2b_state *S)
{
	blake2b_state_internal *state = (blake2b_state_internal *) S;
	/* assume state is fully little endian for now */
	memcpy (state, blake2b_init_le, 64);
	/*memcpy(state, (blake2b_endian_test.s == 1) ? blake2b_init_le : blake2b_init_be, 64);*/
	memset (state->t,
			0,
			sizeof (state->t) + sizeof (state->f) + sizeof (state->leftover));
}

/* initialized the state in serial-key'd mode */
void
blake2b_keyed_init (blake2b_state *S, const unsigned char *key, size_t keylen)
{
	unsigned char k[BLAKE2B_BLOCKBYTES] = {0};

	g_assert (keylen <= BLAKE2B_KEYBYTES);
	memcpy (k, key, keylen);
	blake2b_init (S);
	blake2b_update (S, k, BLAKE2B_BLOCKBYTES);
}

/* hash inlen bytes from in, which may or may not be word aligned, returns the number of bytes used */
static size_t
blake2b_consume_blocks (blake2b_state_internal *state,
		const unsigned char *in,
		size_t inlen)
{
	/* always need to leave at least BLAKE2B_BLOCKBYTES in case this is the final block */
	if (inlen <= BLAKE2B_BLOCKBYTES)
		return 0;

	inlen = ((inlen - 1) & ~(BLAKE2B_BLOCKBYTES - 1));
	if (blake2b_not_aligned (in)) {
		/* copy the unaligned data to an aligned buffer and process in chunks */
		unsigned char buffer[16 * BLAKE2B_BLOCKBYTES];
		size_t left = inlen;
		while (left) {
			const size_t bytes = (left > sizeof (buffer)) ? sizeof (buffer)
														  : left;
			memcpy (buffer, in, bytes);
			blake2b_opt->blake2b_blocks (state, buffer, bytes, BLAKE2B_STRIDE);
			in += bytes;
			left -= bytes;
		}
	}
	else {
		/* word aligned, handle directly */
		blake2b_opt->blake2b_blocks (state, in, inlen, BLAKE2B_STRIDE);
	}

	return inlen;
}

/* update the hash state with inlen bytes from in */
void
blake2b_update (blake2b_state *S, const unsigned char *in, size_t inlen)
{
	blake2b_state_internal *state = (blake2b_state_internal *) S;
	size_t bytes;

	/* blake2b processes the final <=BLOCKBYTES bytes raw, so we can only update if there are at least BLOCKBYTES+1 bytes available */
	if ((state->leftover + inlen) > BLAKE2B_BLOCKBYTES) {
		/* handle the previous data, we know there is enough for at least one block */
		if (state->leftover) {
			bytes = (BLAKE2B_BLOCKBYTES - state->leftover);
			memcpy (state->buffer + state->leftover, in, bytes);
			in += bytes;
			inlen -= bytes;
			state->leftover = 0;
			blake2b_opt->blake2b_blocks (state,
					state->buffer,
					BLAKE2B_BLOCKBYTES,
					BLAKE2B_STRIDE_NONE);
		}

		/* handle the direct data (if any) */
		bytes = blake2b_consume_blocks (state, in, inlen);
		inlen -= bytes;
		in += bytes;
	}

	/* handle leftover data */
	memcpy (state->buffer + state->leftover, in, inlen);
	state->leftover += inlen;
}

/* finalize the hash */
void
blake2b_final (blake2b_state *S, unsigned char *hash)
{
	blake2b_state_internal *state = (blake2b_state_internal *) S;
	memset (&state->f[0], 0xff, 8);
	blake2b_opt->blake2b_blocks (state,
			state->buffer,
			state->leftover,
			BLAKE2B_STRIDE_NONE);
	blake2b_store_hash (state, hash);
}

/* one-shot hash inlen bytes from in */
void
blake2b (unsigned char *hash, const unsigned char *in, size_t inlen)
{
	blake2b_state S;
	blake2b_state_internal *state = (blake2b_state_internal *) &S;
	size_t bytes;

	blake2b_init (&S);

	/* hash until <= 128 bytes left */
	bytes = blake2b_consume_blocks (state, in, inlen);
	in += bytes;
	inlen -= bytes;

	/* final block */
	memset (&state->f[0], 0xff, 8);
	blake2b_opt->blake2b_blocks (state, in, inlen, BLAKE2B_STRIDE_NONE);
	blake2b_store_hash (state, hash);
}

void
blake2b_keyed (unsigned char *hash,
		const unsigned char *in,
		size_t inlen,
		const unsigned char *key,
		size_t keylen)
{
	blake2b_state S;
	blake2b_keyed_init (&S, key, keylen);
	blake2b_update (&S, in, inlen);
	blake2b_final (&S, hash);
}

void
blake2b_load (void)
{
	guint i;

	if (cpu_config != 0) {
		for (i = 0; i < G_N_ELEMENTS (blake2b_list); i++) {
			if (blake2b_list[i].cpu_flags & cpu_config) {
				blake2b_opt = &blake2b_list[i];
				break;
			}
		}
	}
}