aboutsummaryrefslogtreecommitdiffstats
path: root/libhashsum_init_sha0_hasher.c
blob: 3ed574484ca1ce45ca5a4b491386f877e398659c (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* See LICENSE file for copyright and license details. */
#include "common.h"


LIBHASHSUM_1_NONNULL_
static size_t
process(struct libhashsum_hasher *this, const void *data, size_t bytes)
{
	const uint8_t *m = data;
	int i;
	bytes -= bytes & 63U;
	if (bytes > SIZE_MAX >> 3) {
		for (i = 0; i < 8; i++) {
			libsha1_update(&this->state.sha0.s, m, bytes);
			m = &m[bytes >> 3];
		}
	} else if (bytes) {
		libsha1_update(&this->state.sha0.s, m, bytes << 3);
	}
	return bytes;
}


LIBHASHSUM_1_NONNULL_
static int
finalise_const(struct libhashsum_hasher *this, const void *data, size_t bytes, unsigned extra_bits)
{
	const uint8_t *m = data;
	size_t r;

	if (extra_bits > 7U) {
		errno = EINVAL;
		return -1;
	}

	r = process(this, m, bytes);
	m = &m[r];
	bytes -= r;

	libsha1_digest(&this->state.sha0.s, data, (bytes << 3) | extra_bits, this->state.sha0.sum);
	memset(&this->state.sha0.s, 0, sizeof(this->state.sha0.s));
	this->hash_output = this->state.sha0.sum;
	return 0;
}


LIBHASHSUM_1_NONNULL_
static int
finalise(struct libhashsum_hasher *this, void *data, size_t bytes, unsigned extra_bits, size_t size)
{
	(void) size;
	return finalise_const(this, data, bytes, extra_bits);
}


int
libhashsum_init_sha0_hasher(struct libhashsum_hasher *this)
{
	this->algorithm = LIBHASHSUM_SHA0;
	this->input_block_size = 64U;
	this->hash_size = sizeof(this->state.sha0.sum);
	this->hash_output = NULL;
	this->supports_non_whole_bytes = 0;
	this->process = &process;
	this->finalise_const = &finalise_const;
	this->finalise = &finalise;
	libsha1_init(&this->state.sha0.s, LIBSHA1_0);
	return 0;
}