dep-libtomcrypt/tests/argon2_test.c
Steffen Jaeckel c68b9c0b3f 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>
2026-04-12 16:06:53 +02:00

85 lines
2.7 KiB
C

/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
#include <tomcrypt_test.h>
#ifdef LTC_ARGON2
/* RFC 9106 test vectors */
int argon2_test(void)
{
static const unsigned char password[32] = {
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
};
static const unsigned char salt[16] = {
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02
};
static const unsigned char secret[8] = {
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
};
static const unsigned char ad[12] = {
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
0x04, 0x04, 0x04, 0x04
};
static const unsigned char expected_argon2d[32] = {
0x51, 0x2b, 0x39, 0x1b, 0x6f, 0x11, 0x62, 0x97,
0x53, 0x71, 0xd3, 0x09, 0x19, 0x73, 0x42, 0x94,
0xf8, 0x68, 0xe3, 0xbe, 0x39, 0x84, 0xf3, 0xc1,
0xa1, 0x3a, 0x4d, 0xb9, 0xfa, 0xbe, 0x4a, 0xcb
};
static const unsigned char expected_argon2i[32] = {
0xc8, 0x14, 0xd9, 0xd1, 0xdc, 0x7f, 0x37, 0xaa,
0x13, 0xf0, 0xd7, 0x7f, 0x24, 0x94, 0xbd, 0xa1,
0xc8, 0xde, 0x6b, 0x01, 0x6d, 0xd3, 0x88, 0xd2,
0x99, 0x52, 0xa4, 0xc4, 0x67, 0x2b, 0x6c, 0xe8
};
static const unsigned char expected_argon2id[32] = {
0x0d, 0x64, 0x0d, 0xf5, 0x8d, 0x78, 0x76, 0x6c,
0x08, 0xc0, 0x37, 0xa3, 0x4a, 0x8b, 0x53, 0xc9,
0xd0, 0x1e, 0xf0, 0x45, 0x2d, 0x75, 0xb6, 0x5e,
0xb5, 0x25, 0x20, 0xe9, 0x6b, 0x01, 0xe6, 0x59
};
unsigned char tag[32];
const struct {
const char *name;
argon2_type type;
const unsigned char *expected;
unsigned long elen;
} argon_testcase[] = {
{ "Argon2d", ARGON2_D, expected_argon2d, sizeof(expected_argon2d) },
{ "Argon2i", ARGON2_I, expected_argon2i, sizeof(expected_argon2i) },
{ "Argon2id", ARGON2_ID, expected_argon2id, sizeof(expected_argon2id) },
};
size_t n;
for (n = 0; n < LTC_ARRAY_SIZE(argon_testcase); ++n) {
DO(argon2_hash(password, sizeof(password),
salt, sizeof(salt),
secret, sizeof(secret),
ad, sizeof(ad),
3, 32, 4,
argon_testcase[n].type,
tag, sizeof(tag)));
COMPARE_TESTVECTOR(tag, sizeof(tag), argon_testcase[n].expected, argon_testcase[n].elen, argon_testcase[n].name, n);
}
return CRYPT_OK;
}
#else
int argon2_test(void)
{
return CRYPT_NOP;
}
#endif