aboutsummaryrefslogtreecommitdiffstats
path: root/update.c
blob: 8799061bf7bbf7d26979038f05c13d459d89f69d (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
/* See LICENSE file for copyright and license details. */
#include "common.h"


/**
 * Absorb more of the message
 * 
 * @param  state    The hashing state
 * @param  message  The message, in bits, must be equivalent to 0 modulus 8
 * @param  msglen   The length of the message
 */
void
libsha1_update(struct libsha1_state *restrict state, const void *restrict message_, size_t msglen)
{
	const char *restrict message = message_;
	size_t n, off;

	off = (state->message_size / 8) % sizeof(state->chunk);
	state->message_size += msglen;
	msglen /= 8;

	if (off) {
		n = msglen < sizeof(state->chunk) - off ? msglen : sizeof(state->chunk) - off;
		memcpy(&state->chunk[off], message, n);
		if (off + n == sizeof(state->chunk))
			libsha1_process(state, state->chunk);
		message += n;
		msglen -= n;
	}

	while (msglen >= sizeof(state->chunk)) {
		libsha1_process(state, (const unsigned char *)message);
		message += sizeof(state->chunk);
		msglen -= sizeof(state->chunk);
	}

	if (msglen)
		memcpy(state->chunk, message, msglen);
}