Skip to content

Commit 00cf523

Browse files
benma's agentbenma
authored andcommitted
rust: expose memory_get_salt_root to Rust
1 parent 4ab3db2 commit 00cf523

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

src/rust/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rust/bitbox02-sys/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ const ALLOWLIST_FNS: &[&str] = &[
9999
"memory_is_initialized",
100100
"memory_is_mnemonic_passphrase_enabled",
101101
"memory_is_seeded",
102+
"memory_get_salt_root",
102103
"memory_multisig_get_by_hash",
103104
"memory_multisig_set_by_hash",
104105
"memory_set_device_name",

src/rust/bitbox02/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ hex = { workspace = true }
3333
hex = { workspace = true }
3434
bitbox-aes = { path = "../bitbox-aes" }
3535
bitbox02-rust = { path = "../bitbox02-rust" }
36+
hex_lit = { workspace = true }
3637

3738
[features]
3839
# Only to be enabled in unit tests and simulators

src/rust/bitbox02/src/memory.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
extern crate alloc;
1717
use alloc::string::String;
18+
use alloc::vec::Vec;
1819

1920
// deduct one for the null terminator.
2021
pub 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")]
236246
pub 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<(), ()> {
244254
mod 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

Comments
 (0)