Monero
Loading...
Searching...
No Matches
argon2.h
Go to the documentation of this file.
1/*
2Copyright (c) 2018-2019, tevador <tevador@gmail.com>
3
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, 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
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26OF 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 */
97typedef enum Argon2_ErrorCodes {
99
101
104
107
110
113
116
119
122
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
132
135
138
140
143
145
147
149
151
153
156
157/* Memory allocator types --- for external allocation */
158typedef int(*allocate_fptr)(uint8_t **memory, size_t bytes_to_allocate);
159typedef 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 */
188typedef 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 */
216
217/* Argon2 primitive type */
218typedef enum Argon2_type {
221 Argon2_id = 2
223
224/* Version of the algorithm */
225typedef enum Argon2_version {
230
231//Argon2 instance - forward declaration
233
234//Argon2 position = forward declaration
236
237//Argon2 implementation function
239 argon2_position_t position);
240
241#if defined(__cplusplus)
242extern "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 */
254 argon2_position_t position);
255
258
259#if defined(__cplusplus)
260}
261#endif
enum Argon2_type argon2_type
Argon2_ErrorCodes
Definition: argon2.h:97
@ ARGON2_INCORRECT_TYPE
Definition: argon2.h:137
@ ARGON2_LANES_TOO_FEW
Definition: argon2.h:123
@ ARGON2_OK
Definition: argon2.h:98
@ ARGON2_LANES_TOO_MANY
Definition: argon2.h:124
@ ARGON2_INCORRECT_PARAMETER
Definition: argon2.h:136
@ ARGON2_SECRET_TOO_SHORT
Definition: argon2.h:114
@ ARGON2_PWD_TOO_LONG
Definition: argon2.h:106
@ ARGON2_PWD_TOO_SHORT
Definition: argon2.h:105
@ ARGON2_MEMORY_ALLOCATION_ERROR
Definition: argon2.h:131
@ ARGON2_AD_PTR_MISMATCH
Definition: argon2.h:129
@ ARGON2_OUT_PTR_MISMATCH
Definition: argon2.h:139
@ ARGON2_MISSING_ARGS
Definition: argon2.h:144
@ ARGON2_THREAD_FAIL
Definition: argon2.h:150
@ ARGON2_SALT_TOO_SHORT
Definition: argon2.h:108
@ ARGON2_OUTPUT_TOO_LONG
Definition: argon2.h:103
@ ARGON2_AD_TOO_LONG
Definition: argon2.h:112
@ ARGON2_DECODING_LENGTH_FAIL
Definition: argon2.h:152
@ ARGON2_SALT_TOO_LONG
Definition: argon2.h:109
@ ARGON2_MEMORY_TOO_LITTLE
Definition: argon2.h:120
@ ARGON2_TIME_TOO_LARGE
Definition: argon2.h:118
@ ARGON2_THREADS_TOO_MANY
Definition: argon2.h:142
@ ARGON2_ENCODING_FAIL
Definition: argon2.h:146
@ ARGON2_MEMORY_TOO_MUCH
Definition: argon2.h:121
@ ARGON2_PWD_PTR_MISMATCH
Definition: argon2.h:126
@ ARGON2_SECRET_TOO_LONG
Definition: argon2.h:115
@ ARGON2_THREADS_TOO_FEW
Definition: argon2.h:141
@ ARGON2_SECRET_PTR_MISMATCH
Definition: argon2.h:128
@ ARGON2_AD_TOO_SHORT
Definition: argon2.h:111
@ ARGON2_TIME_TOO_SMALL
Definition: argon2.h:117
@ ARGON2_FREE_MEMORY_CBK_NULL
Definition: argon2.h:133
@ ARGON2_OUTPUT_PTR_NULL
Definition: argon2.h:100
@ ARGON2_DECODING_FAIL
Definition: argon2.h:148
@ ARGON2_SALT_PTR_MISMATCH
Definition: argon2.h:127
@ ARGON2_ALLOCATE_MEMORY_CBK_NULL
Definition: argon2.h:134
@ ARGON2_VERIFY_MISMATCH
Definition: argon2.h:154
@ ARGON2_OUTPUT_TOO_SHORT
Definition: argon2.h:102
struct Argon2_Context argon2_context
enum Argon2_ErrorCodes argon2_error_codes
void randomx_argon2_fill_segment_ref(const argon2_instance_t *instance, argon2_position_t position)
Definition: argon2_ref.c:110
int(* allocate_fptr)(uint8_t **memory, size_t bytes_to_allocate)
Definition: argon2.h:158
Argon2_type
Definition: argon2.h:218
@ Argon2_id
Definition: argon2.h:221
@ Argon2_d
Definition: argon2.h:219
@ Argon2_i
Definition: argon2.h:220
enum Argon2_version argon2_version
void(* deallocate_fptr)(uint8_t *memory, size_t bytes_to_allocate)
Definition: argon2.h:159
Argon2_version
Definition: argon2.h:225
@ ARGON2_VERSION_10
Definition: argon2.h:226
@ ARGON2_VERSION_13
Definition: argon2.h:227
@ ARGON2_VERSION_NUMBER
Definition: argon2.h:228
randomx_argon2_impl * randomx_argon2_impl_ssse3()
Definition: argon2_ssse3.c:48
void randomx_argon2_impl(const argon2_instance_t *instance, argon2_position_t position)
Definition: argon2.h:238
randomx_argon2_impl * randomx_argon2_impl_avx2()
Definition: argon2_avx2.c:44
static reverse_alphabet instance
Definition: base58.cpp:73
unsigned int uint32_t
Definition: stdint.h:126
unsigned char uint8_t
Definition: stdint.h:124
Definition: argon2.h:188
uint8_t * pwd
Definition: argon2.h:192
uint32_t outlen
Definition: argon2.h:190
uint32_t flags
Definition: argon2.h:214
uint32_t threads
Definition: argon2.h:207
uint32_t saltlen
Definition: argon2.h:196
uint8_t * ad
Definition: argon2.h:201
uint32_t adlen
Definition: argon2.h:202
uint32_t version
Definition: argon2.h:209
uint8_t * salt
Definition: argon2.h:195
deallocate_fptr free_cbk
Definition: argon2.h:212
uint8_t * out
Definition: argon2.h:189
uint32_t pwdlen
Definition: argon2.h:193
uint32_t m_cost
Definition: argon2.h:205
uint8_t * secret
Definition: argon2.h:198
uint32_t lanes
Definition: argon2.h:206
uint32_t secretlen
Definition: argon2.h:199
uint32_t t_cost
Definition: argon2.h:204
allocate_fptr allocate_cbk
Definition: argon2.h:211
Definition: argon2_core.h:82
Definition: argon2_core.h:101