aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <m@maandree.se>2026-05-21 18:33:57 +0200
committerMattias Andrée <m@maandree.se>2026-05-21 18:33:57 +0200
commit0d63bcee00754c81231b0292abe70420943ab545 (patch)
tree9ad9f04e152b3efd559f9b2a867b3baf74167c71
parentAdd support for custom hash functions (diff)
downloadlibrecrypt-0d63bcee00754c81231b0292abe70420943ab545.tar.gz
librecrypt-0d63bcee00754c81231b0292abe70420943ab545.tar.bz2
librecrypt-0d63bcee00754c81231b0292abe70420943ab545.tar.xz
Add some test code
Signed-off-by: Mattias Andrée <m@maandree.se>
-rw-r--r--argon2/hash.c51
-rw-r--r--librecrypt_find_first_algorithm_.c91
-rw-r--r--librecrypt_settings_prefix.c26
3 files changed, 146 insertions, 22 deletions
diff --git a/argon2/hash.c b/argon2/hash.c
index 949841b..e6f129a 100644
--- a/argon2/hash.c
+++ b/argon2/hash.c
@@ -292,7 +292,7 @@ static int discarded_int;
static void
-check(const char *phrase, const char *settings, const char *hash, size_t hashlen, size_t scratchsize)
+check(const char *phrase, const char *settings, const char *hash, size_t hashlen, size_t scratchsize, LIBRECRYPT_CONTEXT *ctx)
{
size_t i, len = strlen(phrase);
size_t prefix = strlen(settings);
@@ -309,23 +309,23 @@ check(const char *phrase, const char *settings, const char *hash, size_t hashlen
assert(r > 0 && (size_t)r == hashlen);
CANARY_FILL(buf);
- EXPECT(librecrypt__argon2__hash(buf, sizeof(buf), phrase, len, settings, prefix, NULL) == 0);
+ EXPECT(librecrypt__argon2__hash(buf, sizeof(buf), phrase, len, settings, prefix, ctx) == 0);
EXPECT(!memcmp(expected, buf, hashlen));
CANARY_X_CHECK(buf, hashlen, scratchsize);
CANARY_FILL(buf);
- EXPECT(librecrypt__argon2__hash(buf, hashlen, phrase, len, settings, prefix, NULL) == 0);
+ EXPECT(librecrypt__argon2__hash(buf, hashlen, phrase, len, settings, prefix, ctx) == 0);
EXPECT(!memcmp(expected, buf, hashlen));
CANARY_X_CHECK(buf, hashlen, scratchsize);
CANARY_FILL(buf);
- EXPECT(librecrypt__argon2__hash(buf, 0u, phrase, len, settings, prefix, NULL) == 0);
+ EXPECT(librecrypt__argon2__hash(buf, 0u, phrase, len, settings, prefix, ctx) == 0);
CANARY_X_CHECK(buf, 0u, 0u);
- EXPECT(librecrypt__argon2__hash(NULL, 0u, phrase, len, settings, prefix, NULL) == 0);
+ EXPECT(librecrypt__argon2__hash(NULL, 0u, phrase, len, settings, prefix, ctx) == 0);
for (i = 1u; i <= hashlen * 2u; i++) {
CANARY_FILL(buf);
- EXPECT(librecrypt__argon2__hash(buf, i, phrase, len, settings, prefix, NULL) == 0);
+ EXPECT(librecrypt__argon2__hash(buf, i, phrase, len, settings, prefix, ctx) == 0);
EXPECT(!memcmp(expected, buf, MIN(i, hashlen)));
CANARY_X_CHECK(buf, MIN(i, hashlen), scratchsize);
}
@@ -338,10 +338,10 @@ check(const char *phrase, const char *settings, const char *hash, size_t hashlen
#define CHECK(PHRASE, CONF, HASHLEN, HASH)\
do {\
size_t scratchsize = GET_SCRATCH_SIZE(HASHLEN);\
- check(PHRASE, CONF HASH, HASH, (size_t)HASHLEN, scratchsize);\
+ check(PHRASE, CONF HASH, HASH, (size_t)HASHLEN, scratchsize, ctx);\
if ((size_t)HASHLEN == argon2__HASH_SIZE)\
- check(PHRASE, CONF, HASH, (size_t)HASHLEN, scratchsize);\
- check(PHRASE, CONF "*" #HASHLEN, HASH, (size_t)HASHLEN, scratchsize);\
+ check(PHRASE, CONF, HASH, (size_t)HASHLEN, scratchsize, ctx);\
+ check(PHRASE, CONF "*" #HASHLEN, HASH, (size_t)HASHLEN, scratchsize, ctx);\
} while (0)
@@ -350,7 +350,7 @@ check(const char *phrase, const char *settings, const char *hash, size_t hashlen
libtest_set_alloc_failure_in(3u);\
errno = 0;\
EXPECT(librecrypt__argon2__hash(NULL, 0u, NULL, (size_t)UINT32_MAX + 1u,\
- S(ALGO"m=1024,t=10,p=1$ABCDabcdABCDabcd$"), NULL) == -1);\
+ S(ALGO"m=1024,t=10,p=1$ABCDabcdABCDabcd$"), ctx) == -1);\
EXPECT(errno == EINVAL);\
libtest_set_alloc_failure_in(0u)
#else
@@ -363,11 +363,11 @@ check(const char *phrase, const char *settings, const char *hash, size_t hashlen
#define CHECK_BAD(ALGO)\
do {\
errno = 0;\
- EXPECT(librecrypt__argon2__hash(COMMON, S(ALGO"m=0,t=999999999999999999,p=0$AAAABBBB$*0"), NULL) == -1);\
+ EXPECT(librecrypt__argon2__hash(COMMON, S(ALGO"m=0,t=999999999999999999,p=0$AAAABBBB$*0"), ctx) == -1);\
EXPECT(errno == EINVAL);\
\
/* target `if (!salt_encoded)` */\
- EXPECT_ABORT(discarded_int = librecrypt__argon2__hash(COMMON, S(ALGO"m=1024,t=10,p=1$*10$"), NULL));\
+ EXPECT_ABORT(discarded_int = librecrypt__argon2__hash(COMMON, S(ALGO"m=1024,t=10,p=1$*10$"), ctx));\
\
if (!libtest_have_custom_malloc())\
break;\
@@ -377,19 +377,19 @@ check(const char *phrase, const char *settings, const char *hash, size_t hashlen
/* target `salt = malloc((size_t)r);` */\
libtest_set_alloc_failure_in(1u);\
errno = 0;\
- EXPECT(librecrypt__argon2__hash(COMMON, S(ALGO"m=1024,t=10,p=1$AAAABBBBCCCCDDDD$"), NULL) == -1);\
+ EXPECT(librecrypt__argon2__hash(COMMON, S(ALGO"m=1024,t=10,p=1$AAAABBBBCCCCDDDD$"), ctx) == -1);\
EXPECT(errno == ENOMEM);\
\
/* target `scratch = malloc(scratch_size);` */\
libtest_set_alloc_failure_in(2u);\
errno = 0;\
- EXPECT(librecrypt__argon2__hash(COMMON, S(ALGO"m=1024,t=10,p=1$BBBBCCCCDDDDAAAA$"), NULL) == -1);\
+ EXPECT(librecrypt__argon2__hash(COMMON, S(ALGO"m=1024,t=10,p=1$BBBBCCCCDDDDAAAA$"), ctx) == -1);\
EXPECT(errno == ENOMEM);\
\
/* target `libar2_hash` */\
libtest_set_alloc_failure_in(3u);\
errno = 0;\
- EXPECT(librecrypt__argon2__hash(COMMON, S(ALGO"m=1024,t=10,p=1$CCCCDDDDAAAABBBB$"), NULL) == -1);\
+ EXPECT(librecrypt__argon2__hash(COMMON, S(ALGO"m=1024,t=10,p=1$CCCCDDDDAAAABBBB$"), ctx) == -1);\
EXPECT(errno == ENOMEM);\
\
assert(!libtest_get_alloc_failure_in());\
@@ -404,18 +404,21 @@ void *volatile librecrypt_test_phony__ = (void *)(uintptr_t)4096u;
int
main(void)
{
+ LIBRECRYPT_CONTEXT *ctx = NULL;
char buf[1024];
SET_UP_ALARM();
INIT_TEST_ABORT();
INIT_RESOURCE_TEST();
+start_over:
+
#define GET_SCRATCH_SIZE(HASHLEN) ((HASHLEN) > 64u ? ((HASHLEN) + 63u) & ~31u : (HASHLEN))
#if defined(SUPPORT_ARGON2I)
# if SIZE_MAX > UINT32_MAX
errno = 0;
EXPECT(librecrypt__argon2__hash(NULL, 0u, phony, (size_t)UINT32_MAX + 1u, "$argon2i$m=256,t=2,p=1$c29tZXNhbHQ$",
- sizeof("$argon2i$m=256,t=2,p=1$c29tZXNhbHQ$"), NULL) == -1);
+ sizeof("$argon2i$m=256,t=2,p=1$c29tZXNhbHQ$"), ctx) == -1);
EXPECT(errno == EINVAL);
#else
if (libtest_have_custom_malloc()) {
@@ -427,7 +430,7 @@ main(void)
assert(r < sizeof(conf));
libtest_set_alloc_failure_in(1u);
errno = 0;
- EXPECT(librecrypt__argon2__hash(NULL, 0u, NULL, 0u, conf, strlen(conf), NULL) == -1);
+ EXPECT(librecrypt__argon2__hash(NULL, 0u, NULL, 0u, conf, strlen(conf), ctx) == -1);
EXPECT(errno == ENOMEM);
EXPECT(libtest_get_alloc_failure_in() == 0u);
libtest_set_alloc_failure_in(0u);
@@ -437,7 +440,7 @@ main(void)
assert(r < sizeof(conf));
libtest_set_alloc_failure_in(1u);
errno = 0;
- EXPECT(librecrypt__argon2__hash(NULL, 0u, NULL, 0u, conf, strlen(conf), NULL) == -1);
+ EXPECT(librecrypt__argon2__hash(NULL, 0u, NULL, 0u, conf, strlen(conf), ctx) == -1);
EXPECT(errno == ENOMEM);
EXPECT(libtest_get_alloc_failure_in() == 1u);
libtest_set_alloc_failure_in(0u);
@@ -464,11 +467,19 @@ main(void)
#endif
#undef GET_SCRATCH_SIZE
+ if (!ctx) {
+ ctx = librecrypt_create_context();
+ assert(ctx != NULL);
+ goto start_over;
+ }
+
+ /* TODO check with pepper */
+
+ librecrypt_free_context(ctx);
+
STOP_RESOURCE_TEST();
return 0;
}
-/* TODO check with pepper */
-/* TODO check with context but no pepper */
#endif
diff --git a/librecrypt_find_first_algorithm_.c b/librecrypt_find_first_algorithm_.c
index ff967c9..a339d93 100644
--- a/librecrypt_find_first_algorithm_.c
+++ b/librecrypt_find_first_algorithm_.c
@@ -15,7 +15,6 @@ librecrypt_find_first_algorithm_(const char *settings, size_t len, LIBRECRYPT_CO
* priority as library-provided hash functions */
if (!ctx)
goto library_provided;
- /* TODO test */
for (i = 0u; i < ctx->nalgos; i++) {
algo = &ctx->algos[i];
r = (*algo->is_algorithm)(settings, len);
@@ -68,10 +67,50 @@ library_provided:
} while (0)
+static unsigned prio1;
+static unsigned prio2;
+
+static unsigned
+is_algorithm_x1(const char *settings, size_t len)
+{
+ if (len && settings[0u] == 'x')
+ return prio1;
+ return 0;
+}
+
+static unsigned
+is_algorithm_x2(const char *settings, size_t len)
+{
+ if (len && settings[0u] == 'x')
+ return prio2;
+ return 0;
+}
+
+static unsigned
+is_algorithm_y(const char *settings, size_t len)
+{
+ if (len && settings[0u] == 'y')
+ return prio2;
+ return 0;
+}
+
+static unsigned
+is_algorithm_argon2(const char *settings, size_t len)
+{
+ if (len > sizeof("$argon2") - 1u)
+ if (!strncmp(settings, "$argon2", sizeof("$argon2") - 1u))
+ return prio1;
+ return 0;
+}
+
+
+
int
main(void)
{
const struct librecrypt_algorithm *algo;
+ struct librecrypt_algorithm custom[2];
+ LIBRECRYPT_CONTEXT *ctx;
SET_UP_ALARM();
INIT_RESOURCE_TEST();
@@ -84,6 +123,56 @@ main(void)
IF__argon2id__SUPPORTED(CHECK("$argon2id$"));
IF__argon2ds__SUPPORTED(CHECK("$argon2ds$"));
+ ctx = librecrypt_create_context();
+ assert(ctx != NULL);
+ librecrypt_set_custom_algorithms(ctx, custom, ELEMSOF(custom));
+
+ prio1 = 1u;
+ prio2 = 2u;
+ custom[0].is_algorithm = &is_algorithm_x1;
+ custom[1].is_algorithm = &is_algorithm_y;
+ EXPECT(librecrypt_find_first_algorithm_("x", 1u, ctx) == &custom[0u]);
+ EXPECT(librecrypt_find_first_algorithm_("y", 1u, ctx) == &custom[1u]);
+
+ prio1 = 1u;
+ prio2 = 1u;
+ custom[0].is_algorithm = &is_algorithm_x1;
+ custom[1].is_algorithm = &is_algorithm_y;
+ EXPECT(librecrypt_find_first_algorithm_("x", 1u, ctx) == &custom[0u]);
+ EXPECT(librecrypt_find_first_algorithm_("y", 1u, ctx) == &custom[1u]);
+
+ prio1 = 1u;
+ prio2 = 1u;
+ custom[0].is_algorithm = &is_algorithm_x1;
+ custom[1].is_algorithm = &is_algorithm_x2;
+ EXPECT(librecrypt_find_first_algorithm_("x", 1u, ctx) == &custom[0u]);
+ EXPECT(librecrypt_find_first_algorithm_("x", 1u, ctx) == &custom[0u]);
+
+ prio1 = 2u;
+ prio2 = 1u;
+ custom[0].is_algorithm = &is_algorithm_x1;
+ custom[1].is_algorithm = &is_algorithm_x2;
+ EXPECT(librecrypt_find_first_algorithm_("x", 1u, ctx) == &custom[0u]);
+ EXPECT(librecrypt_find_first_algorithm_("x", 1u, ctx) == &custom[0u]);
+
+ prio1 = 1u;
+ prio2 = 2u;
+ custom[0].is_algorithm = &is_algorithm_x1;
+ custom[1].is_algorithm = &is_algorithm_x2;
+ EXPECT(librecrypt_find_first_algorithm_("x", 1u, ctx) == &custom[1u]);
+ EXPECT(librecrypt_find_first_algorithm_("x", 1u, ctx) == &custom[1u]);
+
+ librecrypt_set_custom_algorithms(ctx, custom, 1u);
+ custom[0].is_algorithm = &is_algorithm_argon2;
+ prio1 = UINT_MAX;
+ EXPECT(librecrypt_find_first_algorithm_("$argon2id$", sizeof("$argon2id$") - 1u, ctx) == &custom[0u]);
+ prio1 = 1u;
+ EXPECT(librecrypt_find_first_algorithm_("$argon2id$", sizeof("$argon2id$") - 1u, ctx) == &custom[0u]);
+ prio1 = 0u;
+ EXPECT(librecrypt_find_first_algorithm_("$argon2id$", sizeof("$argon2id$") - 1u, ctx) != &custom[0u]);
+
+ librecrypt_free_context(ctx);
+
STOP_RESOURCE_TEST();
return 0;
}
diff --git a/librecrypt_settings_prefix.c b/librecrypt_settings_prefix.c
index fbb8efe..fbb0262 100644
--- a/librecrypt_settings_prefix.c
+++ b/librecrypt_settings_prefix.c
@@ -36,7 +36,7 @@ librecrypt_settings_prefix(const char *hash, size_t *hashsize_out, LIBRECRYPT_CO
if (!algo)
goto zero;
if (!algo->flexible_hash_size)
- goto zero; /* TODO test with custom hash function */
+ goto zero;
/* Get the hash size */
if (!librecrypt_scan_settings(&hash[ret], len - ret, "%^b",
@@ -79,9 +79,26 @@ out:
} while (0)
+static unsigned
+dumdum_is_algorithm(const char *settings, size_t len)
+{
+ (void) settings;
+ (void) len;
+ return 1u;
+}
+
+
+static struct librecrypt_algorithm dumdum = {
+ .is_algorithm = &dumdum_is_algorithm,
+ .flexible_hash_size = 0
+};
+
+
int
main(void)
{
+ LIBRECRYPT_CONTEXT *ctx;
+
SET_UP_ALARM();
INIT_RESOURCE_TEST();
@@ -150,6 +167,13 @@ main(void)
IF__argon2id__SUPPORTED(CHECK_ZERO("$argon2id$m=8,t=1,p=1$*99$", "#");)
IF__argon2ds__SUPPORTED(CHECK_ZERO("$argon2ds$m=8,t=1,p=1$*99$", "#");)
+ /* Test without flexible hash size (but with context I guess) */
+ ctx = librecrypt_create_context();
+ assert(ctx != NULL);
+ librecrypt_set_custom_algorithms(ctx, &dumdum, 1u);
+ CHECK_ZERO("$dumdum$", "abcd");
+ librecrypt_free_context(ctx);
+
STOP_RESOURCE_TEST();
return 0;
}