Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1/*************************************************************************2* Copyright (c) 2020-2021 Elichai Turkel *3* Distributed under the CC0 software license, see the accompanying file *4* EXAMPLES_COPYING or https://creativecommons.org/publicdomain/zero/1.0 *5*************************************************************************/7/*8* This file is an attempt at collecting best practice methods for obtaining randomness with different operating systems.9* It may be out-of-date. Consult the documentation of the operating system before considering to use the methods below.10*11* Platform randomness sources:12* Linux -> `getrandom(2)`(`sys/random.h`), if not available `/dev/urandom` should be used. http://man7.org/linux/man-pages/man2/getrandom.2.html, https://linux.die.net/man/4/urandom13* macOS -> `getentropy(2)`(`sys/random.h`), if not available `/dev/urandom` should be used. https://www.unix.com/man-page/mojave/2/getentropy, https://opensource.apple.com/source/xnu/xnu-517.12.7/bsd/man/man4/random.4.auto.html14* FreeBSD -> `getrandom(2)`(`sys/random.h`), if not available `kern.arandom` should be used. https://www.freebsd.org/cgi/man.cgi?query=getrandom, https://www.freebsd.org/cgi/man.cgi?query=random&sektion=415* OpenBSD -> `getentropy(2)`(`unistd.h`), if not available `/dev/urandom` should be used. https://man.openbsd.org/getentropy, https://man.openbsd.org/urandom16* Windows -> `BCryptGenRandom`(`bcrypt.h`). https://docs.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom17*/19#if defined(_WIN32)20/*21* The defined WIN32_NO_STATUS macro disables return code definitions in22* windows.h, which avoids "macro redefinition" MSVC warnings in ntstatus.h.23*/24#define WIN32_NO_STATUS25#include <windows.h>26#undef WIN32_NO_STATUS27#include <ntstatus.h>28#include <bcrypt.h>29#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)30#include <sys/random.h>31#elif defined(__OpenBSD__)32#include <unistd.h>33#else34#error "Couldn't identify the OS"35#endif37#include <stddef.h>38#include <limits.h>39#include <stdio.h>42/* Returns 1 on success, and 0 on failure. */43static int fill_random(unsigned char* data, size_t size) {44#if defined(_WIN32)45NTSTATUS res = BCryptGenRandom(NULL, data, size, BCRYPT_USE_SYSTEM_PREFERRED_RNG);46if (res != STATUS_SUCCESS || size > ULONG_MAX) {47return 0;48} else {49return 1;50}51#elif defined(__linux__) || defined(__FreeBSD__)52/* If `getrandom(2)` is not available you should fallback to /dev/urandom */53ssize_t res = getrandom(data, size, 0);54if (res < 0 || (size_t)res != size ) {55return 0;56} else {57return 1;58}59#elif defined(__APPLE__) || defined(__OpenBSD__)60/* If `getentropy(2)` is not available you should fallback to either61* `SecRandomCopyBytes` or /dev/urandom */62int res = getentropy(data, size);63if (res == 0) {64return 1;65} else {66return 0;67}68#endif69return 0;70}72static void print_hex(unsigned char* data, size_t size) {73size_t i;74printf("0x");75for (i = 0; i < size; i++) {76printf("%02x", data[i]);77}78printf("\n");79}81#if defined(_MSC_VER)82// For SecureZeroMemory83#include <Windows.h>84#endif85/* Cleanses memory to prevent leaking sensitive info. Won't be optimized out. */86static void secure_erase(void *ptr, size_t len) {87#if defined(_MSC_VER)88/* SecureZeroMemory is guaranteed not to be optimized out by MSVC. */89SecureZeroMemory(ptr, len);90#elif defined(__GNUC__)91/* We use a memory barrier that scares the compiler away from optimizing out the memset.92*93* Quoting Adam Langley <[email protected]> in commit ad1907fe73334d6c696c8539646c21b11178f20f94* in BoringSSL (ISC License):95* As best as we can tell, this is sufficient to break any optimisations that96* might try to eliminate "superfluous" memsets.97* This method used in memzero_explicit() the Linux kernel, too. Its advantage is that it is98* pretty efficient, because the compiler can still implement the memset() efficiently,99* just not remove it entirely. See "Dead Store Elimination (Still) Considered Harmful" by100* Yang et al. (USENIX Security 2017) for more background.101*/102memset(ptr, 0, len);103__asm__ __volatile__("" : : "r"(ptr) : "memory");104#else105void *(*volatile const volatile_memset)(void *, int, size_t) = memset;106volatile_memset(ptr, 0, len);107#endif108}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.