forked from tari-project/tari
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
63788a2
commit 58dd9e9
Showing
4 changed files
with
188 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,14 +108,92 @@ pub fn handler_get_schnorr_signature(comm: &mut Comm) -> Result<(), AppSW> { | |
Ok(()) | ||
} | ||
|
||
use ledger_device_sdk::random::Random; | ||
use ledger_device_sdk::random::rand_bytes; | ||
use ledger_device_sdk::random::LedgerRng; | ||
use rand_core::CryptoRng; | ||
use rand_core::{RngCore, Error, impls}; | ||
|
||
fn get_random_nonce() -> Zeroizing<RistrettoSecretKey> { | ||
use ledger_device_sdk::random::Random; | ||
use ledger_device_sdk::random::rand_bytes; | ||
let mut raw_bytes = [0u8; 64]; | ||
rand_bytes(&mut raw_bytes); | ||
|
||
SingleMessage::new(&format!("get_random_nonce")).show_and_wait(); | ||
|
||
let mut raw_bytes = [0u8; 64]; | ||
LedgerRng.fill_bytes(&mut raw_bytes); | ||
SingleMessage::new(&format!("{}", raw_bytes.to_hex())).show_and_wait(); | ||
|
||
let raw_bytes: [u8; 64] = core::array::from_fn(|_| u8::random()); | ||
SingleMessage::new(&format!("{}", raw_bytes.to_hex())).show_and_wait(); | ||
|
||
let v = LedgerRng.next_u64(); | ||
SingleMessage::new(&format!("{}", v)).show_and_wait(); | ||
|
||
if let Some(val) = get_random_bytes::<8>() { | ||
SingleMessage::new(&format!("{:?}", val)).show_and_wait(); | ||
} | ||
|
||
Zeroizing::new(RistrettoSecretKey::from_uniform_bytes(&raw_bytes).expect("will not fail")) | ||
} | ||
|
||
use core::ptr; | ||
|
||
|
||
const IO_RNG_BASE: u32 = 0x40000000; // Hypothetical base address | ||
const IO_RNG_STATUS_OFFSET: u32 = 0x000; | ||
const IO_RNG_DATA_OFFSET: u32 = 0x004; | ||
|
||
fn read_random_byte() -> Option<u8> { | ||
unsafe { | ||
// Wait until the RNG has valid data | ||
for _ in 0..1000 { // Timeout to avoid infinite loop | ||
if ptr::read_volatile((IO_RNG_BASE + IO_RNG_STATUS_OFFSET) as *const u32) & 1 != 0 { | ||
// Read the random byte | ||
return Some(ptr::read_volatile((IO_RNG_BASE + IO_RNG_DATA_OFFSET) as *const u8)); | ||
} | ||
} | ||
None // Return None if timeout is reached | ||
} | ||
} | ||
|
||
fn get_random_bytes<const N: usize>() -> Option<[u8; N]> { | ||
let mut array = [0u8; N]; | ||
for i in 0..N { | ||
match read_random_byte() { | ||
Some(byte) => array[i] = byte, | ||
None => return None, // Return None if reading a byte fails | ||
} | ||
} | ||
Some(array) | ||
} | ||
|
||
|
||
// /// [`RngCore`] implementation via the [`rand_bytes`] syscall | ||
// #[derive(Copy, Clone, Debug)] | ||
// pub struct LedgerRng; | ||
// | ||
// /// Implement [`RngCore`] (for `[email protected]`) using ledger syscalls | ||
// /// | ||
// /// For backwards compatibility with `[email protected]` see [rand_compat](https://docs.rs/rand-compat/latest/rand_compat/) | ||
// impl RngCore for LedgerRng { | ||
// fn next_u32(&mut self) -> u32 { | ||
// impls::next_u32_via_fill(self) | ||
// } | ||
// | ||
// fn next_u64(&mut self) -> u64 { | ||
// impls::next_u64_via_fill(self) | ||
// } | ||
// | ||
// fn fill_bytes(&mut self, dest: &mut [u8]) { | ||
// if let Err(e) = self.try_fill_bytes(dest) { | ||
// panic!("Error: {}", e); | ||
// } | ||
// } | ||
// | ||
// fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { | ||
// getrandom(dest)?; | ||
// Ok(()) | ||
// } | ||
// } | ||
// | ||
// /// Mark LedgerRng as safe for cryptographic use | ||
// impl CryptoRng for LedgerRng {} |
Oops, something went wrong.