Changeset View
Changeset View
Standalone View
Standalone View
src/secp256k1/examples/examples_util.h
- This file was moved from src/secp256k1/examples/random.h.
| Show First 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | |||||
| static void print_hex(unsigned char* data, size_t size) { | static void print_hex(unsigned char* data, size_t size) { | ||||
| size_t i; | size_t i; | ||||
| printf("0x"); | printf("0x"); | ||||
| for (i = 0; i < size; i++) { | for (i = 0; i < size; i++) { | ||||
| printf("%02x", data[i]); | printf("%02x", data[i]); | ||||
| } | } | ||||
| printf("\n"); | printf("\n"); | ||||
| } | } | ||||
| #if defined(_MSC_VER) | |||||
| // For SecureZeroMemory | |||||
| #include <Windows.h> | |||||
| #endif | |||||
| /* Cleanses memory to prevent leaking sensitive info. Won't be optimized out. */ | |||||
| static SECP256K1_INLINE void secure_erase(void *ptr, size_t len) { | |||||
| #if defined(_MSC_VER) | |||||
| /* SecureZeroMemory is guaranteed not to be optimized out by MSVC. */ | |||||
| SecureZeroMemory(ptr, len); | |||||
| #elif defined(__GNUC__) | |||||
| /* We use a memory barrier that scares the compiler away from optimizing out the memset. | |||||
| * | |||||
| * Quoting Adam Langley <agl@google.com> in commit ad1907fe73334d6c696c8539646c21b11178f20f | |||||
| * in BoringSSL (ISC License): | |||||
| * As best as we can tell, this is sufficient to break any optimisations that | |||||
| * might try to eliminate "superfluous" memsets. | |||||
| * This method used in memzero_explicit() the Linux kernel, too. Its advantage is that it is | |||||
| * pretty efficient, because the compiler can still implement the memset() efficently, | |||||
| * just not remove it entirely. See "Dead Store Elimination (Still) Considered Harmful" by | |||||
| * Yang et al. (USENIX Security 2017) for more background. | |||||
| */ | |||||
| memset(ptr, 0, len); | |||||
| __asm__ __volatile__("" : : "r"(ptr) : "memory"); | |||||
| #else | |||||
| void *(*volatile const volatile_memset)(void *, int, size_t) = memset; | |||||
| volatile_memset(ptr, 0, len); | |||||
| #endif | |||||
| } | |||||