|
Packit |
94f725 |
/*
|
|
Packit |
94f725 |
* Argon2 PBKDF2 library wrapper
|
|
Packit |
94f725 |
*
|
|
Packit |
94f725 |
* Copyright (C) 2016-2020 Red Hat, Inc. All rights reserved.
|
|
Packit |
94f725 |
* Copyright (C) 2016-2020 Milan Broz
|
|
Packit |
94f725 |
*
|
|
Packit |
94f725 |
* This file is free software; you can redistribute it and/or
|
|
Packit |
94f725 |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit |
94f725 |
* License as published by the Free Software Foundation; either
|
|
Packit |
94f725 |
* version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
94f725 |
*
|
|
Packit |
94f725 |
* This file is distributed in the hope that it will be useful,
|
|
Packit |
94f725 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
94f725 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
94f725 |
* Lesser General Public License for more details.
|
|
Packit |
94f725 |
*
|
|
Packit |
94f725 |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit |
94f725 |
* License along with this file; if not, write to the Free Software
|
|
Packit |
94f725 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
Packit |
94f725 |
*/
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
#include <errno.h>
|
|
Packit |
94f725 |
#include "crypto_backend_internal.h"
|
|
Packit |
94f725 |
#if HAVE_ARGON2_H
|
|
Packit |
94f725 |
#include <argon2.h>
|
|
Packit |
94f725 |
#else
|
|
Packit |
94f725 |
#include "argon2/argon2.h"
|
|
Packit |
94f725 |
#endif
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
#define CONST_CAST(x) (x)(uintptr_t)
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
int argon2(const char *type, const char *password, size_t password_length,
|
|
Packit |
94f725 |
const char *salt, size_t salt_length,
|
|
Packit |
94f725 |
char *key, size_t key_length,
|
|
Packit |
94f725 |
uint32_t iterations, uint32_t memory, uint32_t parallel)
|
|
Packit |
94f725 |
{
|
|
Packit |
94f725 |
#if !USE_INTERNAL_ARGON2 && !HAVE_ARGON2_H
|
|
Packit |
94f725 |
return -EINVAL;
|
|
Packit |
94f725 |
#else
|
|
Packit |
94f725 |
argon2_type atype;
|
|
Packit |
94f725 |
argon2_context context = {
|
|
Packit |
94f725 |
.flags = ARGON2_DEFAULT_FLAGS,
|
|
Packit |
94f725 |
.version = ARGON2_VERSION_NUMBER,
|
|
Packit |
94f725 |
.t_cost = (uint32_t)iterations,
|
|
Packit |
94f725 |
.m_cost = (uint32_t)memory,
|
|
Packit |
94f725 |
.lanes = (uint32_t)parallel,
|
|
Packit |
94f725 |
.threads = (uint32_t)parallel,
|
|
Packit |
94f725 |
.out = (uint8_t *)key,
|
|
Packit |
94f725 |
.outlen = (uint32_t)key_length,
|
|
Packit |
94f725 |
.pwd = CONST_CAST(uint8_t *)password,
|
|
Packit |
94f725 |
.pwdlen = (uint32_t)password_length,
|
|
Packit |
94f725 |
.salt = CONST_CAST(uint8_t *)salt,
|
|
Packit |
94f725 |
.saltlen = (uint32_t)salt_length,
|
|
Packit |
94f725 |
};
|
|
Packit |
94f725 |
int r;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
if (!strcmp(type, "argon2i"))
|
|
Packit |
94f725 |
atype = Argon2_i;
|
|
Packit |
94f725 |
else if(!strcmp(type, "argon2id"))
|
|
Packit |
94f725 |
atype = Argon2_id;
|
|
Packit |
94f725 |
else
|
|
Packit |
94f725 |
return -EINVAL;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
switch (argon2_ctx(&context, atype)) {
|
|
Packit |
94f725 |
case ARGON2_OK:
|
|
Packit |
94f725 |
r = 0;
|
|
Packit |
94f725 |
break;
|
|
Packit |
94f725 |
case ARGON2_MEMORY_ALLOCATION_ERROR:
|
|
Packit |
94f725 |
case ARGON2_FREE_MEMORY_CBK_NULL:
|
|
Packit |
94f725 |
case ARGON2_ALLOCATE_MEMORY_CBK_NULL:
|
|
Packit |
94f725 |
r = -ENOMEM;
|
|
Packit |
94f725 |
break;
|
|
Packit |
94f725 |
default:
|
|
Packit |
94f725 |
r = -EINVAL;
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
return r;
|
|
Packit |
94f725 |
#endif
|
|
Packit |
94f725 |
}
|