crypt-argon2.c revision 1.15 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.15 nia uint64_t max_mem; /* usermem64 returns bytes */
156 1.11 nia size_t max_mem_sz = sizeof(max_mem);
157 1.11 nia /* low values from argon2 test suite... */
158 1.15 nia uint32_t memory = 256; /* 256k; argon2 wants kilobytes */
159 1.13 nia uint32_t time = 3;
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.15 nia max_mem /= 1000000; /* bytes down to mb */
181 1.11 nia if (max_mem > 30000) {
182 1.14 nia memory = 32768;
183 1.14 nia } else if (max_mem > 15000) {
184 1.14 nia memory = 16384;
185 1.14 nia } else if (max_mem > 7000) {
186 1.11 nia memory = 8192;
187 1.14 nia } else if (max_mem > 3000) {
188 1.11 nia memory = 4096;
189 1.14 nia } else if (max_mem > 900) {
190 1.14 nia memory = 1024;
191 1.11 nia } else if (max_mem > 24) {
192 1.11 nia memory = 256;
193 1.11 nia } else {
194 1.11 nia memory = ARGON2_MIN_MEMORY;
195 1.11 nia }
196 1.11 nia } else {
197 1.11 nia memory = *ememory;
198 1.11 nia }
199 1.11 nia
200 1.11 nia if (*etime < ARGON2_MIN_TIME) {
201 1.11 nia /*
202 1.11 nia * just fill these with random stuff since we'll immediately
203 1.11 nia * discard them after calculating hashes for 1 second
204 1.11 nia */
205 1.11 nia arc4random_buf(tmp_pwd, sizeof(tmp_pwd));
206 1.11 nia arc4random_buf(tmp_salt, sizeof(tmp_salt));
207 1.11 nia
208 1.11 nia if (clock_gettime(CLOCK_MONOTONIC, &tp1) == -1)
209 1.11 nia goto error;
210 1.11 nia for (; delta.tv_sec < 1 && time < ARGON2_MAX_TIME; ++time) {
211 1.11 nia if (argon2_hash(time, memory, threads,
212 1.11 nia tmp_pwd, sizeof(tmp_pwd),
213 1.11 nia tmp_salt, sizeof(tmp_salt),
214 1.11 nia tmp_hash, sizeof(tmp_hash),
215 1.11 nia tmp_encoded, sizeof(tmp_encoded),
216 1.11 nia atype, ARGON2_VERSION_NUMBER) != ARGON2_OK) {
217 1.11 nia goto reset;
218 1.11 nia }
219 1.11 nia if (clock_gettime(CLOCK_MONOTONIC, &tp2) == -1)
220 1.11 nia break;
221 1.11 nia if (timespeccmp(&tp1, &tp2, >))
222 1.11 nia break; /* broken system... */
223 1.11 nia timespecsub(&tp2, &tp1, &delta);
224 1.11 nia }
225 1.11 nia } else {
226 1.11 nia time = *etime;
227 1.11 nia }
228 1.11 nia
229 1.11 nia error:
230 1.11 nia *etime = time;
231 1.11 nia *ememory = memory;
232 1.11 nia *ethreads = threads;
233 1.11 nia return 0;
234 1.11 nia reset:
235 1.13 nia time = 3;
236 1.11 nia memory = 256;
237 1.11 nia threads = 1;
238 1.11 nia goto error;
239 1.11 nia }
240 1.11 nia
241 1.11 nia
242 1.1 jhigh /* process params to argon2 */
243 1.1 jhigh /* we don't force param order as input, */
244 1.1 jhigh /* but we do provide the expected order to argon2 api */
245 1.7 nia static int
246 1.7 nia decode_option(argon2_context *ctx, argon2_type *atype, const char *option)
247 1.1 jhigh {
248 1.7 nia size_t tmp = 0;
249 1.7 nia char *in = 0, *inp;
250 1.7 nia char *a = 0;
251 1.7 nia char *p = 0;
252 1.1 jhigh size_t sl;
253 1.7 nia int error = 0;
254 1.1 jhigh
255 1.1 jhigh in = (char *)strdup(option);
256 1.1 jhigh inp = in;
257 1.1 jhigh
258 1.1 jhigh if (*inp == '$') inp++;
259 1.1 jhigh
260 1.1 jhigh a = strsep(&inp, "$");
261 1.1 jhigh
262 1.1 jhigh sl = strlen(a);
263 1.1 jhigh
264 1.1 jhigh if (sl == strlen(ARGON2_ARGON2I_STR) &&
265 1.1 jhigh !(strcmp(ARGON2_ARGON2I_STR, a))) {
266 1.1 jhigh *atype=Argon2_i;
267 1.1 jhigh } else if (sl == strlen(ARGON2_ARGON2D_STR) &&
268 1.1 jhigh !(strcmp(ARGON2_ARGON2D_STR, a))) {
269 1.1 jhigh *atype=Argon2_d;
270 1.1 jhigh }
271 1.1 jhigh else if (sl == strlen(ARGON2_ARGON2ID_STR) &&
272 1.1 jhigh !(strcmp(ARGON2_ARGON2ID_STR, a))) {
273 1.1 jhigh *atype=Argon2_id;
274 1.1 jhigh } else { /* default to id, we assume simple mistake */
275 1.1 jhigh /* don't abandon yet */
276 1.1 jhigh *atype=Argon2_id;
277 1.1 jhigh }
278 1.1 jhigh
279 1.1 jhigh a = strsep(&inp, "$");
280 1.1 jhigh
281 1.3 nia /* parse the version number of the hash, if it's there */
282 1.3 nia if (strncmp(a, "v=", 2) == 0) {
283 1.3 nia a += 2;
284 1.3 nia if ((getnum(a, &tmp))<0) { /* on error, default to current */
285 1.3 nia /* should start thinking about aborting */
286 1.4 nia ctx->version = ARGON2_VERSION_10;
287 1.3 nia } else {
288 1.3 nia ctx->version = tmp;
289 1.3 nia }
290 1.3 nia a = strsep(&inp, "$");
291 1.3 nia } else {
292 1.3 nia /*
293 1.3 nia * This is a parameter list, not a version number, use the
294 1.3 nia * default version.
295 1.3 nia */
296 1.4 nia ctx->version = ARGON2_VERSION_10;
297 1.1 jhigh }
298 1.1 jhigh
299 1.1 jhigh /* parse labelled argon2 params */
300 1.1 jhigh /* m_cost (m)
301 1.1 jhigh * t_cost (t)
302 1.1 jhigh * threads (p)
303 1.1 jhigh */
304 1.1 jhigh while ((p = strsep(&a, ","))) {
305 1.1 jhigh switch (*p) {
306 1.1 jhigh case 'm':
307 1.1 jhigh p += strlen("m=");
308 1.1 jhigh if ((getnum(p, &tmp)) < 0) {
309 1.1 jhigh --error;
310 1.1 jhigh } else {
311 1.1 jhigh ctx->m_cost = tmp;
312 1.1 jhigh }
313 1.1 jhigh break;
314 1.1 jhigh case 't':
315 1.1 jhigh p += strlen("t=");
316 1.1 jhigh if ((getnum(p, &tmp)) < 0) {
317 1.1 jhigh --error;
318 1.1 jhigh } else {
319 1.1 jhigh ctx->t_cost = tmp;
320 1.1 jhigh }
321 1.1 jhigh break;
322 1.1 jhigh case 'p':
323 1.1 jhigh p += strlen("p=");
324 1.1 jhigh if ((getnum(p, &tmp)) < 0) {
325 1.1 jhigh --error;
326 1.1 jhigh } else {
327 1.1 jhigh ctx->threads = tmp;
328 1.1 jhigh }
329 1.1 jhigh break;
330 1.1 jhigh default:
331 1.1 jhigh return -1;
332 1.1 jhigh
333 1.1 jhigh }
334 1.1 jhigh }
335 1.1 jhigh
336 1.1 jhigh a = strsep(&inp, "$");
337 1.1 jhigh
338 1.7 nia sl = ctx->saltlen;
339 1.7 nia
340 1.7 nia if (from_base64(ctx->salt, &sl, a) == NULL)
341 1.7 nia return -1;
342 1.7 nia
343 1.7 nia ctx->saltlen = sl;
344 1.1 jhigh
345 1.1 jhigh a = strsep(&inp, "$");
346 1.1 jhigh
347 1.3 nia if (a) {
348 1.3 nia snprintf((char *)ctx->pwd, ctx->pwdlen, "%s", a);
349 1.1 jhigh } else {
350 1.1 jhigh /* don't care if passwd hash is missing */
351 1.1 jhigh /* if missing, most likely coming from */
352 1.1 jhigh /* pwhash or similar */
353 1.1 jhigh }
354 1.1 jhigh
355 1.1 jhigh /* free our token buffer */
356 1.1 jhigh free(in);
357 1.1 jhigh
358 1.1 jhigh /* 0 on success, <0 otherwise */
359 1.1 jhigh return error;
360 1.1 jhigh }
361 1.1 jhigh
362 1.10 nia crypt_private char *
363 1.1 jhigh __crypt_argon2(const char *pw, const char * salt)
364 1.1 jhigh {
365 1.1 jhigh /* we use the libargon2 api to generate */
366 1.1 jhigh /* return code */
367 1.7 nia int rc = 0;
368 1.1 jhigh /* output buffer */
369 1.1 jhigh char ebuf[32];
370 1.1 jhigh /* argon2 variable, default to id */
371 1.1 jhigh argon2_type atype = Argon2_id;
372 1.1 jhigh /* default to current argon2 version */
373 1.1 jhigh /* argon2 context to collect params */
374 1.1 jhigh argon2_context ctx = ARGON2_CONTEXT_INITIALIZER;
375 1.1 jhigh /* argon2 encoded buffer */
376 1.1 jhigh char encodebuf[256];
377 1.1 jhigh /* argon2 salt buffer */
378 1.1 jhigh char saltbuf[128];
379 1.1 jhigh /* argon2 pwd buffer */
380 1.1 jhigh char pwdbuf[128];
381 1.1 jhigh /* returned static buffer */
382 1.1 jhigh static char rbuf[512];
383 1.1 jhigh
384 1.1 jhigh /* clear buffers */
385 1.6 nia explicit_memset(rbuf, 0, sizeof(rbuf));
386 1.1 jhigh
387 1.1 jhigh /* we use static buffers to avoid allocation */
388 1.1 jhigh /* and easier cleanup */
389 1.1 jhigh ctx.out = (uint8_t *)ebuf;
390 1.1 jhigh ctx.outlen = sizeof(ebuf);
391 1.1 jhigh
392 1.1 jhigh ctx.out = (uint8_t *)encodebuf;
393 1.1 jhigh ctx.outlen = sizeof(encodebuf);
394 1.1 jhigh
395 1.1 jhigh ctx.salt = (uint8_t *)saltbuf;
396 1.1 jhigh ctx.saltlen = sizeof(saltbuf);
397 1.1 jhigh
398 1.7 nia ctx.pwd = (uint8_t *)pwdbuf;
399 1.1 jhigh ctx.pwdlen = sizeof(pwdbuf);
400 1.1 jhigh
401 1.1 jhigh /* decode salt string to argon2 params */
402 1.1 jhigh /* argon2 context for param collection */
403 1.1 jhigh rc = decode_option(&ctx, &atype, salt);
404 1.1 jhigh
405 1.1 jhigh if (rc < 0) {
406 1.3 nia /* unable to parse input params */
407 1.12 nia return NULL;
408 1.1 jhigh }
409 1.1 jhigh
410 1.1 jhigh rc = argon2_hash(ctx.t_cost, ctx.m_cost,
411 1.7 nia ctx.threads, pw, strlen(pw), ctx.salt, ctx.saltlen,
412 1.7 nia ebuf, sizeof(ebuf), encodebuf, sizeof(encodebuf),
413 1.7 nia atype, ctx.version);
414 1.1 jhigh
415 1.1 jhigh if (rc != ARGON2_OK) {
416 1.3 nia fprintf(stderr, "argon2: failed: %s\n",
417 1.3 nia argon2_error_message(rc));
418 1.12 nia return NULL;
419 1.1 jhigh }
420 1.1 jhigh
421 1.6 nia memcpy(rbuf, encodebuf, sizeof(encodebuf));
422 1.1 jhigh
423 1.1 jhigh /* clear buffers */
424 1.6 nia explicit_memset(ebuf, 0, sizeof(ebuf));
425 1.5 nia explicit_memset(encodebuf, 0, sizeof(encodebuf));
426 1.5 nia explicit_memset(saltbuf, 0, sizeof(saltbuf));
427 1.5 nia explicit_memset(pwdbuf, 0, sizeof(pwdbuf));
428 1.1 jhigh
429 1.1 jhigh /* return encoded str */
430 1.1 jhigh return rbuf;
431 1.1 jhigh }
432