/*
* Copyright (c) 2009 The NetBSD Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* If the input length is equal to 1 modulo 4 (which is
* invalid), then there will remain 6 unprocessed bits;
* otherwise, only 0, 2 or 4 bits are buffered. The buffered
* bits must also all be zero.
*/
if (acc_len > 4 || (acc & (((unsigned)1 << acc_len) - 1)) != 0) {
return NULL;
}
*dst_len = len;
return src;
}
/*
* Used to find default parameters that perform well on the host
* machine. Inputs should dereference to either 0 (to estimate),
* or desired value.
*/
crypt_private int
estimate_argon2_params(argon2_type atype, uint32_t *etime,
uint32_t *ememory, uint32_t *ethreads)
{
const int mib[] = { CTL_HW, HW_USERMEM64 };
struct timespec tp1, tp2, delta;
char tmp_salt[16];
char tmp_pwd[16];
uint32_t tmp_hash[32];
char tmp_encoded[256];
struct rlimit rlim;
uint64_t max_mem; /* usermem64 returns bytes */
size_t max_mem_sz = sizeof(max_mem);
/* low values from argon2 test suite... */
uint32_t memory = 256; /* 256k; argon2 wants kilobytes */
uint32_t time = 3;
uint32_t threads = 1;
if (*ememory < ARGON2_MIN_MEMORY) {
/*
* attempt to find a reasonble bound for memory use
*/
if (sysctl(mib, __arraycount(mib),
&max_mem, &max_mem_sz, NULL, 0) < 0) {
goto error;
}
if (getrlimit(RLIMIT_AS, &rlim) < 0)
goto error;
if (max_mem > rlim.rlim_cur && rlim.rlim_cur != RLIM_INFINITY)
max_mem = rlim.rlim_cur;
/*
* Note that adding memory also greatly slows the algorithm.
* Do we need to be concerned about memory usage during
* concurrent connections?
*/
max_mem /= 1000000; /* bytes down to mb */
if (max_mem > 30000) {
memory = 32768;
} else if (max_mem > 15000) {
memory = 16384;
} else if (max_mem > 7000) {
memory = 8192;
} else if (max_mem > 3000) {
memory = 4096;
} else if (max_mem > 900) {
memory = 1024;
} else if (max_mem > 24) {
memory = 256;
} else {
memory = ARGON2_MIN_MEMORY;
}
} else {
memory = *ememory;
}
if (*etime < ARGON2_MIN_TIME) {
/*
* just fill these with random stuff since we'll immediately
* discard them after calculating hashes for 1 second
*/
arc4random_buf(tmp_pwd, sizeof(tmp_pwd));
arc4random_buf(tmp_salt, sizeof(tmp_salt));
if (clock_gettime(CLOCK_MONOTONIC, &tp1) == -1)
goto error;
for (; time < ARGON2_MAX_TIME; ++time) {
if (argon2_hash(time, memory, threads,
tmp_pwd, sizeof(tmp_pwd),
tmp_salt, sizeof(tmp_salt),
tmp_hash, sizeof(tmp_hash),
tmp_encoded, sizeof(tmp_encoded),
atype, ARGON2_VERSION_NUMBER) != ARGON2_OK) {
goto reset;
}
if (clock_gettime(CLOCK_MONOTONIC, &tp2) == -1)
break;
if (timespeccmp(&tp1, &tp2, >))
break; /* broken system... */
timespecsub(&tp2, &tp1, &delta);
if (delta.tv_sec >= 1)
break;
}
} else {
time = *etime;
}
/* process params to argon2 */
/* we don't force param order as input, */
/* but we do provide the expected order to argon2 api */
static int
decode_option(argon2_context *ctx, argon2_type *atype, const char *option)
{
size_t tmp = 0;
char *in = 0, *inp;
char *a = 0;
char *p = 0;
size_t sl;
int error = 0;
in = (char *)strdup(option);
inp = in;
if (*inp == '$') inp++;
a = strsep(&inp, "$");
sl = strlen(a);
if (sl == strlen(ARGON2_ARGON2I_STR) &&
!(strcmp(ARGON2_ARGON2I_STR, a))) {
*atype=Argon2_i;
} else if (sl == strlen(ARGON2_ARGON2D_STR) &&
!(strcmp(ARGON2_ARGON2D_STR, a))) {
*atype=Argon2_d;
}
else if (sl == strlen(ARGON2_ARGON2ID_STR) &&
!(strcmp(ARGON2_ARGON2ID_STR, a))) {
*atype=Argon2_id;
} else { /* default to id, we assume simple mistake */
/* don't abandon yet */
*atype=Argon2_id;
}
a = strsep(&inp, "$");
/* parse the version number of the hash, if it's there */
if (strncmp(a, "v=", 2) == 0) {
a += 2;
if ((getnum(a, &tmp))<0) { /* on error, default to current */
/* should start thinking about aborting */
ctx->version = ARGON2_VERSION_10;
} else {
ctx->version = tmp;
}
a = strsep(&inp, "$");
} else {
/*
* This is a parameter list, not a version number, use the
* default version.
*/
ctx->version = ARGON2_VERSION_10;
}
/* parse labelled argon2 params */
/* m_cost (m)
* t_cost (t)
* threads (p)
*/
while ((p = strsep(&a, ","))) {
switch (*p) {
case 'm':
p += strlen("m=");
if ((getnum(p, &tmp)) < 0) {
--error;
} else {
ctx->m_cost = tmp;
}
break;
case 't':
p += strlen("t=");
if ((getnum(p, &tmp)) < 0) {
--error;
} else {
ctx->t_cost = tmp;
}
break;
case 'p':
p += strlen("p=");
if ((getnum(p, &tmp)) < 0) {
--error;
} else {
ctx->threads = tmp;
}
break;
default:
free(in);
return -1;
}
}
a = strsep(&inp, "$");
if (a == NULL) {
free(in);
return -1;
}
sl = ctx->saltlen;
if (from_base64(ctx->salt, &sl, a) == NULL) {
free(in);
return -1;
}
ctx->saltlen = sl;
a = strsep(&inp, "$");
if (a) {
snprintf((char *)ctx->pwd, ctx->pwdlen, "%s", a);
} else {
/* don't care if passwd hash is missing */
/* if missing, most likely coming from */
/* pwhash or similar */
}
/* free our token buffer */
free(in);
/* 0 on success, <0 otherwise */
return error;
}
crypt_private char *
__crypt_argon2(const char *pw, const char * salt)
{
/* we use the libargon2 api to generate */
/* return code */
int rc = 0;
/* output buffer */
char ebuf[32];
/* argon2 variable, default to id */
argon2_type atype = Argon2_id;
/* default to current argon2 version */
/* argon2 context to collect params */
argon2_context ctx = ARGON2_CONTEXT_INITIALIZER;
/* argon2 encoded buffer */
char encodebuf[256];
/* argon2 salt buffer */
char saltbuf[128];
/* argon2 pwd buffer */
char pwdbuf[128];
/* returned static buffer */
static char rbuf[512];