memory hygiene code

This commit is contained in:
2026-10-19 10:03:36 +02:00
parent 96fe10d789
commit 4ae765f504
2 changed files with 46 additions and 0 deletions
+31
View File
@@ -0,0 +1,31 @@
#include "hygiene.h"
#include <stdint.h>
#include <string.h>
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;
}
+15
View File
@@ -0,0 +1,15 @@
// hygiene.h
// Memory hygiene utils - primarily for keys
#ifndef BRAVAIS_HYGIENE_H
#define BRAVAIS_HYGIENE_H
#include <stddef.h>
// 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