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


static ssize_t
zero_generator(void *out, size_t n, void *user)
{
	(void) user;
	if (n > (size_t)SSIZE_MAX)
		n = (size_t)SSIZE_MAX;
	memset(out, 0, n);
	return (ssize_t)n;
}


ssize_t
librecrypt_hash_(char *restrict out_buffer, size_t size, const char *phrase, size_t len,
                 const char *settings, void *reserved, enum action action)
{
	const struct algorithm *algo;
	ssize_t (*rng)(void *out, size_t n, void *user) = NULL;
	char *settings_scratch = NULL;
	char *phrase_scratches[2] = {NULL, NULL};
	size_t phrase_scratch_sizes[2] = {0u, 0u};
	size_t n, ascii_len, min, prefix, ret = 0u;
	int has_next, phrase_scratch_i = 0;
	ssize_t r_len;
	int r;
	void *new;

	if (reserved != NULL) {
		errno = EINVAL;
		return -1;
	}

	if (!size)
		rng = &zero_generator;

	if (strchr(settings, '*')) {
		if (action != ASCII_CRYPT) {
			errno = EINVAL;
			return -1;
		}
		r_len = librecrypt_realise_salts(out_buffer, size, settings, rng, NULL);
		if (r_len < 0) {
			if (errno == ERANGE)
				errno = ENOMEM;
			return -1;
		} else if ((size_t)r_len >= size) {
			settings_scratch = malloc((size_t)r_len + 1u);
			if (!settings_scratch)
				return -1;
			if (librecrypt_realise_salts(settings_scratch, (size_t)r_len + 1u, settings, rng, NULL) != r_len)
				abort();
			settings = settings_scratch;
		}
	}

next:
	has_next = 0;
	for (n = 0u; settings[n]; n++) {
		if (settings[n] == LIBRECRYPT_ALGORITHM_LINK_DELIMITER) {
			has_next = 1;
			break;
		}
	}

	algo = librecrypt_find_first_algorithm_(settings, n);
	if (!algo) {
		errno = ENOSYS;
		goto fail;
	}

	prefix = (*algo->get_prefix)(settings, n);
	if (has_next && prefix < n) {
		errno = EINVAL;
		goto fail;
	}

	if (action == ASCII_CRYPT) {
		min = size ? size - 1u < prefix ? size - 1u : prefix : 0u;
		size -= min;
		memcpy(out_buffer, settings, min);
		out_buffer = &out_buffer[min];
		ret += prefix;
	}

	if (size && phrase_scratch_sizes[phrase_scratch_i] < algo->hash_size) {
		librecrypt_wipe(phrase_scratches[phrase_scratch_i], phrase_scratch_sizes[phrase_scratch_i]);
		new = realloc(phrase_scratches[phrase_scratch_i], algo->hash_size);
		if (!new) {
			free(phrase_scratches[phrase_scratch_i]);
			phrase_scratches[phrase_scratch_i] = NULL;
			phrase_scratch_sizes[phrase_scratch_i] = 0u;
			goto fail;
		}
		phrase_scratches[phrase_scratch_i] = new;
		phrase_scratch_sizes[phrase_scratch_i] = algo->hash_size;
	}

	if (has_next) {
	hash_to_scratch:
		r = (*algo->hash)(size ? phrase_scratches[phrase_scratch_i] : NULL,
		                  size ? phrase_scratch_sizes[phrase_scratch_i] : 0u,
		                  phrase, len, settings, prefix, reserved);
	} else if (action == BINARY_HASH) {
	hash_to_output:
		r = (*algo->hash)(out_buffer, size, phrase, len, settings, prefix, reserved);
	} else if (size < algo->hash_size) {
		goto hash_to_scratch;
	} else {
		goto hash_to_output;
	}
	if (r < 0)
		goto fail;

	if (!has_next) {
		if (action == BINARY_HASH) {
			ret += algo->hash_size;
		} else if (!size) {
			ascii_len = algo->hash_size % 3u;
			if (ascii_len) {
				if (algo->pad && algo->strict_pad)
					ascii_len = 4u;
				else
					ascii_len += 1u;
			}
			ascii_len += algo->hash_size / 3u * 4u;
			goto include_ascii;
		} else {
			ascii_len = librecrypt_encode(out_buffer, size,
			                              size < algo->hash_size ? phrase_scratches[phrase_scratch_i] : out_buffer,
			                              algo->hash_size, algo->encoding_lut, algo->strict_pad ? algo->pad : '\0');
	include_ascii:
			min = size ? size - 1u < ascii_len ? size - 1u : ascii_len : 0u;
			out_buffer = &out_buffer[min];
			size -= min;
			ret += ascii_len;
		}
	} else {
		phrase = size ? phrase_scratches[phrase_scratch_i] : NULL;
		phrase_scratch_i ^= 1;
		len = algo->hash_size;

		settings = &settings[n];
		if (action == ASCII_CRYPT) {
			ret += 1u;
			if (size) {
				*out_buffer++ = LIBRECRYPT_ALGORITHM_LINK_DELIMITER;
				size -= 1u;
			}
		}
		settings++;
		goto next;
	}

	librecrypt_wipe(phrase_scratches[0u], phrase_scratch_sizes[0u]);
	librecrypt_wipe(phrase_scratches[1u], phrase_scratch_sizes[1u]);
	librecrypt_wipe_str(settings_scratch);
	free(phrase_scratches[0u]);
	free(phrase_scratches[1u]);
	free(settings_scratch);

	if (size && action != BINARY_HASH)
		out_buffer[0] = '\0';
	return (ssize_t)ret;

fail:
	librecrypt_wipe(phrase_scratches[0u], phrase_scratch_sizes[0u]);
	librecrypt_wipe(phrase_scratches[1u], phrase_scratch_sizes[1u]);
	librecrypt_wipe_str(settings_scratch);
	free(phrase_scratches[0u]);
	free(phrase_scratches[1u]);
	free(settings_scratch);
	return -1;
}


#else


/* Tested via librecrypt_hash_binary, librecrypt_hash, and librecrypt_crypt */
CONST int main(void) { return 0; }


#endif