This repository has been archived by the owner on Mar 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs #164 -- expose the kernel's CSPRNG, safely
- Loading branch information
Showing
8 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use core::convert::TryInto; | ||
|
||
use crate::{bindings, c_types, error}; | ||
|
||
/// Fills `dest` with random bytes generated from the kernel's CSPRNG. Ensures | ||
/// that the CSPRNG has been seeded before generating any random bytes, and | ||
/// will block until it's ready. | ||
pub fn getrandom(dest: &mut [u8]) -> error::KernelResult<()> { | ||
let res = unsafe { bindings::wait_for_random_bytes() }; | ||
if res != 0 { | ||
return Err(error::Error::from_kernel_errno(res)); | ||
} | ||
|
||
unsafe { | ||
bindings::get_random_bytes( | ||
dest.as_mut_ptr() as *mut c_types::c_void, | ||
dest.len().try_into()?, | ||
); | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Fills `dest` with random bytes generated from the kernel's CSPRNG. If the | ||
/// CSPRNG is not yet seeded, returns an `Err(EAGAIN)` immediately. Only | ||
/// available on 4.19 and later kernels. | ||
#[cfg(kernel_4_19_0_or_greater)] | ||
pub fn getrandom_nonblock(dest: &mut [u8]) -> error::KernelResult<()> { | ||
if !unsafe { bindings::rng_is_initialized() } { | ||
return Err(error::Error::EAGAIN); | ||
} | ||
getrandom(dest) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "random-tests" | ||
version = "0.1.0" | ||
authors = ["Alex Gaynor <[email protected]>", "Geoffrey Thomas <[email protected]>"] | ||
edition = "2018" | ||
|
||
[lib] | ||
crate-type = ["staticlib"] | ||
test = false | ||
|
||
[features] | ||
default = ["linux-kernel-module"] | ||
|
||
[dependencies] | ||
linux-kernel-module = { path = "../..", optional = true } | ||
|
||
[dev-dependencies] | ||
kernel-module-testlib = { path = "../../testlib" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#![no_std] | ||
|
||
use linux_kernel_module::{self, random}; | ||
|
||
struct EntropySource; | ||
|
||
impl SysctlStorage for EntropySource { | ||
fn store_value(&self, data: &[u8]) -> (usize, error::KernelResult<()>) { | ||
(0, Err(error::Error::EINVAL)) | ||
} | ||
|
||
fn read_value(&self, data: &mut UserSlicePtrWriter) -> (usize, error::KernelResult<()>) { | ||
let mut storage = vec![0; data.len()]; | ||
random::getrandom(&mut storage).map_err(|e| (0, Err(e)))?; | ||
(storage.len(), data.write(storage)) | ||
} | ||
} | ||
|
||
struct RandomTestModule { | ||
_sysctl_entropy: Sysctl<EntropySource>, | ||
} | ||
|
||
impl linux_kernel_module::KernelModule for RandomTestModule { | ||
fn init() -> linux_kernel_module::KernelResult<Self> { | ||
Ok(RandomTestModule { | ||
_sysctl_entropy: Sysctl::register( | ||
cstr!("rust/random-tests"), | ||
cstr!("entropy"), | ||
EntropySource, | ||
Mode::from_int(0o444), | ||
), | ||
}) | ||
} | ||
} | ||
|
||
linux_kernel_module::kernel_module!( | ||
RandomTestModule, | ||
author: "Fish in a Barrel Contributors", | ||
description: "A module for testing the CSPRNG", | ||
license: "GPL" | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use kernel_module_testlib::{assert_dmesg_contains, with_kernel_module}; | ||
|
||
#[test] | ||
fn test_random_entropy() { | ||
with_kernel_module(|| { | ||
let mut keys = HashSet::new(); | ||
for _ in 0..1024 { | ||
let mut key = [0; 16]; | ||
let f = fs::File::open("/proc/sys/rust/random-tests/entropy").unwrap(); | ||
f.read_exact(&mut key).unwrap(); | ||
keys.insert(key); | ||
} | ||
assert_eq!(keys.len(), 1024); | ||
}); | ||
} |