Skip to content
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

Argon2i mem #359

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ RUN = argon2
BENCH = bench
GENKAT = genkat

# NO_THREADS = 1
# Increment on an ABI breaking change
ABI_VERSION = 1

Expand Down
96 changes: 17 additions & 79 deletions include/argon2.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,11 @@ typedef struct Argon2_Context {

/* Argon2 primitive type */
typedef enum Argon2_type {
Argon2_d = 0,
Argon2_i = 1,
Argon2_id = 2
Argon2_i = 1
} argon2_type;

/* Version of the algorithm */
typedef enum Argon2_version {
ARGON2_VERSION_10 = 0x10,
ARGON2_VERSION_13 = 0x13,
ARGON2_VERSION_NUMBER = ARGON2_VERSION_13
} argon2_version;
Expand Down Expand Up @@ -289,35 +286,6 @@ ARGON2_PUBLIC int argon2i_hash_raw(const uint32_t t_cost, const uint32_t m_cost,
const size_t saltlen, void *hash,
const size_t hashlen);

ARGON2_PUBLIC int argon2d_hash_encoded(const uint32_t t_cost,
const uint32_t m_cost,
const uint32_t parallelism,
const void *pwd, const size_t pwdlen,
const void *salt, const size_t saltlen,
const size_t hashlen, char *encoded,
const size_t encodedlen);

ARGON2_PUBLIC int argon2d_hash_raw(const uint32_t t_cost, const uint32_t m_cost,
const uint32_t parallelism, const void *pwd,
const size_t pwdlen, const void *salt,
const size_t saltlen, void *hash,
const size_t hashlen);

ARGON2_PUBLIC int argon2id_hash_encoded(const uint32_t t_cost,
const uint32_t m_cost,
const uint32_t parallelism,
const void *pwd, const size_t pwdlen,
const void *salt, const size_t saltlen,
const size_t hashlen, char *encoded,
const size_t encodedlen);

ARGON2_PUBLIC int argon2id_hash_raw(const uint32_t t_cost,
const uint32_t m_cost,
const uint32_t parallelism, const void *pwd,
const size_t pwdlen, const void *salt,
const size_t saltlen, void *hash,
const size_t hashlen);

/* generic function underlying the above ones */
ARGON2_PUBLIC int argon2_hash(const uint32_t t_cost, const uint32_t m_cost,
const uint32_t parallelism, const void *pwd,
Expand All @@ -337,26 +305,10 @@ ARGON2_PUBLIC int argon2_hash(const uint32_t t_cost, const uint32_t m_cost,
ARGON2_PUBLIC int argon2i_verify(const char *encoded, const void *pwd,
const size_t pwdlen);

ARGON2_PUBLIC int argon2d_verify(const char *encoded, const void *pwd,
const size_t pwdlen);

ARGON2_PUBLIC int argon2id_verify(const char *encoded, const void *pwd,
const size_t pwdlen);

/* generic function underlying the above ones */
ARGON2_PUBLIC int argon2_verify(const char *encoded, const void *pwd,
const size_t pwdlen, argon2_type type);

/**
* Argon2d: Version of Argon2 that picks memory blocks depending
* on the password and salt. Only for side-channel-free
* environment!!
*****
* @param context Pointer to current Argon2 context
* @return Zero if successful, a non zero error code otherwise
*/
ARGON2_PUBLIC int argon2d_ctx(argon2_context *context);

/**
* Argon2i: Version of Argon2 that picks memory blocks
* independent on the password and salt. Good for side-channels,
Expand All @@ -367,26 +319,6 @@ ARGON2_PUBLIC int argon2d_ctx(argon2_context *context);
*/
ARGON2_PUBLIC int argon2i_ctx(argon2_context *context);

/**
* Argon2id: Version of Argon2 where the first half-pass over memory is
* password-independent, the rest are password-dependent (on the password and
* salt). OK against side channels (they reduce to 1/2-pass Argon2i), and
* better with w.r.t. tradeoff attacks (similar to Argon2d).
*****
* @param context Pointer to current Argon2 context
* @return Zero if successful, a non zero error code otherwise
*/
ARGON2_PUBLIC int argon2id_ctx(argon2_context *context);

/**
* Verify if a given password is correct for Argon2d hashing
* @param context Pointer to current Argon2 context
* @param hash The password hash to verify. The length of the hash is
* specified by the context outlen member
* @return Zero if successful, a non zero error code otherwise
*/
ARGON2_PUBLIC int argon2d_verify_ctx(argon2_context *context, const char *hash);

/**
* Verify if a given password is correct for Argon2i hashing
* @param context Pointer to current Argon2 context
Expand All @@ -396,16 +328,6 @@ ARGON2_PUBLIC int argon2d_verify_ctx(argon2_context *context, const char *hash);
*/
ARGON2_PUBLIC int argon2i_verify_ctx(argon2_context *context, const char *hash);

/**
* Verify if a given password is correct for Argon2id hashing
* @param context Pointer to current Argon2 context
* @param hash The password hash to verify. The length of the hash is
* specified by the context outlen member
* @return Zero if successful, a non zero error code otherwise
*/
ARGON2_PUBLIC int argon2id_verify_ctx(argon2_context *context,
const char *hash);

/* generic function underlying the above ones */
ARGON2_PUBLIC int argon2_verify_ctx(argon2_context *context, const char *hash,
argon2_type type);
Expand All @@ -430,6 +352,22 @@ ARGON2_PUBLIC size_t argon2_encodedlen(uint32_t t_cost, uint32_t m_cost,
uint32_t parallelism, uint32_t saltlen,
uint32_t hashlen, argon2_type type);

/* ARIONUM-Customization Begin */

/**
* Exposes the encode_string function, for raw context-level
* end to end usage of this library.
* @param dst Destination encoded buffer
* @param dst_len The length of the buffer
* @param ctx The argon2 context to use to generate an encoded string
* @param type The argon2 variant used in encoding.
* @return Zero if successful, non-zero code otherwise.
*/
ARGON2_PUBLIC int encode_ctx(char *dst, const size_t dst_len, argon2_context *ctx,
argon2_type type);

/* ARIONUM-Customization End */

#if defined(__cplusplus)
}
#endif
Expand Down
87 changes: 16 additions & 71 deletions src/argon2.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@

const char *argon2_type2string(argon2_type type, int uppercase) {
switch (type) {
case Argon2_d:
return uppercase ? "Argon2d" : "argon2d";
case Argon2_i:
return uppercase ? "Argon2i" : "argon2i";
case Argon2_id:
return uppercase ? "Argon2id" : "argon2id";
}

return NULL;
Expand All @@ -46,7 +42,7 @@ int argon2_ctx(argon2_context *context, argon2_type type) {
return result;
}

if (Argon2_d != type && Argon2_i != type && Argon2_id != type) {
if (Argon2_i != type) {
return ARGON2_INCORRECT_TYPE;
}

Expand Down Expand Up @@ -176,64 +172,39 @@ int argon2_hash(const uint32_t t_cost, const uint32_t m_cost,
return ARGON2_OK;
}

int argon2i_hash_encoded(const uint32_t t_cost, const uint32_t m_cost,
const uint32_t parallelism, const void *pwd,
const size_t pwdlen, const void *salt,
const size_t saltlen, const size_t hashlen,
char *encoded, const size_t encodedlen) {
/* ARIONUM-Customization Begin */

return argon2_hash(t_cost, m_cost, parallelism, pwd, pwdlen, salt, saltlen,
NULL, hashlen, encoded, encodedlen, Argon2_i,
ARGON2_VERSION_NUMBER);
int encode_ctx(char *dst, const size_t dst_len, argon2_context *ctx,
argon2_type type) {
if (dst && dst_len) {
if (encode_string(dst, dst_len, ctx, type) != ARGON2_OK) {
clear_internal_memory(dst, dst_len);
return ARGON2_ENCODING_FAIL;
}
}
return ARGON2_OK;
}

int argon2i_hash_raw(const uint32_t t_cost, const uint32_t m_cost,
const uint32_t parallelism, const void *pwd,
const size_t pwdlen, const void *salt,
const size_t saltlen, void *hash, const size_t hashlen) {

return argon2_hash(t_cost, m_cost, parallelism, pwd, pwdlen, salt, saltlen,
hash, hashlen, NULL, 0, Argon2_i, ARGON2_VERSION_NUMBER);
}
/* ARIONUM-Customization End */

int argon2d_hash_encoded(const uint32_t t_cost, const uint32_t m_cost,
int argon2i_hash_encoded(const uint32_t t_cost, const uint32_t m_cost,
const uint32_t parallelism, const void *pwd,
const size_t pwdlen, const void *salt,
const size_t saltlen, const size_t hashlen,
char *encoded, const size_t encodedlen) {

return argon2_hash(t_cost, m_cost, parallelism, pwd, pwdlen, salt, saltlen,
NULL, hashlen, encoded, encodedlen, Argon2_d,
NULL, hashlen, encoded, encodedlen, Argon2_i,
ARGON2_VERSION_NUMBER);
}

int argon2d_hash_raw(const uint32_t t_cost, const uint32_t m_cost,
int argon2i_hash_raw(const uint32_t t_cost, const uint32_t m_cost,
const uint32_t parallelism, const void *pwd,
const size_t pwdlen, const void *salt,
const size_t saltlen, void *hash, const size_t hashlen) {

return argon2_hash(t_cost, m_cost, parallelism, pwd, pwdlen, salt, saltlen,
hash, hashlen, NULL, 0, Argon2_d, ARGON2_VERSION_NUMBER);
}

int argon2id_hash_encoded(const uint32_t t_cost, const uint32_t m_cost,
const uint32_t parallelism, const void *pwd,
const size_t pwdlen, const void *salt,
const size_t saltlen, const size_t hashlen,
char *encoded, const size_t encodedlen) {

return argon2_hash(t_cost, m_cost, parallelism, pwd, pwdlen, salt, saltlen,
NULL, hashlen, encoded, encodedlen, Argon2_id,
ARGON2_VERSION_NUMBER);
}

int argon2id_hash_raw(const uint32_t t_cost, const uint32_t m_cost,
const uint32_t parallelism, const void *pwd,
const size_t pwdlen, const void *salt,
const size_t saltlen, void *hash, const size_t hashlen) {
return argon2_hash(t_cost, m_cost, parallelism, pwd, pwdlen, salt, saltlen,
hash, hashlen, NULL, 0, Argon2_id,
ARGON2_VERSION_NUMBER);
hash, hashlen, NULL, 0, Argon2_i, ARGON2_VERSION_NUMBER);
}

static int argon2_compare(const uint8_t *b1, const uint8_t *b2, size_t len) {
Expand Down Expand Up @@ -317,28 +288,10 @@ int argon2i_verify(const char *encoded, const void *pwd, const size_t pwdlen) {
return argon2_verify(encoded, pwd, pwdlen, Argon2_i);
}

int argon2d_verify(const char *encoded, const void *pwd, const size_t pwdlen) {

return argon2_verify(encoded, pwd, pwdlen, Argon2_d);
}

int argon2id_verify(const char *encoded, const void *pwd, const size_t pwdlen) {

return argon2_verify(encoded, pwd, pwdlen, Argon2_id);
}

int argon2d_ctx(argon2_context *context) {
return argon2_ctx(context, Argon2_d);
}

int argon2i_ctx(argon2_context *context) {
return argon2_ctx(context, Argon2_i);
}

int argon2id_ctx(argon2_context *context) {
return argon2_ctx(context, Argon2_id);
}

int argon2_verify_ctx(argon2_context *context, const char *hash,
argon2_type type) {
int ret = argon2_ctx(context, type);
Expand All @@ -353,18 +306,10 @@ int argon2_verify_ctx(argon2_context *context, const char *hash,
return ARGON2_OK;
}

int argon2d_verify_ctx(argon2_context *context, const char *hash) {
return argon2_verify_ctx(context, hash, Argon2_d);
}

int argon2i_verify_ctx(argon2_context *context, const char *hash) {
return argon2_verify_ctx(context, hash, Argon2_i);
}

int argon2id_verify_ctx(argon2_context *context, const char *hash) {
return argon2_verify_ctx(context, hash, Argon2_id);
}

const char *argon2_error_message(int error_code) {
switch (error_code) {
case ARGON2_OK:
Expand Down
4 changes: 2 additions & 2 deletions src/bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static void benchmark() {
uint32_t t_cost = 3;
uint32_t m_cost;
uint32_t thread_test[4] = {1, 2, 4, 8};
argon2_type types[3] = {Argon2_i, Argon2_d, Argon2_id};
argon2_type types[1] = {Argon2_i};

memset(pwd_array, 0, inlen);
memset(salt_array, 1, inlen);
Expand All @@ -74,7 +74,7 @@ static void benchmark() {
uint32_t thread_n = thread_test[i];

unsigned j;
for (j = 0; j < 3; ++j) {
for (j = 0; j < 1; ++j) {
clock_t start_time, stop_time;
uint64_t start_cycles, stop_cycles;
uint64_t delta;
Expand Down
8 changes: 5 additions & 3 deletions src/blake2/blake2b.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ int blake2b_init_key(blake2b_state *S, size_t outlen, const void *key,
memcpy(block, key, keylen);
blake2b_update(S, block, BLAKE2B_BLOCKBYTES);
/* Burn the key from stack */
clear_internal_memory(block, BLAKE2B_BLOCKBYTES);
/*unsafe: clear_internal_memory(block, BLAKE2B_BLOCKBYTES);*/
}
return 0;
}
Expand Down Expand Up @@ -285,9 +285,11 @@ int blake2b_final(blake2b_state *S, void *out, size_t outlen) {
}

memcpy(out, buffer, S->outlen);
/* unsafe
clear_internal_memory(buffer, sizeof(buffer));
clear_internal_memory(S->buf, sizeof(S->buf));
clear_internal_memory(S->h, sizeof(S->h));
*/
return 0;
}

Expand Down Expand Up @@ -325,7 +327,7 @@ int blake2b(void *out, size_t outlen, const void *in, size_t inlen,
ret = blake2b_final(&S, out, outlen);

fail:
clear_internal_memory(&S, sizeof(S));
/*unsafe: clear_internal_memory(&S, sizeof(S));*/
return ret;
}

Expand Down Expand Up @@ -383,7 +385,7 @@ int blake2b_long(void *pout, size_t outlen, const void *in, size_t inlen) {
memcpy(out, out_buffer, toproduce);
}
fail:
clear_internal_memory(&blake_state, sizeof(blake_state));
/*unsafe: clear_internal_memory(&blake_state, sizeof(blake_state));*/
return ret;
#undef TRY
}
Expand Down
Loading