crypt-argon2.c revision 1.11 1 1.9 jhigh /*
2 1.9 jhigh * Copyright (c) 2009 The NetBSD Foundation, Inc.
3 1.9 jhigh * All rights reserved.
4 1.9 jhigh *
5 1.9 jhigh * Redistribution and use in source and binary forms, with or without
6 1.9 jhigh * modification, are permitted provided that the following conditions
7 1.9 jhigh * are met:
8 1.9 jhigh * 1. Redistributions of source code must retain the above copyright
9 1.9 jhigh * notice, this list of conditions and the following disclaimer.
10 1.9 jhigh * 2. Redistributions in binary form must reproduce the above copyright
11 1.9 jhigh * notice, this list of conditions and the following disclaimer in the
12 1.9 jhigh * documentation and/or other materials provided with the distribution.
13 1.9 jhigh *
14 1.9 jhigh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
15 1.9 jhigh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16 1.9 jhigh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 1.9 jhigh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
18 1.9 jhigh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 1.9 jhigh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 1.9 jhigh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 1.9 jhigh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 1.9 jhigh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 1.9 jhigh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 1.9 jhigh * POSSIBILITY OF SUCH DAMAGE.
25 1.9 jhigh */
26 1.9 jhigh
27 1.11 nia #include <sys/resource.h>
28 1.11 nia #include <sys/param.h>
29 1.11 nia #include <sys/sysctl.h>
30 1.11 nia #include <sys/syslimits.h>
31 1.11 nia
32 1.1 jhigh #include <stdlib.h>
33 1.1 jhigh #include <stdio.h>
34 1.1 jhigh #include <unistd.h>
35 1.1 jhigh #include <stdio.h>
36 1.1 jhigh #include <string.h>
37 1.1 jhigh #include <time.h>
38 1.1 jhigh #include <pwd.h>
39 1.1 jhigh #include <errno.h>
40 1.1 jhigh #include <argon2.h>
41 1.1 jhigh
42 1.1 jhigh #include <err.h>
43 1.1 jhigh #include "crypt.h"
44 1.1 jhigh
45 1.11 nia crypt_private int
46 1.11 nia estimate_argon2_params(argon2_type, uint32_t *,
47 1.11 nia uint32_t *, uint32_t *);
48 1.11 nia
49 1.1 jhigh /* defaults pulled from run.c */
50 1.1 jhigh #define HASHLEN 32
51 1.1 jhigh #define T_COST_DEF 3
52 1.1 jhigh #define LOG_M_COST_DEF 12 /* 2^12 = 4 MiB */
53 1.1 jhigh #define LANES_DEF 1
54 1.1 jhigh #define THREADS_DEF 1
55 1.1 jhigh #define OUTLEN_DEF 32
56 1.1 jhigh #define MAX_PASS_LEN 128
57 1.1 jhigh
58 1.1 jhigh #define ARGON2_CONTEXT_INITIALIZER \
59 1.1 jhigh {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
60 1.1 jhigh T_COST_DEF, LOG_M_COST_DEF,\
61 1.1 jhigh LANES_DEF, THREADS_DEF, \
62 1.1 jhigh ARGON2_VERSION_NUMBER, 0, 0, ARGON2_DEFAULT_FLAGS}
63 1.1 jhigh
64 1.1 jhigh #define ARGON2_ARGON2_STR "argon2"
65 1.1 jhigh #define ARGON2_ARGON2I_STR "argon2i"
66 1.1 jhigh #define ARGON2_ARGON2D_STR "argon2d"
67 1.1 jhigh #define ARGON2_ARGON2ID_STR "argon2id"
68 1.1 jhigh
69 1.11 nia /*
70 1.11 nia * Unpadded Base64 calculations are taken from the Apache2/CC-0
71 1.11 nia * licensed libargon2 for compatibility
72 1.11 nia */
73 1.7 nia
74 1.7 nia /*
75 1.7 nia * Some macros for constant-time comparisons. These work over values in
76 1.7 nia * the 0..255 range. Returned value is 0x00 on "false", 0xFF on "true".
77 1.7 nia */
78 1.7 nia #define EQ(x, y) ((((0U - ((unsigned)(x) ^ (unsigned)(y))) >> 8) & 0xFF) ^ 0xFF)
79 1.7 nia #define GT(x, y) ((((unsigned)(y) - (unsigned)(x)) >> 8) & 0xFF)
80 1.7 nia #define GE(x, y) (GT(y, x) ^ 0xFF)
81 1.7 nia #define LT(x, y) GT(y, x)
82 1.7 nia #define LE(x, y) GE(y, x)
83 1.7 nia
84 1.7 nia static unsigned
85 1.7 nia b64_char_to_byte(int c)
86 1.7 nia {
87 1.7 nia unsigned x;
88 1.7 nia
89 1.7 nia x = (GE(c, 'A') & LE(c, 'Z') & (c - 'A')) |
90 1.7 nia (GE(c, 'a') & LE(c, 'z') & (c - ('a' - 26))) |
91 1.7 nia (GE(c, '0') & LE(c, '9') & (c - ('0' - 52))) | (EQ(c, '+') & 62) |
92 1.7 nia (EQ(c, '/') & 63);
93 1.7 nia return x | (EQ(x, 0) & (EQ(c, 'A') ^ 0xFF));
94 1.7 nia }
95 1.7 nia
96 1.7 nia static const char *
97 1.7 nia from_base64(void *dst, size_t *dst_len, const char *src)
98 1.7 nia {
99 1.7 nia size_t len;
100 1.7 nia unsigned char *buf;
101 1.7 nia unsigned acc, acc_len;
102 1.7 nia
103 1.7 nia buf = (unsigned char *)dst;
104 1.7 nia len = 0;
105 1.7 nia acc = 0;
106 1.7 nia acc_len = 0;
107 1.7 nia for (;;) {
108 1.7 nia unsigned d;
109 1.7 nia
110 1.7 nia d = b64_char_to_byte(*src);
111 1.7 nia if (d == 0xFF) {
112 1.7 nia break;
113 1.7 nia }
114 1.7 nia src++;
115 1.7 nia acc = (acc << 6) + d;
116 1.7 nia acc_len += 6;
117 1.7 nia if (acc_len >= 8) {
118 1.7 nia acc_len -= 8;
119 1.7 nia if ((len++) >= *dst_len) {
120 1.7 nia return NULL;
121 1.7 nia }
122 1.7 nia *buf++ = (acc >> acc_len) & 0xFF;
123 1.7 nia }
124 1.7 nia }
125 1.7 nia
126 1.7 nia /*
127 1.7 nia * If the input length is equal to 1 modulo 4 (which is
128 1.7 nia * invalid), then there will remain 6 unprocessed bits;
129 1.7 nia * otherwise, only 0, 2 or 4 bits are buffered. The buffered
130 1.7 nia * bits must also all be zero.
131 1.7 nia */
132 1.7 nia if (acc_len > 4 || (acc & (((unsigned)1 << acc_len) - 1)) != 0) {
133 1.7 nia return NULL;
134 1.7 nia }
135 1.7 nia *dst_len = len;
136 1.7 nia return src;
137 1.7 nia }
138 1.7 nia
139 1.11 nia /*
140 1.11 nia * Used to find default parameters that perform well on the host
141 1.11 nia * machine. Inputs should dereference to either 0 (to estimate),
142 1.11 nia * or desired value.
143 1.11 nia */
144 1.11 nia crypt_private int
145 1.11 nia estimate_argon2_params(argon2_type atype, uint32_t *etime,
146 1.11 nia uint32_t *ememory, uint32_t *ethreads)
147 1.11 nia {
148 1.11 nia const int mib[] = { CTL_HW, HW_USERMEM64 };
149 1.11 nia struct timespec tp1, tp2, delta;
150 1.11 nia char tmp_salt[16];
151 1.11 nia char tmp_pwd[16];
152 1.11 nia uint32_t tmp_hash[32];
153 1.11 nia char tmp_encoded[256];
154 1.11 nia struct rlimit rlim;
155 1.11 nia uint64_t max_mem;
156 1.11 nia size_t max_mem_sz = sizeof(max_mem);
157 1.11 nia /* low values from argon2 test suite... */
158 1.11 nia uint32_t memory = 256;
159 1.11 nia uint32_t time = 2;
160 1.11 nia uint32_t threads = 1;
161 1.11 nia
162 1.11 nia if (*ememory < ARGON2_MIN_MEMORY) {
163 1.11 nia /*
164 1.11 nia * attempt to find a reasonble bound for memory use
165 1.11 nia */
166 1.11 nia if (sysctl(mib, __arraycount(mib),
167 1.11 nia &max_mem, &max_mem_sz, NULL, 0) < 0) {
168 1.11 nia goto error;
169 1.11 nia }
170 1.11 nia if (getrlimit(RLIMIT_AS, &rlim) < 0)
171 1.11 nia goto error;
172 1.11 nia if (max_mem > rlim.rlim_cur && rlim.rlim_cur != RLIM_INFINITY)
173 1.11 nia max_mem = rlim.rlim_cur;
174 1.11 nia
175 1.11 nia /*
176 1.11 nia * Note that adding memory also greatly slows the algorithm.
177 1.11 nia * Do we need to be concerned about memory usage during
178 1.11 nia * concurrent connections?
179 1.11 nia */
180 1.11 nia max_mem /= 1000000;
181 1.11 nia if (max_mem > 30000) {
182 1.11 nia memory = 8192;
183 1.11 nia } else if (max_mem > 7000) {
184 1.11 nia memory = 4096;
185 1.11 nia } else if (max_mem > 24) {
186 1.11 nia memory = 256;
187 1.11 nia } else {
188 1.11 nia memory = ARGON2_MIN_MEMORY;
189 1.11 nia }
190 1.11 nia } else {
191 1.11 nia memory = *ememory;
192 1.11 nia }
193 1.11 nia
194 1.11 nia if (*etime < ARGON2_MIN_TIME) {
195 1.11 nia /*
196 1.11 nia * just fill these with random stuff since we'll immediately
197 1.11 nia * discard them after calculating hashes for 1 second
198 1.11 nia */
199 1.11 nia arc4random_buf(tmp_pwd, sizeof(tmp_pwd));
200 1.11 nia arc4random_buf(tmp_salt, sizeof(tmp_salt));
201 1.11 nia
202 1.11 nia if (clock_gettime(CLOCK_MONOTONIC, &tp1) == -1)
203 1.11 nia goto error;
204 1.11 nia for (; delta.tv_sec < 1 && time < ARGON2_MAX_TIME; ++time) {
205 1.11 nia if (argon2_hash(time, memory, threads,
206 1.11 nia tmp_pwd, sizeof(tmp_pwd),
207 1.11 nia tmp_salt, sizeof(tmp_salt),
208 1.11 nia tmp_hash, sizeof(tmp_hash),
209 1.11 nia tmp_encoded, sizeof(tmp_encoded),
210 1.11 nia atype, ARGON2_VERSION_NUMBER) != ARGON2_OK) {
211 1.11 nia goto reset;
212 1.11 nia }
213 1.11 nia if (clock_gettime(CLOCK_MONOTONIC, &tp2) == -1)
214 1.11 nia break;
215 1.11 nia if (timespeccmp(&tp1, &tp2, >))
216 1.11 nia break; /* broken system... */
217 1.11 nia timespecsub(&tp2, &tp1, &delta);
218 1.11 nia }
219 1.11 nia } else {
220 1.11 nia time = *etime;
221 1.11 nia }
222 1.11 nia
223 1.11 nia error:
224 1.11 nia *etime = time;
225 1.11 nia *ememory = memory;
226 1.11 nia *ethreads = threads;
227 1.11 nia return 0;
228 1.11 nia reset:
229 1.11 nia time = 2;
230 1.11 nia memory = 256;
231 1.11 nia threads = 1;
232 1.11 nia goto error;
233 1.11 nia }
234 1.11 nia
235 1.11 nia
236 1.1 jhigh /* process params to argon2 */
237 1.1 jhigh /* we don't force param order as input, */
238 1.1 jhigh /* but we do provide the expected order to argon2 api */
239 1.7 nia static int
240 1.7 nia decode_option(argon2_context *ctx, argon2_type *atype, const char *option)
241 1.1 jhigh {
242 1.7 nia size_t tmp = 0;
243 1.7 nia char *in = 0, *inp;
244 1.7 nia char *a = 0;
245 1.7 nia char *p = 0;
246 1.1 jhigh size_t sl;
247 1.7 nia int error = 0;
248 1.1 jhigh
249 1.1 jhigh in = (char *)strdup(option);
250 1.1 jhigh inp = in;
251 1.1 jhigh
252 1.1 jhigh if (*inp == '$') inp++;
253 1.1 jhigh
254 1.1 jhigh a = strsep(&inp, "$");
255 1.1 jhigh
256 1.1 jhigh sl = strlen(a);
257 1.1 jhigh
258 1.1 jhigh if (sl == strlen(ARGON2_ARGON2I_STR) &&
259 1.1 jhigh !(strcmp(ARGON2_ARGON2I_STR, a))) {
260 1.1 jhigh *atype=Argon2_i;
261 1.1 jhigh } else if (sl == strlen(ARGON2_ARGON2D_STR) &&
262 1.1 jhigh !(strcmp(ARGON2_ARGON2D_STR, a))) {
263 1.1 jhigh *atype=Argon2_d;
264 1.1 jhigh }
265 1.1 jhigh else if (sl == strlen(ARGON2_ARGON2ID_STR) &&
266 1.1 jhigh !(strcmp(ARGON2_ARGON2ID_STR, a))) {
267 1.1 jhigh *atype=Argon2_id;
268 1.1 jhigh } else { /* default to id, we assume simple mistake */
269 1.1 jhigh /* don't abandon yet */
270 1.1 jhigh *atype=Argon2_id;
271 1.1 jhigh }
272 1.1 jhigh
273 1.1 jhigh a = strsep(&inp, "$");
274 1.1 jhigh
275 1.3 nia /* parse the version number of the hash, if it's there */
276 1.3 nia if (strncmp(a, "v=", 2) == 0) {
277 1.3 nia a += 2;
278 1.3 nia if ((getnum(a, &tmp))<0) { /* on error, default to current */
279 1.3 nia /* should start thinking about aborting */
280 1.4 nia ctx->version = ARGON2_VERSION_10;
281 1.3 nia } else {
282 1.3 nia ctx->version = tmp;
283 1.3 nia }
284 1.3 nia a = strsep(&inp, "$");
285 1.3 nia } else {
286 1.3 nia /*
287 1.3 nia * This is a parameter list, not a version number, use the
288 1.3 nia * default version.
289 1.3 nia */
290 1.4 nia ctx->version = ARGON2_VERSION_10;
291 1.1 jhigh }
292 1.1 jhigh
293 1.1 jhigh /* parse labelled argon2 params */
294 1.1 jhigh /* m_cost (m)
295 1.1 jhigh * t_cost (t)
296 1.1 jhigh * threads (p)
297 1.1 jhigh */
298 1.1 jhigh while ((p = strsep(&a, ","))) {
299 1.1 jhigh switch (*p) {
300 1.1 jhigh case 'm':
301 1.1 jhigh p += strlen("m=");
302 1.1 jhigh if ((getnum(p, &tmp)) < 0) {
303 1.1 jhigh --error;
304 1.1 jhigh } else {
305 1.1 jhigh ctx->m_cost = tmp;
306 1.1 jhigh }
307 1.1 jhigh break;
308 1.1 jhigh case 't':
309 1.1 jhigh p += strlen("t=");
310 1.1 jhigh if ((getnum(p, &tmp)) < 0) {
311 1.1 jhigh --error;
312 1.1 jhigh } else {
313 1.1 jhigh ctx->t_cost = tmp;
314 1.1 jhigh }
315 1.1 jhigh break;
316 1.1 jhigh case 'p':
317 1.1 jhigh p += strlen("p=");
318 1.1 jhigh if ((getnum(p, &tmp)) < 0) {
319 1.1 jhigh --error;
320 1.1 jhigh } else {
321 1.1 jhigh ctx->threads = tmp;
322 1.1 jhigh }
323 1.1 jhigh break;
324 1.1 jhigh default:
325 1.1 jhigh return -1;
326 1.1 jhigh
327 1.1 jhigh }
328 1.1 jhigh }
329 1.1 jhigh
330 1.1 jhigh a = strsep(&inp, "$");
331 1.1 jhigh
332 1.7 nia sl = ctx->saltlen;
333 1.7 nia
334 1.7 nia if (from_base64(ctx->salt, &sl, a) == NULL)
335 1.7 nia return -1;
336 1.7 nia
337 1.7 nia ctx->saltlen = sl;
338 1.1 jhigh
339 1.1 jhigh a = strsep(&inp, "$");
340 1.1 jhigh
341 1.3 nia if (a) {
342 1.3 nia snprintf((char *)ctx->pwd, ctx->pwdlen, "%s", a);
343 1.1 jhigh } else {
344 1.1 jhigh /* don't care if passwd hash is missing */
345 1.1 jhigh /* if missing, most likely coming from */
346 1.1 jhigh /* pwhash or similar */
347 1.1 jhigh }
348 1.1 jhigh
349 1.1 jhigh /* free our token buffer */
350 1.1 jhigh free(in);
351 1.1 jhigh
352 1.1 jhigh /* 0 on success, <0 otherwise */
353 1.1 jhigh return error;
354 1.1 jhigh }
355 1.1 jhigh
356 1.10 nia crypt_private char *
357 1.1 jhigh __crypt_argon2(const char *pw, const char * salt)
358 1.1 jhigh {
359 1.1 jhigh /* we use the libargon2 api to generate */
360 1.1 jhigh /* return code */
361 1.7 nia int rc = 0;
362 1.1 jhigh /* output buffer */
363 1.1 jhigh char ebuf[32];
364 1.1 jhigh /* argon2 variable, default to id */
365 1.1 jhigh argon2_type atype = Argon2_id;
366 1.1 jhigh /* default to current argon2 version */
367 1.1 jhigh /* argon2 context to collect params */
368 1.1 jhigh argon2_context ctx = ARGON2_CONTEXT_INITIALIZER;
369 1.1 jhigh /* argon2 encoded buffer */
370 1.1 jhigh char encodebuf[256];
371 1.1 jhigh /* argon2 salt buffer */
372 1.1 jhigh char saltbuf[128];
373 1.1 jhigh /* argon2 pwd buffer */
374 1.1 jhigh char pwdbuf[128];
375 1.1 jhigh /* returned static buffer */
376 1.1 jhigh static char rbuf[512];
377 1.1 jhigh
378 1.1 jhigh /* clear buffers */
379 1.6 nia explicit_memset(rbuf, 0, sizeof(rbuf));
380 1.1 jhigh
381 1.1 jhigh /* we use static buffers to avoid allocation */
382 1.1 jhigh /* and easier cleanup */
383 1.1 jhigh ctx.out = (uint8_t *)ebuf;
384 1.1 jhigh ctx.outlen = sizeof(ebuf);
385 1.1 jhigh
386 1.1 jhigh ctx.out = (uint8_t *)encodebuf;
387 1.1 jhigh ctx.outlen = sizeof(encodebuf);
388 1.1 jhigh
389 1.1 jhigh ctx.salt = (uint8_t *)saltbuf;
390 1.1 jhigh ctx.saltlen = sizeof(saltbuf);
391 1.1 jhigh
392 1.7 nia ctx.pwd = (uint8_t *)pwdbuf;
393 1.1 jhigh ctx.pwdlen = sizeof(pwdbuf);
394 1.1 jhigh
395 1.1 jhigh /* decode salt string to argon2 params */
396 1.1 jhigh /* argon2 context for param collection */
397 1.1 jhigh rc = decode_option(&ctx, &atype, salt);
398 1.1 jhigh
399 1.1 jhigh if (rc < 0) {
400 1.3 nia /* unable to parse input params */
401 1.1 jhigh return 0;
402 1.1 jhigh }
403 1.1 jhigh
404 1.1 jhigh rc = argon2_hash(ctx.t_cost, ctx.m_cost,
405 1.7 nia ctx.threads, pw, strlen(pw), ctx.salt, ctx.saltlen,
406 1.7 nia ebuf, sizeof(ebuf), encodebuf, sizeof(encodebuf),
407 1.7 nia atype, ctx.version);
408 1.1 jhigh
409 1.1 jhigh if (rc != ARGON2_OK) {
410 1.3 nia fprintf(stderr, "argon2: failed: %s\n",
411 1.3 nia argon2_error_message(rc));
412 1.1 jhigh return 0;
413 1.1 jhigh }
414 1.1 jhigh
415 1.6 nia memcpy(rbuf, encodebuf, sizeof(encodebuf));
416 1.1 jhigh
417 1.1 jhigh /* clear buffers */
418 1.6 nia explicit_memset(ebuf, 0, sizeof(ebuf));
419 1.5 nia explicit_memset(encodebuf, 0, sizeof(encodebuf));
420 1.5 nia explicit_memset(saltbuf, 0, sizeof(saltbuf));
421 1.5 nia explicit_memset(pwdbuf, 0, sizeof(pwdbuf));
422 1.1 jhigh
423 1.1 jhigh /* return encoded str */
424 1.1 jhigh return rbuf;
425 1.1 jhigh }
426