diff options
| author | Mattias Andrée <m@maandree.se> | 2026-05-21 17:12:20 +0200 |
|---|---|---|
| committer | Mattias Andrée <m@maandree.se> | 2026-05-21 17:12:20 +0200 |
| commit | b29f4153e83623f24bebe99976e1368ef31bb008 (patch) | |
| tree | 65473709df1194a2f9277dc5fb47add5e41430c0 /librecrypt_get_pepper_.c | |
| parent | Add (so far untested and undocument) support for pepper (diff) | |
| download | librecrypt-b29f4153e83623f24bebe99976e1368ef31bb008.tar.gz librecrypt-b29f4153e83623f24bebe99976e1368ef31bb008.tar.bz2 librecrypt-b29f4153e83623f24bebe99976e1368ef31bb008.tar.xz | |
Add support for custom hash functions
Signed-off-by: Mattias Andrée <m@maandree.se>
Diffstat (limited to 'librecrypt_get_pepper_.c')
| -rw-r--r-- | librecrypt_get_pepper_.c | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/librecrypt_get_pepper_.c b/librecrypt_get_pepper_.c new file mode 100644 index 0000000..df2a82d --- /dev/null +++ b/librecrypt_get_pepper_.c @@ -0,0 +1,152 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" +#ifndef TEST + + +#if defined(__GNUC__) +# pragma GCC diagnostic ignored "-Wswitch-enum" +#endif + + +struct pepper * +librecrypt_get_pepper_(LIBRECRYPT_CONTEXT *ctx, enum librecrypt_hash_algorithm algo, size_t len) +{ + switch (algo) { + +#if defined(SUPPORT_ARGON2I) || defined(SUPPORT_ARGON2D) || defined(SUPPORT_ARGON2ID) || defined(SUPPORT_ARGON2DS) +# if defined(SUPPORT_ARGON2I) + case LIBRECRYPT_ARGON2I_V1_0: + case LIBRECRYPT_ARGON2I_V1_3: +# endif +# if defined(SUPPORT_ARGON2D) + case LIBRECRYPT_ARGON2D_V1_0: + case LIBRECRYPT_ARGON2D_V1_3: +# endif +# if defined(SUPPORT_ARGON2ID) + case LIBRECRYPT_ARGON2ID_V1_0: + case LIBRECRYPT_ARGON2ID_V1_3: +# endif +# if defined(SUPPORT_ARGON2DS) + case LIBRECRYPT_ARGON2DS_V1_0: + case LIBRECRYPT_ARGON2DS_V1_3: +# endif +# if SIZE_MAX > UINT32_MAX /* LIBAR2_MAX_KEYLEN is just UINT32_MAX cast to size_t; keep it simple: don't include <libar2.h> */ + if (len > UINT32_MAX) { + errno = EINVAL; + return NULL; + } +# endif + return &ctx->peppers[algo]; +#endif + + default: + errno = ENOSYS; + return NULL; + } +} + + +#else + + +#define CHECK_DISABLED(ALGO)\ + do {\ + errno = 0;\ + EXPECT(librecrypt_get_pepper_(ctx, ALGO, 0u) == NULL);\ + EXPECT(errno == ENOSYS);\ + errno = 0;\ + EXPECT(librecrypt_get_pepper_(ctx, ALGO, 1u) == NULL);\ + EXPECT(errno == ENOSYS);\ + errno = 0;\ + EXPECT(librecrypt_get_pepper_(ctx, ALGO, 64u) == NULL);\ + EXPECT(errno == ENOSYS);\ + errno = 0;\ + EXPECT(librecrypt_get_pepper_(ctx, ALGO, SIZE_MAX) == NULL);\ + EXPECT(errno == ENOSYS);\ + } while (0) + +#define CHECK_ARGON2_(ALGO)\ + do {\ + pepper = librecrypt_get_pepper_(ctx, ALGO, 0u);\ + EXPECT(pepper != NULL);\ + EXPECT(pepper->data == NULL);\ + EXPECT(pepper->len == 0u);\ + pepper = librecrypt_get_pepper_(ctx, ALGO, 1u);\ + EXPECT(pepper != NULL);\ + EXPECT(pepper->data == NULL);\ + EXPECT(pepper->len == 0u);\ + pepper = librecrypt_get_pepper_(ctx, ALGO, UINT32_MAX);\ + EXPECT(pepper != NULL);\ + EXPECT(pepper->data == NULL);\ + EXPECT(pepper->len == 0u);\ + } while (0) + +#if SIZE_MAX > UINT32_MAX +# define CHECK_ARGON2(ALGO)\ + do {\ + CHECK_ARGON2_(ALGO);\ + errno = 0;\ + EXPECT(librecrypt_get_pepper_(ctx, ALGO, (size_t)UINT32_MAX + 1u) == NULL);\ + EXPECT(errno == EINVAL);\ + } while (0) +#else +# define CHECK_ARGON2(ALGO) CHECK_ARGON2_(ALGO) +#endif + + +int +main(void) +{ + LIBRECRYPT_CONTEXT *ctx; + struct pepper *pepper; + + SET_UP_ALARM(); + INIT_RESOURCE_TEST(); + + ctx = librecrypt_create_context(); + assert(ctx != NULL); + +#if defined(SUPPORT_ARGON2I) + CHECK_ARGON2(LIBRECRYPT_ARGON2I_V1_0); + CHECK_ARGON2(LIBRECRYPT_ARGON2I_V1_3); +#else + CHECK_DISABLED(LIBRECRYPT_ARGON2I_V1_0); + CHECK_DISABLED(LIBRECRYPT_ARGON2I_V1_3); +#endif + +#if defined(SUPPORT_ARGON2D) + CHECK_ARGON2(LIBRECRYPT_ARGON2D_V1_0); + CHECK_ARGON2(LIBRECRYPT_ARGON2D_V1_3); +#else + CHECK_DISABLED(LIBRECRYPT_ARGON2D_V1_0); + CHECK_DISABLED(LIBRECRYPT_ARGON2D_V1_3); +#endif + +#if defined(SUPPORT_ARGON2ID) + CHECK_ARGON2(LIBRECRYPT_ARGON2ID_V1_0); + CHECK_ARGON2(LIBRECRYPT_ARGON2ID_V1_3); +#else + CHECK_DISABLED(LIBRECRYPT_ARGON2ID_V1_0); + CHECK_DISABLED(LIBRECRYPT_ARGON2ID_V1_3); +#endif + +#if defined(SUPPORT_ARGON2DS) + CHECK_ARGON2(LIBRECRYPT_ARGON2DS_V1_0); + CHECK_ARGON2(LIBRECRYPT_ARGON2DS_V1_3); +#else + CHECK_DISABLED(LIBRECRYPT_ARGON2DS_V1_0); + CHECK_DISABLED(LIBRECRYPT_ARGON2DS_V1_3); +#endif + + CHECK_DISABLED(LIBRECRYPT_HASH_ALGORITHM_END); + + /* Further tested in librecrypt_set_pepper.c */ + + librecrypt_free_context(ctx); + + STOP_RESOURCE_TEST(); + return 0; +} + + +#endif |
