aboutsummaryrefslogtreecommitdiffstats
path: root/argon2/hash.c
diff options
context:
space:
mode:
authorMattias Andrée <m@maandree.se>2026-05-16 02:11:29 +0200
committerMattias Andrée <m@maandree.se>2026-05-16 02:11:29 +0200
commit8e7bfadb3eb9f43eb0b670b908b479325722fee5 (patch)
treee7f26f67cc4da2846e0ac3f8201e1e82de3c6112 /argon2/hash.c
parentm (diff)
downloadlibrecrypt-8e7bfadb3eb9f43eb0b670b908b479325722fee5.tar.gz
librecrypt-8e7bfadb3eb9f43eb0b670b908b479325722fee5.tar.bz2
librecrypt-8e7bfadb3eb9f43eb0b670b908b479325722fee5.tar.xz
Add WITH_LIBAR2SIMPLIFIED=false + work on fuzzing code
Signed-off-by: Mattias Andrée <m@maandree.se>
Diffstat (limited to '')
-rw-r--r--argon2/hash.c80
1 files changed, 79 insertions, 1 deletions
diff --git a/argon2/hash.c b/argon2/hash.c
index a40e54c..d06ec1e 100644
--- a/argon2/hash.c
+++ b/argon2/hash.c
@@ -3,7 +3,11 @@
#ifndef TEST
#include <libar2.h>
-#include <libar2simplified.h>
+#ifndef NO_LIBAR2SIMPLIFIED
+# include <libar2simplified.h>
+#else
+# define libar2simplified_init_context init_context
+#endif
#define RANGE(MIN, MAX) (uintmax_t)(MIN), (uintmax_t)(MAX)
@@ -11,6 +15,80 @@
#define REMOVE_CONST(X) (*(void **)(void *)&(X))
+#ifdef NO_LIBAR2SIMPLIFIED
+
+static void *
+allocate(size_t num, size_t size, size_t alignment, struct libar2_context *ctx)
+{
+ size_t extra, pad;
+ void *ptr;
+ unsigned char *p;
+ int err;
+
+ (void) ctx;
+
+ alignment = MAX(alignment, sizeof(void *));
+ extra = 2u * sizeof(size_t);
+ pad = -extra & (alignment - 1u);
+
+ err = ENOMEM;
+ if (num > (SIZE_MAX - extra - pad) / size ||
+ (err = posix_memalign(&ptr, alignment, pad + extra + (size *= num)))) {
+ errno = err;
+ return NULL;
+ }
+
+ p = ptr;
+ p += pad;
+ memcpy(p, &pad, sizeof(size_t));
+ p += sizeof(size_t);
+ memcpy(p, &size, sizeof(size_t));
+ p += sizeof(size_t);
+ return p;
+}
+
+
+static void
+deallocate(void *ptr, struct libar2_context *ctx)
+{
+ size_t size, pad;
+ char *p = ptr;
+
+ (void) ctx;
+
+ p -= sizeof(size_t);
+ memcpy(&size, p, sizeof(size_t));
+ p -= sizeof(size_t);
+ memcpy(&pad, p, sizeof(size_t));
+ p -= pad;
+
+ librecrypt_wipe(p, size + pad + 2u * sizeof(size_t));
+ free(p);
+}
+
+
+static int
+init_thread_pool(size_t desired, size_t *createdp, struct libar2_context *ctx)
+{
+ (void) desired;
+ (void) ctx;
+ *createdp = 0u;
+ return 0;
+}
+
+
+static void
+init_context(struct libar2_context *ctxp)
+{
+ memset(ctxp, 0, sizeof(*ctxp));
+ ctxp->allocate = &allocate;
+ ctxp->deallocate = &deallocate;
+ ctxp->init_thread_pool = &init_thread_pool;
+}
+
+#endif
+
+
int
librecrypt__argon2__hash(char *restrict out_buffer, size_t size, const char *phrase, size_t len,
const char *settings, size_t prefix, void *reserved)