2016-10-24 22:08:15 +02:00
|
|
|
#![feature(asm)]
|
2016-11-02 06:44:13 +01:00
|
|
|
#![feature(rand)]
|
2016-10-24 22:08:15 +02:00
|
|
|
|
|
|
|
extern crate syscall;
|
2016-11-02 06:44:13 +01:00
|
|
|
extern crate raw_cpuid;
|
|
|
|
extern crate rand;
|
2016-10-24 22:08:15 +02:00
|
|
|
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{Read, Write};
|
|
|
|
|
2016-11-02 06:44:13 +01:00
|
|
|
use rand::chacha::ChaChaRng;
|
|
|
|
use rand::Rng;
|
|
|
|
|
|
|
|
use raw_cpuid::CpuId;
|
|
|
|
|
|
|
|
use syscall::{Packet, Result, SchemeMut};
|
2016-10-24 22:08:15 +02:00
|
|
|
|
|
|
|
//TODO: Use a CSPRNG, allow write of entropy
|
2016-11-02 06:44:13 +01:00
|
|
|
struct RandScheme {
|
|
|
|
prng: ChaChaRng
|
|
|
|
}
|
2016-10-24 22:08:15 +02:00
|
|
|
|
2016-11-02 06:44:13 +01:00
|
|
|
impl SchemeMut for RandScheme {
|
|
|
|
fn open(&mut self, _path: &[u8], _flags: usize, _uid: u32, _gid: u32) -> Result<usize> {
|
2016-10-24 22:08:15 +02:00
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
|
2016-11-02 06:44:13 +01:00
|
|
|
fn dup(&mut self, file: usize, _buf: &[u8]) -> Result<usize> {
|
2016-10-24 22:08:15 +02:00
|
|
|
Ok(file)
|
|
|
|
}
|
|
|
|
|
2016-11-02 06:44:13 +01:00
|
|
|
fn read(&mut self, _file: usize, buf: &mut [u8]) -> Result<usize> {
|
2016-10-24 22:08:15 +02:00
|
|
|
let mut i = 0;
|
|
|
|
for chunk in buf.chunks_mut(8) {
|
2016-11-02 06:44:13 +01:00
|
|
|
let mut rand = self.prng.next_u64();
|
2016-10-24 22:08:15 +02:00
|
|
|
for b in chunk.iter_mut() {
|
|
|
|
*b = rand as u8;
|
|
|
|
rand = rand >> 8;
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(i)
|
|
|
|
}
|
|
|
|
|
2017-04-15 17:50:47 +02:00
|
|
|
fn fpath(&mut self, _file: usize, buf: &mut [u8]) -> Result<usize> {
|
|
|
|
let mut i = 0;
|
|
|
|
let scheme_path = b"rand";
|
|
|
|
while i < buf.len() && i < scheme_path.len() {
|
|
|
|
buf[i] = scheme_path[i];
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
Ok(i)
|
|
|
|
}
|
|
|
|
|
2016-11-02 06:44:13 +01:00
|
|
|
fn close(&mut self, _file: usize) -> Result<usize> {
|
2016-10-24 22:08:15 +02:00
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main(){
|
2016-11-02 03:16:44 +01:00
|
|
|
let has_rdrand = CpuId::new().get_feature_info().unwrap().has_rdrand();
|
|
|
|
|
2016-11-11 04:02:51 +01:00
|
|
|
// Daemonize
|
|
|
|
if unsafe { syscall::clone(0).unwrap() } == 0 {
|
2016-10-24 22:08:15 +02:00
|
|
|
let mut socket = File::create(":rand").expect("rand: failed to create rand scheme");
|
2016-11-02 03:16:44 +01:00
|
|
|
|
2016-11-02 06:44:13 +01:00
|
|
|
let mut rng = ChaChaRng::new_unseeded();
|
2016-11-02 03:16:44 +01:00
|
|
|
|
2016-11-02 06:44:13 +01:00
|
|
|
if has_rdrand {
|
2016-11-02 03:16:44 +01:00
|
|
|
println!("rand: seeding with rdrand");
|
|
|
|
let rand: u64;
|
2016-11-02 06:44:13 +01:00
|
|
|
unsafe {
|
|
|
|
asm!("rdrand rax"
|
|
|
|
: "={rax}"(rand)
|
|
|
|
:
|
|
|
|
:
|
|
|
|
: "intel", "volatile");
|
|
|
|
}
|
|
|
|
rng.set_counter(0, rand);
|
2016-11-02 03:16:44 +01:00
|
|
|
} else {
|
|
|
|
println!("rand: unseeded");
|
2016-11-02 06:44:13 +01:00
|
|
|
}
|
2016-11-02 03:16:44 +01:00
|
|
|
|
2016-11-02 06:44:13 +01:00
|
|
|
let mut scheme = RandScheme{prng: rng};
|
2016-10-24 22:08:15 +02:00
|
|
|
loop {
|
|
|
|
let mut packet = Packet::default();
|
|
|
|
socket.read(&mut packet).expect("rand: failed to read events from rand scheme");
|
|
|
|
scheme.handle(&mut packet);
|
|
|
|
socket.write(&packet).expect("rand: failed to write responses to rand scheme");
|
|
|
|
}
|
2016-11-11 04:02:51 +01:00
|
|
|
}
|
2016-10-24 22:08:15 +02:00
|
|
|
}
|