sorcery-argon2 1.0.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/codeql.yml +74 -0
- data/.github/workflows/ruby.yml +13 -3
- data/.rubocop.yml +112 -2
- data/CHANGELOG.md +8 -0
- data/MAINTAINING.md +8 -3
- data/README.md +155 -14
- data/bin/setup +4 -0
- data/ext/argon2_wrap/{Makefile → Makefile.real} +1 -0
- data/ext/argon2_wrap/extconf.rb +4 -1
- data/ext/argon2_wrap/libargon2_wrap.so +0 -0
- data/ext/argon2_wrap/tests +0 -0
- data/ext/phc-winner-argon2/.git +1 -0
- data/ext/phc-winner-argon2/.gitattributes +10 -0
- data/ext/phc-winner-argon2/.gitignore +22 -0
- data/ext/phc-winner-argon2/.travis.yml +25 -0
- data/ext/phc-winner-argon2/Argon2.sln +158 -0
- data/ext/phc-winner-argon2/CHANGELOG.md +32 -0
- data/ext/phc-winner-argon2/LICENSE +314 -0
- data/ext/phc-winner-argon2/Makefile +255 -0
- data/ext/phc-winner-argon2/Package.swift +46 -0
- data/ext/phc-winner-argon2/README.md +303 -0
- data/ext/phc-winner-argon2/appveyor.yml +25 -0
- data/ext/phc-winner-argon2/argon2-specs.pdf +0 -0
- data/ext/phc-winner-argon2/export.sh +7 -0
- data/ext/phc-winner-argon2/include/argon2.h +437 -0
- data/ext/phc-winner-argon2/kats/argon2d +12304 -0
- data/ext/phc-winner-argon2/kats/argon2d.shasum +1 -0
- data/ext/phc-winner-argon2/kats/argon2d_v16 +12304 -0
- data/ext/phc-winner-argon2/kats/argon2d_v16.shasum +1 -0
- data/ext/phc-winner-argon2/kats/argon2i +12304 -0
- data/ext/phc-winner-argon2/kats/argon2i.shasum +1 -0
- data/ext/phc-winner-argon2/kats/argon2i_v16 +12304 -0
- data/ext/phc-winner-argon2/kats/argon2i_v16.shasum +1 -0
- data/ext/phc-winner-argon2/kats/argon2id +12304 -0
- data/ext/phc-winner-argon2/kats/argon2id.shasum +1 -0
- data/ext/phc-winner-argon2/kats/argon2id_v16 +12304 -0
- data/ext/phc-winner-argon2/kats/argon2id_v16.shasum +1 -0
- data/ext/phc-winner-argon2/kats/check-sums.ps1 +42 -0
- data/ext/phc-winner-argon2/kats/check-sums.sh +13 -0
- data/ext/phc-winner-argon2/kats/test.ps1 +50 -0
- data/ext/phc-winner-argon2/kats/test.sh +49 -0
- data/ext/phc-winner-argon2/latex/IEEEtran.cls +6347 -0
- data/ext/phc-winner-argon2/latex/Makefile +18 -0
- data/ext/phc-winner-argon2/latex/argon2-specs.tex +920 -0
- data/ext/phc-winner-argon2/latex/pics/argon2-par.pdf +0 -0
- data/ext/phc-winner-argon2/latex/pics/compression.pdf +0 -0
- data/ext/phc-winner-argon2/latex/pics/generic.pdf +0 -0
- data/ext/phc-winner-argon2/latex/pics/power-distribution.jpg +0 -0
- data/ext/phc-winner-argon2/latex/tradeoff.bib +822 -0
- data/ext/phc-winner-argon2/libargon2.pc.in +18 -0
- data/ext/phc-winner-argon2/man/argon2.1 +57 -0
- data/ext/phc-winner-argon2/src/argon2.c +452 -0
- data/ext/phc-winner-argon2/src/bench.c +111 -0
- data/ext/phc-winner-argon2/src/blake2/blake2-impl.h +156 -0
- data/ext/phc-winner-argon2/src/blake2/blake2.h +89 -0
- data/ext/phc-winner-argon2/src/blake2/blake2b.c +390 -0
- data/ext/phc-winner-argon2/src/blake2/blamka-round-opt.h +471 -0
- data/ext/phc-winner-argon2/src/blake2/blamka-round-ref.h +56 -0
- data/ext/phc-winner-argon2/src/core.c +648 -0
- data/ext/phc-winner-argon2/src/core.h +228 -0
- data/ext/phc-winner-argon2/src/encoding.c +463 -0
- data/ext/phc-winner-argon2/src/encoding.h +57 -0
- data/ext/phc-winner-argon2/src/genkat.c +213 -0
- data/ext/phc-winner-argon2/src/genkat.h +51 -0
- data/ext/phc-winner-argon2/src/opt.c +283 -0
- data/ext/phc-winner-argon2/src/ref.c +194 -0
- data/ext/phc-winner-argon2/src/run.c +337 -0
- data/ext/phc-winner-argon2/src/test.c +289 -0
- data/ext/phc-winner-argon2/src/thread.c +57 -0
- data/ext/phc-winner-argon2/src/thread.h +67 -0
- data/ext/phc-winner-argon2/vs2015/Argon2Opt/Argon2Opt.vcxproj +231 -0
- data/ext/phc-winner-argon2/vs2015/Argon2Opt/Argon2Opt.vcxproj.filters +69 -0
- data/ext/phc-winner-argon2/vs2015/Argon2OptBench/Argon2OptBench.vcxproj +231 -0
- data/ext/phc-winner-argon2/vs2015/Argon2OptBench/Argon2OptBench.vcxproj.filters +69 -0
- data/ext/phc-winner-argon2/vs2015/Argon2OptDll/Argon2OptDll.vcxproj +230 -0
- data/ext/phc-winner-argon2/vs2015/Argon2OptDll/Argon2OptDll.vcxproj.filters +66 -0
- data/ext/phc-winner-argon2/vs2015/Argon2OptGenKAT/Argon2OptGenKAT.vcxproj +244 -0
- data/ext/phc-winner-argon2/vs2015/Argon2OptGenKAT/Argon2OptGenKAT.vcxproj.filters +72 -0
- data/ext/phc-winner-argon2/vs2015/Argon2OptTestCI/Argon2OptTestCI.vcxproj +235 -0
- data/ext/phc-winner-argon2/vs2015/Argon2OptTestCI/Argon2OptTestCI.vcxproj.filters +69 -0
- data/ext/phc-winner-argon2/vs2015/Argon2Ref/Argon2Ref.vcxproj +243 -0
- data/ext/phc-winner-argon2/vs2015/Argon2Ref/Argon2Ref.vcxproj.filters +69 -0
- data/ext/phc-winner-argon2/vs2015/Argon2RefBench/Argon2RefBench.vcxproj +231 -0
- data/ext/phc-winner-argon2/vs2015/Argon2RefBench/Argon2RefBench.vcxproj.filters +69 -0
- data/ext/phc-winner-argon2/vs2015/Argon2RefDll/Argon2RefDll.vcxproj +230 -0
- data/ext/phc-winner-argon2/vs2015/Argon2RefDll/Argon2RefDll.vcxproj.filters +66 -0
- data/ext/phc-winner-argon2/vs2015/Argon2RefGenKAT/Argon2RefGenKAT.vcxproj +232 -0
- data/ext/phc-winner-argon2/vs2015/Argon2RefGenKAT/Argon2RefGenKAT.vcxproj.filters +72 -0
- data/ext/phc-winner-argon2/vs2015/Argon2RefTestCI/Argon2RefTestCI.vcxproj +231 -0
- data/ext/phc-winner-argon2/vs2015/Argon2RefTestCI/Argon2RefTestCI.vcxproj.filters +69 -0
- data/lib/argon2/ffi_engine.rb +4 -4
- data/lib/argon2/password.rb +28 -5
- data/lib/argon2/version.rb +1 -1
- data/sorcery-argon2.gemspec +3 -2
- metadata +91 -8
@@ -0,0 +1,18 @@
|
|
1
|
+
# libargon2 info for pkg-config
|
2
|
+
## Template for downstream installers:
|
3
|
+
## - replace @UPSTREAM_VER@ with current version, e.g. '20160406'
|
4
|
+
## - replace @HOST_MULTIARCH@ with target arch lib, e.g. 'lib', 'lib/x86_64-linux-gnu' or 'lib64'
|
5
|
+
## - replace @PREFIX@ with install path, e.g. '/usr', '/usr/local', '/usr/pkg'
|
6
|
+
## - replace @INCLUDE@ with incluse path, e.g. 'include' or 'include/argon2'
|
7
|
+
|
8
|
+
prefix=@PREFIX@
|
9
|
+
exec_prefix=${prefix}
|
10
|
+
libdir=${prefix}/@HOST_MULTIARCH@
|
11
|
+
includedir=${prefix}/@INCLUDE@
|
12
|
+
|
13
|
+
Name: libargon2
|
14
|
+
Description: Development libraries for libargon2
|
15
|
+
Version: @UPSTREAM_VER@
|
16
|
+
Libs: -L${libdir} -largon2 @EXTRA_LIBS@
|
17
|
+
Cflags: -I${includedir}
|
18
|
+
URL: https://github.com/P-H-C/phc-winner-argon2
|
@@ -0,0 +1,57 @@
|
|
1
|
+
.TH ARGON2 "1" "April 2016" "argon2 " "User Commands"
|
2
|
+
|
3
|
+
.SH NAME
|
4
|
+
argon2 \- generate argon2 hashes
|
5
|
+
|
6
|
+
.SH SYNOPSIS
|
7
|
+
.B argon2 salt
|
8
|
+
.RB [ OPTIONS ]
|
9
|
+
|
10
|
+
.SH DESCRIPTION
|
11
|
+
Generate Argon2 hashes from the command line.
|
12
|
+
|
13
|
+
The supplied salt (the first argument to the command) must be at least
|
14
|
+
8 octets in length, and the password is supplied on standard input.
|
15
|
+
|
16
|
+
By default, this uses Argon2i variant (where memory access is
|
17
|
+
independent of secret data) which is the preferred one for password
|
18
|
+
hashing and password-based key derivation.
|
19
|
+
|
20
|
+
.SH OPTIONS
|
21
|
+
.TP
|
22
|
+
.B \-h
|
23
|
+
Display tool usage
|
24
|
+
.TP
|
25
|
+
.B \-d
|
26
|
+
Use Argon2d instead of Argon2i (Argon2i is the default)
|
27
|
+
.TP
|
28
|
+
.B \-id
|
29
|
+
Use Argon2id instead of Argon2i (Argon2i is the default)
|
30
|
+
.TP
|
31
|
+
.BI \-t " N"
|
32
|
+
Sets the number of iterations to N (default = 3)
|
33
|
+
.TP
|
34
|
+
.BI \-m " N"
|
35
|
+
Sets the memory usage of 2^N KiB (default = 12)
|
36
|
+
.TP
|
37
|
+
.BI \-p " N"
|
38
|
+
Sets parallelism to N threads (default = 1)
|
39
|
+
.TP
|
40
|
+
.BI \-l " N"
|
41
|
+
Sets hash output length to N bytes (default = 32)
|
42
|
+
.TP
|
43
|
+
.B \-e
|
44
|
+
Output only encoded hash
|
45
|
+
.TP
|
46
|
+
.B \-r
|
47
|
+
Output only the raw bytes of the hash
|
48
|
+
.TP
|
49
|
+
.B \-v (10|13)
|
50
|
+
Argon2 version (defaults to the most recent version, currently 13)
|
51
|
+
|
52
|
+
.SH COPYRIGHT
|
53
|
+
This manpage was written by \fBDaniel Kahn Gillmor\fR for the Debian
|
54
|
+
distribution (but may be used by others). It is released, like the
|
55
|
+
rest of this Argon2 implementation, under a dual license. You may use this work
|
56
|
+
under the terms of a Creative Commons CC0 1.0 License/Waiver or the Apache
|
57
|
+
Public License 2.0, at your option.
|
@@ -0,0 +1,452 @@
|
|
1
|
+
/*
|
2
|
+
* Argon2 reference source code package - reference C implementations
|
3
|
+
*
|
4
|
+
* Copyright 2015
|
5
|
+
* Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
|
6
|
+
*
|
7
|
+
* You may use this work under the terms of a Creative Commons CC0 1.0
|
8
|
+
* License/Waiver or the Apache Public License 2.0, at your option. The terms of
|
9
|
+
* these licenses can be found at:
|
10
|
+
*
|
11
|
+
* - CC0 1.0 Universal : https://creativecommons.org/publicdomain/zero/1.0
|
12
|
+
* - Apache 2.0 : https://www.apache.org/licenses/LICENSE-2.0
|
13
|
+
*
|
14
|
+
* You should have received a copy of both of these licenses along with this
|
15
|
+
* software. If not, they may be obtained at the above URLs.
|
16
|
+
*/
|
17
|
+
|
18
|
+
#include <string.h>
|
19
|
+
#include <stdlib.h>
|
20
|
+
#include <stdio.h>
|
21
|
+
|
22
|
+
#include "argon2.h"
|
23
|
+
#include "encoding.h"
|
24
|
+
#include "core.h"
|
25
|
+
|
26
|
+
const char *argon2_type2string(argon2_type type, int uppercase) {
|
27
|
+
switch (type) {
|
28
|
+
case Argon2_d:
|
29
|
+
return uppercase ? "Argon2d" : "argon2d";
|
30
|
+
case Argon2_i:
|
31
|
+
return uppercase ? "Argon2i" : "argon2i";
|
32
|
+
case Argon2_id:
|
33
|
+
return uppercase ? "Argon2id" : "argon2id";
|
34
|
+
}
|
35
|
+
|
36
|
+
return NULL;
|
37
|
+
}
|
38
|
+
|
39
|
+
int argon2_ctx(argon2_context *context, argon2_type type) {
|
40
|
+
/* 1. Validate all inputs */
|
41
|
+
int result = validate_inputs(context);
|
42
|
+
uint32_t memory_blocks, segment_length;
|
43
|
+
argon2_instance_t instance;
|
44
|
+
|
45
|
+
if (ARGON2_OK != result) {
|
46
|
+
return result;
|
47
|
+
}
|
48
|
+
|
49
|
+
if (Argon2_d != type && Argon2_i != type && Argon2_id != type) {
|
50
|
+
return ARGON2_INCORRECT_TYPE;
|
51
|
+
}
|
52
|
+
|
53
|
+
/* 2. Align memory size */
|
54
|
+
/* Minimum memory_blocks = 8L blocks, where L is the number of lanes */
|
55
|
+
memory_blocks = context->m_cost;
|
56
|
+
|
57
|
+
if (memory_blocks < 2 * ARGON2_SYNC_POINTS * context->lanes) {
|
58
|
+
memory_blocks = 2 * ARGON2_SYNC_POINTS * context->lanes;
|
59
|
+
}
|
60
|
+
|
61
|
+
segment_length = memory_blocks / (context->lanes * ARGON2_SYNC_POINTS);
|
62
|
+
/* Ensure that all segments have equal length */
|
63
|
+
memory_blocks = segment_length * (context->lanes * ARGON2_SYNC_POINTS);
|
64
|
+
|
65
|
+
instance.version = context->version;
|
66
|
+
instance.memory = NULL;
|
67
|
+
instance.passes = context->t_cost;
|
68
|
+
instance.memory_blocks = memory_blocks;
|
69
|
+
instance.segment_length = segment_length;
|
70
|
+
instance.lane_length = segment_length * ARGON2_SYNC_POINTS;
|
71
|
+
instance.lanes = context->lanes;
|
72
|
+
instance.threads = context->threads;
|
73
|
+
instance.type = type;
|
74
|
+
|
75
|
+
if (instance.threads > instance.lanes) {
|
76
|
+
instance.threads = instance.lanes;
|
77
|
+
}
|
78
|
+
|
79
|
+
/* 3. Initialization: Hashing inputs, allocating memory, filling first
|
80
|
+
* blocks
|
81
|
+
*/
|
82
|
+
result = initialize(&instance, context);
|
83
|
+
|
84
|
+
if (ARGON2_OK != result) {
|
85
|
+
return result;
|
86
|
+
}
|
87
|
+
|
88
|
+
/* 4. Filling memory */
|
89
|
+
result = fill_memory_blocks(&instance);
|
90
|
+
|
91
|
+
if (ARGON2_OK != result) {
|
92
|
+
return result;
|
93
|
+
}
|
94
|
+
/* 5. Finalization */
|
95
|
+
finalize(context, &instance);
|
96
|
+
|
97
|
+
return ARGON2_OK;
|
98
|
+
}
|
99
|
+
|
100
|
+
int argon2_hash(const uint32_t t_cost, const uint32_t m_cost,
|
101
|
+
const uint32_t parallelism, const void *pwd,
|
102
|
+
const size_t pwdlen, const void *salt, const size_t saltlen,
|
103
|
+
void *hash, const size_t hashlen, char *encoded,
|
104
|
+
const size_t encodedlen, argon2_type type,
|
105
|
+
const uint32_t version){
|
106
|
+
|
107
|
+
argon2_context context;
|
108
|
+
int result;
|
109
|
+
uint8_t *out;
|
110
|
+
|
111
|
+
if (pwdlen > ARGON2_MAX_PWD_LENGTH) {
|
112
|
+
return ARGON2_PWD_TOO_LONG;
|
113
|
+
}
|
114
|
+
|
115
|
+
if (saltlen > ARGON2_MAX_SALT_LENGTH) {
|
116
|
+
return ARGON2_SALT_TOO_LONG;
|
117
|
+
}
|
118
|
+
|
119
|
+
if (hashlen > ARGON2_MAX_OUTLEN) {
|
120
|
+
return ARGON2_OUTPUT_TOO_LONG;
|
121
|
+
}
|
122
|
+
|
123
|
+
if (hashlen < ARGON2_MIN_OUTLEN) {
|
124
|
+
return ARGON2_OUTPUT_TOO_SHORT;
|
125
|
+
}
|
126
|
+
|
127
|
+
out = malloc(hashlen);
|
128
|
+
if (!out) {
|
129
|
+
return ARGON2_MEMORY_ALLOCATION_ERROR;
|
130
|
+
}
|
131
|
+
|
132
|
+
context.out = (uint8_t *)out;
|
133
|
+
context.outlen = (uint32_t)hashlen;
|
134
|
+
context.pwd = CONST_CAST(uint8_t *)pwd;
|
135
|
+
context.pwdlen = (uint32_t)pwdlen;
|
136
|
+
context.salt = CONST_CAST(uint8_t *)salt;
|
137
|
+
context.saltlen = (uint32_t)saltlen;
|
138
|
+
context.secret = NULL;
|
139
|
+
context.secretlen = 0;
|
140
|
+
context.ad = NULL;
|
141
|
+
context.adlen = 0;
|
142
|
+
context.t_cost = t_cost;
|
143
|
+
context.m_cost = m_cost;
|
144
|
+
context.lanes = parallelism;
|
145
|
+
context.threads = parallelism;
|
146
|
+
context.allocate_cbk = NULL;
|
147
|
+
context.free_cbk = NULL;
|
148
|
+
context.flags = ARGON2_DEFAULT_FLAGS;
|
149
|
+
context.version = version;
|
150
|
+
|
151
|
+
result = argon2_ctx(&context, type);
|
152
|
+
|
153
|
+
if (result != ARGON2_OK) {
|
154
|
+
clear_internal_memory(out, hashlen);
|
155
|
+
free(out);
|
156
|
+
return result;
|
157
|
+
}
|
158
|
+
|
159
|
+
/* if raw hash requested, write it */
|
160
|
+
if (hash) {
|
161
|
+
memcpy(hash, out, hashlen);
|
162
|
+
}
|
163
|
+
|
164
|
+
/* if encoding requested, write it */
|
165
|
+
if (encoded && encodedlen) {
|
166
|
+
if (encode_string(encoded, encodedlen, &context, type) != ARGON2_OK) {
|
167
|
+
clear_internal_memory(out, hashlen); /* wipe buffers if error */
|
168
|
+
clear_internal_memory(encoded, encodedlen);
|
169
|
+
free(out);
|
170
|
+
return ARGON2_ENCODING_FAIL;
|
171
|
+
}
|
172
|
+
}
|
173
|
+
clear_internal_memory(out, hashlen);
|
174
|
+
free(out);
|
175
|
+
|
176
|
+
return ARGON2_OK;
|
177
|
+
}
|
178
|
+
|
179
|
+
int argon2i_hash_encoded(const uint32_t t_cost, const uint32_t m_cost,
|
180
|
+
const uint32_t parallelism, const void *pwd,
|
181
|
+
const size_t pwdlen, const void *salt,
|
182
|
+
const size_t saltlen, const size_t hashlen,
|
183
|
+
char *encoded, const size_t encodedlen) {
|
184
|
+
|
185
|
+
return argon2_hash(t_cost, m_cost, parallelism, pwd, pwdlen, salt, saltlen,
|
186
|
+
NULL, hashlen, encoded, encodedlen, Argon2_i,
|
187
|
+
ARGON2_VERSION_NUMBER);
|
188
|
+
}
|
189
|
+
|
190
|
+
int argon2i_hash_raw(const uint32_t t_cost, const uint32_t m_cost,
|
191
|
+
const uint32_t parallelism, const void *pwd,
|
192
|
+
const size_t pwdlen, const void *salt,
|
193
|
+
const size_t saltlen, void *hash, const size_t hashlen) {
|
194
|
+
|
195
|
+
return argon2_hash(t_cost, m_cost, parallelism, pwd, pwdlen, salt, saltlen,
|
196
|
+
hash, hashlen, NULL, 0, Argon2_i, ARGON2_VERSION_NUMBER);
|
197
|
+
}
|
198
|
+
|
199
|
+
int argon2d_hash_encoded(const uint32_t t_cost, const uint32_t m_cost,
|
200
|
+
const uint32_t parallelism, const void *pwd,
|
201
|
+
const size_t pwdlen, const void *salt,
|
202
|
+
const size_t saltlen, const size_t hashlen,
|
203
|
+
char *encoded, const size_t encodedlen) {
|
204
|
+
|
205
|
+
return argon2_hash(t_cost, m_cost, parallelism, pwd, pwdlen, salt, saltlen,
|
206
|
+
NULL, hashlen, encoded, encodedlen, Argon2_d,
|
207
|
+
ARGON2_VERSION_NUMBER);
|
208
|
+
}
|
209
|
+
|
210
|
+
int argon2d_hash_raw(const uint32_t t_cost, const uint32_t m_cost,
|
211
|
+
const uint32_t parallelism, const void *pwd,
|
212
|
+
const size_t pwdlen, const void *salt,
|
213
|
+
const size_t saltlen, void *hash, const size_t hashlen) {
|
214
|
+
|
215
|
+
return argon2_hash(t_cost, m_cost, parallelism, pwd, pwdlen, salt, saltlen,
|
216
|
+
hash, hashlen, NULL, 0, Argon2_d, ARGON2_VERSION_NUMBER);
|
217
|
+
}
|
218
|
+
|
219
|
+
int argon2id_hash_encoded(const uint32_t t_cost, const uint32_t m_cost,
|
220
|
+
const uint32_t parallelism, const void *pwd,
|
221
|
+
const size_t pwdlen, const void *salt,
|
222
|
+
const size_t saltlen, const size_t hashlen,
|
223
|
+
char *encoded, const size_t encodedlen) {
|
224
|
+
|
225
|
+
return argon2_hash(t_cost, m_cost, parallelism, pwd, pwdlen, salt, saltlen,
|
226
|
+
NULL, hashlen, encoded, encodedlen, Argon2_id,
|
227
|
+
ARGON2_VERSION_NUMBER);
|
228
|
+
}
|
229
|
+
|
230
|
+
int argon2id_hash_raw(const uint32_t t_cost, const uint32_t m_cost,
|
231
|
+
const uint32_t parallelism, const void *pwd,
|
232
|
+
const size_t pwdlen, const void *salt,
|
233
|
+
const size_t saltlen, void *hash, const size_t hashlen) {
|
234
|
+
return argon2_hash(t_cost, m_cost, parallelism, pwd, pwdlen, salt, saltlen,
|
235
|
+
hash, hashlen, NULL, 0, Argon2_id,
|
236
|
+
ARGON2_VERSION_NUMBER);
|
237
|
+
}
|
238
|
+
|
239
|
+
static int argon2_compare(const uint8_t *b1, const uint8_t *b2, size_t len) {
|
240
|
+
size_t i;
|
241
|
+
uint8_t d = 0U;
|
242
|
+
|
243
|
+
for (i = 0U; i < len; i++) {
|
244
|
+
d |= b1[i] ^ b2[i];
|
245
|
+
}
|
246
|
+
return (int)((1 & ((d - 1) >> 8)) - 1);
|
247
|
+
}
|
248
|
+
|
249
|
+
int argon2_verify(const char *encoded, const void *pwd, const size_t pwdlen,
|
250
|
+
argon2_type type) {
|
251
|
+
|
252
|
+
argon2_context ctx;
|
253
|
+
uint8_t *desired_result = NULL;
|
254
|
+
|
255
|
+
int ret = ARGON2_OK;
|
256
|
+
|
257
|
+
size_t encoded_len;
|
258
|
+
uint32_t max_field_len;
|
259
|
+
|
260
|
+
if (pwdlen > ARGON2_MAX_PWD_LENGTH) {
|
261
|
+
return ARGON2_PWD_TOO_LONG;
|
262
|
+
}
|
263
|
+
|
264
|
+
if (encoded == NULL) {
|
265
|
+
return ARGON2_DECODING_FAIL;
|
266
|
+
}
|
267
|
+
|
268
|
+
encoded_len = strlen(encoded);
|
269
|
+
if (encoded_len > UINT32_MAX) {
|
270
|
+
return ARGON2_DECODING_FAIL;
|
271
|
+
}
|
272
|
+
|
273
|
+
/* No field can be longer than the encoded length */
|
274
|
+
max_field_len = (uint32_t)encoded_len;
|
275
|
+
|
276
|
+
ctx.saltlen = max_field_len;
|
277
|
+
ctx.outlen = max_field_len;
|
278
|
+
|
279
|
+
ctx.salt = malloc(ctx.saltlen);
|
280
|
+
ctx.out = malloc(ctx.outlen);
|
281
|
+
if (!ctx.salt || !ctx.out) {
|
282
|
+
ret = ARGON2_MEMORY_ALLOCATION_ERROR;
|
283
|
+
goto fail;
|
284
|
+
}
|
285
|
+
|
286
|
+
ctx.pwd = (uint8_t *)pwd;
|
287
|
+
ctx.pwdlen = (uint32_t)pwdlen;
|
288
|
+
|
289
|
+
ret = decode_string(&ctx, encoded, type);
|
290
|
+
if (ret != ARGON2_OK) {
|
291
|
+
goto fail;
|
292
|
+
}
|
293
|
+
|
294
|
+
/* Set aside the desired result, and get a new buffer. */
|
295
|
+
desired_result = ctx.out;
|
296
|
+
ctx.out = malloc(ctx.outlen);
|
297
|
+
if (!ctx.out) {
|
298
|
+
ret = ARGON2_MEMORY_ALLOCATION_ERROR;
|
299
|
+
goto fail;
|
300
|
+
}
|
301
|
+
|
302
|
+
ret = argon2_verify_ctx(&ctx, (char *)desired_result, type);
|
303
|
+
if (ret != ARGON2_OK) {
|
304
|
+
goto fail;
|
305
|
+
}
|
306
|
+
|
307
|
+
fail:
|
308
|
+
free(ctx.salt);
|
309
|
+
free(ctx.out);
|
310
|
+
free(desired_result);
|
311
|
+
|
312
|
+
return ret;
|
313
|
+
}
|
314
|
+
|
315
|
+
int argon2i_verify(const char *encoded, const void *pwd, const size_t pwdlen) {
|
316
|
+
|
317
|
+
return argon2_verify(encoded, pwd, pwdlen, Argon2_i);
|
318
|
+
}
|
319
|
+
|
320
|
+
int argon2d_verify(const char *encoded, const void *pwd, const size_t pwdlen) {
|
321
|
+
|
322
|
+
return argon2_verify(encoded, pwd, pwdlen, Argon2_d);
|
323
|
+
}
|
324
|
+
|
325
|
+
int argon2id_verify(const char *encoded, const void *pwd, const size_t pwdlen) {
|
326
|
+
|
327
|
+
return argon2_verify(encoded, pwd, pwdlen, Argon2_id);
|
328
|
+
}
|
329
|
+
|
330
|
+
int argon2d_ctx(argon2_context *context) {
|
331
|
+
return argon2_ctx(context, Argon2_d);
|
332
|
+
}
|
333
|
+
|
334
|
+
int argon2i_ctx(argon2_context *context) {
|
335
|
+
return argon2_ctx(context, Argon2_i);
|
336
|
+
}
|
337
|
+
|
338
|
+
int argon2id_ctx(argon2_context *context) {
|
339
|
+
return argon2_ctx(context, Argon2_id);
|
340
|
+
}
|
341
|
+
|
342
|
+
int argon2_verify_ctx(argon2_context *context, const char *hash,
|
343
|
+
argon2_type type) {
|
344
|
+
int ret = argon2_ctx(context, type);
|
345
|
+
if (ret != ARGON2_OK) {
|
346
|
+
return ret;
|
347
|
+
}
|
348
|
+
|
349
|
+
if (argon2_compare((uint8_t *)hash, context->out, context->outlen)) {
|
350
|
+
return ARGON2_VERIFY_MISMATCH;
|
351
|
+
}
|
352
|
+
|
353
|
+
return ARGON2_OK;
|
354
|
+
}
|
355
|
+
|
356
|
+
int argon2d_verify_ctx(argon2_context *context, const char *hash) {
|
357
|
+
return argon2_verify_ctx(context, hash, Argon2_d);
|
358
|
+
}
|
359
|
+
|
360
|
+
int argon2i_verify_ctx(argon2_context *context, const char *hash) {
|
361
|
+
return argon2_verify_ctx(context, hash, Argon2_i);
|
362
|
+
}
|
363
|
+
|
364
|
+
int argon2id_verify_ctx(argon2_context *context, const char *hash) {
|
365
|
+
return argon2_verify_ctx(context, hash, Argon2_id);
|
366
|
+
}
|
367
|
+
|
368
|
+
const char *argon2_error_message(int error_code) {
|
369
|
+
switch (error_code) {
|
370
|
+
case ARGON2_OK:
|
371
|
+
return "OK";
|
372
|
+
case ARGON2_OUTPUT_PTR_NULL:
|
373
|
+
return "Output pointer is NULL";
|
374
|
+
case ARGON2_OUTPUT_TOO_SHORT:
|
375
|
+
return "Output is too short";
|
376
|
+
case ARGON2_OUTPUT_TOO_LONG:
|
377
|
+
return "Output is too long";
|
378
|
+
case ARGON2_PWD_TOO_SHORT:
|
379
|
+
return "Password is too short";
|
380
|
+
case ARGON2_PWD_TOO_LONG:
|
381
|
+
return "Password is too long";
|
382
|
+
case ARGON2_SALT_TOO_SHORT:
|
383
|
+
return "Salt is too short";
|
384
|
+
case ARGON2_SALT_TOO_LONG:
|
385
|
+
return "Salt is too long";
|
386
|
+
case ARGON2_AD_TOO_SHORT:
|
387
|
+
return "Associated data is too short";
|
388
|
+
case ARGON2_AD_TOO_LONG:
|
389
|
+
return "Associated data is too long";
|
390
|
+
case ARGON2_SECRET_TOO_SHORT:
|
391
|
+
return "Secret is too short";
|
392
|
+
case ARGON2_SECRET_TOO_LONG:
|
393
|
+
return "Secret is too long";
|
394
|
+
case ARGON2_TIME_TOO_SMALL:
|
395
|
+
return "Time cost is too small";
|
396
|
+
case ARGON2_TIME_TOO_LARGE:
|
397
|
+
return "Time cost is too large";
|
398
|
+
case ARGON2_MEMORY_TOO_LITTLE:
|
399
|
+
return "Memory cost is too small";
|
400
|
+
case ARGON2_MEMORY_TOO_MUCH:
|
401
|
+
return "Memory cost is too large";
|
402
|
+
case ARGON2_LANES_TOO_FEW:
|
403
|
+
return "Too few lanes";
|
404
|
+
case ARGON2_LANES_TOO_MANY:
|
405
|
+
return "Too many lanes";
|
406
|
+
case ARGON2_PWD_PTR_MISMATCH:
|
407
|
+
return "Password pointer is NULL, but password length is not 0";
|
408
|
+
case ARGON2_SALT_PTR_MISMATCH:
|
409
|
+
return "Salt pointer is NULL, but salt length is not 0";
|
410
|
+
case ARGON2_SECRET_PTR_MISMATCH:
|
411
|
+
return "Secret pointer is NULL, but secret length is not 0";
|
412
|
+
case ARGON2_AD_PTR_MISMATCH:
|
413
|
+
return "Associated data pointer is NULL, but ad length is not 0";
|
414
|
+
case ARGON2_MEMORY_ALLOCATION_ERROR:
|
415
|
+
return "Memory allocation error";
|
416
|
+
case ARGON2_FREE_MEMORY_CBK_NULL:
|
417
|
+
return "The free memory callback is NULL";
|
418
|
+
case ARGON2_ALLOCATE_MEMORY_CBK_NULL:
|
419
|
+
return "The allocate memory callback is NULL";
|
420
|
+
case ARGON2_INCORRECT_PARAMETER:
|
421
|
+
return "Argon2_Context context is NULL";
|
422
|
+
case ARGON2_INCORRECT_TYPE:
|
423
|
+
return "There is no such version of Argon2";
|
424
|
+
case ARGON2_OUT_PTR_MISMATCH:
|
425
|
+
return "Output pointer mismatch";
|
426
|
+
case ARGON2_THREADS_TOO_FEW:
|
427
|
+
return "Not enough threads";
|
428
|
+
case ARGON2_THREADS_TOO_MANY:
|
429
|
+
return "Too many threads";
|
430
|
+
case ARGON2_MISSING_ARGS:
|
431
|
+
return "Missing arguments";
|
432
|
+
case ARGON2_ENCODING_FAIL:
|
433
|
+
return "Encoding failed";
|
434
|
+
case ARGON2_DECODING_FAIL:
|
435
|
+
return "Decoding failed";
|
436
|
+
case ARGON2_THREAD_FAIL:
|
437
|
+
return "Threading failure";
|
438
|
+
case ARGON2_DECODING_LENGTH_FAIL:
|
439
|
+
return "Some of encoded parameters are too long or too short";
|
440
|
+
case ARGON2_VERIFY_MISMATCH:
|
441
|
+
return "The password does not match the supplied hash";
|
442
|
+
default:
|
443
|
+
return "Unknown error code";
|
444
|
+
}
|
445
|
+
}
|
446
|
+
|
447
|
+
size_t argon2_encodedlen(uint32_t t_cost, uint32_t m_cost, uint32_t parallelism,
|
448
|
+
uint32_t saltlen, uint32_t hashlen, argon2_type type) {
|
449
|
+
return strlen("$$v=$m=,t=,p=$$") + strlen(argon2_type2string(type, 0)) +
|
450
|
+
numlen(t_cost) + numlen(m_cost) + numlen(parallelism) +
|
451
|
+
b64len(saltlen) + b64len(hashlen) + numlen(ARGON2_VERSION_NUMBER) + 1;
|
452
|
+
}
|
@@ -0,0 +1,111 @@
|
|
1
|
+
/*
|
2
|
+
* Argon2 reference source code package - reference C implementations
|
3
|
+
*
|
4
|
+
* Copyright 2015
|
5
|
+
* Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
|
6
|
+
*
|
7
|
+
* You may use this work under the terms of a Creative Commons CC0 1.0
|
8
|
+
* License/Waiver or the Apache Public License 2.0, at your option. The terms of
|
9
|
+
* these licenses can be found at:
|
10
|
+
*
|
11
|
+
* - CC0 1.0 Universal : https://creativecommons.org/publicdomain/zero/1.0
|
12
|
+
* - Apache 2.0 : https://www.apache.org/licenses/LICENSE-2.0
|
13
|
+
*
|
14
|
+
* You should have received a copy of both of these licenses along with this
|
15
|
+
* software. If not, they may be obtained at the above URLs.
|
16
|
+
*/
|
17
|
+
|
18
|
+
#include <stdio.h>
|
19
|
+
#include <stdint.h>
|
20
|
+
#include <stdlib.h>
|
21
|
+
#include <string.h>
|
22
|
+
#include <time.h>
|
23
|
+
#ifdef _WIN32
|
24
|
+
#include <intrin.h>
|
25
|
+
#endif
|
26
|
+
|
27
|
+
#include "argon2.h"
|
28
|
+
|
29
|
+
static uint64_t rdtsc(void) {
|
30
|
+
#ifdef _WIN32
|
31
|
+
return __rdtsc();
|
32
|
+
#else
|
33
|
+
#if defined(__amd64__) || defined(__x86_64__)
|
34
|
+
uint64_t rax, rdx;
|
35
|
+
__asm__ __volatile__("rdtsc" : "=a"(rax), "=d"(rdx) : :);
|
36
|
+
return (rdx << 32) | rax;
|
37
|
+
#elif defined(__i386__) || defined(__i386) || defined(__X86__)
|
38
|
+
uint64_t rax;
|
39
|
+
__asm__ __volatile__("rdtsc" : "=A"(rax) : :);
|
40
|
+
return rax;
|
41
|
+
#else
|
42
|
+
#error "Not implemented!"
|
43
|
+
#endif
|
44
|
+
#endif
|
45
|
+
}
|
46
|
+
|
47
|
+
/*
|
48
|
+
* Benchmarks Argon2 with salt length 16, password length 16, t_cost 3,
|
49
|
+
and different m_cost and threads
|
50
|
+
*/
|
51
|
+
static void benchmark() {
|
52
|
+
#define BENCH_OUTLEN 16
|
53
|
+
#define BENCH_INLEN 16
|
54
|
+
const uint32_t inlen = BENCH_INLEN;
|
55
|
+
const unsigned outlen = BENCH_OUTLEN;
|
56
|
+
unsigned char out[BENCH_OUTLEN];
|
57
|
+
unsigned char pwd_array[BENCH_INLEN];
|
58
|
+
unsigned char salt_array[BENCH_INLEN];
|
59
|
+
#undef BENCH_INLEN
|
60
|
+
#undef BENCH_OUTLEN
|
61
|
+
|
62
|
+
uint32_t t_cost = 3;
|
63
|
+
uint32_t m_cost;
|
64
|
+
uint32_t thread_test[4] = {1, 2, 4, 8};
|
65
|
+
argon2_type types[3] = {Argon2_i, Argon2_d, Argon2_id};
|
66
|
+
|
67
|
+
memset(pwd_array, 0, inlen);
|
68
|
+
memset(salt_array, 1, inlen);
|
69
|
+
|
70
|
+
for (m_cost = (uint32_t)1 << 10; m_cost <= (uint32_t)1 << 22; m_cost *= 2) {
|
71
|
+
unsigned i;
|
72
|
+
for (i = 0; i < 4; ++i) {
|
73
|
+
double run_time = 0;
|
74
|
+
uint32_t thread_n = thread_test[i];
|
75
|
+
|
76
|
+
unsigned j;
|
77
|
+
for (j = 0; j < 3; ++j) {
|
78
|
+
clock_t start_time, stop_time;
|
79
|
+
uint64_t start_cycles, stop_cycles;
|
80
|
+
uint64_t delta;
|
81
|
+
double mcycles;
|
82
|
+
|
83
|
+
argon2_type type = types[j];
|
84
|
+
start_time = clock();
|
85
|
+
start_cycles = rdtsc();
|
86
|
+
|
87
|
+
argon2_hash(t_cost, m_cost, thread_n, pwd_array, inlen,
|
88
|
+
salt_array, inlen, out, outlen, NULL, 0, type,
|
89
|
+
ARGON2_VERSION_NUMBER);
|
90
|
+
|
91
|
+
stop_cycles = rdtsc();
|
92
|
+
stop_time = clock();
|
93
|
+
|
94
|
+
delta = (stop_cycles - start_cycles) / (m_cost);
|
95
|
+
mcycles = (double)(stop_cycles - start_cycles) / (1UL << 20);
|
96
|
+
run_time += ((double)stop_time - start_time) / (CLOCKS_PER_SEC);
|
97
|
+
|
98
|
+
printf("%s %d iterations %d MiB %d threads: %2.2f cpb %2.2f "
|
99
|
+
"Mcycles \n", argon2_type2string(type, 1), t_cost,
|
100
|
+
m_cost >> 10, thread_n, (float)delta / 1024, mcycles);
|
101
|
+
}
|
102
|
+
|
103
|
+
printf("%2.4f seconds\n\n", run_time);
|
104
|
+
}
|
105
|
+
}
|
106
|
+
}
|
107
|
+
|
108
|
+
int main() {
|
109
|
+
benchmark();
|
110
|
+
return ARGON2_OK;
|
111
|
+
}
|