Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created February 25, 2026 23:40
Show Gist options
  • Select an option

  • Save rust-play/fbe35fc20f2fba9eae1d7b6e096278a7 to your computer and use it in GitHub Desktop.

Select an option

Save rust-play/fbe35fc20f2fba9eae1d7b6e096278a7 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use std::fs::OpenOptions;
use std::os::unix::io::AsRawFd;
use std::time::Instant;
use sha2::{Sha256, Digest};
use libc::{ioctl, c_int};
// Linux ioctl structure for entropy injection
#[repr(C)]
struct RandPoolInfo {
entropy_count: c_int, // Bits of entropy (not bytes)
buf_size: c_int, // Size of buffer in bytes
buf: [u8; 32], // The entropy data
}
// IOCTL command for RNDADDENTROPY
const RNDADDENTROPY: u64 = 0x40085203;
/// 1. Collect Entropy via CPU Timing Jitter
fn gather_jitter_entropy(samples: usize) -> [u8; 32] {
let mut hasher = Sha256::new();
println!("Step 1: Sampling CPU jitter ({} iterations)...", samples);
for i in 0..samples {
let start = Instant::now();
// Create a small, unpredictable execution load
let mut x: u64 = i as u64;
for j in 0..50 {
x = x.wrapping_mul(j as u64).wrapping_add(0xDEADBEEF);
}
let duration = start.elapsed().as_nanos();
// Mix the nanosecond delta and the calculation result into the hash
hasher.update(&duration.to_le_bytes());
hasher.update(&x.to_le_bytes());
}
let result = hasher.finalize();
let mut bytes = [0u8; 32];
bytes.copy_from_slice(&result);
bytes
}
/// 2. Inject Entropy into the Linux Kernel
fn inject_to_kernel(entropy_data: [u8; 32]) -> std::io::Result<()> {
println!("Step 2: Preparing kernel injection...");
let info = RandPoolInfo {
entropy_count: 256, // We claim 256 bits of "true" entropy
buf_size: 32,
buf: entropy_data,
};
// Must open /dev/random with write access
let file = OpenOptions::new().write(true).open("/dev/random")?;
let fd = file.as_raw_fd();
println!("Step 3: Calling ioctl(RNDADDENTROPY)...");
let result = unsafe { ioctl(fd, RNDADDENTROPY, &info) };
if result == 0 {
Ok(())
} else {
Err(std::io::Error::last_os_error())
}
}
fn main() {
println!("--- Custom Entropy Injection Tool ---");
// 1. Gather
let noise = gather_jitter_entropy(10_000);
println!("Entropy Digest: {:02x?}", &noise[..8]); // Show first 8 bytes
std::process::exit(0);
#[allow(unreachable_code)]
// 2. Inject
match inject_to_kernel(noise) {
Ok(_) => println!("\n✅ Success: Kernel entropy pool updated and credited."),
Err(e) => {
eprintln!("\n❌ Failure: {}", e);
if e.kind() == std::io::ErrorKind::PermissionDenied {
println!("Hint: You must run this with sudo or CAP_SYS_ADMIN.");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment