Monero
external
randomx
src
argon2.h
Go to the documentation of this file.
1
/*
2
Copyright (c) 2018-2019, tevador <tevador@gmail.com>
3
4
All rights reserved.
5
6
Redistribution and use in source and binary forms, with or without
7
modification, are permitted provided that the following conditions are met:
8
* Redistributions of source code must retain the above copyright
9
notice, this list of conditions and the following disclaimer.
10
* Redistributions in binary form must reproduce the above copyright
11
notice, this list of conditions and the following disclaimer in the
12
documentation and/or other materials provided with the distribution.
13
* Neither the name of the copyright holder nor the
14
names of its contributors may be used to endorse or promote products
15
derived from this software without specific prior written permission.
16
17
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
*/
28
29
/* Original code from Argon2 reference source code package used under CC0 Licence
30
* https://github.com/P-H-C/phc-winner-argon2
31
* Copyright 2015
32
* Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
33
*/
34
35
#pragma once
36
37
#include <
stdint.h
>
38
#include <stddef.h>
39
#include <limits.h>
40
41
/*
42
* Argon2 input parameter restrictions
43
*/
44
45
/* Minimum and maximum number of lanes (degree of parallelism) */
46
#define ARGON2_MIN_LANES UINT32_C(1)
47
#define ARGON2_MAX_LANES UINT32_C(0xFFFFFF)
48
49
/* Minimum and maximum number of threads */
50
#define ARGON2_MIN_THREADS UINT32_C(1)
51
#define ARGON2_MAX_THREADS UINT32_C(0xFFFFFF)
52
53
/* Number of synchronization points between lanes per pass */
54
#define ARGON2_SYNC_POINTS UINT32_C(4)
55
56
/* Minimum and maximum digest size in bytes */
57
#define ARGON2_MIN_OUTLEN UINT32_C(4)
58
#define ARGON2_MAX_OUTLEN UINT32_C(0xFFFFFFFF)
59
60
/* Minimum and maximum number of memory blocks (each of BLOCK_SIZE bytes) */
61
#define ARGON2_MIN_MEMORY (2 * ARGON2_SYNC_POINTS)
/* 2 blocks per slice */
62
63
#define ARGON2_MIN(a, b) ((a) < (b) ? (a) : (b))
64
/* Max memory size is addressing-space/2, topping at 2^32 blocks (4 TB) */
65
#define ARGON2_MAX_MEMORY_BITS \
66
ARGON2_MIN(UINT32_C(32), (sizeof(void *) * CHAR_BIT - 10 - 1))
67
#define ARGON2_MAX_MEMORY \
68
ARGON2_MIN(UINT32_C(0xFFFFFFFF), UINT64_C(1) << ARGON2_MAX_MEMORY_BITS)
69
70
/* Minimum and maximum number of passes */
71
#define ARGON2_MIN_TIME UINT32_C(1)
72
#define ARGON2_MAX_TIME UINT32_C(0xFFFFFFFF)
73
74
/* Minimum and maximum password length in bytes */
75
#define ARGON2_MIN_PWD_LENGTH UINT32_C(0)
76
#define ARGON2_MAX_PWD_LENGTH UINT32_C(0xFFFFFFFF)
77
78
/* Minimum and maximum associated data length in bytes */
79
#define ARGON2_MIN_AD_LENGTH UINT32_C(0)
80
#define ARGON2_MAX_AD_LENGTH UINT32_C(0xFFFFFFFF)
81
82
/* Minimum and maximum salt length in bytes */
83
#define ARGON2_MIN_SALT_LENGTH UINT32_C(8)
84
#define ARGON2_MAX_SALT_LENGTH UINT32_C(0xFFFFFFFF)
85
86
/* Minimum and maximum key length in bytes */
87
#define ARGON2_MIN_SECRET UINT32_C(0)
88
#define ARGON2_MAX_SECRET UINT32_C(0xFFFFFFFF)
89
90
/* Flags to determine which fields are securely wiped (default = no wipe). */
91
#define ARGON2_DEFAULT_FLAGS UINT32_C(0)
92
#define ARGON2_FLAG_CLEAR_PASSWORD (UINT32_C(1) << 0)
93
#define ARGON2_FLAG_CLEAR_SECRET (UINT32_C(1) << 1)
94
95
96
/* Error codes */
97
typedef
enum
Argon2_ErrorCodes
{
98
ARGON2_OK
= 0,
99
100
ARGON2_OUTPUT_PTR_NULL
= -1,
101
102
ARGON2_OUTPUT_TOO_SHORT
= -2,
103
ARGON2_OUTPUT_TOO_LONG
= -3,
104
105
ARGON2_PWD_TOO_SHORT
= -4,
106
ARGON2_PWD_TOO_LONG
= -5,
107
108
ARGON2_SALT_TOO_SHORT
= -6,
109
ARGON2_SALT_TOO_LONG
= -7,
110
111
ARGON2_AD_TOO_SHORT
= -8,
112
ARGON2_AD_TOO_LONG
= -9,
113
114
ARGON2_SECRET_TOO_SHORT
= -10,
115
ARGON2_SECRET_TOO_LONG
= -11,
116
117
ARGON2_TIME_TOO_SMALL
= -12,
118
ARGON2_TIME_TOO_LARGE
= -13,
119
120
ARGON2_MEMORY_TOO_LITTLE
= -14,
121
ARGON2_MEMORY_TOO_MUCH
= -15,
122
123
ARGON2_LANES_TOO_FEW
= -16,
124
ARGON2_LANES_TOO_MANY
= -17,
125
126
ARGON2_PWD_PTR_MISMATCH
= -18,
/* NULL ptr with non-zero length */
127
ARGON2_SALT_PTR_MISMATCH
= -19,
/* NULL ptr with non-zero length */
128
ARGON2_SECRET_PTR_MISMATCH
= -20,
/* NULL ptr with non-zero length */
129
ARGON2_AD_PTR_MISMATCH
= -21,
/* NULL ptr with non-zero length */
130
131
ARGON2_MEMORY_ALLOCATION_ERROR
= -22,
132
133
ARGON2_FREE_MEMORY_CBK_NULL
= -23,
134
ARGON2_ALLOCATE_MEMORY_CBK_NULL
= -24,
135
136
ARGON2_INCORRECT_PARAMETER
= -25,
137
ARGON2_INCORRECT_TYPE
= -26,
138
139
ARGON2_OUT_PTR_MISMATCH
= -27,
140
141
ARGON2_THREADS_TOO_FEW
= -28,
142
ARGON2_THREADS_TOO_MANY
= -29,
143
144
ARGON2_MISSING_ARGS
= -30,
145
146
ARGON2_ENCODING_FAIL
= -31,
147
148
ARGON2_DECODING_FAIL
= -32,
149
150
ARGON2_THREAD_FAIL
= -33,
151
152
ARGON2_DECODING_LENGTH_FAIL
= -34,
153
154
ARGON2_VERIFY_MISMATCH
= -35
155
}
argon2_error_codes
;
156
157
/* Memory allocator types --- for external allocation */
158
typedef
int
(*
allocate_fptr
)(
uint8_t
**memory,
size_t
bytes_to_allocate);
159
typedef
void(*
deallocate_fptr
)(
uint8_t
*memory,
size_t
bytes_to_allocate);
160
161
/* Argon2 external data structures */
162
163
/*
164
*****
165
* Context: structure to hold Argon2 inputs:
166
* output array and its length,
167
* password and its length,
168
* salt and its length,
169
* secret and its length,
170
* associated data and its length,
171
* number of passes, amount of used memory (in KBytes, can be rounded up a bit)
172
* number of parallel threads that will be run.
173
* All the parameters above affect the output hash value.
174
* Additionally, two function pointers can be provided to allocate and
175
* deallocate the memory (if NULL, memory will be allocated internally).
176
* Also, three flags indicate whether to erase password, secret as soon as they
177
* are pre-hashed (and thus not needed anymore), and the entire memory
178
*****
179
* Simplest situation: you have output array out[8], password is stored in
180
* pwd[32], salt is stored in salt[16], you do not have keys nor associated
181
* data. You need to spend 1 GB of RAM and you run 5 passes of Argon2d with
182
* 4 parallel lanes.
183
* You want to erase the password, but you're OK with last pass not being
184
* erased. You want to use the default memory allocator.
185
* Then you initialize:
186
Argon2_Context(out,8,pwd,32,salt,16,NULL,0,NULL,0,5,1<<20,4,4,NULL,NULL,true,false,false,false)
187
*/
188
typedef
struct
Argon2_Context
{
189
uint8_t
*
out
;
/* output array */
190
uint32_t
outlen
;
/* digest length */
191
192
uint8_t
*
pwd
;
/* password array */
193
uint32_t
pwdlen
;
/* password length */
194
195
uint8_t
*
salt
;
/* salt array */
196
uint32_t
saltlen
;
/* salt length */
197
198
uint8_t
*
secret
;
/* key array */
199
uint32_t
secretlen
;
/* key length */
200
201
uint8_t
*
ad
;
/* associated data array */
202
uint32_t
adlen
;
/* associated data length */
203
204
uint32_t
t_cost
;
/* number of passes */
205
uint32_t
m_cost
;
/* amount of memory requested (KB) */
206
uint32_t
lanes
;
/* number of lanes */
207
uint32_t
threads
;
/* maximum number of threads */
208
209
uint32_t
version
;
/* version number */
210
211
allocate_fptr
allocate_cbk
;
/* pointer to memory allocator */
212
deallocate_fptr
free_cbk
;
/* pointer to memory deallocator */
213
214
uint32_t
flags
;
/* array of bool options */
215
}
argon2_context
;
216
217
/* Argon2 primitive type */
218
typedef
enum
Argon2_type
{
219
Argon2_d
= 0,
220
Argon2_i
= 1,
221
Argon2_id
= 2
222
}
argon2_type
;
223
224
/* Version of the algorithm */
225
typedef
enum
Argon2_version
{
226
ARGON2_VERSION_10
= 0x10,
227
ARGON2_VERSION_13
= 0x13,
228
ARGON2_VERSION_NUMBER
=
ARGON2_VERSION_13
229
}
argon2_version
;
230
231
//Argon2 instance - forward declaration
232
typedef
struct
Argon2_instance_t
argon2_instance_t
;
233
234
//Argon2 position = forward declaration
235
typedef
struct
Argon2_position_t
argon2_position_t
;
236
237
//Argon2 implementation function
238
typedef
void
randomx_argon2_impl
(
const
argon2_instance_t
*
instance
,
239
argon2_position_t
position);
240
241
#if defined(__cplusplus)
242
extern
"C"
{
243
#endif
244
245
/*
246
* Function that fills the segment using previous segments also from other
247
* threads
248
* @param context current context
249
* @param instance Pointer to the current instance
250
* @param position Current position
251
* @pre all block pointers must be valid
252
*/
253
void
randomx_argon2_fill_segment_ref
(
const
argon2_instance_t
*
instance
,
254
argon2_position_t
position);
255
256
randomx_argon2_impl
*
randomx_argon2_impl_ssse3
();
257
randomx_argon2_impl
*
randomx_argon2_impl_avx2
();
258
259
#if defined(__cplusplus)
260
}
261
#endif
Argon2_Context::lanes
uint32_t lanes
Definition:
argon2.h:206
ARGON2_MEMORY_TOO_LITTLE
Definition:
argon2.h:120
Argon2_Context::outlen
uint32_t outlen
Definition:
argon2.h:190
argon2_type
enum Argon2_type argon2_type
argon2_version
enum Argon2_version argon2_version
Argon2_Context::out
uint8_t * out
Definition:
argon2.h:189
ARGON2_AD_PTR_MISMATCH
Definition:
argon2.h:129
ARGON2_MEMORY_TOO_MUCH
Definition:
argon2.h:121
ARGON2_DECODING_LENGTH_FAIL
Definition:
argon2.h:152
ARGON2_INCORRECT_TYPE
Definition:
argon2.h:137
ARGON2_VERSION_13
Definition:
argon2.h:227
ARGON2_AD_TOO_LONG
Definition:
argon2.h:112
Argon2_Context::secretlen
uint32_t secretlen
Definition:
argon2.h:199
Argon2_Context::pwd
uint8_t * pwd
Definition:
argon2.h:192
ARGON2_VERSION_10
Definition:
argon2.h:226
ARGON2_OUTPUT_TOO_SHORT
Definition:
argon2.h:102
deallocate_fptr
void(* deallocate_fptr)(uint8_t *memory, size_t bytes_to_allocate)
Definition:
argon2.h:159
Argon2_ErrorCodes
Argon2_ErrorCodes
Definition:
argon2.h:97
Argon2_Context::pwdlen
uint32_t pwdlen
Definition:
argon2.h:193
Argon2_position_t
Definition:
argon2_core.h:101
ARGON2_SECRET_TOO_SHORT
Definition:
argon2.h:114
ARGON2_LANES_TOO_MANY
Definition:
argon2.h:124
Argon2_Context::adlen
uint32_t adlen
Definition:
argon2.h:202
ARGON2_SALT_PTR_MISMATCH
Definition:
argon2.h:127
Argon2_Context::version
uint32_t version
Definition:
argon2.h:209
ARGON2_INCORRECT_PARAMETER
Definition:
argon2.h:136
ARGON2_MEMORY_ALLOCATION_ERROR
Definition:
argon2.h:131
ARGON2_SECRET_PTR_MISMATCH
Definition:
argon2.h:128
Argon2_instance_t
Definition:
argon2_core.h:82
ARGON2_SECRET_TOO_LONG
Definition:
argon2.h:115
uint8_t
unsigned char uint8_t
Definition:
stdint.h:124
Argon2_Context::t_cost
uint32_t t_cost
Definition:
argon2.h:204
ARGON2_PWD_TOO_SHORT
Definition:
argon2.h:105
ARGON2_OK
Definition:
argon2.h:98
randomx_argon2_impl_ssse3
randomx_argon2_impl * randomx_argon2_impl_ssse3()
Definition:
argon2_ssse3.c:48
ARGON2_LANES_TOO_FEW
Definition:
argon2.h:123
ARGON2_DECODING_FAIL
Definition:
argon2.h:148
ARGON2_PWD_PTR_MISMATCH
Definition:
argon2.h:126
randomx_argon2_impl
void randomx_argon2_impl(const argon2_instance_t *instance, argon2_position_t position)
Definition:
argon2.h:238
Argon2_d
Definition:
argon2.h:219
ARGON2_SALT_TOO_LONG
Definition:
argon2.h:109
ARGON2_PWD_TOO_LONG
Definition:
argon2.h:106
ARGON2_THREAD_FAIL
Definition:
argon2.h:150
Argon2_Context::allocate_cbk
allocate_fptr allocate_cbk
Definition:
argon2.h:211
uint32_t
unsigned int uint32_t
Definition:
stdint.h:126
ARGON2_TIME_TOO_LARGE
Definition:
argon2.h:118
ARGON2_SALT_TOO_SHORT
Definition:
argon2.h:108
ARGON2_VERSION_NUMBER
Definition:
argon2.h:228
Argon2_Context
Definition:
argon2.h:188
Argon2_id
Definition:
argon2.h:221
randomx_argon2_impl_avx2
randomx_argon2_impl * randomx_argon2_impl_avx2()
Definition:
argon2_avx2.c:44
argon2_error_codes
enum Argon2_ErrorCodes argon2_error_codes
pymoduletest.int
int
Definition:
pymoduletest.py:17
ARGON2_ALLOCATE_MEMORY_CBK_NULL
Definition:
argon2.h:134
Argon2_Context::secret
uint8_t * secret
Definition:
argon2.h:198
Argon2_Context::ad
uint8_t * ad
Definition:
argon2.h:201
instance
static reverse_alphabet instance
Definition:
base58.cpp:73
Argon2_Context::flags
uint32_t flags
Definition:
argon2.h:214
ARGON2_OUTPUT_TOO_LONG
Definition:
argon2.h:103
allocate_fptr
int(* allocate_fptr)(uint8_t **memory, size_t bytes_to_allocate)
Definition:
argon2.h:158
Argon2_Context::saltlen
uint32_t saltlen
Definition:
argon2.h:196
ARGON2_MISSING_ARGS
Definition:
argon2.h:144
ARGON2_OUT_PTR_MISMATCH
Definition:
argon2.h:139
Argon2_i
Definition:
argon2.h:220
stdint.h
argon2_context
struct Argon2_Context argon2_context
ARGON2_VERIFY_MISMATCH
Definition:
argon2.h:154
Argon2_Context::free_cbk
deallocate_fptr free_cbk
Definition:
argon2.h:212
ARGON2_FREE_MEMORY_CBK_NULL
Definition:
argon2.h:133
Argon2_Context::m_cost
uint32_t m_cost
Definition:
argon2.h:205
ARGON2_THREADS_TOO_MANY
Definition:
argon2.h:142
randomx_argon2_fill_segment_ref
void randomx_argon2_fill_segment_ref(const argon2_instance_t *instance, argon2_position_t position)
Definition:
argon2_ref.c:110
Argon2_type
Argon2_type
Definition:
argon2.h:218
Argon2_Context::threads
uint32_t threads
Definition:
argon2.h:207
ARGON2_TIME_TOO_SMALL
Definition:
argon2.h:117
ARGON2_OUTPUT_PTR_NULL
Definition:
argon2.h:100
ARGON2_THREADS_TOO_FEW
Definition:
argon2.h:141
Argon2_version
Argon2_version
Definition:
argon2.h:225
ARGON2_AD_TOO_SHORT
Definition:
argon2.h:111
ARGON2_ENCODING_FAIL
Definition:
argon2.h:146
Argon2_Context::salt
uint8_t * salt
Definition:
argon2.h:195
Generated on Sun Oct 12 2025 12:00:00 for Monero by
1.8.14