mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
port trng server to use flatipc
This commit is contained in:
parent
489c614ec6
commit
e9ab865e09
4 changed files with 28 additions and 28 deletions
|
|
@ -22,6 +22,8 @@ rkyv = { version = "0.8.8", default-features = false, features = [
|
||||||
xous-ipc = "0.10.1"
|
xous-ipc = "0.10.1"
|
||||||
rand_core = "0.6.4" # the 0.6.4 API is necessary for compatibility with curve25519-dalek crates
|
rand_core = "0.6.4" # the 0.6.4 API is necessary for compatibility with curve25519-dalek crates
|
||||||
utralib = { version = "0.1.25", optional = true, default-features = false }
|
utralib = { version = "0.1.25", optional = true, default-features = false }
|
||||||
|
flatipc = { path = "../../libs/flatipc" }
|
||||||
|
flatipc-derive = { path = "../../libs/flatipc-derive" }
|
||||||
|
|
||||||
[target.'cfg(any(windows,unix))'.dependencies]
|
[target.'cfg(any(windows,unix))'.dependencies]
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
|
|
|
||||||
|
|
@ -37,15 +37,11 @@ pub struct TrngErrors {
|
||||||
pub pending_mask: u32,
|
pub pending_mask: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Performance issue just noticed: the data field is exactly 4096 bytes long, which means
|
#[derive(Debug, flatipc::Ipc)]
|
||||||
/// the "len" field overflows the structure to be 2 pages. This will cause a lot of extra
|
/// Note that this structure is mirrored in imports/getrandom/src/xous.rs
|
||||||
/// zero-ing of pages, thrashing the cache and also pegging the CPU for useless work.
|
#[repr(C)]
|
||||||
/// Consider revising the data field down to 1023 words in length, but need to revisit the
|
|
||||||
/// library implemnetations to make sure this doesn't break any existing code.
|
|
||||||
/// Note that this structure had to be mirrored into the local "getrandom" implementation
|
|
||||||
#[derive(Debug, Copy, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
|
|
||||||
pub struct TrngBuf {
|
pub struct TrngBuf {
|
||||||
pub data: [u32; 1024],
|
pub data: [u32; 1020],
|
||||||
pub len: u16,
|
pub len: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -69,7 +69,6 @@ impl Trng {
|
||||||
let mut buf = tb.into_ipc();
|
let mut buf = tb.into_ipc();
|
||||||
buf.lend_mut(self.conn, api::Opcode::FillTrng.to_usize().unwrap())
|
buf.lend_mut(self.conn, api::Opcode::FillTrng.to_usize().unwrap())
|
||||||
.or(Err(xous::Error::InternalError))?;
|
.or(Err(xous::Error::InternalError))?;
|
||||||
println!("{}", tb.len); // this shouldn't be possible
|
|
||||||
if buf.len as usize != data.len() {
|
if buf.len as usize != data.len() {
|
||||||
return Err(xous::Error::InternalError);
|
return Err(xous::Error::InternalError);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
mod api;
|
mod api;
|
||||||
use api::*;
|
use api::*;
|
||||||
|
use flatipc::Ipc;
|
||||||
use log::info;
|
use log::info;
|
||||||
use num_traits::*;
|
use num_traits::*;
|
||||||
use xous::CID;
|
use xous::CID;
|
||||||
|
|
@ -24,11 +25,11 @@ mod implementation {
|
||||||
use susres::{RegManager, RegOrField, SuspendResume};
|
use susres::{RegManager, RegOrField, SuspendResume};
|
||||||
use utralib::generated::*;
|
use utralib::generated::*;
|
||||||
|
|
||||||
use crate::api::{
|
|
||||||
ExcursionTest, HealthTests, MiniRunsTest, NistTests, TrngBuf, TrngErrors, TrngTestBuf, TrngTestMode,
|
|
||||||
TRNG_TEST_BUF_LEN,
|
|
||||||
};
|
|
||||||
use crate::TEST_CACHE_LEN;
|
use crate::TEST_CACHE_LEN;
|
||||||
|
use crate::api::{
|
||||||
|
ExcursionTest, HealthTests, MiniRunsTest, NistTests, TRNG_TEST_BUF_LEN, TrngErrors, TrngTestBuf,
|
||||||
|
TrngTestMode,
|
||||||
|
};
|
||||||
|
|
||||||
pub struct Trng {
|
pub struct Trng {
|
||||||
csr: utralib::CSR<u32>,
|
csr: utralib::CSR<u32>,
|
||||||
|
|
@ -370,12 +371,11 @@ mod implementation {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_buf(&mut self, len: u16) -> TrngBuf {
|
pub fn get_buf(&mut self, buf: &mut trng::api::IpcTrngBuf) {
|
||||||
let mut tb = TrngBuf { data: [0; 1024], len };
|
let len = buf.len as usize;
|
||||||
for i in 0..len as usize {
|
for d in buf.data[..len].iter_mut() {
|
||||||
tb.data[i] = self.get_data_eager();
|
*d = self.get_data_eager();
|
||||||
}
|
}
|
||||||
tb
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_trng(&mut self, count: usize) -> [u32; 2] {
|
pub fn get_trng(&mut self, count: usize) -> [u32; 2] {
|
||||||
|
|
@ -520,11 +520,11 @@ mod implementation {
|
||||||
// a stub to try to avoid breaking hosted mode for as long as possible.
|
// a stub to try to avoid breaking hosted mode for as long as possible.
|
||||||
#[cfg(not(target_os = "xous"))]
|
#[cfg(not(target_os = "xous"))]
|
||||||
mod implementation {
|
mod implementation {
|
||||||
|
use rand_chacha::ChaCha8Rng;
|
||||||
use rand_chacha::rand_core::RngCore;
|
use rand_chacha::rand_core::RngCore;
|
||||||
use rand_chacha::rand_core::SeedableRng;
|
use rand_chacha::rand_core::SeedableRng;
|
||||||
use rand_chacha::ChaCha8Rng;
|
|
||||||
|
|
||||||
use crate::api::{HealthTests, TrngBuf, TrngErrors, TrngTestBuf, TrngTestMode, TRNG_TEST_BUF_LEN};
|
use crate::api::{HealthTests, TRNG_TEST_BUF_LEN, TrngErrors, TrngTestBuf, TrngTestMode};
|
||||||
|
|
||||||
pub struct Trng {
|
pub struct Trng {
|
||||||
rng: ChaCha8Rng,
|
rng: ChaCha8Rng,
|
||||||
|
|
@ -555,16 +555,15 @@ mod implementation {
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn wait_full(&self) {}
|
pub fn wait_full(&self) {}
|
||||||
|
|
||||||
pub fn get_buf(&mut self, len: u16) -> TrngBuf {
|
pub fn get_buf(&mut self, buf: &mut trng::api::IpcTrngBuf) {
|
||||||
if self.msgcount < 3 {
|
if self.msgcount < 3 {
|
||||||
log::info!("hosted mode TRNG is *not* random, it is a deterministic LFSR");
|
log::info!("hosted mode TRNG is *not* random, it is a deterministic LFSR");
|
||||||
}
|
}
|
||||||
self.msgcount += 1;
|
self.msgcount += 1;
|
||||||
let mut data = [0; 1024];
|
let len = buf.len as usize;
|
||||||
for d in data.iter_mut() {
|
for d in buf.data[..len].iter_mut() {
|
||||||
*d = self.rng.next_u32();
|
*d = self.rng.next_u32();
|
||||||
}
|
}
|
||||||
TrngBuf { data, len }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_trng(&mut self, _count: usize) -> [u32; 2] {
|
pub fn get_trng(&mut self, _count: usize) -> [u32; 2] {
|
||||||
|
|
@ -698,10 +697,14 @@ fn main() -> ! {
|
||||||
buffer.replace(trng.get_errors()).unwrap();
|
buffer.replace(trng.get_errors()).unwrap();
|
||||||
}
|
}
|
||||||
Some(api::Opcode::FillTrng) => {
|
Some(api::Opcode::FillTrng) => {
|
||||||
let mut buffer =
|
let mm = msg.body.memory_message_mut().unwrap();
|
||||||
unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
|
// safety: the slice is `u8` and all values are representable
|
||||||
let len = buffer.as_flat::<TrngBuf, _>().unwrap().len;
|
let buffer = trng::api::IpcTrngBuf::from_slice_mut(
|
||||||
buffer.replace(trng.get_buf(len.into())).unwrap();
|
unsafe { mm.buf.as_slice_mut() },
|
||||||
|
mm.offset.unwrap().get(),
|
||||||
|
)
|
||||||
|
.expect("couldn't unpack FillTrng buffer");
|
||||||
|
trng.get_buf(buffer);
|
||||||
}
|
}
|
||||||
Some(api::Opcode::TestSetMode) => xous::msg_blocking_scalar_unpack!(msg, mode_code, _, _, _, {
|
Some(api::Opcode::TestSetMode) => xous::msg_blocking_scalar_unpack!(msg, mode_code, _, _, _, {
|
||||||
if let Some(mode) = FromPrimitive::from_usize(mode_code) {
|
if let Some(mode) = FromPrimitive::from_usize(mode_code) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue