2021-01-13 07:04:14 +00:00
#![ cfg_attr(target_os = " none " , no_std) ]
#![ cfg_attr(target_os = " none " , no_main) ]
mod api ;
2021-04-01 09:20:36 +00:00
use num_traits ::FromPrimitive ;
2021-01-13 07:04:14 +00:00
2021-04-01 17:56:22 +00:00
use log ::info ;
2021-01-13 07:04:14 +00:00
#[ cfg(target_os = " none " ) ]
mod implementation {
use utralib ::generated ::* ;
// use crate::api::*;
2021-04-01 17:56:22 +00:00
use log ::info ;
2021-04-29 09:24:31 +00:00
use susres ::{ RegManager , RegOrField , SuspendResume } ;
2021-01-13 07:04:14 +00:00
pub struct Trng {
csr : utralib ::CSR < u32 > ,
// TODO: allocate a software buffer for whitened TRNGs
2021-04-29 09:24:31 +00:00
susres_manager : RegManager ::< { utra ::trng_server ::TRNG_SERVER_NUMREGS } > , // probably can be reduced to save space?
2021-01-13 07:04:14 +00:00
}
impl Trng {
pub fn new ( ) -> Trng {
let csr = xous ::syscall ::map_memory (
xous ::MemoryAddress ::new ( utra ::trng_server ::HW_TRNG_SERVER_BASE ) ,
None ,
4096 ,
xous ::MemoryFlags ::R | xous ::MemoryFlags ::W ,
)
. expect ( " couldn't map TRNG CSR range " ) ;
let mut trng = Trng {
csr : CSR ::new ( csr . as_mut_ptr ( ) as * mut u32 ) ,
2021-04-29 09:24:31 +00:00
susres_manager : RegManager ::new ( csr . as_mut_ptr ( ) as * mut u32 ) ,
2021-01-13 07:04:14 +00:00
} ;
///// configure power settings and which generator to use
2021-04-19 19:21:53 +00:00
if ! ( cfg! ( feature = " avalanchetest " ) | | cfg! ( feature = " ringosctest " ) ) {
trng . csr . wo ( utra ::trng_server ::CONTROL ,
trng . csr . ms ( utra ::trng_server ::CONTROL_ENABLE , 1 )
| trng . csr . ms ( utra ::trng_server ::CONTROL_POWERSAVE , 1 )
// | self.server_csr.ms(utra::trng_server::CONTROL_AV_DIS, 1) // disable the AV generator to characterize the RO
// | self.server_csr.ms(utra::trng_server::CONTROL_RO_DIS, 1) // disable the RO to characterize only the AV
) ;
log ::trace! ( " TRNG configured for normal operation (av+ro): 0x{:08x} " , trng . csr . r ( utra ::trng_server ::CONTROL ) ) ;
} else if cfg! ( feature = " avalanchetest " ) & & ! cfg! ( feature = " ringosctest " ) {
// avalanche test only
trng . csr . wo ( utra ::trng_server ::CONTROL ,
trng . csr . ms ( utra ::trng_server ::CONTROL_ENABLE , 1 )
| trng . csr . ms ( utra ::trng_server ::CONTROL_POWERSAVE , 1 ) // question: do we want power save on or off for testing?? let's leave it on for now, it will make the test run slower but maybe more accurate to our usual use case
| trng . csr . ms ( utra ::trng_server ::CONTROL_RO_DIS , 1 ) // disable the RO to characterize only the AV
) ;
log ::info! ( " TRNG configured for avalanche testing: 0x{:08x} " , trng . csr . r ( utra ::trng_server ::CONTROL ) ) ;
} else if ! cfg! ( feature = " avalanchetest " ) & & cfg! ( feature = " ringosctest " ) {
// ring osc test only
trng . csr . wo ( utra ::trng_server ::CONTROL ,
trng . csr . ms ( utra ::trng_server ::CONTROL_ENABLE , 1 )
| trng . csr . ms ( utra ::trng_server ::CONTROL_POWERSAVE , 1 )
| trng . csr . ms ( utra ::trng_server ::CONTROL_AV_DIS , 1 ) // disable the AV generator to characterize the RO
) ;
log ::info! ( " TRNG configured for ring oscillator testing: 0x{:08x} " , trng . csr . r ( utra ::trng_server ::CONTROL ) ) ;
} else {
// both on
trng . csr . wo ( utra ::trng_server ::CONTROL ,
trng . csr . ms ( utra ::trng_server ::CONTROL_ENABLE , 1 )
| trng . csr . ms ( utra ::trng_server ::CONTROL_POWERSAVE , 1 )
) ;
log ::info! ( " TRNG configured for both avalanche and ring oscillator testing: 0x{:08x} " , trng . csr . r ( utra ::trng_server ::CONTROL ) ) ;
}
2021-01-13 07:04:14 +00:00
2021-04-29 09:24:31 +00:00
trng . susres_manager . push ( RegOrField ::Reg ( utra ::trng_server ::CONTROL ) , None ) ;
2021-01-13 07:04:14 +00:00
/* * * 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
TRNG versus rate of TRNG , such that we could use these without any whitening .
* * * /
///// configure avalanche
// delay in microseconds for avalanche poweron after powersave
trng . csr . wo ( utra ::trng_server ::AV_CONFIG ,
trng . csr . ms ( utra ::trng_server ::AV_CONFIG_POWERDELAY , 50_000 )
| trng . csr . ms ( utra ::trng_server ::AV_CONFIG_SAMPLES , 32 )
) ;
2021-04-29 09:24:31 +00:00
trng . susres_manager . push ( RegOrField ::Reg ( utra ::trng_server ::AV_CONFIG ) , None ) ;
2021-01-13 07:04:14 +00:00
///// configure ring oscillator
trng . csr . wo ( utra ::trng_server ::RO_CONFIG ,
trng . csr . ms ( utra ::trng_server ::RO_CONFIG_DELAY , 4 )
| trng . csr . ms ( utra ::trng_server ::RO_CONFIG_DWELL , 100 )
| trng . csr . ms ( utra ::trng_server ::RO_CONFIG_GANG , 1 )
| trng . csr . ms ( utra ::trng_server ::RO_CONFIG_FUZZ , 1 )
| trng . csr . ms ( utra ::trng_server ::RO_CONFIG_OVERSAMPLING , 3 )
) ;
2021-04-29 09:24:31 +00:00
trng . susres_manager . push ( RegOrField ::Reg ( utra ::trng_server ::RO_CONFIG ) , None ) ;
2021-01-13 07:04:14 +00:00
2021-04-01 19:10:13 +00:00
info! ( " hardware initialized " ) ;
2021-01-13 07:04:14 +00:00
trng
}
2021-04-19 19:21:53 +00:00
// for the test procedure
#[ cfg(any(feature = " avalanchetest " , feature= " ringosctest " )) ]
pub fn get_trng_csr ( & self ) -> * mut u32 {
self . csr . base
}
2021-01-13 07:04:14 +00:00
pub fn get_data_eager ( & self ) -> u32 {
while self . csr . rf ( utra ::trng_server ::STATUS_AVAIL ) = = 0 {
xous ::yield_slice ( ) ;
}
self . csr . rf ( utra ::trng_server ::DATA_DATA )
}
#[ allow(dead_code) ]
pub fn wait_full ( & self ) {
while self . csr . rf ( utra ::trng_server ::STATUS_FULL ) = = 0 {
xous ::yield_slice ( ) ;
}
}
pub fn get_trng ( & self , count : usize ) -> [ u32 ; 2 ] {
// TODO: use SHA hardware unit to robustify the TRNG output against potential hardware failures
// TODO: health monitoring of raw TRNG output
let mut ret : [ u32 ; 2 ] = [ 0 , 0 ] ;
/*
in the final implementation the algorithm should be :
1 ) check fullness of software - whitened pool
2 ) if software pool is full enough , return values from there
3 ) if pool is low , activate hardware TRNG and refill the pool ( uses SHA unit )
4 ) during pool - filling , perform statistics on the hardware TRNG output to check health
5 ) confirm health is OK
* /
// for now, we just take data directly from the hardware-managed raw TRNG pool
ret [ 0 ] = self . get_data_eager ( ) ;
// we don't just draw down TRNGs if not requested, because they are a finite resource
if count > 1 {
ret [ 1 ] = self . get_data_eager ( ) ;
}
ret
}
2021-04-29 09:24:31 +00:00
pub fn suspend ( & mut self ) {
self . susres_manager . suspend ( ) ;
}
pub fn resume ( & mut self ) {
self . susres_manager . resume ( ) ;
// pump the engine to discard the initial 0's in the execution pipeline
self . get_trng ( 2 ) ;
}
2021-01-13 07:04:14 +00:00
}
}
// a stub to try to avoid breaking hosted mode for as long as possible.
#[ cfg(not(target_os = " none " )) ]
mod implementation {
2021-03-03 14:50:46 +00:00
use log ::info ;
2021-01-13 07:04:14 +00:00
pub struct Trng {
seed : u32 ,
}
impl Trng {
pub fn new ( ) -> Trng {
Trng {
seed : 0x1afe_cafe ,
}
}
2021-01-13 07:09:53 +00:00
fn move_lfsr ( & self , mut lfsr : u32 ) -> u32 {
2021-01-13 07:04:14 +00:00
lfsr ^ = lfsr > > 7 ;
lfsr ^ = lfsr < < 9 ;
lfsr ^ = lfsr > > 13 ;
lfsr
}
2021-03-03 14:50:46 +00:00
#[ allow(dead_code) ]
2021-01-13 07:04:14 +00:00
pub fn wait_full ( & self ) { }
2021-01-13 07:09:53 +00:00
pub fn get_trng ( & mut self , _count : usize ) -> [ u32 ; 2 ] {
2021-04-01 19:10:13 +00:00
info! ( " hosted mode TRNG is *not* random, it is an LFSR " ) ;
2021-01-13 07:09:53 +00:00
let mut ret : [ u32 ; 2 ] = [ 0 ; 2 ] ;
self . seed = self . move_lfsr ( self . seed ) ;
2021-01-13 07:04:14 +00:00
ret [ 0 ] = self . seed ;
2021-01-13 07:09:53 +00:00
self . seed = self . move_lfsr ( self . seed ) ;
2021-01-13 07:04:14 +00:00
ret [ 1 ] = self . seed ;
ret
}
2021-04-29 09:24:31 +00:00
pub fn suspend ( & self ) {
}
pub fn resume ( & self ) {
}
2021-01-13 07:04:14 +00:00
}
}
2021-04-19 19:21:53 +00:00
#[ cfg(any(feature = " avalanchetest " , feature= " ringosctest " )) ]
pub const TRNG_BUFF_LEN : usize = 512 * 1024 ;
#[ cfg(any(feature = " avalanchetest " , feature= " ringosctest " )) ]
pub enum WhichMessible {
One ,
Two ,
}
#[ cfg(any(feature = " avalanchetest " , feature= " ringosctest " )) ]
struct Tester {
server_csr : utralib ::CSR < u32 > ,
messible_csr : utralib ::CSR < u32 > ,
messible2_csr : utralib ::CSR < u32 > ,
buffer_a : xous ::MemoryRange ,
buffer_b : xous ::MemoryRange ,
ticktimer : ticktimer_server ::Ticktimer ,
}
#[ cfg(any(feature = " avalanchetest " , feature= " ringosctest " )) ]
impl Tester {
pub fn new ( server_csr : * mut u32 ) -> Tester {
use utralib ::generated ::* ;
let buff_a = xous ::syscall ::map_memory (
xous ::MemoryAddress ::new ( 0x4020_0000 ) , // fix this at a known physical address
None ,
crate ::TRNG_BUFF_LEN ,
xous ::MemoryFlags ::R | xous ::MemoryFlags ::W ,
)
. expect ( " couldn't map TRNG comms buffer A " ) ;
let buff_b = xous ::syscall ::map_memory (
xous ::MemoryAddress ::new ( 0x4030_0000 ) , // fix this at a known physical address
None ,
crate ::TRNG_BUFF_LEN ,
xous ::MemoryFlags ::R | xous ::MemoryFlags ::W ,
)
. expect ( " couldn't map TRNG comms buffer B " ) ;
let messible = xous ::syscall ::map_memory (
xous ::MemoryAddress ::new ( utra ::messible ::HW_MESSIBLE_BASE ) ,
None ,
4096 ,
xous ::MemoryFlags ::R | xous ::MemoryFlags ::W ,
)
. expect ( " couldn't map messible " ) ;
let messible2 = xous ::syscall ::map_memory (
xous ::MemoryAddress ::new ( utra ::messible2 ::HW_MESSIBLE2_BASE ) ,
None ,
4096 ,
xous ::MemoryFlags ::R | xous ::MemoryFlags ::W ,
)
. expect ( " couldn't map messible " ) ;
Tester {
server_csr : CSR ::new ( server_csr ) ,
buffer_a : buff_a ,
buffer_b : buff_b ,
messible_csr : CSR ::new ( messible . as_mut_ptr ( ) as * mut u32 ) ,
messible2_csr : CSR ::new ( messible2 . as_mut_ptr ( ) as * mut u32 ) ,
ticktimer : ticktimer_server ::Ticktimer ::new ( ) . unwrap ( ) ,
}
}
pub fn messible_send ( & mut self , which : WhichMessible , value : u8 ) {
use utralib ::generated ::* ;
match which {
WhichMessible ::One = > self . messible_csr . wfo ( utra ::messible ::IN_IN , value as u32 ) ,
WhichMessible ::Two = > self . messible2_csr . wfo ( utra ::messible2 ::IN_IN , value as u32 ) ,
}
}
#[ allow(dead_code) ]
pub fn messible_get ( & mut self , which : WhichMessible ) -> u8 {
use utralib ::generated ::* ;
match which {
WhichMessible ::One = > self . messible_csr . rf ( utra ::messible ::OUT_OUT ) as u8 ,
WhichMessible ::Two = > self . messible2_csr . rf ( utra ::messible2 ::OUT_OUT ) as u8 ,
}
}
pub fn messible_wait_get ( & mut self , which : WhichMessible ) -> u8 {
use utralib ::generated ::* ;
match which {
WhichMessible ::One = > {
while self . messible_csr . rf ( utra ::messible ::STATUS_HAVE ) = = 0 {
//xous::yield_slice();
self . ticktimer . sleep_ms ( 50 ) . unwrap ( ) ;
}
self . messible_csr . rf ( utra ::messible ::OUT_OUT ) as u8
} ,
WhichMessible ::Two = > {
while self . messible2_csr . rf ( utra ::messible2 ::STATUS_HAVE ) = = 0 {
//xous::yield_slice();
self . ticktimer . sleep_ms ( 50 ) . unwrap ( ) ;
}
self . messible2_csr . rf ( utra ::messible2 ::OUT_OUT ) as u8
} ,
}
}
pub fn get_buff_a ( & self ) -> * mut u32 {
self . buffer_a . as_mut_ptr ( ) as * mut u32
}
pub fn get_buff_b ( & self ) -> * mut u32 {
self . buffer_b . as_mut_ptr ( ) as * mut u32
}
pub fn get_data_eager ( & self ) -> u32 {
use utralib ::generated ::* ;
while self . server_csr . rf ( utra ::trng_server ::STATUS_AVAIL ) = = 0 {
xous ::yield_slice ( ) ;
}
self . server_csr . rf ( utra ::trng_server ::DATA_DATA )
}
#[ allow(dead_code) ]
pub fn wait_full ( & self ) {
use utralib ::generated ::* ;
while self . server_csr . rf ( utra ::trng_server ::STATUS_FULL ) = = 0 {
xous ::yield_slice ( ) ;
}
}
}
#[ cfg(any(feature = " avalanchetest " , feature= " ringosctest " )) ]
fn tester_thread ( csr : usize ) {
let mut trng = Tester ::new ( csr as * mut u32 ) ;
// just create buffers out of pointers. Definitely. Unsafe.
let buff_a = trng . get_buff_a ( ) as * mut u32 ;
let buff_b = trng . get_buff_b ( ) as * mut u32 ;
for i in 0 .. TRNG_BUFF_LEN / 4 {
// buff_a[i] = trng.get_data_eager();
unsafe { buff_a . add ( i ) . write_volatile ( trng . get_data_eager ( ) ) } ;
}
for i in 0 .. TRNG_BUFF_LEN / 4 {
// buff_b[i] = trng.get_data_eager();
unsafe { buff_b . add ( i ) . write_volatile ( trng . get_data_eager ( ) ) } ;
}
log ::info! ( " TRNG_TESTER: starting service " ) ;
let mut phase = 1 ;
trng . messible_send ( WhichMessible ::One , phase ) ; // indicate buffer A is ready to go
loop {
trng . messible_wait_get ( WhichMessible ::Two ) ;
phase + = 1 ;
if phase % 2 = = 1 {
log ::info! ( " TRNG_TESTER: filling A " ) ;
for i in 0 .. TRNG_BUFF_LEN / 4 {
//buff_a[i] = trng.get_data_eager();
unsafe { buff_a . add ( i ) . write_volatile ( trng . get_data_eager ( ) ) } ;
}
trng . messible_send ( WhichMessible ::One , phase ) ;
} else {
log ::info! ( " TRNG_TESTER: filling B " ) ;
for i in 0 .. TRNG_BUFF_LEN / 4 {
//buff_b[i] = trng.get_data_eager();
unsafe { buff_b . add ( i ) . write_volatile ( trng . get_data_eager ( ) ) } ;
}
trng . messible_send ( WhichMessible ::One , phase ) ;
}
}
}
2021-01-13 07:04:14 +00:00
#[ xous::xous_main ]
fn xmain ( ) -> ! {
use crate ::implementation ::Trng ;
log_server ::init_wait ( ) . unwrap ( ) ;
2021-04-06 14:38:11 +00:00
log ::set_max_level ( log ::LevelFilter ::Info ) ;
2021-04-01 19:10:13 +00:00
info! ( " my PID is {} " , xous ::process ::id ( ) ) ;
2021-01-13 07:04:14 +00:00
2021-03-29 18:13:23 +00:00
let xns = xous_names ::XousNames ::new ( ) . unwrap ( ) ;
2021-04-01 19:10:13 +00:00
let trng_sid = xns . register_name ( api ::SERVER_NAME_TRNG ) . expect ( " can't register server " ) ;
2021-04-05 17:52:26 +00:00
log ::trace! ( " registered with NS -- {:?} " , trng_sid ) ;
2021-01-13 07:04:14 +00:00
2021-01-13 07:09:53 +00:00
let mut trng = Trng ::new ( ) ;
2021-01-13 15:17:09 +00:00
2021-04-26 17:37:58 +00:00
#[ cfg(feature = " avalanchetest " ) ]
log ::info! ( " TRNG built with avalanche test enabled " ) ;
#[ cfg(feature = " ringosctest " ) ]
log ::info! ( " TRNG built with ring oscillator test enabled " ) ;
2021-04-20 11:27:29 +00:00
#[ cfg(any(feature = " avalanchetest " , feature= " ringosctest " )) ]
xous ::create_thread_1 ( tester_thread , trng . get_trng_csr ( ) as usize ) . expect ( " couldn't create test thread " ) ;
2021-04-19 19:21:53 +00:00
2021-04-05 17:52:26 +00:00
// 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 ) ;
log ::trace! ( " ready to accept requests " ) ;
2021-01-13 07:04:14 +00:00
2021-04-29 09:24:31 +00:00
// register a suspend/resume listener
let sr_cid = xous ::connect ( trng_sid ) . expect ( " couldn't create suspend callback connection " ) ;
2021-04-29 15:15:47 +00:00
let mut susres = susres ::Susres ::new ( & xns , api ::Opcode ::SuspendResume as u32 , sr_cid ) . expect ( " couldn't create suspend/resume object " ) ;
2021-04-29 09:24:31 +00:00
2021-01-13 07:04:14 +00:00
loop {
2021-04-01 09:20:36 +00:00
let msg = xous ::receive_message ( trng_sid ) . unwrap ( ) ;
match FromPrimitive ::from_usize ( msg . body . id ( ) ) {
2021-04-05 15:50:00 +00:00
Some ( api ::Opcode ::GetTrng ) = > xous ::msg_blocking_scalar_unpack! ( msg , count , _ , _ , _ , {
2021-04-02 07:38:38 +00:00
let val : [ u32 ; 2 ] = trng . get_trng ( count ) ;
xous ::return_scalar2 ( msg . sender , val [ 0 ] as _ , val [ 1 ] as _ )
. expect ( " couldn't return GetTrng request " ) ;
} ) ,
2021-04-29 09:24:31 +00:00
Some ( api ::Opcode ::SuspendResume ) = > xous ::msg_scalar_unpack! ( msg , token , _ , _ , _ , {
trng . suspend ( ) ;
susres . suspend_until_resume ( token ) . expect ( " couldn't execute suspend/resume " ) ;
trng . resume ( ) ;
} ) ,
2021-05-21 07:56:58 +00:00
Some ( api ::Opcode ::Quit ) = > {
2021-04-05 15:50:00 +00:00
break
2021-05-21 07:56:58 +00:00
} ,
None = > {
log ::error! ( " couldn't convert opcode, ignoring " ) ;
2021-01-13 07:04:14 +00:00
}
}
}
2021-04-05 15:50:00 +00:00
// clean up our program
log ::trace! ( " main loop exit, destroying servers " ) ;
xns . unregister_server ( trng_sid ) . unwrap ( ) ;
xous ::destroy_server ( trng_sid ) . unwrap ( ) ;
log ::trace! ( " quitting " ) ;
2021-05-20 03:01:00 +00:00
xous ::terminate_process ( 0 )
2021-01-13 07:04:14 +00:00
}