-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Argon2 #150
Open
maandree
wants to merge
2
commits into
besser82:develop
Choose a base branch
from
maandree:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -189,6 +189,28 @@ The algorithm was specifically designed to make it costly to perform | |
large-scale custom hardware attacks by requiring large amounts of memory. | ||
In 2016, the scrypt algorithm was published by IETF as RFC 7914. | ||
.hash "$7$" "\e$7\e$[./A-Za-z0-9]{11,97}\e$[./A-Za-z0-9]{43}" unlimited 8 256 256 "up to 512 (128+ recommended)" "6 to 11 (logarithmic)" | ||
.Ss argon2_d | ||
argon2_d is one of the variants of Argon2 key derivation functions that won | ||
the Password Hashing Competition 2015. argon2d is designed for environments | ||
taht are safe from side-channel attacks. Acceptable for new hashes, but | ||
not widely supported. | ||
.hash "$argon2d$" "\e$argon2d\e$(v=(16|19)\e$)?m=[1-9][0-9]*,t=[1-9][0-9]*,p=[1-9][0-9]*\e$[A-Za-z0-9+/]{11,}\e$[A-Za-z0-9+/]{6,}" 4,294,967,295 8 "32 to 34,359,738,360 (256 default)" "32 to 34,359,738,360 (256 default)" "64 to 34,359,738,360 (128+ recommended)" "1 to 4,294,967,295" | ||
.Ss argon2_i | ||
argon2_i is one of the variants of Argon2 key derivation functions that won | ||
the Password Hashing Competition 2015. argon2i is designed for environments | ||
taht are not safe from side-channel attacks. Acceptable for new hashes, but | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same typo as above |
||
not widely supported. | ||
.hash "$argon2i$" "\e$argon2i\e$(v=(16|19)\e$)?m=[1-9][0-9]*,t=[1-9][0-9]*,p=[1-9][0-9]*\e$[A-Za-z0-9+/]{11,}\e$[A-Za-z0-9+/]{6,}" 4,294,967,295 8 "32 to 34,359,738,360 (256 default)" "32 to 34,359,738,360 (256 default)" "64 to 34,359,738,360 (128+ recommended)" "1 to 4,294,967,295" | ||
.Ss argon2_id | ||
argon2_id is one of the variants of Argon2 key derivation functions that won | ||
the Password Hashing Competition 2015. argon2id is a hybrid construction | ||
between argon2i and argon2d. Acceptable for new hashes, but not widely supported. | ||
.hash "$argon2id$" "\e$argon2id\e$(v=(16|19)\e$)?m=[1-9][0-9]*,t=[1-9][0-9]*,p=[1-9][0-9]*\e$[A-Za-z0-9+/]{11,}\e$[A-Za-z0-9+/]{6,}" 4,294,967,295 8 "32 to 34,359,738,360 (256 default)" "32 to 34,359,738,360 (256 default)" "64 to 34,359,738,360 (128+ recommended)" "1 to 4,294,967,295" | ||
.Ss argon2_ds | ||
argon2_ds is a later addition to the Argon2 family of key derivation functions. | ||
It adds a substitution-box mechanism to argon2d. Acceptable for new hashes, but | ||
not widely supported. | ||
.hash "$argon2ds$" "\e$argon2ds\e$(v=(16|19)\e$)?m=[1-9][0-9]*,t=[1-9][0-9]*,p=[1-9][0-9]*\e$[A-Za-z0-9+/]{11,}\e$[A-Za-z0-9+/]{6,}" 4,294,967,295 8 "32 to 34,359,738,360 (256 default)" "32 to 34,359,738,360 (256 default)" "64 to 34,359,738,360 (128+ recommended)" "1 to 4,294,967,295" | ||
.Ss bcrypt | ||
A hash based on the Blowfish block cipher, | ||
modified to have an extra-expensive key schedule. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,232 @@ | ||
/** | ||
* ISC License | ||
* | ||
* © 2022 Mattias Andrée <[email protected]> | ||
* | ||
* Permission to use, copy, modify, and/or distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
|
||
#include "crypt-port.h" | ||
#include "crypt-hashes.h" | ||
|
||
#if INCLUDE_argon2_d || INCLUDE_argon2_i || INCLUDE_argon2_id || INCLUDE_argon2_ds | ||
|
||
#include <libar2simplified.h> | ||
#include <libar2.h> | ||
|
||
#include <errno.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#define REMOVE_CONST(X)\ | ||
(*(void **)(void *)&(X)) | ||
|
||
#define DEFAULT_TAG_SIZE 32 | ||
#define DEFAULT_VERSION LIBAR2_ARGON2_VERSION_13 | ||
#define DEFAULT_M_COST 4096 | ||
#define DEFAULT_LANES 1 | ||
|
||
static void | ||
crypt_argon2_rn (const char *prefix, | ||
const char *phrase, size_t phr_size, | ||
const char *setting, size_t set_size, | ||
uint8_t *output, size_t o_size, | ||
void *scratch, size_t s_size) | ||
{ | ||
struct libar2_argon2_parameters *params = NULL; | ||
struct libar2_context ctx; | ||
char *settings_end; | ||
int free_scratch = 0; | ||
size_t required_s_size; | ||
size_t required_o_size; | ||
size_t offset; | ||
|
||
if (strncmp (setting, prefix, strlen (prefix)) || !set_size) | ||
goto einval; | ||
|
||
params = libar2simplified_decode (setting, NULL, &settings_end, NULL); | ||
if (!params) | ||
goto fail; | ||
if (*settings_end) | ||
goto einval; | ||
if (!params->hashlen) | ||
params->hashlen = DEFAULT_TAG_SIZE; | ||
|
||
required_o_size = libar2_encode_params (NULL, params) - 1; | ||
required_o_size += libar2_encode_base64 (NULL, NULL, params->hashlen); | ||
if (o_size < required_o_size) | ||
goto einval; | ||
|
||
libar2simplified_init_context (&ctx); | ||
ctx.autoerase_message = 0; /* allows `phrase` to be read-only */ | ||
required_s_size = libar2_hash_buf_size (params); | ||
if (required_s_size > s_size) | ||
{ | ||
if (free_scratch) | ||
free (scratch); | ||
scratch = malloc (required_s_size); | ||
if (!scratch) | ||
{ | ||
errno = ENOMEM; | ||
goto fail; | ||
} | ||
free_scratch = 1; | ||
} | ||
|
||
if (libar2_hash (scratch, REMOVE_CONST (phrase), phr_size, params, &ctx)) | ||
goto fail; | ||
|
||
offset = libar2_encode_params ((char *) output, params) - 1; | ||
libar2_encode_base64 ((char *) &output[offset], scratch, params->hashlen); | ||
|
||
free(params); | ||
errno = 0; | ||
return; | ||
|
||
einval: | ||
errno = EINVAL; | ||
fail: | ||
free (params); | ||
if (free_scratch) | ||
{ | ||
free (scratch); | ||
} | ||
return; | ||
} | ||
|
||
static void | ||
gensalt_argon2_rn (enum libar2_argon2_type type, | ||
unsigned long count, | ||
const uint8_t *rbytes, size_t nrbytes, | ||
uint8_t *output, size_t o_size) | ||
{ | ||
struct libar2_argon2_parameters params; | ||
|
||
if (count >> 31 > 1) | ||
goto erange; | ||
|
||
memset (¶ms, 0, sizeof (params)); | ||
params.type = type; | ||
params.version = DEFAULT_VERSION; | ||
params.t_cost = (uint_least32_t) count; | ||
params.m_cost = DEFAULT_M_COST; | ||
params.lanes = DEFAULT_LANES; | ||
params.salt = REMOVE_CONST (rbytes); /* libar2 does not use `const` because | ||
* it has an option to erase the salt */ | ||
params.saltlen = nrbytes; | ||
params.hashlen = DEFAULT_TAG_SIZE; | ||
if (libar2_validate_params (¶ms, NULL) != LIBAR2_OK) | ||
goto erange; | ||
|
||
if (o_size < libar2_encode_params (NULL, ¶ms)) | ||
goto erange; | ||
|
||
if (libar2_encode_params ((char *) output, ¶ms) > o_size) | ||
abort(); | ||
|
||
return; | ||
|
||
erange: | ||
errno = ERANGE; | ||
return; | ||
} | ||
|
||
#if INCLUDE_argon2_d | ||
void | ||
crypt_argon2_d_rn (const char *phrase, size_t phr_size, | ||
const char *setting, size_t set_size, | ||
uint8_t *output, size_t o_size, | ||
void *scratch, size_t s_size) | ||
{ | ||
crypt_argon2_rn ("$argon2d$", phrase, phr_size, setting, | ||
set_size, output, o_size, scratch, s_size); | ||
} | ||
#endif /* INCLUDE_argon2_d */ | ||
|
||
#if INCLUDE_argon2_i | ||
void | ||
crypt_argon2_i_rn (const char *phrase, size_t phr_size, | ||
const char *setting, size_t set_size, | ||
uint8_t *output, size_t o_size, | ||
void *scratch, size_t s_size) | ||
{ | ||
crypt_argon2_rn ("$argon2i$", phrase, phr_size, setting, | ||
set_size, output, o_size, scratch, s_size); | ||
} | ||
#endif /* INCLUDE_argon2_i */ | ||
|
||
#if INCLUDE_argon2_id | ||
void | ||
crypt_argon2_id_rn (const char *phrase, size_t phr_size, | ||
const char *setting, size_t set_size, | ||
uint8_t *output, size_t o_size, | ||
void *scratch, size_t s_size) | ||
{ | ||
crypt_argon2_rn ("$argon2id$", phrase, phr_size, setting, | ||
set_size, output, o_size, scratch, s_size); | ||
} | ||
#endif /* INCLUDE_argon2_id */ | ||
|
||
#if INCLUDE_argon2_ds | ||
void | ||
crypt_argon2_ds_rn (const char *phrase, size_t phr_size, | ||
const char *setting, size_t set_size, | ||
uint8_t *output, size_t o_size, | ||
void *scratch, size_t s_size) | ||
{ | ||
crypt_argon2_rn ("$argon2ds$", phrase, phr_size, setting, | ||
set_size, output, o_size, scratch, s_size); | ||
} | ||
#endif /* INCLUDE_argon2_ds */ | ||
|
||
#if INCLUDE_argon2_d | ||
void | ||
gensalt_argon2_d_rn (unsigned long count, | ||
const uint8_t *rbytes, size_t nrbytes, | ||
uint8_t *output, size_t o_size) | ||
{ | ||
gensalt_argon2_rn(LIBAR2_ARGON2D, count, rbytes, nrbytes, output, o_size); | ||
} | ||
#endif /* INCLUDE_argon2_d */ | ||
|
||
#if INCLUDE_argon2_i | ||
void | ||
gensalt_argon2_i_rn (unsigned long count, | ||
const uint8_t *rbytes, size_t nrbytes, | ||
uint8_t *output, size_t o_size) | ||
{ | ||
gensalt_argon2_rn(LIBAR2_ARGON2I, count, rbytes, nrbytes, output, o_size); | ||
} | ||
#endif /* INCLUDE_argon2_i */ | ||
|
||
#if INCLUDE_argon2_id | ||
void | ||
gensalt_argon2_id_rn (unsigned long count, | ||
const uint8_t *rbytes, size_t nrbytes, | ||
uint8_t *output, size_t o_size) | ||
{ | ||
gensalt_argon2_rn(LIBAR2_ARGON2ID, count, rbytes, nrbytes, output, o_size); | ||
} | ||
#endif /* INCLUDE_argon2_ds */ | ||
|
||
#if INCLUDE_argon2_ds | ||
void | ||
gensalt_argon2_ds_rn (unsigned long count, | ||
const uint8_t *rbytes, size_t nrbytes, | ||
uint8_t *output, size_t o_size) | ||
{ | ||
gensalt_argon2_rn(LIBAR2_ARGON2DS, count, rbytes, nrbytes, output, o_size); | ||
} | ||
#endif /* INCLUDE_argon2_ds */ | ||
|
||
#endif /* INCLUDE_argon2_d || INCLUDE_argon2_i || INCLUDE_argon2_id || INCLUDE_argon2_ds */ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo