1515
1616extern crate alloc;
1717use alloc:: string:: String ;
18+ use alloc:: vec:: Vec ;
1819
1920// deduct one for the null terminator.
2021pub const DEVICE_NAME_MAX_LEN : usize = bitbox02_sys:: MEMORY_DEVICE_NAME_MAX_LEN as usize - 1 ;
@@ -232,6 +233,15 @@ pub fn ble_enable(enable: bool) -> Result<(), ()> {
232233 if res { Ok ( ( ) ) } else { Err ( ( ) ) }
233234}
234235
236+ pub fn get_salt_root ( ) -> Result < zeroize:: Zeroizing < Vec < u8 > > , ( ) > {
237+ let mut salt_root = zeroize:: Zeroizing :: new ( vec ! [ 0u8 ; 32 ] ) ;
238+ if unsafe { bitbox02_sys:: memory_get_salt_root ( salt_root. as_mut_ptr ( ) ) } {
239+ Ok ( salt_root)
240+ } else {
241+ Err ( ( ) )
242+ }
243+ }
244+
235245#[ cfg( feature = "testing" ) ]
236246pub fn set_salt_root ( salt_root : & [ u8 ; 32 ] ) -> Result < ( ) , ( ) > {
237247 match unsafe { bitbox02_sys:: memory_set_salt_root ( salt_root. as_ptr ( ) ) } {
@@ -244,9 +254,29 @@ pub fn set_salt_root(salt_root: &[u8; 32]) -> Result<(), ()> {
244254mod tests {
245255 use super :: * ;
246256
257+ use hex_lit:: hex;
258+
247259 #[ test]
248260 fn test_get_attestation_bootloader_hash ( ) {
249- let expected: [ u8 ; 32 ] = * b"\x71 \x3d \xf0 \xd5 \x8c \x71 \x7d \x40 \x31 \x78 \x7c \xdc \x8f \xa3 \x5b \x90 \x25 \x82 \xbe \x6a \xb6 \xa2 \x2e \x09 \xde \x44 \x77 \xd3 \x0e \x22 \x30 \xfc " ;
261+ let expected: [ u8 ; 32 ] =
262+ hex ! ( "713df0d58c717d4031787cdc8fa35b902582be6ab6a22e09de4477d30e2230fc" ) ;
250263 assert_eq ! ( get_attestation_bootloader_hash( ) , expected) ;
251264 }
265+
266+ #[ test]
267+ fn test_get_salt_root_roundtrip ( ) {
268+ let original = get_salt_root ( ) . unwrap ( ) ;
269+
270+ let expected = hex ! ( "00112233445566778899aabbccddeefffeeddccbbaa998877665544332211000" ) ;
271+
272+ set_salt_root ( expected. as_slice ( ) . try_into ( ) . unwrap ( ) ) . unwrap ( ) ;
273+ let salt_root = get_salt_root ( ) . unwrap ( ) ;
274+ assert_eq ! ( salt_root. as_slice( ) , & expected) ;
275+
276+ let erased = [ 0xffu8 ; 32 ] ;
277+ set_salt_root ( & erased) . unwrap ( ) ;
278+ assert ! ( get_salt_root( ) . is_err( ) ) ;
279+
280+ set_salt_root ( original. as_slice ( ) . try_into ( ) . unwrap ( ) ) . unwrap ( ) ;
281+ }
252282}
0 commit comments