mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
format services/trng
This commit is contained in:
parent
a4d3f8bc5e
commit
ff710f41dc
3 changed files with 150 additions and 188 deletions
|
|
@ -94,8 +94,10 @@ pub(crate) enum EventCallback {
|
||||||
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
|
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
|
||||||
pub(crate) struct ScalarHook {
|
pub(crate) struct ScalarHook {
|
||||||
pub sid: (u32, u32, u32, u32),
|
pub sid: (u32, u32, u32, u32),
|
||||||
pub id: u32, // ID of the scalar message to send through (e.g. the discriminant of the Enum on the caller's side API)
|
pub id: u32, /* ID of the scalar message to send through (e.g. the discriminant of the Enum on the
|
||||||
pub cid: xous::CID, // caller-side connection ID for the scalar message to route to. Created by the caller before hooking.
|
* caller's side API) */
|
||||||
|
pub cid: xous::CID, /* caller-side connection ID for the scalar message to route to. Created by the
|
||||||
|
* caller before hooking. */
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive, PartialEq, Eq, Copy, Clone)]
|
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive, PartialEq, Eq, Copy, Clone)]
|
||||||
|
|
@ -108,10 +110,10 @@ pub enum TrngTestMode {
|
||||||
Ro,
|
Ro,
|
||||||
// Combined RO + AV data without any CPRNG conditioning
|
// Combined RO + AV data without any CPRNG conditioning
|
||||||
Both,
|
Both,
|
||||||
// Output of the CPRNG that is constantly re-seeded by RO + AV data. This is the "normal" mode of operation.
|
// Output of the CPRNG that is constantly re-seeded by RO + AV data. This is the "normal" mode of
|
||||||
// The CPRNG serves as a "belt and suspenders" safety measure over raw RO + AV data, so that small drop-outs
|
// operation. The CPRNG serves as a "belt and suspenders" safety measure over raw RO + AV data, so
|
||||||
// in the TRNG don't lead to disastrous consequences. Of course this also masks large failures, but there are
|
// that small drop-outs in the TRNG don't lead to disastrous consequences. Of course this also masks
|
||||||
// online tests that should help to pick that up.
|
// large failures, but there are online tests that should help to pick that up.
|
||||||
Cprng,
|
Cprng,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,10 @@
|
||||||
pub mod api;
|
pub mod api;
|
||||||
use api::{TrngTestMode, TRNG_TEST_BUF_LEN};
|
use api::{TrngTestMode, TRNG_TEST_BUF_LEN};
|
||||||
use num_traits::*;
|
use num_traits::*;
|
||||||
|
// the 0.5.1 API is necessary for compatibility with curve25519-dalek crates
|
||||||
|
use rand_core::{CryptoRng, RngCore};
|
||||||
use xous::{send_message, CID};
|
use xous::{send_message, CID};
|
||||||
use xous_ipc::Buffer;
|
use xous_ipc::Buffer;
|
||||||
// the 0.5.1 API is necessary for compatibility with curve25519-dalek crates
|
|
||||||
use rand_core::{RngCore, CryptoRng};
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Trng {
|
pub struct Trng {
|
||||||
|
|
@ -16,14 +16,11 @@ pub struct Trng {
|
||||||
impl Trng {
|
impl Trng {
|
||||||
pub fn new(xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
|
pub fn new(xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
|
||||||
REFCOUNT.fetch_add(1, Ordering::Relaxed);
|
REFCOUNT.fetch_add(1, Ordering::Relaxed);
|
||||||
let conn = xns
|
let conn =
|
||||||
.request_connection_blocking(api::SERVER_NAME_TRNG)
|
xns.request_connection_blocking(api::SERVER_NAME_TRNG).expect("Can't connect to TRNG server");
|
||||||
.expect("Can't connect to TRNG server");
|
Ok(Trng { conn, error_sid: None })
|
||||||
Ok(Trng {
|
|
||||||
conn,
|
|
||||||
error_sid: None,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_u32(&self) -> Result<u32, xous::Error> {
|
pub fn get_u32(&self) -> Result<u32, xous::Error> {
|
||||||
let response = send_message(
|
let response = send_message(
|
||||||
self.conn,
|
self.conn,
|
||||||
|
|
@ -42,6 +39,7 @@ impl Trng {
|
||||||
panic!("unexpected return value: {:#?}", response);
|
panic!("unexpected return value: {:#?}", response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_u64(&self) -> Result<u64, xous::Error> {
|
pub fn get_u64(&self) -> Result<u64, xous::Error> {
|
||||||
let response = send_message(
|
let response = send_message(
|
||||||
self.conn,
|
self.conn,
|
||||||
|
|
@ -60,11 +58,9 @@ impl Trng {
|
||||||
panic!("unexpected return value: {:#?}", response);
|
panic!("unexpected return value: {:#?}", response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fill_buf(&self, data: &mut [u32]) -> Result<(), xous::Error> {
|
pub fn fill_buf(&self, data: &mut [u32]) -> Result<(), xous::Error> {
|
||||||
let mut tb = api::TrngBuf {
|
let mut tb = api::TrngBuf { data: [0; 1024], len: 0 };
|
||||||
data: [0; 1024],
|
|
||||||
len: 0,
|
|
||||||
};
|
|
||||||
if data.len() > tb.data.len() {
|
if data.len() > tb.data.len() {
|
||||||
return Err(xous::Error::OutOfMemory);
|
return Err(xous::Error::OutOfMemory);
|
||||||
}
|
}
|
||||||
|
|
@ -81,6 +77,7 @@ impl Trng {
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn hook_error_callback(&mut self, id: u32, cid: CID) -> Result<(), xous::Error> {
|
pub fn hook_error_callback(&mut self, id: u32, cid: CID) -> Result<(), xous::Error> {
|
||||||
if self.error_sid.is_none() {
|
if self.error_sid.is_none() {
|
||||||
let sid = xous::create_server().unwrap();
|
let sid = xous::create_server().unwrap();
|
||||||
|
|
@ -94,18 +91,14 @@ impl Trng {
|
||||||
sid_tuple.3 as usize,
|
sid_tuple.3 as usize,
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let hookdata = api::ScalarHook {
|
let hookdata = api::ScalarHook { sid: sid_tuple, id, cid };
|
||||||
sid: sid_tuple,
|
|
||||||
id,
|
|
||||||
cid,
|
|
||||||
};
|
|
||||||
let buf = Buffer::into_buf(hookdata).or(Err(xous::Error::InternalError))?;
|
let buf = Buffer::into_buf(hookdata).or(Err(xous::Error::InternalError))?;
|
||||||
buf.lend(self.conn, api::Opcode::ErrorSubscribe.to_u32().unwrap())
|
buf.lend(self.conn, api::Opcode::ErrorSubscribe.to_u32().unwrap()).map(|_| ())
|
||||||
.map(|_| ())
|
|
||||||
} else {
|
} else {
|
||||||
Err(xous::Error::MemoryInUse) // can't hook it twice
|
Err(xous::Error::MemoryInUse) // can't hook it twice
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_health_tests(&self) -> Result<api::HealthTests, xous::Error> {
|
pub fn get_health_tests(&self) -> Result<api::HealthTests, xous::Error> {
|
||||||
let ht = api::HealthTests::default();
|
let ht = api::HealthTests::default();
|
||||||
let mut buf = Buffer::into_buf(ht).or(Err(xous::Error::InternalError))?;
|
let mut buf = Buffer::into_buf(ht).or(Err(xous::Error::InternalError))?;
|
||||||
|
|
@ -113,6 +106,7 @@ impl Trng {
|
||||||
.or(Err(xous::Error::InternalError))?;
|
.or(Err(xous::Error::InternalError))?;
|
||||||
Ok(buf.to_original().unwrap())
|
Ok(buf.to_original().unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_error_stats(&self) -> Result<api::TrngErrors, xous::Error> {
|
pub fn get_error_stats(&self) -> Result<api::TrngErrors, xous::Error> {
|
||||||
let errs = api::TrngErrors::default();
|
let errs = api::TrngErrors::default();
|
||||||
let mut buf = Buffer::into_buf(errs).or(Err(xous::Error::InternalError))?;
|
let mut buf = Buffer::into_buf(errs).or(Err(xous::Error::InternalError))?;
|
||||||
|
|
@ -120,31 +114,27 @@ impl Trng {
|
||||||
.or(Err(xous::Error::InternalError))?;
|
.or(Err(xous::Error::InternalError))?;
|
||||||
Ok(buf.to_original().unwrap())
|
Ok(buf.to_original().unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This is copied out of the 0.5 API for rand_core
|
/// This is copied out of the 0.5 API for rand_core
|
||||||
pub fn fill_bytes_via_next(&mut self, dest: &mut [u8]) {
|
pub fn fill_bytes_via_next(&mut self, dest: &mut [u8]) {
|
||||||
use core::mem::transmute;
|
use core::mem::transmute;
|
||||||
let mut left = dest;
|
let mut left = dest;
|
||||||
while left.len() >= 8 {
|
while left.len() >= 8 {
|
||||||
let (l, r) = {left}.split_at_mut(8);
|
let (l, r) = { left }.split_at_mut(8);
|
||||||
left = r;
|
left = r;
|
||||||
let chunk: [u8; 8] = unsafe {
|
let chunk: [u8; 8] = unsafe { transmute(self.next_u64().to_le()) };
|
||||||
transmute(self.next_u64().to_le())
|
|
||||||
};
|
|
||||||
l.copy_from_slice(&chunk);
|
l.copy_from_slice(&chunk);
|
||||||
}
|
}
|
||||||
let n = left.len();
|
let n = left.len();
|
||||||
if n > 4 {
|
if n > 4 {
|
||||||
let chunk: [u8; 8] = unsafe {
|
let chunk: [u8; 8] = unsafe { transmute(self.next_u64().to_le()) };
|
||||||
transmute(self.next_u64().to_le())
|
|
||||||
};
|
|
||||||
left.copy_from_slice(&chunk[..n]);
|
left.copy_from_slice(&chunk[..n]);
|
||||||
} else if n > 0 {
|
} else if n > 0 {
|
||||||
let chunk: [u8; 4] = unsafe {
|
let chunk: [u8; 4] = unsafe { transmute(self.next_u32().to_le()) };
|
||||||
transmute(self.next_u32().to_le())
|
|
||||||
};
|
|
||||||
left.copy_from_slice(&chunk[..n]);
|
left.copy_from_slice(&chunk[..n]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the test mode according to the argument. Blocks until mode is set.
|
/// Sets the test mode according to the argument. Blocks until mode is set.
|
||||||
pub fn set_test_mode(&self, test_mode: TrngTestMode) {
|
pub fn set_test_mode(&self, test_mode: TrngTestMode) {
|
||||||
send_message(
|
send_message(
|
||||||
|
|
@ -159,16 +149,14 @@ impl Trng {
|
||||||
)
|
)
|
||||||
.expect("TRNG|LIB: can't set test mode");
|
.expect("TRNG|LIB: can't set test mode");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets test data from the TRNG. If hte test mode was not previously set, this will
|
/// Gets test data from the TRNG. If hte test mode was not previously set, this will
|
||||||
/// eventually cause a panic. We don't add extra overhead code to make this safer
|
/// eventually cause a panic. We don't add extra overhead code to make this safer
|
||||||
/// because as a test mode the caller expected to know what they are doing (and adding
|
/// because as a test mode the caller expected to know what they are doing (and adding
|
||||||
/// more safety code increases overhead for the 99.9999999% of the time when we aren't
|
/// more safety code increases overhead for the 99.9999999% of the time when we aren't
|
||||||
/// using this test code).
|
/// using this test code).
|
||||||
pub fn get_test_data(&self) -> Result<[u8; TRNG_TEST_BUF_LEN], xous::Error> {
|
pub fn get_test_data(&self) -> Result<[u8; TRNG_TEST_BUF_LEN], xous::Error> {
|
||||||
let tb = api::TrngTestBuf {
|
let tb = api::TrngTestBuf { data: [0; TRNG_TEST_BUF_LEN], len: 0 };
|
||||||
data: [0; TRNG_TEST_BUF_LEN],
|
|
||||||
len: 0,
|
|
||||||
};
|
|
||||||
let mut buf = Buffer::into_buf(tb).or(Err(xous::Error::InternalError))?;
|
let mut buf = Buffer::into_buf(tb).or(Err(xous::Error::InternalError))?;
|
||||||
buf.lend_mut(self.conn, api::Opcode::TestGetData.to_u32().unwrap())
|
buf.lend_mut(self.conn, api::Opcode::TestGetData.to_u32().unwrap())
|
||||||
.or(Err(xous::Error::InternalError))?;
|
.or(Err(xous::Error::InternalError))?;
|
||||||
|
|
@ -184,12 +172,10 @@ impl Trng {
|
||||||
|
|
||||||
impl RngCore for Trng {
|
impl RngCore for Trng {
|
||||||
// legacy (0.5) trng apis
|
// legacy (0.5) trng apis
|
||||||
fn next_u32(&mut self) -> u32 {
|
fn next_u32(&mut self) -> u32 { self.get_u32().expect("couldn't get random u32 from server") }
|
||||||
self.get_u32().expect("couldn't get random u32 from server")
|
|
||||||
}
|
fn next_u64(&mut self) -> u64 { self.get_u64().expect("couldn't get random u64 from server") }
|
||||||
fn next_u64(&mut self) -> u64 {
|
|
||||||
self.get_u64().expect("couldn't get random u64 from server")
|
|
||||||
}
|
|
||||||
fn fill_bytes(&mut self, dest: &mut [u8]) {
|
fn fill_bytes(&mut self, dest: &mut [u8]) {
|
||||||
// smaller than 64 bytes (512 bits), just use 8x next_u64 calls to fill.
|
// smaller than 64 bytes (512 bits), just use 8x next_u64 calls to fill.
|
||||||
if dest.len() < 64 {
|
if dest.len() < 64 {
|
||||||
|
|
@ -199,8 +185,7 @@ impl RngCore for Trng {
|
||||||
let chunks_page = dest.chunks_exact_mut(4096);
|
let chunks_page = dest.chunks_exact_mut(4096);
|
||||||
for chunks in chunks_page.into_iter() {
|
for chunks in chunks_page.into_iter() {
|
||||||
let mut data: [u32; 4096 / 4] = [0; 4096 / 4];
|
let mut data: [u32; 4096 / 4] = [0; 4096 / 4];
|
||||||
self.fill_buf(&mut data)
|
self.fill_buf(&mut data).expect("couldn't fill page-sized TRNG buffer");
|
||||||
.expect("couldn't fill page-sized TRNG buffer");
|
|
||||||
for (&src, dst) in data.iter().zip(chunks.chunks_exact_mut(4)) {
|
for (&src, dst) in data.iter().zip(chunks.chunks_exact_mut(4)) {
|
||||||
for (&src_byte, dst_byte) in src.to_le_bytes().iter().zip(dst.iter_mut()) {
|
for (&src_byte, dst_byte) in src.to_le_bytes().iter().zip(dst.iter_mut()) {
|
||||||
*dst_byte = src_byte;
|
*dst_byte = src_byte;
|
||||||
|
|
@ -208,14 +193,10 @@ impl RngCore for Trng {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// a mid-sized chunk to span the gap between page and our smallest granularity
|
// a mid-sized chunk to span the gap between page and our smallest granularity
|
||||||
let chunks_512 = dest
|
let chunks_512 = dest.chunks_exact_mut(4096).into_remainder().chunks_exact_mut(512);
|
||||||
.chunks_exact_mut(4096)
|
|
||||||
.into_remainder()
|
|
||||||
.chunks_exact_mut(512);
|
|
||||||
for chunks in chunks_512.into_iter() {
|
for chunks in chunks_512.into_iter() {
|
||||||
let mut data: [u32; 512 / 4] = [0; 512 / 4];
|
let mut data: [u32; 512 / 4] = [0; 512 / 4];
|
||||||
self.fill_buf(&mut data)
|
self.fill_buf(&mut data).expect("couldn't fill mid-sized TRNG buffer");
|
||||||
.expect("couldn't fill mid-sized TRNG buffer");
|
|
||||||
for (&src, dst) in data.iter().zip(chunks.chunks_exact_mut(4)) {
|
for (&src, dst) in data.iter().zip(chunks.chunks_exact_mut(4)) {
|
||||||
for (&src_byte, dst_byte) in src.to_le_bytes().iter().zip(dst.iter_mut()) {
|
for (&src_byte, dst_byte) in src.to_le_bytes().iter().zip(dst.iter_mut()) {
|
||||||
*dst_byte = src_byte;
|
*dst_byte = src_byte;
|
||||||
|
|
@ -231,8 +212,7 @@ impl RngCore for Trng {
|
||||||
.chunks_exact_mut(64);
|
.chunks_exact_mut(64);
|
||||||
for chunks in chunks_smallest.into_iter() {
|
for chunks in chunks_smallest.into_iter() {
|
||||||
let mut data: [u32; 64 / 4] = [0; 64 / 4];
|
let mut data: [u32; 64 / 4] = [0; 64 / 4];
|
||||||
self.fill_buf(&mut data)
|
self.fill_buf(&mut data).expect("couldn't fill small-sized TRNG buffer");
|
||||||
.expect("couldn't fill small-sized TRNG buffer");
|
|
||||||
for (&src, dst) in data.iter().zip(chunks.chunks_exact_mut(4)) {
|
for (&src, dst) in data.iter().zip(chunks.chunks_exact_mut(4)) {
|
||||||
for (&src_byte, dst_byte) in src.to_le_bytes().iter().zip(dst.iter_mut()) {
|
for (&src_byte, dst_byte) in src.to_le_bytes().iter().zip(dst.iter_mut()) {
|
||||||
*dst_byte = src_byte;
|
*dst_byte = src_byte;
|
||||||
|
|
@ -249,6 +229,7 @@ impl RngCore for Trng {
|
||||||
.into_remainder();
|
.into_remainder();
|
||||||
self.fill_bytes_via_next(leftovers);
|
self.fill_bytes_via_next(leftovers);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
|
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
|
||||||
Ok(self.fill_bytes(dest))
|
Ok(self.fill_bytes(dest))
|
||||||
}
|
}
|
||||||
|
|
@ -260,7 +241,8 @@ use core::sync::atomic::{AtomicU32, Ordering};
|
||||||
static REFCOUNT: AtomicU32 = AtomicU32::new(0);
|
static REFCOUNT: AtomicU32 = AtomicU32::new(0);
|
||||||
impl Drop for Trng {
|
impl Drop for Trng {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
// de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection.
|
// de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the
|
||||||
|
// connection.
|
||||||
if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 {
|
if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 {
|
||||||
unsafe {
|
unsafe {
|
||||||
xous::disconnect(self.conn).unwrap();
|
xous::disconnect(self.conn).unwrap();
|
||||||
|
|
|
||||||
|
|
@ -3,15 +3,13 @@
|
||||||
|
|
||||||
mod api;
|
mod api;
|
||||||
use api::*;
|
use api::*;
|
||||||
|
use log::info;
|
||||||
use num_traits::*;
|
use num_traits::*;
|
||||||
use xous::CID;
|
use xous::CID;
|
||||||
use xous_ipc::Buffer;
|
use xous_ipc::Buffer;
|
||||||
|
|
||||||
use log::info;
|
// when we are testing, allocate a fairly large chunk of data, because the overhead of switching and
|
||||||
|
// conditioning the TRNG into the test mode is fairly high
|
||||||
// when we are testing, allocate a fairly large chunk of data, because the overhead of switching and conditioning
|
|
||||||
// the TRNG into the test mode is fairly high
|
|
||||||
const TEST_CACHE_LEN: usize = 128 * 1024;
|
const TEST_CACHE_LEN: usize = 128 * 1024;
|
||||||
#[derive(Copy, Clone, Debug)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
struct ScalarCallback {
|
struct ScalarCallback {
|
||||||
|
|
@ -20,17 +18,22 @@ struct ScalarCallback {
|
||||||
cb_to_client_id: u32,
|
cb_to_client_id: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature="precursor", feature="renode"))]
|
#[cfg(any(feature = "precursor", feature = "renode"))]
|
||||||
mod implementation {
|
mod implementation {
|
||||||
use crate::api::{ExcursionTest, HealthTests, MiniRunsTest, NistTests, TrngBuf, TrngErrors, TrngTestMode, TRNG_TEST_BUF_LEN, TrngTestBuf};
|
|
||||||
use crate::TEST_CACHE_LEN;
|
|
||||||
use num_traits::*;
|
use num_traits::*;
|
||||||
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;
|
||||||
|
|
||||||
pub struct Trng {
|
pub struct Trng {
|
||||||
csr: utralib::CSR<u32>,
|
csr: utralib::CSR<u32>,
|
||||||
susres_manager: RegManager<{ utra::trng_server::TRNG_SERVER_NUMREGS }>, // probably can be reduced to save space?
|
susres_manager: RegManager<{ utra::trng_server::TRNG_SERVER_NUMREGS }>, /* probably can be reduced
|
||||||
|
* to save space? */
|
||||||
conn: xous::CID,
|
conn: xous::CID,
|
||||||
errors: TrngErrors,
|
errors: TrngErrors,
|
||||||
err_stat: HealthTests,
|
err_stat: HealthTests,
|
||||||
|
|
@ -49,16 +52,14 @@ mod implementation {
|
||||||
min: trng.csr.rf(utra::trng_server::AV_EXCURSION0_LAST_ERR_MIN) as u16,
|
min: trng.csr.rf(utra::trng_server::AV_EXCURSION0_LAST_ERR_MIN) as u16,
|
||||||
max: trng.csr.rf(utra::trng_server::AV_EXCURSION0_LAST_ERR_MAX) as u16,
|
max: trng.csr.rf(utra::trng_server::AV_EXCURSION0_LAST_ERR_MAX) as u16,
|
||||||
});
|
});
|
||||||
trng.csr
|
trng.csr.rmwf(utra::trng_server::AV_EXCURSION0_CTRL_RESET, 1);
|
||||||
.rmwf(utra::trng_server::AV_EXCURSION0_CTRL_RESET, 1);
|
|
||||||
}
|
}
|
||||||
if (pending & trng.csr.ms(utra::trng_server::EV_PENDING_EXCURSION1, 1)) != 0 {
|
if (pending & trng.csr.ms(utra::trng_server::EV_PENDING_EXCURSION1, 1)) != 0 {
|
||||||
trng.errors.excursion_errs[1] = Some(ExcursionTest {
|
trng.errors.excursion_errs[1] = Some(ExcursionTest {
|
||||||
min: trng.csr.rf(utra::trng_server::AV_EXCURSION1_LAST_ERR_MIN) as u16,
|
min: trng.csr.rf(utra::trng_server::AV_EXCURSION1_LAST_ERR_MIN) as u16,
|
||||||
max: trng.csr.rf(utra::trng_server::AV_EXCURSION1_LAST_ERR_MAX) as u16,
|
max: trng.csr.rf(utra::trng_server::AV_EXCURSION1_LAST_ERR_MAX) as u16,
|
||||||
});
|
});
|
||||||
trng.csr
|
trng.csr.rmwf(utra::trng_server::AV_EXCURSION1_CTRL_RESET, 1);
|
||||||
.rmwf(utra::trng_server::AV_EXCURSION1_CTRL_RESET, 1);
|
|
||||||
}
|
}
|
||||||
if (pending & trng.csr.ms(utra::trng_server::EV_PENDING_HEALTH, 1)) != 0 {
|
if (pending & trng.csr.ms(utra::trng_server::EV_PENDING_HEALTH, 1)) != 0 {
|
||||||
let av_repcount = trng.csr.rf(utra::trng_server::NIST_ERRORS_AV_REPCOUNT);
|
let av_repcount = trng.csr.rf(utra::trng_server::NIST_ERRORS_AV_REPCOUNT);
|
||||||
|
|
@ -80,24 +81,17 @@ mod implementation {
|
||||||
}
|
}
|
||||||
// record error summaries and errors from non-health sources
|
// record error summaries and errors from non-health sources
|
||||||
trng.errors.nist_errs = trng.csr.r(utra::trng_server::NIST_ERRORS);
|
trng.errors.nist_errs = trng.csr.r(utra::trng_server::NIST_ERRORS);
|
||||||
trng.errors.server_underruns =
|
trng.errors.server_underruns = trng.csr.rf(utra::trng_server::UNDERRUNS_SERVER_UNDERRUN) as u16;
|
||||||
trng.csr.rf(utra::trng_server::UNDERRUNS_SERVER_UNDERRUN) as u16;
|
trng.errors.kernel_underruns = trng.csr.rf(utra::trng_server::UNDERRUNS_KERNEL_UNDERRUN) as u16;
|
||||||
trng.errors.kernel_underruns =
|
|
||||||
trng.csr.rf(utra::trng_server::UNDERRUNS_KERNEL_UNDERRUN) as u16;
|
|
||||||
|
|
||||||
// reset any error flags. try to do this a bit away from the pending clear, so it has time to take effect
|
// reset any error flags. try to do this a bit away from the pending clear, so it has time to take
|
||||||
|
// effect
|
||||||
trng.csr.rmwf(utra::trng_server::CONTROL_CLR_ERR, 1);
|
trng.csr.rmwf(utra::trng_server::CONTROL_CLR_ERR, 1);
|
||||||
|
|
||||||
// notify the main loop of the error condition
|
// notify the main loop of the error condition
|
||||||
xous::try_send_message(
|
xous::try_send_message(
|
||||||
trng.conn,
|
trng.conn,
|
||||||
xous::Message::new_scalar(
|
xous::Message::new_scalar(crate::api::Opcode::ErrorNotification.to_usize().unwrap(), 0, 0, 0, 0),
|
||||||
crate::api::Opcode::ErrorNotification.to_usize().unwrap(),
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
@ -118,9 +112,7 @@ mod implementation {
|
||||||
|
|
||||||
let mut trng = Trng {
|
let mut trng = Trng {
|
||||||
csr: CSR::new(csr.as_mut_ptr() as *mut u32),
|
csr: CSR::new(csr.as_mut_ptr() as *mut u32),
|
||||||
conn: xns
|
conn: xns.request_connection_blocking(crate::api::SERVER_NAME_TRNG).unwrap(),
|
||||||
.request_connection_blocking(crate::api::SERVER_NAME_TRNG)
|
|
||||||
.unwrap(),
|
|
||||||
susres_manager: RegManager::new(csr.as_mut_ptr() as *mut u32),
|
susres_manager: RegManager::new(csr.as_mut_ptr() as *mut u32),
|
||||||
errors: TrngErrors {
|
errors: TrngErrors {
|
||||||
excursion_errs: [None; 2],
|
excursion_errs: [None; 2],
|
||||||
|
|
@ -147,8 +139,7 @@ mod implementation {
|
||||||
trng.csr.r(utra::trng_server::CONTROL)
|
trng.csr.r(utra::trng_server::CONTROL)
|
||||||
);
|
);
|
||||||
|
|
||||||
trng.susres_manager
|
trng.susres_manager.push(RegOrField::Reg(utra::trng_server::CONTROL), None);
|
||||||
.push(RegOrField::Reg(utra::trng_server::CONTROL), None);
|
|
||||||
|
|
||||||
/*** TRNG tuning parameters: these were configured and tested in a long run against Dieharder
|
/*** TRNG tuning parameters: these were configured and tested in a long run against Dieharder
|
||||||
There is a rate of TRNG generation vs. quality trade-off. The tuning below is toward quality of
|
There is a rate of TRNG generation vs. quality trade-off. The tuning below is toward quality of
|
||||||
|
|
@ -161,8 +152,7 @@ mod implementation {
|
||||||
trng.csr.ms(utra::trng_server::AV_CONFIG_POWERDELAY, 50_000)
|
trng.csr.ms(utra::trng_server::AV_CONFIG_POWERDELAY, 50_000)
|
||||||
| trng.csr.ms(utra::trng_server::AV_CONFIG_SAMPLES, 32),
|
| trng.csr.ms(utra::trng_server::AV_CONFIG_SAMPLES, 32),
|
||||||
);
|
);
|
||||||
trng.susres_manager
|
trng.susres_manager.push(RegOrField::Reg(utra::trng_server::AV_CONFIG), None);
|
||||||
.push(RegOrField::Reg(utra::trng_server::AV_CONFIG), None);
|
|
||||||
|
|
||||||
///// configure ring oscillator
|
///// configure ring oscillator
|
||||||
trng.csr.wo(
|
trng.csr.wo(
|
||||||
|
|
@ -173,20 +163,16 @@ mod implementation {
|
||||||
| trng.csr.ms(utra::trng_server::RO_CONFIG_FUZZ, 1)
|
| trng.csr.ms(utra::trng_server::RO_CONFIG_FUZZ, 1)
|
||||||
| trng.csr.ms(utra::trng_server::RO_CONFIG_OVERSAMPLING, 3),
|
| trng.csr.ms(utra::trng_server::RO_CONFIG_OVERSAMPLING, 3),
|
||||||
);
|
);
|
||||||
trng.susres_manager
|
trng.susres_manager.push(RegOrField::Reg(utra::trng_server::RO_CONFIG), None);
|
||||||
.push(RegOrField::Reg(utra::trng_server::RO_CONFIG), None);
|
|
||||||
|
|
||||||
// slightly reduce the frequency of these to reduce power
|
// slightly reduce the frequency of these to reduce power
|
||||||
trng.csr.wo(
|
trng.csr.wo(
|
||||||
utra::trng_server::CHACHA,
|
utra::trng_server::CHACHA,
|
||||||
trng.csr.ms(utra::trng_server::CHACHA_RESEED_INTERVAL, 2)
|
trng.csr.ms(utra::trng_server::CHACHA_RESEED_INTERVAL, 2)
|
||||||
| trng
|
| trng.csr.ms(utra::trng_server::CHACHA_SELFMIX_INTERVAL, 1000)
|
||||||
.csr
|
|
||||||
.ms(utra::trng_server::CHACHA_SELFMIX_INTERVAL, 1000)
|
|
||||||
| trng.csr.ms(utra::trng_server::CHACHA_SELFMIX_ENA, 1),
|
| trng.csr.ms(utra::trng_server::CHACHA_SELFMIX_ENA, 1),
|
||||||
);
|
);
|
||||||
trng.susres_manager
|
trng.susres_manager.push(RegOrField::Reg(utra::trng_server::CHACHA), None);
|
||||||
.push(RegOrField::Reg(utra::trng_server::CHACHA), None);
|
|
||||||
|
|
||||||
log::debug!("hardware initialized");
|
log::debug!("hardware initialized");
|
||||||
|
|
||||||
|
|
@ -208,6 +194,7 @@ mod implementation {
|
||||||
|
|
||||||
trng
|
trng
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init(&mut self) {
|
pub fn init(&mut self) {
|
||||||
// handle error interrupts
|
// handle error interrupts
|
||||||
xous::claim_interrupt(
|
xous::claim_interrupt(
|
||||||
|
|
@ -230,18 +217,13 @@ mod implementation {
|
||||||
| self.csr.ms(utra::trng_server::EV_ENABLE_EXCURSION0, 1)
|
| self.csr.ms(utra::trng_server::EV_ENABLE_EXCURSION0, 1)
|
||||||
| self.csr.ms(utra::trng_server::EV_ENABLE_EXCURSION1, 1),
|
| self.csr.ms(utra::trng_server::EV_ENABLE_EXCURSION1, 1),
|
||||||
);
|
);
|
||||||
self.susres_manager
|
self.susres_manager.push_fixed_value(RegOrField::Reg(utra::trng_server::EV_PENDING), 0xFFFF_FFFF);
|
||||||
.push_fixed_value(RegOrField::Reg(utra::trng_server::EV_PENDING), 0xFFFF_FFFF);
|
self.susres_manager.push(RegOrField::Reg(utra::trng_server::EV_ENABLE), None);
|
||||||
self.susres_manager
|
|
||||||
.push(RegOrField::Reg(utra::trng_server::EV_ENABLE), None);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_errors(&self) -> TrngErrors {
|
pub fn get_errors(&self) -> TrngErrors { self.errors }
|
||||||
self.errors
|
|
||||||
}
|
pub fn get_err_stats(&self) -> HealthTests { self.err_stat }
|
||||||
pub fn get_err_stats(&self) -> HealthTests {
|
|
||||||
self.err_stat
|
|
||||||
}
|
|
||||||
|
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
pub fn get_tests(&self) -> HealthTests {
|
pub fn get_tests(&self) -> HealthTests {
|
||||||
|
|
@ -389,10 +371,7 @@ mod implementation {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_buf(&mut self, len: u16) -> TrngBuf {
|
pub fn get_buf(&mut self, len: u16) -> TrngBuf {
|
||||||
let mut tb = TrngBuf {
|
let mut tb = TrngBuf { data: [0; 1024], len };
|
||||||
data: [0; 1024],
|
|
||||||
len,
|
|
||||||
};
|
|
||||||
for i in 0..len as usize {
|
for i in 0..len as usize {
|
||||||
tb.data[i] = self.get_data_eager();
|
tb.data[i] = self.get_data_eager();
|
||||||
}
|
}
|
||||||
|
|
@ -413,9 +392,8 @@ mod implementation {
|
||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn suspend(&mut self) {
|
pub fn suspend(&mut self) { self.susres_manager.suspend(); }
|
||||||
self.susres_manager.suspend();
|
|
||||||
}
|
|
||||||
pub fn resume(&mut self) {
|
pub fn resume(&mut self) {
|
||||||
self.susres_manager.resume();
|
self.susres_manager.resume();
|
||||||
// pump the engine to discard the initial 0's in the execution pipeline
|
// pump the engine to discard the initial 0's in the execution pipeline
|
||||||
|
|
@ -423,7 +401,8 @@ mod implementation {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flush_trng_fifo(&mut self) {
|
fn flush_trng_fifo(&mut self) {
|
||||||
// The hardware FIFO Is 1024 entries deep x 32 bits wide. This is a hard-coded parameter derived from the trng_managed.py source code (line 1014)
|
// The hardware FIFO Is 1024 entries deep x 32 bits wide. This is a hard-coded parameter derived
|
||||||
|
// from the trng_managed.py source code (line 1014)
|
||||||
for _ in 0..1024 {
|
for _ in 0..1024 {
|
||||||
while self.csr.rf(utra::trng_server::STATUS_AVAIL) == 0 {
|
while self.csr.rf(utra::trng_server::STATUS_AVAIL) == 0 {
|
||||||
xous::yield_slice();
|
xous::yield_slice();
|
||||||
|
|
@ -431,12 +410,11 @@ mod implementation {
|
||||||
let _ = self.csr.rf(utra::trng_server::DATA_DATA);
|
let _ = self.csr.rf(utra::trng_server::DATA_DATA);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// This routine will "always" return a full buffer; if it is not full, that should be considered an error condition.
|
|
||||||
pub fn get_trng_testmode(&mut self, test_data: &mut Vec::<u8>, test_mode: TrngTestMode) -> TrngTestBuf {
|
/// This routine will "always" return a full buffer; if it is not full, that should be considered an
|
||||||
let mut retbuf = TrngTestBuf {
|
/// error condition.
|
||||||
data: [0u8; TRNG_TEST_BUF_LEN],
|
pub fn get_trng_testmode(&mut self, test_data: &mut Vec<u8>, test_mode: TrngTestMode) -> TrngTestBuf {
|
||||||
len: 0
|
let mut retbuf = TrngTestBuf { data: [0u8; TRNG_TEST_BUF_LEN], len: 0 };
|
||||||
};
|
|
||||||
// bail if the test mode isn't set
|
// bail if the test mode isn't set
|
||||||
if test_mode == TrngTestMode::None {
|
if test_mode == TrngTestMode::None {
|
||||||
return retbuf;
|
return retbuf;
|
||||||
|
|
@ -457,8 +435,10 @@ mod implementation {
|
||||||
self.csr.wo(
|
self.csr.wo(
|
||||||
utra::trng_server::CONTROL,
|
utra::trng_server::CONTROL,
|
||||||
self.csr.ms(utra::trng_server::CONTROL_ENABLE, 1)
|
self.csr.ms(utra::trng_server::CONTROL_ENABLE, 1)
|
||||||
| self.csr.ms(utra::trng_server::CONTROL_POWERSAVE, 1)
|
| self.csr.ms(utra::trng_server::CONTROL_POWERSAVE, 1)
|
||||||
| self.csr.ms(utra::trng_server::CONTROL_RO_DIS, 1), // disable the RO to characterize only the AV
|
| self.csr.ms(utra::trng_server::CONTROL_RO_DIS, 1), /* disable the RO to
|
||||||
|
* characterize only
|
||||||
|
* the AV */
|
||||||
);
|
);
|
||||||
self.flush_trng_fifo();
|
self.flush_trng_fifo();
|
||||||
}
|
}
|
||||||
|
|
@ -468,13 +448,22 @@ mod implementation {
|
||||||
utra::trng_server::CONTROL,
|
utra::trng_server::CONTROL,
|
||||||
self.csr.ms(utra::trng_server::CONTROL_ENABLE, 1)
|
self.csr.ms(utra::trng_server::CONTROL_ENABLE, 1)
|
||||||
| self.csr.ms(utra::trng_server::CONTROL_POWERSAVE, 1)
|
| self.csr.ms(utra::trng_server::CONTROL_POWERSAVE, 1)
|
||||||
| self.csr.ms(utra::trng_server::CONTROL_AV_DIS, 1), // disable the AV generator to characterize the RO
|
| self.csr.ms(utra::trng_server::CONTROL_AV_DIS, 1), /* disable the AV
|
||||||
|
* generator to
|
||||||
|
* characterize the
|
||||||
|
* RO */
|
||||||
);
|
);
|
||||||
self.flush_trng_fifo();
|
self.flush_trng_fifo();
|
||||||
}
|
}
|
||||||
TrngTestMode::Both | TrngTestMode::Cprng => {
|
TrngTestMode::Both | TrngTestMode::Cprng => {
|
||||||
assert!(self.csr.rf(utra::trng_server::CONTROL_RO_DIS) == 0, "TRNG test mode inconsistency (RO reads as disabled; expected it to be enabled)");
|
assert!(
|
||||||
assert!(self.csr.rf(utra::trng_server::CONTROL_AV_DIS) == 0, "TRNG test mode inconsistency (AV reads as disabled; expected it to be enabled)");
|
self.csr.rf(utra::trng_server::CONTROL_RO_DIS) == 0,
|
||||||
|
"TRNG test mode inconsistency (RO reads as disabled; expected it to be enabled)"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
self.csr.rf(utra::trng_server::CONTROL_AV_DIS) == 0,
|
||||||
|
"TRNG test mode inconsistency (AV reads as disabled; expected it to be enabled)"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
TrngTestMode::None => {
|
TrngTestMode::None => {
|
||||||
panic!("TRNG test buffer request with no test mode set.");
|
panic!("TRNG test buffer request with no test mode set.");
|
||||||
|
|
@ -516,7 +505,7 @@ mod implementation {
|
||||||
self.flush_trng_fifo();
|
self.flush_trng_fifo();
|
||||||
}
|
}
|
||||||
// the raw TRNG data already matches the expected system mode; nothing to do
|
// the raw TRNG data already matches the expected system mode; nothing to do
|
||||||
_ => {},
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
// copy the test data into a return structure
|
// copy the test data into a return structure
|
||||||
|
|
@ -531,10 +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::SeedableRng;
|
|
||||||
use rand_chacha::rand_core::RngCore;
|
use rand_chacha::rand_core::RngCore;
|
||||||
use crate::api::{HealthTests, TrngBuf, TrngErrors, TRNG_TEST_BUF_LEN, TrngTestBuf, TrngTestMode};
|
use rand_chacha::rand_core::SeedableRng;
|
||||||
|
use rand_chacha::ChaCha8Rng;
|
||||||
|
|
||||||
|
use crate::api::{HealthTests, TrngBuf, TrngErrors, TrngTestBuf, TrngTestMode, TRNG_TEST_BUF_LEN};
|
||||||
|
|
||||||
pub struct Trng {
|
pub struct Trng {
|
||||||
rng: ChaCha8Rng,
|
rng: ChaCha8Rng,
|
||||||
|
|
@ -545,11 +535,14 @@ mod implementation {
|
||||||
impl Trng {
|
impl Trng {
|
||||||
pub fn new(_xns: &xous_names::XousNames) -> Trng {
|
pub fn new(_xns: &xous_names::XousNames) -> Trng {
|
||||||
Trng {
|
Trng {
|
||||||
rng: ChaCha8Rng::seed_from_u64(xous::TESTING_RNG_SEED.load(core::sync::atomic::Ordering::SeqCst)),
|
rng: ChaCha8Rng::seed_from_u64(
|
||||||
|
xous::TESTING_RNG_SEED.load(core::sync::atomic::Ordering::SeqCst),
|
||||||
|
),
|
||||||
seed: 0x1afe_cafe,
|
seed: 0x1afe_cafe,
|
||||||
msgcount: 0,
|
msgcount: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init(&mut self) {}
|
pub fn init(&mut self) {}
|
||||||
|
|
||||||
fn move_lfsr(&self, mut lfsr: u32) -> u32 {
|
fn move_lfsr(&self, mut lfsr: u32) -> u32 {
|
||||||
|
|
@ -571,10 +564,7 @@ mod implementation {
|
||||||
for d in data.iter_mut() {
|
for d in data.iter_mut() {
|
||||||
*d = self.rng.next_u32();
|
*d = self.rng.next_u32();
|
||||||
}
|
}
|
||||||
TrngBuf {
|
TrngBuf { data, len }
|
||||||
data,
|
|
||||||
len,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_trng(&mut self, _count: usize) -> [u32; 2] {
|
pub fn get_trng(&mut self, _count: usize) -> [u32; 2] {
|
||||||
|
|
@ -590,11 +580,13 @@ mod implementation {
|
||||||
|
|
||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn suspend(&self) {}
|
pub fn suspend(&self) {}
|
||||||
|
|
||||||
pub fn resume(&self) {}
|
pub fn resume(&self) {}
|
||||||
pub fn get_tests(&self) -> HealthTests {
|
|
||||||
HealthTests::default()
|
pub fn get_tests(&self) -> HealthTests { HealthTests::default() }
|
||||||
}
|
|
||||||
pub fn get_errors(&self) -> TrngErrors {
|
pub fn get_errors(&self) -> TrngErrors {
|
||||||
TrngErrors {
|
TrngErrors {
|
||||||
excursion_errs: [None; 2],
|
excursion_errs: [None; 2],
|
||||||
|
|
@ -608,14 +600,15 @@ mod implementation {
|
||||||
pending_mask: 0,
|
pending_mask: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn get_err_stats(&self) -> HealthTests {
|
|
||||||
HealthTests::default()
|
pub fn get_err_stats(&self) -> HealthTests { HealthTests::default() }
|
||||||
}
|
|
||||||
pub fn get_trng_testmode(&mut self, _test_data: &mut Vec::<u8>, _test_mode: TrngTestMode) -> TrngTestBuf {
|
pub fn get_trng_testmode(
|
||||||
TrngTestBuf {
|
&mut self,
|
||||||
data: [0u8; TRNG_TEST_BUF_LEN],
|
_test_data: &mut Vec<u8>,
|
||||||
len: 0
|
_test_mode: TrngTestMode,
|
||||||
}
|
) -> TrngTestBuf {
|
||||||
|
TrngTestBuf { data: [0u8; TRNG_TEST_BUF_LEN], len: 0 }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -629,9 +622,7 @@ fn main() -> ! {
|
||||||
|
|
||||||
let xns = xous_names::XousNames::new().unwrap();
|
let xns = xous_names::XousNames::new().unwrap();
|
||||||
// unlimited connections allowed, anyone including less-trusted processes can get a random number
|
// unlimited connections allowed, anyone including less-trusted processes can get a random number
|
||||||
let trng_sid = xns
|
let trng_sid = xns.register_name(api::SERVER_NAME_TRNG, None).expect("can't register server");
|
||||||
.register_name(api::SERVER_NAME_TRNG, None)
|
|
||||||
.expect("can't register server");
|
|
||||||
log::trace!("registered with NS -- {:?}", trng_sid);
|
log::trace!("registered with NS -- {:?}", trng_sid);
|
||||||
|
|
||||||
let mut trng = Box::new(Trng::new(&xns));
|
let mut trng = Box::new(Trng::new(&xns));
|
||||||
|
|
@ -646,26 +637,28 @@ fn main() -> ! {
|
||||||
#[cfg(feature = "urandomtest")]
|
#[cfg(feature = "urandomtest")]
|
||||||
log::info!("TRNG built with urandom test enabled");
|
log::info!("TRNG built with urandom test enabled");
|
||||||
|
|
||||||
#[cfg(any(
|
#[cfg(any(feature = "avalanchetest", feature = "ringosctest", feature = "urandomtest"))]
|
||||||
feature = "avalanchetest",
|
xous::create_thread_1(tester_thread, trng.get_trng_csr() as usize).expect("couldn't create test thread");
|
||||||
feature = "ringosctest",
|
|
||||||
feature = "urandomtest"
|
|
||||||
))]
|
|
||||||
xous::create_thread_1(tester_thread, trng.get_trng_csr() as usize)
|
|
||||||
.expect("couldn't create test thread");
|
|
||||||
|
|
||||||
// pump the TRNG hardware to clear the first number out, sometimes it is 0 due to clock-sync issues on the fifo
|
// pump the TRNG hardware to clear the first number out, sometimes it is 0 due to clock-sync issues on the
|
||||||
|
// fifo
|
||||||
trng.get_trng(2);
|
trng.get_trng(2);
|
||||||
log::trace!("ready to accept requests");
|
log::trace!("ready to accept requests");
|
||||||
|
|
||||||
// register a suspend/resume listener
|
// register a suspend/resume listener
|
||||||
let sr_cid = xous::connect(trng_sid).expect("couldn't create suspend callback connection");
|
let sr_cid = xous::connect(trng_sid).expect("couldn't create suspend callback connection");
|
||||||
let mut susres = susres::Susres::new(Some(susres::SuspendOrder::Later), &xns, api::Opcode::SuspendResume as u32, sr_cid)
|
let mut susres = susres::Susres::new(
|
||||||
.expect("couldn't create suspend/resume object");
|
Some(susres::SuspendOrder::Later),
|
||||||
|
&xns,
|
||||||
|
api::Opcode::SuspendResume as u32,
|
||||||
|
sr_cid,
|
||||||
|
)
|
||||||
|
.expect("couldn't create suspend/resume object");
|
||||||
|
|
||||||
let mut error_cb_conns: [Option<ScalarCallback>; 32] = [None; 32];
|
let mut error_cb_conns: [Option<ScalarCallback>; 32] = [None; 32];
|
||||||
|
|
||||||
// storage for testing data. We heap-allocate it, so that it's not a burden when we aren't testing the TRNG
|
// storage for testing data. We heap-allocate it, so that it's not a burden when we aren't testing the
|
||||||
|
// TRNG
|
||||||
let mut test_data = Vec::<u8>::new();
|
let mut test_data = Vec::<u8>::new();
|
||||||
let mut test_mode = TrngTestMode::None;
|
let mut test_mode = TrngTestMode::None;
|
||||||
|
|
||||||
|
|
@ -681,41 +674,32 @@ fn main() -> ! {
|
||||||
}),
|
}),
|
||||||
Some(api::Opcode::SuspendResume) => xous::msg_scalar_unpack!(msg, token, _, _, _, {
|
Some(api::Opcode::SuspendResume) => xous::msg_scalar_unpack!(msg, token, _, _, _, {
|
||||||
trng.suspend();
|
trng.suspend();
|
||||||
susres
|
susres.suspend_until_resume(token).expect("couldn't execute suspend/resume");
|
||||||
.suspend_until_resume(token)
|
|
||||||
.expect("couldn't execute suspend/resume");
|
|
||||||
trng.resume();
|
trng.resume();
|
||||||
}),
|
}),
|
||||||
Some(api::Opcode::ErrorSubscribe) => {
|
Some(api::Opcode::ErrorSubscribe) => {
|
||||||
let buffer =
|
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
|
||||||
unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
|
|
||||||
let hookdata = buffer.to_original::<api::ScalarHook, _>().unwrap();
|
let hookdata = buffer.to_original::<api::ScalarHook, _>().unwrap();
|
||||||
do_hook(hookdata, &mut error_cb_conns);
|
do_hook(hookdata, &mut error_cb_conns);
|
||||||
}
|
}
|
||||||
Some(api::Opcode::ErrorNotification) => {
|
Some(api::Opcode::ErrorNotification) => {
|
||||||
log::error!(
|
log::error!("Got a notification interrupt from the TRNG. Syndrome: {:?}", trng.get_errors());
|
||||||
"Got a notification interrupt from the TRNG. Syndrome: {:?}",
|
|
||||||
trng.get_errors()
|
|
||||||
);
|
|
||||||
log::error!("Stats: {:?}", trng.get_err_stats());
|
log::error!("Stats: {:?}", trng.get_err_stats());
|
||||||
send_event(&error_cb_conns);
|
send_event(&error_cb_conns);
|
||||||
}
|
}
|
||||||
Some(api::Opcode::HealthStats) => {
|
Some(api::Opcode::HealthStats) => {
|
||||||
let mut buffer = unsafe {
|
let mut buffer =
|
||||||
Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap())
|
unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
|
||||||
};
|
|
||||||
buffer.replace(trng.get_tests()).unwrap();
|
buffer.replace(trng.get_tests()).unwrap();
|
||||||
}
|
}
|
||||||
Some(api::Opcode::ErrorStats) => {
|
Some(api::Opcode::ErrorStats) => {
|
||||||
let mut buffer = unsafe {
|
let mut buffer =
|
||||||
Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap())
|
unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
|
||||||
};
|
|
||||||
buffer.replace(trng.get_errors()).unwrap();
|
buffer.replace(trng.get_errors()).unwrap();
|
||||||
}
|
}
|
||||||
Some(api::Opcode::FillTrng) => {
|
Some(api::Opcode::FillTrng) => {
|
||||||
let mut buffer = unsafe {
|
let mut buffer =
|
||||||
Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap())
|
unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
|
||||||
};
|
|
||||||
let len = buffer.as_flat::<TrngBuf, _>().unwrap().len;
|
let len = buffer.as_flat::<TrngBuf, _>().unwrap().len;
|
||||||
buffer.replace(trng.get_buf(len)).unwrap();
|
buffer.replace(trng.get_buf(len)).unwrap();
|
||||||
}
|
}
|
||||||
|
|
@ -744,9 +728,8 @@ fn main() -> ! {
|
||||||
xous::return_scalar(msg.sender, 1).ok();
|
xous::return_scalar(msg.sender, 1).ok();
|
||||||
}),
|
}),
|
||||||
Some(api::Opcode::TestGetData) => {
|
Some(api::Opcode::TestGetData) => {
|
||||||
let mut buffer = unsafe {
|
let mut buffer =
|
||||||
Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap())
|
unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
|
||||||
};
|
|
||||||
buffer.replace(trng.get_trng_testmode(&mut test_data, test_mode)).unwrap();
|
buffer.replace(trng.get_trng_testmode(&mut test_data, test_mode)).unwrap();
|
||||||
}
|
}
|
||||||
Some(api::Opcode::Quit) => break,
|
Some(api::Opcode::Quit) => break,
|
||||||
|
|
@ -790,13 +773,7 @@ fn unhook(cb_conns: &mut [Option<ScalarCallback>; 32]) {
|
||||||
if let Some(scb) = entry {
|
if let Some(scb) = entry {
|
||||||
xous::send_message(
|
xous::send_message(
|
||||||
scb.server_to_cb_cid,
|
scb.server_to_cb_cid,
|
||||||
xous::Message::new_blocking_scalar(
|
xous::Message::new_blocking_scalar(api::EventCallback::Drop.to_usize().unwrap(), 0, 0, 0, 0),
|
||||||
api::EventCallback::Drop.to_usize().unwrap(),
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
@ -809,7 +786,8 @@ fn unhook(cb_conns: &mut [Option<ScalarCallback>; 32]) {
|
||||||
fn send_event(cb_conns: &[Option<ScalarCallback>; 32]) {
|
fn send_event(cb_conns: &[Option<ScalarCallback>; 32]) {
|
||||||
for entry in cb_conns.iter() {
|
for entry in cb_conns.iter() {
|
||||||
if let Some(scb) = entry {
|
if let Some(scb) = entry {
|
||||||
// note that the "which" argument is only used for GPIO events, to indicate which pin had the event
|
// note that the "which" argument is only used for GPIO events, to indicate which pin had the
|
||||||
|
// event
|
||||||
xous::send_message(
|
xous::send_message(
|
||||||
scb.server_to_cb_cid,
|
scb.server_to_cb_cid,
|
||||||
xous::Message::new_scalar(
|
xous::Message::new_scalar(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue