Skip to content

Commit c68b9c0

Browse files
committed
Minor changes of argon2 implementation.
* We continue using `LTC_ARGCHK()` until we've decided on something else. * Call the enum/struct the same as the typedef. * Some other small adjustments/fixes. Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
1 parent 458b392 commit c68b9c0

3 files changed

Lines changed: 37 additions & 46 deletions

File tree

src/headers/tomcrypt_misc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,12 @@ int base16_decode(const char *in, unsigned long inlen,
5555

5656
/* ---- Argon2 password hashing function (RFC 9106) ---- */
5757
#ifdef LTC_ARGON2
58-
typedef enum {
58+
typedef enum argon2_type {
5959
ARGON2_D = 0,
6060
ARGON2_I = 1,
6161
ARGON2_ID = 2
6262
} argon2_type;
63+
6364
int argon2_hash(const unsigned char *pwd, unsigned long pwdlen,
6465
const unsigned char *salt, unsigned long saltlen,
6566
const unsigned char *secret, unsigned long secretlen,

src/misc/argon2/argon2.c

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
#define ARGON2_BLAKE2B_OUTBYTES 64
2020

2121
/* 1024-byte memory block */
22-
typedef struct argon2_block_ {
22+
typedef struct argon2_block {
2323
ulong64 v[ARGON2_QWORDS_IN_BLOCK];
2424
} argon2_block;
2525

2626
/* instance state */
27-
typedef struct {
27+
typedef struct argon2_instance {
2828
argon2_block *memory;
2929
ulong32 passes;
3030
ulong32 memory_blocks;
@@ -35,7 +35,7 @@ typedef struct {
3535
} argon2_instance;
3636

3737
/* position within the memory matrix */
38-
typedef struct {
38+
typedef struct argon2_position {
3939
ulong32 pass;
4040
ulong32 lane;
4141
unsigned char slice;
@@ -491,17 +491,15 @@ int argon2_hash(const unsigned char *pwd, unsigned long pwdlen,
491491
int err;
492492

493493
LTC_ARGCHK(out != NULL);
494-
495-
/* Validate inputs */
496-
if (outlen < ARGON2_MIN_OUTLEN) return CRYPT_INVALID_ARG;
497-
if (pwd == NULL && pwdlen != 0) return CRYPT_INVALID_ARG;
498-
if (salt == NULL && saltlen != 0) return CRYPT_INVALID_ARG;
499-
if (secret == NULL && secretlen != 0) return CRYPT_INVALID_ARG;
500-
if (ad == NULL && adlen != 0) return CRYPT_INVALID_ARG;
501-
if (t_cost < 1) return CRYPT_INVALID_ARG;
502-
if (parallelism < 1) return CRYPT_INVALID_ARG;
503-
if (m_cost < 8 * parallelism) return CRYPT_INVALID_ARG;
504-
if (type != ARGON2_D && type != ARGON2_I && type != ARGON2_ID) return CRYPT_INVALID_ARG;
494+
LTC_ARGCHK(outlen >= ARGON2_MIN_OUTLEN);
495+
LTC_ARGCHK(pwd != NULL || pwdlen == 0);
496+
LTC_ARGCHK(salt != NULL || saltlen == 0);
497+
LTC_ARGCHK(secret != NULL || secretlen == 0);
498+
LTC_ARGCHK(ad != NULL || adlen == 0);
499+
LTC_ARGCHK(t_cost >= 1);
500+
LTC_ARGCHK(parallelism >= 1);
501+
LTC_ARGCHK(m_cost >= 8 * parallelism);
502+
LTC_ARGCHK(type == ARGON2_D || type == ARGON2_I || type == ARGON2_ID);
505503

506504
/* Align memory: ensure memory_blocks is a multiple of 4*parallelism */
507505
memory_blocks = (ulong32)m_cost;
@@ -525,9 +523,9 @@ int argon2_hash(const unsigned char *pwd, unsigned long pwdlen,
525523
unsigned long alloc_size = (unsigned long)memory_blocks * sizeof(argon2_block);
526524
/* overflow check */
527525
if (alloc_size / sizeof(argon2_block) != memory_blocks) {
528-
return CRYPT_MEM;
526+
return CRYPT_OVERFLOW;
529527
}
530-
instance.memory = (argon2_block *)XMALLOC(alloc_size);
528+
instance.memory = XMALLOC(alloc_size);
531529
if (instance.memory == NULL) {
532530
return CRYPT_MEM;
533531
}
@@ -548,8 +546,6 @@ int argon2_hash(const unsigned char *pwd, unsigned long pwdlen,
548546
err = s_fill_first_blocks(blockhash, &instance);
549547
if (err != CRYPT_OK) goto cleanup;
550548

551-
zeromem(blockhash, ARGON2_PREHASH_SEED_LEN);
552-
553549
/* Fill memory */
554550
s_fill_memory(&instance);
555551

tests/argon2_test.c

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,35 +48,29 @@ int argon2_test(void)
4848

4949
unsigned char tag[32];
5050

51-
/* Argon2d */
52-
DO(argon2_hash(password, sizeof(password),
53-
salt, sizeof(salt),
54-
secret, sizeof(secret),
55-
ad, sizeof(ad),
56-
3, 32, 4,
57-
ARGON2_D,
58-
tag, sizeof(tag)));
59-
COMPARE_TESTVECTOR(tag, sizeof(tag), expected_argon2d, sizeof(expected_argon2d), "Argon2d", 0);
51+
const struct {
52+
const char *name;
53+
argon2_type type;
54+
const unsigned char *expected;
55+
unsigned long elen;
56+
} argon_testcase[] = {
57+
{ "Argon2d", ARGON2_D, expected_argon2d, sizeof(expected_argon2d) },
58+
{ "Argon2i", ARGON2_I, expected_argon2i, sizeof(expected_argon2i) },
59+
{ "Argon2id", ARGON2_ID, expected_argon2id, sizeof(expected_argon2id) },
60+
};
6061

61-
/* Argon2i */
62-
DO(argon2_hash(password, sizeof(password),
63-
salt, sizeof(salt),
64-
secret, sizeof(secret),
65-
ad, sizeof(ad),
66-
3, 32, 4,
67-
ARGON2_I,
68-
tag, sizeof(tag)));
69-
COMPARE_TESTVECTOR(tag, sizeof(tag), expected_argon2i, sizeof(expected_argon2i), "Argon2i", 1);
62+
size_t n;
7063

71-
/* Argon2id */
72-
DO(argon2_hash(password, sizeof(password),
73-
salt, sizeof(salt),
74-
secret, sizeof(secret),
75-
ad, sizeof(ad),
76-
3, 32, 4,
77-
ARGON2_ID,
78-
tag, sizeof(tag)));
79-
COMPARE_TESTVECTOR(tag, sizeof(tag), expected_argon2id, sizeof(expected_argon2id), "Argon2id", 2);
64+
for (n = 0; n < LTC_ARRAY_SIZE(argon_testcase); ++n) {
65+
DO(argon2_hash(password, sizeof(password),
66+
salt, sizeof(salt),
67+
secret, sizeof(secret),
68+
ad, sizeof(ad),
69+
3, 32, 4,
70+
argon_testcase[n].type,
71+
tag, sizeof(tag)));
72+
COMPARE_TESTVECTOR(tag, sizeof(tag), argon_testcase[n].expected, argon_testcase[n].elen, argon_testcase[n].name, n);
73+
}
8074

8175
return CRYPT_OK;
8276
}

0 commit comments

Comments
 (0)