memory hygiene code
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user