aboutsummaryrefslogtreecommitdiff
path: root/lib/Utils.Cryptography/monocypher/argon2.c
blob: 2606e73378f0a8eb8cf4a21e469f5b3747c68ea1 (plain)
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
/*
* Copyright (c) 2024 Vaughn Nugent
*
* vnlib_monocypher is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 2 of the License,
* or (at your option) any later version.
*
* vnlib_monocypher is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with vnlib_monocypher. If not, see http://www.gnu.org/licenses/.
*/

#include "argon2.h"
#include <monocypher.h>

#define ARGON2_WORK_AREA_MULTIPLIER 1024

VNLIB_EXPORT uint32_t VNLIB_CC Argon2CalcWorkAreaSize(const argon2Ctx* context)
{
	return context->m_cost * ARGON2_WORK_AREA_MULTIPLIER;
}

/*
* The purpose of this function is to remap the Argon2 context/function call
* interface to the Monocypher library version. Also performing some basic
* input validation that also matches the Argon2 library.
*/

VNLIB_EXPORT argon2_error_codes VNLIB_CC Argon2ComputeHash(const argon2Ctx* context, void* workArea)
{
	crypto_argon2_config config;
	crypto_argon2_inputs inputs;
	crypto_argon2_extras extras;

	if (!context || !workArea)
	{
		return ERR_INVALID_PTR;
	}

	config.algorithm = context->version;
	config.nb_blocks = context->m_cost;	
	config.nb_passes = context->t_cost;
	config.nb_lanes = context->threads;	

	inputs.pass_size = context->pwdlen;
	inputs.pass = context->pwd;

	inputs.salt_size = context->saltlen;
	inputs.salt = context->salt;

	/* must specify a password input */
	if (inputs.pass_size < 1)
	{
		return ARGON2_PWD_TOO_SHORT;
	}

	if (!inputs.pass)
	{
		return ARGON2_PWD_PTR_MISMATCH;
	}

	if (inputs.salt_size < 1)
	{
		return ARGON2_SALT_TOO_SHORT;
	}

	/* Verify salt pointer 1is not invalid  */
	if (!inputs.salt)
	{
		return ARGON2_SALT_PTR_MISMATCH;
	}

	extras.ad = context->ad;
	extras.ad_size = context->adlen;

	extras.key = context->secret;
	extras.key_size = context->secretlen;

	//If key is set, verify a valid pointer
	if (extras.key_size > 0 && !extras.key)
	{
		return ARGON2_SECRET_PTR_MISMATCH;
	}

	if (context->outlen < 1)
	{
		return ARGON2_OUTPUT_TOO_SHORT;
	}

	if (!context->out)
	{
		return ARGON2_OUTPUT_PTR_NULL;
	}

	/* invoke lib function */
	crypto_argon2(context->out, context->outlen, workArea, config, inputs, extras);

	return ARGON2_OK;
}