aboutsummaryrefslogtreecommitdiffstats
path: root/libkeccak_state_initialise.c
blob: 545d3df5064d523aafc7f209affa83756c7f9163 (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include "common.h"


/**
 * Initialise a state according to hashing specifications
 * 
 * @param   state  The state that should be initialised
 * @param   spec   The specifications for the state
 * @return         Zero on success, -1 on error
 */
int
libkeccak_state_initialise(struct libkeccak_state *restrict state, const struct libkeccak_spec *restrict spec)
{
	long int x;

	state->r = spec->bitrate;
	state->n = spec->output;
	state->c = spec->capacity;
	state->b = state->r + state->c;
	state->w = x = state->b / 25;
	state->l = 0;

	if (x & 0xF0L) {
		state->l |= 4;
		x >>= 4;
	}
	if (x & 0x0CL) {
		state->l |= 2;
		x >>= 2;
	}
	if (x & 0x02L) {
		state->l |= 1;
	}

	state->nr = 12 + (state->l << 1);
	state->wmod = (state->w == 64) ? ~0ULL : (uint64_t)((1ULL << state->w) - 1);
	for (x = 0; x < 25; x++)
		state->S.w64[x] = 0;
	state->mptr = 0;
	state->mlen = (size_t)(state->r * state->b) >> 2;

	state->M = malloc(state->mlen * sizeof(char));
	return state->M == NULL ? -1 : 0;
}