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


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 & (this->input_block_size - 1U);
	if (bytes > SIZE_MAX >> 3) {
		for (i = 0; i < 8; i++) {
			libsha2_update(&this->state.sha2.s, m, bytes);
			m = &m[bytes >> 3];
		}
	} else if (bytes) {
		libsha2_update(&this->state.sha2.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;

	if (!this->hash_output)
		this->hash_output = this->state.sha2.sum;

	libsha2_digest(&this->state.sha2.s, data, (bytes << 3) | extra_bits, this->hash_output);
	memset(&this->state.sha2.s, 0, sizeof(this->state.sha2.s));
	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_sha2_hasher(struct libhashsum_hasher *this, unsigned algobits, size_t hashbits)
{
	enum libsha2_algorithm algo;

	if (algobits == 32U && hashbits == 224U) {
		this->algorithm = LIBHASHSUM_SHA_224;
		algo = LIBSHA2_224;
		this->algorithm_string = "SHA-224";
	} else if (algobits == 32U && hashbits == 256U) {
		this->algorithm = LIBHASHSUM_SHA_256;
		algo = LIBSHA2_256;
		this->algorithm_string = "SHA-256";
	} else if (algobits == 64U && hashbits == 384U) {
		this->algorithm = LIBHASHSUM_SHA_384;
		algo = LIBSHA2_384;
		this->algorithm_string = "SHA-384";
	} else if (algobits == 64U && hashbits == 512U) {
		this->algorithm = LIBHASHSUM_SHA_512;
		algo = LIBSHA2_512;
		this->algorithm_string = "SHA-512";
	} else if (algobits == 64U && hashbits == 224U) {
		this->algorithm = LIBHASHSUM_SHA_512_224;
		algo = LIBSHA2_512_224;
		this->algorithm_string = "SHA-512/224";
	} else if (algobits == 64U && hashbits == 256U) {
		this->algorithm = LIBHASHSUM_SHA_512_256;
		algo = LIBSHA2_512_256;
		this->algorithm_string = "SHA-512/256";
	} else {
		errno = EINVAL;
		return -1;
	}

	this->input_block_size = (size_t)algobits << 1;
	this->hash_size = hashbits >> 3;
	this->hash_output = NULL;
	this->supports_non_whole_bytes = 1;
	this->process = &process;
	this->finalise_const = &finalise_const;
	this->finalise = &finalise;
	this->stretch = NULL;
	this->destroy = NULL;
	libsha2_init(&this->state.sha2.s, algo);
	return 0;
}


#else
int
libhashsum_init_sha2_hasher(struct libhashsum_hasher *this, unsigned algobits, size_t hashbits)
{
	(void) this;
	(void) algobits;
	(void) hashbits;
	errno = ENOSYS;
	return -1;
}
#endif