Monero
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 
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 */
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 */
216 
217 /* Argon2 primitive type */
218 typedef enum Argon2_type {
219  Argon2_d = 0,
220  Argon2_i = 1,
222 } argon2_type;
223 
224 /* Version of the algorithm */
225 typedef 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)
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  */
254  argon2_position_t position);
255 
258 
259 #if defined(__cplusplus)
260 }
261 #endif
uint32_t lanes
Definition: argon2.h:206
Definition: argon2.h:120
uint32_t outlen
Definition: argon2.h:190
enum Argon2_type argon2_type
enum Argon2_version argon2_version
uint8_t * out
Definition: argon2.h:189
Definition: argon2.h:129
Definition: argon2.h:121
Definition: argon2.h:152
Definition: argon2.h:137
Definition: argon2.h:227
Definition: argon2.h:112
uint32_t secretlen
Definition: argon2.h:199
uint8_t * pwd
Definition: argon2.h:192
Definition: argon2.h:226
Definition: argon2.h:102
void(* deallocate_fptr)(uint8_t *memory, size_t bytes_to_allocate)
Definition: argon2.h:159
Argon2_ErrorCodes
Definition: argon2.h:97
uint32_t pwdlen
Definition: argon2.h:193
Definition: argon2_core.h:101
Definition: argon2.h:114
Definition: argon2.h:124
uint32_t adlen
Definition: argon2.h:202
Definition: argon2.h:127
uint32_t version
Definition: argon2.h:209
Definition: argon2.h:136
Definition: argon2.h:131
Definition: argon2.h:128
Definition: argon2_core.h:82
Definition: argon2.h:115
unsigned char uint8_t
Definition: stdint.h:124
uint32_t t_cost
Definition: argon2.h:204
Definition: argon2.h:105
Definition: argon2.h:98
randomx_argon2_impl * randomx_argon2_impl_ssse3()
Definition: argon2_ssse3.c:48
Definition: argon2.h:123
Definition: argon2.h:148
Definition: argon2.h:126
void randomx_argon2_impl(const argon2_instance_t *instance, argon2_position_t position)
Definition: argon2.h:238
Definition: argon2.h:219
Definition: argon2.h:109
Definition: argon2.h:106
Definition: argon2.h:150
allocate_fptr allocate_cbk
Definition: argon2.h:211
unsigned int uint32_t
Definition: stdint.h:126
Definition: argon2.h:118
Definition: argon2.h:108
Definition: argon2.h:228
Definition: argon2.h:188
Definition: argon2.h:221
randomx_argon2_impl * randomx_argon2_impl_avx2()
Definition: argon2_avx2.c:44
enum Argon2_ErrorCodes argon2_error_codes
int
Definition: pymoduletest.py:17
Definition: argon2.h:134
uint8_t * secret
Definition: argon2.h:198
uint8_t * ad
Definition: argon2.h:201
static reverse_alphabet instance
Definition: base58.cpp:73
uint32_t flags
Definition: argon2.h:214
Definition: argon2.h:103
int(* allocate_fptr)(uint8_t **memory, size_t bytes_to_allocate)
Definition: argon2.h:158
uint32_t saltlen
Definition: argon2.h:196
Definition: argon2.h:144
Definition: argon2.h:139
Definition: argon2.h:220
struct Argon2_Context argon2_context
Definition: argon2.h:154
deallocate_fptr free_cbk
Definition: argon2.h:212
Definition: argon2.h:133
uint32_t m_cost
Definition: argon2.h:205
Definition: argon2.h:142
void randomx_argon2_fill_segment_ref(const argon2_instance_t *instance, argon2_position_t position)
Definition: argon2_ref.c:110
Argon2_type
Definition: argon2.h:218
uint32_t threads
Definition: argon2.h:207
Definition: argon2.h:117
Definition: argon2.h:100
Definition: argon2.h:141
Argon2_version
Definition: argon2.h:225
Definition: argon2.h:111
Definition: argon2.h:146
uint8_t * salt
Definition: argon2.h:195