From 4ae765f504f22253530730b1833b9eeb80926c3f Mon Sep 17 00:00:00 2001 From: DcruBro Date: Mon, 19 Oct 2026 10:03:36 +0200 Subject: [PATCH] memory hygiene code --- src/util/hygiene.c | 31 +++++++++++++++++++++++++++++++ src/util/hygiene.h | 15 +++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/util/hygiene.c create mode 100644 src/util/hygiene.h diff --git a/src/util/hygiene.c b/src/util/hygiene.c new file mode 100644 index 0000000..ef4ffa8 --- /dev/null +++ b/src/util/hygiene.c @@ -0,0 +1,31 @@ +#include "hygiene.h" + +#include +#include + +static void* (*volatile volatileMemset)(void*, int, size_t) = memset; + +void bravais_hygiene_zero(void* ptr, size_t len) { + if (!ptr || len <= 0) { + return; + } + + volatileMemset(ptr, 0, len); +} + +bool bravais_ct_equal(const void* a, const void* b, size_t len) { + if (!a || !b || len <= 0) { + return false; + } + + const volatile uint8_t* left = a; + const volatile uint8_t* right = b; + + uint8_t diff = 0; + + for (size_t i = 0; i < len; i++) { + diff |= (uint8_t)(left[i] ^ right[i]); + } + + return diff == 0; +} diff --git a/src/util/hygiene.h b/src/util/hygiene.h new file mode 100644 index 0000000..37866b4 --- /dev/null +++ b/src/util/hygiene.h @@ -0,0 +1,15 @@ +// hygiene.h +// Memory hygiene utils - primarily for keys + +#ifndef BRAVAIS_HYGIENE_H +#define BRAVAIS_HYGIENE_H + +#include + +// Zero out a memory region +void bravais_hygiene_zero(void* ptr, size_t len); + +// Constant-time equality check +bool bravais_ct_equal(const void* a, const void* b, size_t len); + +#endif // BRAVAIS_HYGIENE_H