Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ChaCha20 RNG #116

Merged
merged 3 commits into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ linked_list_allocator = "0.8.4"
pbkdf2 = { version = "0.3.0", default-features = false }
pc-keyboard = "0.5.1"
pic8259_simple = "0.2.0"
rand = { version = "0.7.3", default-features = false }
rand_chacha = { version = "0.2.2", default-features = false }
rand_core = { version = "0.5.1", default-features = false }
raw-cpuid = "8.1.0"
sha2 = { version = "0.8.2", default-features = false }
smoltcp = { version = "0.6.0", default-features = false, features = ["alloc", "ethernet", "socket-tcp", "socket-udp", "proto-ipv4", "proto-dhcpv4"] }
Expand Down
34 changes: 20 additions & 14 deletions src/kernel/random.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
use rand_chacha::ChaChaRng;
use rand_core::{RngCore, SeedableRng};
use x86_64::instructions::random::RdRand;

pub fn rand64() -> Option<u64> {
match RdRand::new() {
Some(rand) => rand.get_u64(),
None => None,
pub fn get_u64() -> u64 {
let mut seed = [0u8; 32];
if let Some(rdrand) = RdRand::new() {
for i in 0..4 {
if let Some(rand) = rdrand.get_u64() {
let bytes = rand.to_be_bytes();
for j in 0..8 {
seed[8 * i + j] = bytes[j];
}
}
}
}

let mut chacha = ChaChaRng::from_seed(seed);
chacha.next_u64()
}

pub fn rand32() -> Option<u32> {
match RdRand::new() {
Some(rand) => rand.get_u32(),
None => None,
}
pub fn get_u32() -> u32 {
get_u64() as u32
}

pub fn rand16() -> Option<u16> {
match RdRand::new() {
Some(rand) => rand.get_u16(),
None => None,
}
pub fn get_u16() -> u16 {
get_u64() as u16
}
4 changes: 2 additions & 2 deletions src/user/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl Message {
pub fn query(qname: &str, qtype: QueryType, qclass: QueryClass) -> Self {
let mut datagram = Vec::new();

let id = kernel::random::rand16().expect("random id");
let id = kernel::random::get_u16();
for b in id.to_be_bytes().iter() {
datagram.push(*b); // Transaction ID
}
Expand Down Expand Up @@ -124,7 +124,7 @@ pub fn resolve(name: &str) -> Result<IpAddress, ResponseCode> {
let dns_port = 53;
let server = IpEndpoint::new(dns_address, dns_port);

let local_port = 49152 + kernel::random::rand16().expect("random port") % 16384;
let local_port = 49152 + kernel::random::get_u16() % 16384;
let client = IpEndpoint::new(IpAddress::Unspecified, local_port);

let qname = name;
Expand Down
2 changes: 1 addition & 1 deletion src/user/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub fn main(args: &[&str]) -> user::shell::ExitCode {

state = match state {
State::Connect if !socket.is_active() => {
let local_port = 49152 + kernel::random::rand16().expect("random port") % 16384;
let local_port = 49152 + kernel::random::get_u16() % 16384;
if is_verbose {
print!("* Connecting to {}:{}\n", address, url.port);
}
Expand Down
9 changes: 9 additions & 0 deletions src/user/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ pub fn main(args: &[&str]) -> user::shell::ExitCode {
print!("{:.6}\n", kernel::clock::uptime());
user::shell::ExitCode::CommandSuccessful
},
"/dev/random" => {
loop {
// Generate ASCII graphic chars
let i = (kernel::random::get_u32() % (0x72 - 0x20)) + 0x20;
if let Some(c) = core::char::from_u32(i) {
print!("{}", c);
}
}
},
_ => {
if pathname.starts_with("/net/") {
// Examples:
Expand Down
3 changes: 2 additions & 1 deletion src/user/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ const AUTOCOMPLETE_COMMANDS: [&str; 29] = [
];

// TODO: Scan /dev
const AUTOCOMPLETE_DEVICES: [&str; 5] = [
const AUTOCOMPLETE_DEVICES: [&str; 6] = [
"/dev/ata",
"/dev/clk",
"/dev/clk/uptime",
"/dev/clk/realtime",
"/dev/random",
"/dev/rtc",
];

Expand Down
2 changes: 1 addition & 1 deletion src/user/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub fn main(args: &[&str]) -> user::shell::ExitCode {

state = match state {
State::Connect if !socket.is_active() => {
let local_port = 49152 + kernel::random::rand16().expect("random port") % 16384;
let local_port = 49152 + kernel::random::get_u16() % 16384;
print!("Connecting to {}:{}\n", address, port);
if socket.connect((address, port), local_port).is_err() {
print!("Could not connect to {}:{}\n", address, port);
Expand Down
2 changes: 1 addition & 1 deletion src/user/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub fn hash(password: &str) -> String {

// Generating salt
for i in 0..2 {
let num = kernel::random::rand64().unwrap();
let num = kernel::random::get_u64();
let buf = num.to_be_bytes();
let n = buf.len();
for j in 0..n {
Expand Down