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


char *
libar2simplified_crypt(char *msg, const char *paramstr, char *rv)
{
	struct libar2_argon2_parameters *params = NULL;
	char *end, *ret = NULL, *hash = NULL;
	size_t size;

	params = libar2simplified_decode(paramstr, NULL, &end, NULL);
	if (!params)
		goto out;
	if (*end) {
		errno = EINVAL;
		goto out;
	}

	if (!rv) {
		size = libar2_hash_buf_size(params);
		if (!size || !(hash = malloc(size))) {
			errno = ENOMEM;
			goto out;
		}
	}
	if (libar2simplified_hash(rv ? rv : hash, msg, strlen(msg), params))
		goto out;

	ret = libar2simplified_encode(params, rv ? rv : hash);
	if (rv) {
		stpcpy(rv, ret);
		free(ret);
		ret = rv;
	}

out:
	if (params)
		libar2_erase(params->salt, params->saltlen);
	free(params);
	free(hash);
	return ret;
}