From 8a0e403f4f2ffbbbb81171c6b3b0080634baf149 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Wed, 20 May 2026 19:55:12 +0200 Subject: Add librecrypt_is_enabled MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- librecrypt_is_enabled.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 librecrypt_is_enabled.c (limited to 'librecrypt_is_enabled.c') diff --git a/librecrypt_is_enabled.c b/librecrypt_is_enabled.c new file mode 100644 index 0000000..1bc9429 --- /dev/null +++ b/librecrypt_is_enabled.c @@ -0,0 +1,86 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" +#ifndef TEST + + +#define INCLUDE(ALGO, VAL) IF__##ALGO##__SUPPORTED((UINT64_C(1) << (VAL)) |) + +static const uint64_t enabled = + INCLUDE(argon2i, LIBRECRYPT_ARGON2I_V1_0) + INCLUDE(argon2i, LIBRECRYPT_ARGON2I_V1_3) + INCLUDE(argon2d, LIBRECRYPT_ARGON2D_V1_0) + INCLUDE(argon2d, LIBRECRYPT_ARGON2D_V1_3) + INCLUDE(argon2id, LIBRECRYPT_ARGON2ID_V1_0) + INCLUDE(argon2id, LIBRECRYPT_ARGON2ID_V1_3) + INCLUDE(argon2ds, LIBRECRYPT_ARGON2DS_V1_0) + INCLUDE(argon2ds, LIBRECRYPT_ARGON2DS_V1_3) + UINT64_C(0); + + +int +librecrypt_is_enabled(enum librecrypt_hash_algorithm algo) +{ +#if defined(__clang__) +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wtautological-unsigned-enum-zero-compare" +#endif + + if (algo < 0 || algo >= 64) + return 0; + +#if defined(__clang__) +# pragma clang diagnostic pop +#endif + + /* Version 1.0 and 1.3 of Argon2 are supported in all + * versions of libar2, so there is no need to check + * libar2_latest_argon2_version*/ + + return (int)(enabled >> (unsigned)algo) & 1; +} + + +#else + + +#if defined(__clang__) +# pragma clang diagnostic ignored "-Wassign-enum" +# pragma clang diagnostic ignored "-Wsign-conversion" +#endif + + +#define CHECK(ALGO, EXPECTED)\ + do {\ + EXPECT(librecrypt_is_enabled(ALGO) == (EXPECTED));\ + if ((int)(ALGO) > highest)\ + highest = (int)(ALGO);\ + } while (0) + + +int +main(void) +{ + int i, highest = -1; + + SET_UP_ALARM(); + INIT_RESOURCE_TEST(); + + CHECK((enum librecrypt_hash_algorithm)-1, 0); + CHECK(LIBRECRYPT_ARGON2I_V1_0, IF__argon2i__SUPPORTED(1 + ) 0); + CHECK(LIBRECRYPT_ARGON2I_V1_3, IF__argon2i__SUPPORTED(1 + ) 0); + CHECK(LIBRECRYPT_ARGON2D_V1_0, IF__argon2i__SUPPORTED(1 + ) 0); + CHECK(LIBRECRYPT_ARGON2D_V1_3, IF__argon2i__SUPPORTED(1 + ) 0); + CHECK(LIBRECRYPT_ARGON2ID_V1_0, IF__argon2i__SUPPORTED(1 + ) 0); + CHECK(LIBRECRYPT_ARGON2ID_V1_3, IF__argon2i__SUPPORTED(1 + ) 0); + CHECK(LIBRECRYPT_ARGON2DS_V1_0, IF__argon2i__SUPPORTED(1 + ) 0); + CHECK(LIBRECRYPT_ARGON2DS_V1_3, IF__argon2i__SUPPORTED(1 + ) 0); + + for (i = 0; i < 1024 && highest != INT_MAX; i++) + CHECK((enum librecrypt_hash_algorithm)(highest + 1), 0); + + STOP_RESOURCE_TEST(); + return 0; +} + + +#endif -- cgit v1.2.3-70-g09d2