2 * Argon2 PBKDF2 library wrapper
4 * Copyright (C) 2016-2021 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2016-2021 Milan Broz
7 * This file is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This file is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this file; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "crypto_backend_internal.h"
27 #include "argon2/argon2.h"
30 #define CONST_CAST(x) (x)(uintptr_t)
32 int argon2(const char *type, const char *password, size_t password_length,
33 const char *salt, size_t salt_length,
34 char *key, size_t key_length,
35 uint32_t iterations, uint32_t memory, uint32_t parallel)
37 #if !USE_INTERNAL_ARGON2 && !HAVE_ARGON2_H
41 argon2_context context = {
42 .flags = ARGON2_DEFAULT_FLAGS,
43 .version = ARGON2_VERSION_NUMBER,
44 .t_cost = (uint32_t)iterations,
45 .m_cost = (uint32_t)memory,
46 .lanes = (uint32_t)parallel,
47 .threads = (uint32_t)parallel,
48 .out = (uint8_t *)key,
49 .outlen = (uint32_t)key_length,
50 .pwd = CONST_CAST(uint8_t *)password,
51 .pwdlen = (uint32_t)password_length,
52 .salt = CONST_CAST(uint8_t *)salt,
53 .saltlen = (uint32_t)salt_length,
57 if (!strcmp(type, "argon2i"))
59 else if(!strcmp(type, "argon2id"))
64 switch (argon2_ctx(&context, atype)) {
68 case ARGON2_MEMORY_ALLOCATION_ERROR:
69 case ARGON2_FREE_MEMORY_CBK_NULL:
70 case ARGON2_ALLOCATE_MEMORY_CBK_NULL: