Skip to content

Commit 5a00420

Browse files
committed
Merge branch 'rm-resets'
2 parents 84aef71 + 1c787d4 commit 5a00420

File tree

3 files changed

+75
-17
lines changed

3 files changed

+75
-17
lines changed

src/rust/bitbox02-rust/src/hal.rs

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use crate::workflow::RealWorkflows;
1616
pub use crate::workflow::Workflows as Ui;
1717

18+
use alloc::boxed::Box;
1819
use alloc::string::String;
1920
use alloc::vec::Vec;
2021

@@ -33,10 +34,15 @@ pub trait Sd {
3334
async fn write_bin(&mut self, filename: &str, dir: &str, data: &[u8]) -> Result<(), ()>;
3435
}
3536

37+
pub trait Random {
38+
fn random_32_bytes(&mut self) -> Box<zeroize::Zeroizing<[u8; 32]>>;
39+
}
40+
3641
/// Hardware abstraction layer for BitBox devices.
3742
pub trait Hal {
3843
fn ui(&mut self) -> &mut impl Ui;
3944
fn sd(&mut self) -> &mut impl Sd;
45+
fn random(&mut self) -> &mut impl Random;
4046
}
4147

4248
pub struct BitBox02Sd;
@@ -82,16 +88,27 @@ impl Sd for BitBox02Sd {
8288
}
8389
}
8490

91+
pub struct BitBox02Random;
92+
93+
impl Random for BitBox02Random {
94+
#[inline(always)]
95+
fn random_32_bytes(&mut self) -> Box<zeroize::Zeroizing<[u8; 32]>> {
96+
bitbox02::random::random_32_bytes()
97+
}
98+
}
99+
85100
pub struct BitBox02Hal {
86101
ui: RealWorkflows,
87102
sd: BitBox02Sd,
103+
random: BitBox02Random,
88104
}
89105

90106
impl BitBox02Hal {
91107
pub const fn new() -> Self {
92108
Self {
93109
ui: crate::workflow::RealWorkflows,
94110
sd: BitBox02Sd,
111+
random: BitBox02Random,
95112
}
96113
}
97114
}
@@ -103,14 +120,38 @@ impl Hal for BitBox02Hal {
103120
fn sd(&mut self) -> &mut impl Sd {
104121
&mut self.sd
105122
}
123+
fn random(&mut self) -> &mut impl Random {
124+
&mut self.random
125+
}
106126
}
107127

108128
#[cfg(feature = "testing")]
109129
pub mod testing {
130+
use alloc::boxed::Box;
110131
use alloc::collections::BTreeMap;
111132
use alloc::string::String;
112133
use alloc::vec::Vec;
113134

135+
use bitcoin::hashes::{Hash, sha256};
136+
137+
pub struct TestingRandom {
138+
counter: u32,
139+
}
140+
141+
impl TestingRandom {
142+
pub fn new() -> Self {
143+
Self { counter: 0 }
144+
}
145+
}
146+
147+
impl super::Random for TestingRandom {
148+
fn random_32_bytes(&mut self) -> Box<zeroize::Zeroizing<[u8; 32]>> {
149+
self.counter += 1;
150+
let hash = sha256::Hash::hash(&self.counter.to_be_bytes());
151+
Box::new(zeroize::Zeroizing::new(hash.to_byte_array()))
152+
}
153+
}
154+
114155
pub struct TestingSd {
115156
pub inserted: Option<bool>,
116157
files: BTreeMap<String, BTreeMap<String, Vec<u8>>>,
@@ -172,13 +213,15 @@ pub mod testing {
172213
pub struct TestingHal<'a> {
173214
pub ui: crate::workflow::testing::TestingWorkflows<'a>,
174215
pub sd: TestingSd,
216+
pub random: TestingRandom,
175217
}
176218

177219
impl TestingHal<'_> {
178220
pub fn new() -> Self {
179221
Self {
180222
ui: crate::workflow::testing::TestingWorkflows::new(),
181223
sd: TestingSd::new(),
224+
random: TestingRandom::new(),
182225
}
183226
}
184227
}
@@ -190,12 +233,16 @@ pub mod testing {
190233
fn sd(&mut self) -> &mut impl super::Sd {
191234
&mut self.sd
192235
}
236+
fn random(&mut self) -> &mut impl super::Random {
237+
&mut self.random
238+
}
193239
}
194240

195241
#[cfg(test)]
196242
mod tests {
197243
use super::*;
198-
use crate::hal::Sd;
244+
use crate::hal::{Random, Sd};
245+
use hex_lit::hex;
199246

200247
use util::bb02_async::block_on;
201248

@@ -230,5 +277,20 @@ pub mod testing {
230277
assert!(block_on(sd.erase_file_in_subdir("file1.txt", "dir1")).is_ok());
231278
assert_eq!(block_on(sd.list_subdir(Some("dir1"))), Ok(vec![]));
232279
}
280+
281+
#[test]
282+
fn test_random() {
283+
let mut random = TestingRandom::new();
284+
let first = random.random_32_bytes();
285+
let second = random.random_32_bytes();
286+
assert_eq!(
287+
first.as_slice(),
288+
&hex!("b40711a88c7039756fb8a73827eabe2c0fe5a0346ca7e0a104adc0fc764f528d"),
289+
);
290+
assert_eq!(
291+
second.as_slice(),
292+
&hex!("433ebf5bc03dffa38536673207a21281612cef5faa9bc7a4d5b9be2fdb12cf1a"),
293+
);
294+
}
233295
}
234296
}

src/rust/bitbox02-rust/src/hww/api/bitcoin/signtx.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,7 @@ async fn _process(
11951195

11961196
next_response.next.has_signature = true;
11971197
next_response.next.signature = crate::keystore::secp256k1_schnorr_sign(
1198+
hal.random(),
11981199
&tx_input.keypath,
11991200
&sighash,
12001201
if let TaprootSpendInfo::KeySpend(tweak_hash) = &spend_info {
@@ -2155,7 +2156,6 @@ mod tests {
21552156
}));
21562157

21572158
mock_unlocked();
2158-
bitbox02::random::fake_reset();
21592159
let mut init_request = transaction.borrow().init_request();
21602160
init_request.script_configs[0] = pb::BtcScriptConfigWithKeypath {
21612161
script_config: Some(pb::BtcScriptConfig {
@@ -2172,7 +2172,7 @@ mod tests {
21722172
assert_eq!(
21732173
next.signature,
21742174
hex!(
2175-
"472ef2aa293d5697649a5364d40567d6eaf508fca9e51321c5a48de42c32b4bbc2d0cee4ab6fea1f3b137a1cbca2abe72aa945c50e95e02fa8ac354fddf2ca10"
2175+
"74fa05435a838a76ab34105f783d8d69136977b85df4644dec6afc85bba669ddb7c127d7a5a6d3cb406843b6e4366a276872228bb9efa4e3c22cfd07be3198b5"
21762176
)
21772177
);
21782178
}
@@ -2232,7 +2232,6 @@ mod tests {
22322232

22332233
mock_host_responder(transaction.clone());
22342234
mock_unlocked();
2235-
bitbox02::random::fake_reset();
22362235
let result = block_on(process(
22372236
&mut TestingHal::new(),
22382237
&transaction.borrow().init_request(),
@@ -3227,7 +3226,6 @@ mod tests {
32273226
"sudden tenant fault inject concert weather maid people chunk youth stumble grit",
32283227
"",
32293228
);
3230-
bitbox02::random::fake_reset();
32313229
// For the policy registration below.
32323230
mock_memory();
32333231

@@ -3352,7 +3350,6 @@ mod tests {
33523350
"sudden tenant fault inject concert weather maid people chunk youth stumble grit",
33533351
"",
33543352
);
3355-
bitbox02::random::fake_reset();
33563353
// For the policy registration below.
33573354
mock_memory();
33583355

@@ -3390,7 +3387,7 @@ mod tests {
33903387
assert_eq!(
33913388
next.signature,
33923389
hex!(
3393-
"f4b760fa7f1ca8a00149bf439c07dcd3aafe4c98111607cece4b80066f7ef2e4406d18831990def0bf4a5b5647dc426ef1f749524adf0a6896844cd90b796031"
3390+
"63bb140c52b30f8625219dac0951cad4a6c1c2c5c6a014be40fd46a80ab77207780626f7d568e885f26484bbc3624714a26234a0da5236775cbfae5ed7a6ad8d"
33943391
)
33953392
);
33963393
}
@@ -3412,7 +3409,6 @@ mod tests {
34123409
"sudden tenant fault inject concert weather maid people chunk youth stumble grit",
34133410
"",
34143411
);
3415-
bitbox02::random::fake_reset();
34163412
// For the policy registration below.
34173413
mock_memory();
34183414

@@ -3710,7 +3706,6 @@ mod tests {
37103706

37113707
mock_host_responder(transaction.clone());
37123708
mock_unlocked();
3713-
bitbox02::random::fake_reset();
37143709
let init_request = transaction.borrow().init_request();
37153710

37163711
let mut mock_hal = TestingHal::new();
@@ -3789,7 +3784,6 @@ mod tests {
37893784

37903785
mock_host_responder(transaction.clone());
37913786
mock_unlocked();
3792-
bitbox02::random::fake_reset();
37933787
let init_request = transaction.borrow().init_request();
37943788

37953789
let mut mock_hal = TestingHal::new();
@@ -3829,7 +3823,6 @@ mod tests {
38293823

38303824
mock_host_responder(transaction.clone());
38313825
mock_unlocked();
3832-
bitbox02::random::fake_reset();
38333826
let init_request = transaction.borrow().init_request();
38343827

38353828
let mut mock_hal = TestingHal::new();
@@ -3861,7 +3854,6 @@ mod tests {
38613854

38623855
mock_host_responder(transaction.clone());
38633856
mock_unlocked();
3864-
bitbox02::random::fake_reset();
38653857
let init_request = transaction.borrow().init_request();
38663858

38673859
let mut mock_hal = TestingHal::new();

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ pub fn secp256k1_nonce_commit(
431431
/// Sign a message using the private key at the keypath, which is optionally tweaked with the given
432432
/// tweak.
433433
pub fn secp256k1_schnorr_sign(
434+
random: &mut impl crate::hal::Random,
434435
keypath: &[u32],
435436
msg: &[u8; 32],
436437
tweak: Option<&[u8; 32]>,
@@ -448,10 +449,11 @@ pub fn secp256k1_schnorr_sign(
448449
.map_err(|_| ())?;
449450
}
450451

452+
let aux_rand = random.random_32_bytes();
451453
let sig = SECP256K1.sign_schnorr_with_aux_rand(
452454
&bitcoin::secp256k1::Message::from_digest(*msg),
453455
&keypair,
454-
&bitbox02::random::random_32_bytes(),
456+
&aux_rand,
455457
);
456458
Ok(sig.serialize())
457459
}
@@ -522,6 +524,7 @@ pub mod testing {
522524
mod tests {
523525
use super::*;
524526

527+
use crate::hal::{Random, testing::TestingRandom};
525528
use hex_lit::hex;
526529

527530
use bitbox02::testing::mock_memory;
@@ -1308,10 +1311,10 @@ mod tests {
13081311
};
13091312

13101313
// Test without tweak
1311-
bitbox02::random::fake_reset();
13121314

13131315
bitbox02::securechip::fake_event_counter_reset();
1314-
let sig = secp256k1_schnorr_sign(&keypath, &msg, None).unwrap();
1316+
let mut random = crate::hal::testing::TestingRandom::new();
1317+
let sig = secp256k1_schnorr_sign(&mut random, &keypath, &msg, None).unwrap();
13151318
assert_eq!(bitbox02::securechip::fake_event_counter(), 1);
13161319

13171320
assert!(
@@ -1325,13 +1328,14 @@ mod tests {
13251328
);
13261329

13271330
// Test with tweak
1328-
bitbox02::random::fake_reset();
13291331
let tweak = secp256k1::Scalar::from_be_bytes(hex!(
13301332
"a39fb163dbd9b5e0840af3cc1ee41d5b31245c5dd8d6bdc3d026d09b8964997c"
13311333
))
13321334
.unwrap();
13331335
let (tweaked_pubkey, _) = expected_pubkey.add_tweak(SECP256K1, &tweak).unwrap();
1334-
let sig = secp256k1_schnorr_sign(&keypath, &msg, Some(&tweak.to_be_bytes())).unwrap();
1336+
let mut random = crate::hal::testing::TestingRandom::new();
1337+
let sig = secp256k1_schnorr_sign(&mut random, &keypath, &msg, Some(&tweak.to_be_bytes()))
1338+
.unwrap();
13351339
assert!(
13361340
SECP256K1
13371341
.verify_schnorr(

0 commit comments

Comments
 (0)