aboutsummaryrefslogtreecommitdiffstats
path: root/libar2simplified_decode_r.c
blob: 3f9dfeaa7b88a39319343264ad3838e0bf8ac6cb (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifdef __linux__
#include <sys/auxv.h>
#include <sys/random.h>
#include <stdint.h>
#endif
#include <time.h>


static size_t
decode_u32(const char *s, uint_least32_t *outp)
{
	uint_least32_t digit;
	size_t i;

	if ((s[0] == '0' && s[1] == '0') || !isdigit(s[0])) {
		errno = EINVAL;
		return 0;
	}

	*outp = 0;
	for (i = 0; isdigit(s[i]); i++) {
		digit = (uint_least32_t)(s[i] & 15);
		if (*outp > ((uint_least32_t)0xFFFFffffUL - digit) / 10) {
			errno = ERANGE;
			return 0;
		}
		*outp = *outp * 10 + digit;
	}

	return i;
}


static int
random_salt(char *out, size_t n, int (*random_byte_generator)(char *out, size_t n, void *user_data), void *user_data)
{
#define ALPHABET "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
	static int srand_called = 0;

	unsigned seed;
	double x;
	size_t i;
	int xi;
#ifdef __linux__
	ssize_t r;
# ifdef AT_RANDOM
	uintptr_t raddr_;
	uint16_t *raddr;
# endif
#endif

	if (random_byte_generator) {
		if (random_byte_generator(out, n, user_data))
			return -1;
	} else {
		i = 0;
#ifdef __linux__
		for (; i < n; i += (size_t)r) {
			r = getrandom(&out[i], n - i, GRND_NONBLOCK);
			if (r < 0)
				break;
		}
#endif
		if (i < n) {
			if (!srand_called) {
				seed = (unsigned)time(NULL) ^ (unsigned)rand();
				seed ^= (unsigned)(uintptr_t)out;
#if defined(__linux__) && defined(AT_RANDOM)
				raddr_ = (uintptr_t)getauxval(AT_RANDOM);
				if (raddr_) {
					raddr = (void *)raddr_;
					seed ^= (unsigned)raddr[0];
					seed ^= (unsigned)raddr[1];
					seed ^= (unsigned)raddr[2];
					seed ^= (unsigned)raddr[3];
					seed ^= (unsigned)raddr[4];
					seed ^= (unsigned)raddr[5];
					seed ^= (unsigned)raddr[6];
					seed ^= (unsigned)raddr[7];
				}
#endif
				srand(seed);
				srand_called = 1;
			}
			do {
				xi = rand();
				x = (double)xi;
				x /= (double)RAND_MAX;
				x *= 63;
				out[i] = (char)x;
			} while (++i < n);
		}
	}

	for (i = 0; i < n; i++)
		out[i] = ALPHABET[out[i] % 64];
	return 0;
}


static void *
allocate(size_t num, size_t size, size_t alignment, struct libar2_context *ctx)
{
	(void) ctx;
	(void) alignment;
	return malloc(num * size);
}


static void
deallocate(void *ptr, struct libar2_context *ctx)
{
	(void) ctx;
	free(ptr);
}


struct libar2_argon2_parameters *
libar2simplified_decode_r(const char *str, char **tagp, char **endp,
                          int (*random_byte_generator)(char *out, size_t n, void *user_data),
                          void *user_data)
{
	struct libar2_argon2_parameters params, *ret;
	const char *p = str;
	const char *end;
	char *str_free = NULL;
	char *buf = NULL;
	size_t n, saltsize, offset;
	uint_least32_t saltlen, hashlen;
	struct libar2_context ctx;

	if (*p != '$')
		goto einval;
	p = strchr(&p[1], '$');
	if (!p)
		goto einval;
	if (p[1] == 'v' && p[2] == '=') {
		p = strchr(&p[1], '$');
		if (!p)
			goto einval;
	}
	p = strchr(&p[1], '$');
	if (!p)
		goto einval;
	p++;
	end = strchr(p, '$');
	if (!end)
		goto einval;

	if (*p == '*') {
		n = decode_u32(&p[1], &saltlen);
		if (!n++)
			goto fail;
		if (&p[n] != end)
			goto einval;
		params.saltlen = (size_t)saltlen;
		saltsize = libar2_encode_base64(NULL, NULL, saltlen) - 1;
		offset = (size_t)(p - str);
		str_free = malloc(offset + saltsize + strlen(&p[n]) + 1);
		if (!str_free)
			goto enomem;
		memcpy(str_free, str, offset);
		if (random_salt(&str_free[offset], saltsize, random_byte_generator, user_data))
			goto fail;
		offset += saltsize;
		stpcpy(&str_free[offset], &p[n]);
		str = str_free;
	}
	end++;

	ctx.allocate = allocate;
	ctx.deallocate = deallocate;

	if (!libar2_decode_params(str, &params, &buf, &ctx))
		goto fail;

	if (*end == '*') {
		n = decode_u32(&end[1], &hashlen);
		if (!n++)
			goto fail;
		end = &end[n];
		params.hashlen = (size_t)hashlen;
		if (tagp)
			*tagp = NULL;
	} else {
		if (tagp)
			*tagp = *(void **)(void *)&end;
		end = &end[libar2_encode_base64(NULL, NULL, params.hashlen) - 1];
	}

	ret = malloc(sizeof(params) + params.saltlen);
	if (!ret)
		goto enomem;
	memcpy(ret, &params, sizeof(params));
	if (buf) {
		memcpy(&((char *)ret)[sizeof(params)], buf, params.saltlen);
		ret->salt = &((unsigned char *)ret)[sizeof(params)];
		deallocate(buf, &ctx);
	}

	if (endp)
		*endp = *(void **)(void *)&end;

	free(str_free);
	return ret;

einval:
	errno = EINVAL;
	return NULL;

fail:
	free(str_free);
	return NULL;

enomem:
	free(str_free);
	errno = ENOMEM;
	return NULL;
}