aboutsummaryrefslogtreecommitdiffstats
path: root/libhashsum_init_hasher_from_string.c
blob: ffd23ebf8eec5b213cf05f37b767b3f1e7b8c51d (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* See LICENSE file for copyright and license details. */
#include "common.h"


static int
parse_value(size_t *valp, const char *p, const char **end_out)
{
	size_t digit;
	if (*valp)
		return -1;
	if (*p++ != '=' || !('1' <= *p && *p <= '9'))
		return -1;
	while (isdigit(*p)) {
		digit = (size_t)(*p++ & 15);
		if (*valp > (SIZE_MAX - digit) / 10U)
			return -1;
		*valp = *valp * 10U + digit;
	}
	*end_out = p;
	return 0;
}


static int
with_n(int (*initfunc)(struct libhashsum_hasher *, size_t), struct libhashsum_hasher *this, const char *algorithm)
{
	const char *p;
	size_t n = 0;

	p = strchr(algorithm, '[');
	if (!p || *++p == ']') {
		if (p && *++p)
			goto einval;
		return initfunc(this, 0);
	}

	if (*p != 'n' && *p != 'N')
		goto einval;
	if (parse_value(&n, &p[1], &p))
		goto einval;

	if (*p++ != ']' || *p)
		goto einval;

	return initfunc(this, n);

einval:
	errno = EINVAL;
	return -1;
}


static int
with_rcnz(int (*initfunc)(struct libhashsum_hasher *, size_t, size_t, size_t, size_t),
         struct libhashsum_hasher *this, const char *algorithm)
{
	const char *p;
	size_t r = 0, c = 0, n = 0, z = 0;

	p = strchr(algorithm, '[');
	if (!p || *++p == ']') {
		if (p && *++p)
			goto einval;
		return initfunc(this, 0, 0, 0, 0);
	}

	for (;;) {
		if (*p == 'r' || *p == 'R') {
			if (parse_value(&r, &p[1], &p))
				goto einval;
		} else if (*p == 'c' || *p == 'C') {
			if (parse_value(&c, &p[1], &p))
				goto einval;
		} else if (*p == 'n' || *p == 'N') {
			if (parse_value(&n, &p[1], &p))
				goto einval;
		} else if (*p == 'z' || *p == 'Z') {
			if (parse_value(&z, &p[1], &p))
				goto einval;
		} else if (*p == ']') {
			break;
		} else {
			goto einval;
		}
		if (*p == ']')
			break;
		if (*p++ != ',')
			goto einval;
	}
	if (p[1])
		goto einval;

	return initfunc(this, r, c, n, z);

einval:
	errno = EINVAL;
	return -1;
}


static int
with_salt(int (*initfunc)(struct libhashsum_hasher *, const void *),
          struct libhashsum_hasher *this, const char *algorithm, size_t saltbytes)
{
	const char *p;
	uint8_t a, b, salt[32];
	size_t salti = 0;

	p = strchr(algorithm, '[');
	if (!p || *++p == ']') {
		if (p && *++p)
			goto einval;
		return initfunc(this, NULL);
	}

	if (*p != 's' && *p != 'S')
		goto einval;
	p++;
	if (*p != 'a' && *p != 'A')
		goto einval;
	p++;
	if (*p != 'l' && *p != 'L')
		goto einval;
	p++;
	if (*p != 't' && *p != 'T')
		goto einval;
	p++;
	if (*p != '=')
		goto einval;
	p++;

	if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
		p = &p[2];

	for (;;) {
		if (isxdigit(p[0]) && isxdigit(p[1])) {
			if (!saltbytes)
				goto einval;
			saltbytes--;
		} else if (isxdigit(p[0])) {
			goto einval;
		} else if (saltbytes) {
			goto einval;
		} else {
			break;
		}

		a = (uint8_t)(((p[0] & 15) + (p[0] > '9' ? 9 : 0)) << 4);
		b = (uint8_t)(((p[1] & 15) + (p[1] > '9' ? 9 : 0)) << 0);
		salt[salti++] = (uint8_t)(a | b);
		p = &p[2];
	}

	if (*p++ != ']' || *p)
		goto einval;

	return initfunc(this, salti ? salt : NULL);

einval:
	errno = EINVAL;
	return -1;
}


static int
blake2(int (*initfunc)(struct libhashsum_hasher *, size_t, const void *, const void *, const void *, size_t),
       struct libhashsum_hasher *this, const char *algorithm, size_t saltbytes, size_t pepperbytes)
{
	const char *p;
	uint8_t a, b, salt[16], pepper[16], *key = NULL;
	size_t n = 0, salti = 0, pepperi = 0, keyi = 0, i;
	int r;

	p = strchr(algorithm, '[');
	if (!p || *++p == ']') {
		if (p && *++p)
			goto einval;
		return initfunc(this, 0, NULL, NULL, NULL, 0);
	}

next:
	if (!salti && !strncasecmp(p, "salt=", 5U)) {
		p = &p[5];
		if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
			p = &p[2];
		for (;;) {
			if (isxdigit(p[0]) && isxdigit(p[1])) {
				if (!saltbytes)
					goto einval;
				saltbytes--;
			} else if (isxdigit(p[0])) {
				goto einval;
			} else if (saltbytes) {
				goto einval;
			} else {
				break;
			}
			a = (uint8_t)(((p[0] & 15) + (p[0] > '9' ? 9 : 0)) << 4);
			b = (uint8_t)(((p[1] & 15) + (p[1] > '9' ? 9 : 0)) << 0);
			salt[salti++] = (uint8_t)(a | b);
			p = &p[2];
		}
	} else if (!pepperi && !strncasecmp(p, "pepper=", 7U)) {
		p = &p[7];
		if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
			p = &p[2];
		for (;;) {
			if (isxdigit(p[0]) && isxdigit(p[1])) {
				if (!pepperbytes)
					goto einval;
				pepperbytes--;
			} else if (isxdigit(p[0])) {
				goto einval;
			} else if (pepperbytes) {
				goto einval;
			} else {
				break;
			}
			a = (uint8_t)(((p[0] & 15) + (p[0] > '9' ? 9 : 0)) << 4);
			b = (uint8_t)(((p[1] & 15) + (p[1] > '9' ? 9 : 0)) << 0);
			pepper[pepperi++] = (uint8_t)(a | b);
			p = &p[2];
		}
	} else if (!keyi && !strncasecmp(p, "key=", 4U)) {
		p = &p[4];
		if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
			p = &p[2];
		while (isxdigit(p[keyi | 0]) && isxdigit(p[keyi | 1]))
			keyi += 2U;
		if (isxdigit(p[keyi]))
			goto einval;
		keyi /= 2U;
		if (keyi) {
			key = malloc(keyi);
			if (!key)
				goto fail;
		}
		for (i = 0; i < keyi; i++) {
			a = (uint8_t)(((p[0] & 15) + (p[0] > '9' ? 9 : 0)) << 4);
			b = (uint8_t)(((p[1] & 15) + (p[1] > '9' ? 9 : 0)) << 0);
			key[i] = (uint8_t)(a | b);
			p = &p[2];
		}
	} else if (*p == 'n' || *p == 'N') {
		if (parse_value(&n, &p[1], &p))
			goto einval;
	} else {
		goto einval;
	}

	if (*p == ',') {
		p++;
		goto next;
	}

	if (*p++ != ']' || *p)
		goto einval;

	r = initfunc(this, n, salti ? salt : NULL, pepperi ? pepper : NULL, key, keyi);
	free(key);
	return r;

einval:
	errno = EINVAL;
fail:
	free(key);
	return -1;
}


int
libhashsum_init_hasher_from_string(struct libhashsum_hasher *this, const char *algorithm)
{
	enum libhashsum_algorithm algo;
	if (!libhashsum_get_algorithm_from_string(&algo, algorithm)) {
		errno = EINVAL;
		return -1;
	}
	switch (algo) {
	case LIBHASHSUM_MD2:          return libhashsum_init_md2_hasher(this);
	case LIBHASHSUM_MD4:          return libhashsum_init_md4_hasher(this);
	case LIBHASHSUM_MD5:          return libhashsum_init_md5_hasher(this);
	case LIBHASHSUM_RIPEMD_128:   return libhashsum_init_ripemd_128_hasher(this);
	case LIBHASHSUM_RIPEMD_160:   return libhashsum_init_ripemd_160_hasher(this);
	case LIBHASHSUM_RIPEMD_256:   return libhashsum_init_ripemd_256_hasher(this);
	case LIBHASHSUM_RIPEMD_320:   return libhashsum_init_ripemd_320_hasher(this);
	case LIBHASHSUM_SHA0:         return libhashsum_init_sha0_hasher(this);
	case LIBHASHSUM_SHA1:         return libhashsum_init_sha1_hasher(this);
	case LIBHASHSUM_SHA_224:      return libhashsum_init_sha_224_hasher(this);
	case LIBHASHSUM_SHA_256:      return libhashsum_init_sha_256_hasher(this);
	case LIBHASHSUM_SHA_384:      return libhashsum_init_sha_384_hasher(this);
	case LIBHASHSUM_SHA_512:      return libhashsum_init_sha_512_hasher(this);
	case LIBHASHSUM_SHA_512_224:  return libhashsum_init_sha_512_224_hasher(this);
	case LIBHASHSUM_SHA_512_256:  return libhashsum_init_sha_512_256_hasher(this);
	case LIBHASHSUM_KECCAK_224:   return libhashsum_init_keccak_224_hasher(this);
	case LIBHASHSUM_KECCAK_256:   return libhashsum_init_keccak_256_hasher(this);
	case LIBHASHSUM_KECCAK_384:   return libhashsum_init_keccak_384_hasher(this);
	case LIBHASHSUM_KECCAK_512:   return libhashsum_init_keccak_512_hasher(this);
	case LIBHASHSUM_SHA3_224:     return libhashsum_init_sha3_224_hasher(this);
	case LIBHASHSUM_SHA3_256:     return libhashsum_init_sha3_256_hasher(this);
	case LIBHASHSUM_SHA3_384:     return libhashsum_init_sha3_384_hasher(this);
	case LIBHASHSUM_SHA3_512:     return libhashsum_init_sha3_512_hasher(this);
	case LIBHASHSUM_SHAKE128:     return with_n(&libhashsum_init_shake128_hasher, this, algorithm);
	case LIBHASHSUM_SHAKE256:     return with_n(&libhashsum_init_shake256_hasher, this, algorithm);
	case LIBHASHSUM_SHAKE512:     return with_n(&libhashsum_init_shake512_hasher, this, algorithm);
	case LIBHASHSUM_RAWSHAKE128:  return with_n(&libhashsum_init_rawshake128_hasher, this, algorithm);
	case LIBHASHSUM_RAWSHAKE256:  return with_n(&libhashsum_init_rawshake256_hasher, this, algorithm);
	case LIBHASHSUM_RAWSHAKE512:  return with_n(&libhashsum_init_rawshake512_hasher, this, algorithm);
	case LIBHASHSUM_KECCAK:       return with_rcnz(&libhashsum_init_keccak_hasher, this, algorithm);
	case LIBHASHSUM_BLAKE224:     return with_salt(&libhashsum_init_blake224_hasher, this, algorithm, 16U);
	case LIBHASHSUM_BLAKE256:     return with_salt(&libhashsum_init_blake256_hasher, this, algorithm, 16U);
	case LIBHASHSUM_BLAKE384:     return with_salt(&libhashsum_init_blake384_hasher, this, algorithm, 32U);
	case LIBHASHSUM_BLAKE512:     return with_salt(&libhashsum_init_blake512_hasher, this, algorithm, 32U);
	case LIBHASHSUM_BLAKE2S:      return blake2(&libhashsum_init_blake2s_hasher, this, algorithm, 8U, 8U);
	case LIBHASHSUM_BLAKE2B:      return blake2(&libhashsum_init_blake2b_hasher, this, algorithm, 16U, 16U);
	default:
		abort();
	}
}