Skip to content

Commit c4605fa

Browse files
committed
wip
1 parent 7f939ae commit c4605fa

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/rust/bitbox02-rust/src/keystore.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub mod ed25519;
1818
use alloc::vec::Vec;
1919

2020
use crate::bip32;
21-
use bitbox02::{keystore, memory, securechip};
21+
use bitbox02::{keystore, memory, random, securechip};
2222

2323
#[derive(Debug)]
2424
pub enum Error {
@@ -58,10 +58,9 @@ pub fn encrypt_and_store_seed(seed: &[u8], password: &str) -> Result<(), Error>
5858
securechip::init_new_password(password).map_err(|_| Error::SecureChip)?;
5959
let secret: zeroize::Zeroizing<Vec<u8>> =
6060
securechip::stretch_password(password).map_err(|_| Error::SecureChip)?;
61-
// TODO: set IV randomly using random_32_bytes().
62-
let iv = &[0u8; 16];
61+
let iv: &[u8; 16] = &random::combined_32_bytes()[..16].try_into().unwrap();
6362
let encrypted_seed: Vec<u8> =
64-
bitbox_aes::encrypt_with_hmac(iv, secret.as_slice().try_into().unwrap(), seed);
63+
bitbox_aes::encrypt_with_hmac(&iv, secret.as_slice().try_into().unwrap(), seed);
6564
memory::set_encrypted_seed_and_hmac(&encrypted_seed).map_err(|_| Error::Memory)?;
6665

6766
// Verify seed.

src/rust/bitbox02-sys/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ const ALLOWLIST_FNS: &[&str] = &[
108108
"progress_create",
109109
"progress_set",
110110
"random_32_bytes_mcu",
111+
"random_32_bytes",
111112
"random_mock_reset",
112113
"reboot",
113114
"reset_reset",

src/rust/bitbox02/src/random.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@ pub fn mcu_32_bytes(out: &mut [u8; 32]) {
2929
}
3030
}
3131

32+
#[cfg(target_arch = "arm")]
33+
pub fn combined_32_bytes() -> [u8; 32] {
34+
let mut out = [0u8; 32];
35+
unsafe { bitbox02_sys::random_32_bytes(out.as_mut_ptr()) }
36+
out
37+
}
38+
39+
#[cfg(not(target_arch = "arm"))]
40+
pub fn combined_32_bytes() -> [u8; 32] {
41+
let mut out = [0u8; 32];
42+
mcu_32_bytes(&mut out);
43+
out
44+
}
45+
3246
#[cfg(feature = "testing")]
3347
pub fn mock_reset() {
3448
unsafe {

0 commit comments

Comments
 (0)