xous-core/services/trng/src/lib.rs
bunnie 827888ccbb fix up the REFCOUNT on connections
Was doing disjoint loads and stores on an Atomic, really, should
be using the fetch_add, fetch_sub variants to be proper.

Didn't seem to impact anything but maybe there's a future version
of me that high fives me for doing this.
2022-01-31 23:13:10 +08:00

224 lines
8.2 KiB
Rust

#![cfg_attr(target_os = "none", no_std)]
pub mod api;
use num_traits::*;
use xous::{send_message, CID};
use xous_ipc::Buffer;
#[derive(Debug)]
pub struct Trng {
conn: CID,
error_sid: Option<xous::SID>,
}
impl Trng {
pub fn new(xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
let conn = xns
.request_connection_blocking(api::SERVER_NAME_TRNG)
.expect("Can't connect to TRNG server");
REFCOUNT.fetch_add(1, Ordering::Relaxed);
Ok(Trng {
conn,
error_sid: None,
})
}
pub fn get_u32(&self) -> Result<u32, xous::Error> {
let response = send_message(
self.conn,
xous::Message::new_blocking_scalar(
api::Opcode::GetTrng.to_usize().unwrap(),
1, /* count */
0,
0,
0,
),
)
.expect("TRNG|LIB: can't get_u32");
if let xous::Result::Scalar2(trng, _) = response {
Ok(trng as u32)
} else {
panic!("unexpected return value: {:#?}", response);
}
}
pub fn get_u64(&self) -> Result<u64, xous::Error> {
let response = send_message(
self.conn,
xous::Message::new_blocking_scalar(
api::Opcode::GetTrng.to_usize().unwrap(),
2, /* count */
0,
0,
0,
),
)
.expect("TRNG|LIB: can't get_u32");
if let xous::Result::Scalar2(lo, hi) = response {
Ok(lo as u64 | ((hi as u64) << 32))
} else {
panic!("unexpected return value: {:#?}", response);
}
}
pub fn fill_buf(&self, data: &mut [u32]) -> Result<(), xous::Error> {
let mut tb = api::TrngBuf {
data: [0; 1024],
len: 0,
};
if data.len() > tb.data.len() {
return Err(xous::Error::OutOfMemory);
}
tb.len = data.len() as u16;
let mut buf = Buffer::into_buf(tb).or(Err(xous::Error::InternalError))?;
buf.lend_mut(self.conn, api::Opcode::FillTrng.to_u32().unwrap())
.or(Err(xous::Error::InternalError))?;
let rtb: api::TrngBuf = buf.to_original().unwrap();
if rtb.len as usize != data.len() {
return Err(xous::Error::InternalError);
}
for (&src, dst) in rtb.data.iter().zip(data.iter_mut()) {
*dst = src;
}
Ok(())
}
pub fn hook_error_callback(&mut self, id: u32, cid: CID) -> Result<(), xous::Error> {
if self.error_sid.is_none() {
let sid = xous::create_server().unwrap();
self.error_sid = Some(sid);
let sid_tuple = sid.to_u32();
xous::create_thread_4(
error_cb_server,
sid_tuple.0 as usize,
sid_tuple.1 as usize,
sid_tuple.2 as usize,
sid_tuple.3 as usize,
)
.unwrap();
let hookdata = api::ScalarHook {
sid: sid_tuple,
id,
cid,
};
let buf = Buffer::into_buf(hookdata).or(Err(xous::Error::InternalError))?;
buf.lend(self.conn, api::Opcode::ErrorSubscribe.to_u32().unwrap())
.map(|_| ())
} else {
Err(xous::Error::MemoryInUse) // can't hook it twice
}
}
pub fn get_health_tests(&self) -> Result<api::HealthTests, xous::Error> {
let ht = api::HealthTests::default();
let mut buf = Buffer::into_buf(ht).or(Err(xous::Error::InternalError))?;
buf.lend_mut(self.conn, api::Opcode::HealthStats.to_u32().unwrap())
.or(Err(xous::Error::InternalError))?;
Ok(buf.to_original().unwrap())
}
pub fn get_error_stats(&self) -> Result<api::TrngErrors, xous::Error> {
let errs = api::TrngErrors::default();
let mut buf = Buffer::into_buf(errs).or(Err(xous::Error::InternalError))?;
buf.lend_mut(self.conn, api::Opcode::ErrorStats.to_u32().unwrap())
.or(Err(xous::Error::InternalError))?;
Ok(buf.to_original().unwrap())
}
}
use core::sync::atomic::{AtomicU32, Ordering};
static REFCOUNT: AtomicU32 = AtomicU32::new(0);
impl Drop for Trng {
fn drop(&mut self) {
// 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 {
unsafe {
xous::disconnect(self.conn).unwrap();
}
}
}
}
fn error_cb_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
let sid = xous::SID::from_u32(sid0 as u32, sid1 as u32, sid2 as u32, sid3 as u32);
loop {
let msg = xous::receive_message(sid).unwrap();
match FromPrimitive::from_usize(msg.body.id()) {
Some(api::EventCallback::Event) => xous::msg_scalar_unpack!(msg, cid, id, _, _, {
// directly pass the scalar message onto the CID with the ID memorized in the original hook
send_message(cid as u32, xous::Message::new_scalar(id, 0, 0, 0, 0)).unwrap();
}),
Some(api::EventCallback::Drop) => {
break; // this exits the loop and kills the thread
}
None => (),
}
}
xous::destroy_server(sid).unwrap();
}
use rand_core::{impls, CryptoRng, RngCore};
impl CryptoRng for Trng {}
impl RngCore for Trng {
fn next_u32(&mut self) -> u32 {
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 fill_bytes(&mut self, dest: &mut [u8]) {
// smaller than 64 bytes (512 bits), just use 8x next_u64 calls to fill.
if dest.len() < 64 {
return impls::fill_bytes_via_next(self, dest);
}
// one page is the max size we can do with the fill_buf() API
let chunks_page = dest.chunks_exact_mut(4096);
for chunks in chunks_page.into_iter() {
let mut data: [u32; 4096 / 4] = [0; 4096 / 4];
self.fill_buf(&mut data)
.expect("couldn't fill page-sized TRNG buffer");
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()) {
*dst_byte = src_byte;
}
}
}
// a mid-sized chunk to span the gap between page and our smallest granularity
let chunks_512 = dest
.chunks_exact_mut(4096)
.into_remainder()
.chunks_exact_mut(512);
for chunks in chunks_512.into_iter() {
let mut data: [u32; 512 / 4] = [0; 512 / 4];
self.fill_buf(&mut data)
.expect("couldn't fill mid-sized TRNG buffer");
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()) {
*dst_byte = src_byte;
}
}
}
// our smallest-sized "standard" chunk
let chunks_smallest = dest
.chunks_exact_mut(4096)
.into_remainder()
.chunks_exact_mut(512)
.into_remainder()
.chunks_exact_mut(64);
for chunks in chunks_smallest.into_iter() {
let mut data: [u32; 64 / 4] = [0; 64 / 4];
self.fill_buf(&mut data)
.expect("couldn't fill small-sized TRNG buffer");
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()) {
*dst_byte = src_byte;
}
}
}
// any leftover bytes
let leftovers = dest
.chunks_exact_mut(4096)
.into_remainder()
.chunks_exact_mut(512)
.into_remainder()
.chunks_exact_mut(64)
.into_remainder();
impls::fill_bytes_via_next(self, leftovers);
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
Ok(self.fill_bytes(dest))
}
}