libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
securemem.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
13#include <cstddef>
14#include <promeki/namespace.h>
15#include <promeki/platform.h>
16#include <promeki/error.h>
17
18#if defined(PROMEKI_PLATFORM_WINDOWS)
19#include <Windows.h>
20#include <memoryapi.h>
21#elif defined(PROMEKI_PLATFORM_POSIX)
22#include <sys/mman.h>
23#include <string.h> // explicit_bzero
24#endif
25
26PROMEKI_NAMESPACE_BEGIN
27
42inline void secureZero(void *ptr, size_t size) {
43 if (ptr == nullptr || size == 0) return;
44#if defined(PROMEKI_PLATFORM_WINDOWS)
45 SecureZeroMemory(ptr, size);
46#elif defined(PROMEKI_LIBC_GLIBC) && \
47 (PROMEKI_LIBC_GLIBC_VERSION_MAJOR > 2 || \
48 (PROMEKI_LIBC_GLIBC_VERSION_MAJOR == 2 && PROMEKI_LIBC_GLIBC_VERSION_MINOR >= 25))
49 explicit_bzero(ptr, size);
50#elif defined(PROMEKI_PLATFORM_BSD)
51 explicit_bzero(ptr, size);
52#else
53 volatile unsigned char *p = static_cast<volatile unsigned char *>(ptr);
54 while (size--) *p++ = 0;
55#endif
56}
57
71Error secureLock(void *ptr, size_t size);
72
85Error secureUnlock(void *ptr, size_t size);
86
87PROMEKI_NAMESPACE_END
88
89#endif // PROMEKI_ENABLE_CORE