@@ -19,7 +19,7 @@ use alloc::string::String;
1919use alloc:: vec:: Vec ;
2020
2121use crate :: bip32;
22- use crate :: hal:: Random ;
22+ use crate :: hal:: { Random , SecureChip } ;
2323pub use bitbox02:: keystore:: SignResult ;
2424use bitbox02:: { keystore, securechip} ;
2525
@@ -211,9 +211,10 @@ pub fn encrypt_and_store_seed(
211211
212212 bitbox02:: usb_processing:: timeout_reset ( LONG_TIMEOUT ) ;
213213
214- securechip:: init_new_password ( password) ?;
214+ hal. securechip ( ) . init_new_password ( password) ?;
215+
216+ let secret = hal. securechip ( ) . stretch_password ( password) ?;
215217
216- let secret = securechip:: stretch_password ( password) ?;
217218 let iv_rand = hal. random ( ) . random_32_bytes ( ) ;
218219 let iv: & [ u8 ; 16 ] = iv_rand. first_chunk :: < 16 > ( ) . unwrap ( ) ;
219220 let encrypted = bitbox_aes:: encrypt_with_hmac ( iv, & secret, seed) ;
@@ -243,13 +244,16 @@ fn check_retained_seed(seed: &[u8]) -> Result<(), ()> {
243244 Ok ( ( ) )
244245}
245246
246- fn get_and_decrypt_seed ( password : & str ) -> Result < zeroize:: Zeroizing < Vec < u8 > > , Error > {
247+ fn get_and_decrypt_seed (
248+ hal : & mut impl crate :: hal:: Hal ,
249+ password : & str ,
250+ ) -> Result < zeroize:: Zeroizing < Vec < u8 > > , Error > {
247251 let encrypted = bitbox02:: memory:: get_encrypted_seed_and_hmac ( ) . map_err ( |_| Error :: Memory ) ?;
248252 // Our Optiga securechip implementation fails password stretching if the password is
249253 // wrong, so it already returns an error here. The ATECC stretches the password without checking
250254 // if the password is correct, and we determine if it is correct in the seed decryption
251255 // step below.
252- let secret = securechip:: stretch_password ( password) ?;
256+ let secret = hal . securechip ( ) . stretch_password ( password) ?;
253257 let seed = match bitbox_aes:: decrypt_with_hmac ( & secret, & encrypted) {
254258 Ok ( seed) => seed,
255259 Err ( ( ) ) => return Err ( Error :: IncorrectPassword ) ,
@@ -279,7 +283,7 @@ pub fn unlock(
279283 }
280284 bitbox02:: usb_processing:: timeout_reset ( LONG_TIMEOUT ) ;
281285 bitbox02:: memory:: smarteeprom_increment_unlock_attempts ( ) ;
282- let seed = match get_and_decrypt_seed ( password) {
286+ let seed = match get_and_decrypt_seed ( hal , password) {
283287 Ok ( seed) => seed,
284288 err @ Err ( _) => {
285289 if get_remaining_unlock_attempts ( ) == 0 {
@@ -892,17 +896,17 @@ mod tests {
892896 ) ) ;
893897
894898 // First call: unlock. The first one does a seed rentention (1 securechip event).
895- bitbox02 :: securechip:: fake_event_counter_reset ( ) ;
899+ mock_hal . securechip . event_counter_reset ( ) ;
896900 assert_eq ! ( unlock( & mut mock_hal, "password" ) . unwrap( ) . as_slice( ) , seed) ;
897- assert_eq ! ( bitbox02 :: securechip:: fake_event_counter ( ) , 6 ) ;
901+ assert_eq ! ( mock_hal . securechip. get_event_counter ( ) , 6 ) ;
898902
899903 // Loop to check that unlocking works while unlocked.
900904 for _ in 0 ..2 {
901905 // Further calls perform a password check.The password check does not do the retention
902906 // so it ends up needing one secure chip operation less.
903- bitbox02 :: securechip:: fake_event_counter_reset ( ) ;
907+ mock_hal . securechip . event_counter_reset ( ) ;
904908 assert_eq ! ( unlock( & mut mock_hal, "password" ) . unwrap( ) . as_slice( ) , seed) ;
905- assert_eq ! ( bitbox02 :: securechip:: fake_event_counter ( ) , 5 ) ;
909+ assert_eq ! ( mock_hal . securechip. get_event_counter ( ) , 5 ) ;
906910 }
907911
908912 // Also check that the retained seed was encrypted with the expected encryption key.
@@ -1510,33 +1514,35 @@ mod tests {
15101514 lock ( ) ;
15111515 let seed = & seed[ ..test. seed_len ] ;
15121516
1517+ let mut mock_hal = crate :: hal:: testing:: TestingHal :: new ( ) ;
1518+
15131519 assert ! (
15141520 block_on( unlock_bip39(
1515- & mut crate :: hal :: testing :: TestingRandom :: new ( ) ,
1521+ & mut mock_hal . random ,
15161522 seed,
15171523 test. mnemonic_passphrase,
15181524 async || { }
15191525 ) )
15201526 . is_err( )
15211527 ) ;
15221528
1523- bitbox02 :: securechip:: fake_event_counter_reset ( ) ;
1524- assert ! ( encrypt_and_store_seed( & mut TestingHal :: new ( ) , seed, "foo" ) . is_ok( ) ) ;
1525- assert_eq ! ( bitbox02 :: securechip:: fake_event_counter ( ) , 7 ) ;
1529+ mock_hal . securechip . event_counter_reset ( ) ;
1530+ assert ! ( encrypt_and_store_seed( & mut mock_hal , seed, "foo" ) . is_ok( ) ) ;
1531+ assert_eq ! ( mock_hal . securechip. get_event_counter ( ) , 7 ) ;
15261532
15271533 assert ! ( is_locked( ) ) ;
15281534
1529- bitbox02 :: securechip:: fake_event_counter_reset ( ) ;
1535+ mock_hal . securechip . event_counter_reset ( ) ;
15301536 assert ! (
15311537 block_on( unlock_bip39(
1532- & mut crate :: hal :: testing :: TestingRandom :: new ( ) ,
1538+ & mut mock_hal . random ,
15331539 seed,
15341540 test. mnemonic_passphrase,
15351541 async || { }
15361542 ) )
15371543 . is_ok( )
15381544 ) ;
1539- assert_eq ! ( bitbox02 :: securechip:: fake_event_counter ( ) , 1 ) ;
1545+ assert_eq ! ( mock_hal . securechip. get_event_counter ( ) , 1 ) ;
15401546
15411547 assert ! ( !is_locked( ) ) ;
15421548 assert_eq ! (
@@ -1545,9 +1551,9 @@ mod tests {
15451551 ) ;
15461552 let keypath = & [ 44 + HARDENED , 0 + HARDENED , 0 + HARDENED ] ;
15471553
1548- bitbox02 :: securechip:: fake_event_counter_reset ( ) ;
1554+ mock_hal . securechip . event_counter_reset ( ) ;
15491555 let xpub = get_xpub_once ( keypath) . unwrap ( ) ;
1550- assert_eq ! ( bitbox02 :: securechip:: fake_event_counter ( ) , 1 ) ;
1556+ assert_eq ! ( mock_hal . securechip. get_event_counter ( ) , 1 ) ;
15511557
15521558 assert_eq ! (
15531559 xpub. serialize_str( crate :: bip32:: XPubType :: Xpub ) . unwrap( ) ,
0 commit comments