Skip to content

Commit 55c9f76

Browse files
committed
Bug fix using same seed sequence for channel key geneartion
1 parent b297d5b commit 55c9f76

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

src/chain/keysinterface.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use bitcoin::blockdata::transaction::{OutPoint, TxOut};
66
use bitcoin::blockdata::script::{Script, Builder};
77
use bitcoin::blockdata::opcodes;
88
use bitcoin::network::constants::Network;
9-
use bitcoin::util::hash::Hash160;
9+
use bitcoin::util::hash::{Hash160,Sha256dHash};
1010
use bitcoin::util::bip32::{ExtendedPrivKey, ExtendedPubKey, ChildNumber};
1111

1212
use secp256k1::key::{SecretKey, PublicKey};
@@ -17,8 +17,12 @@ use crypto::hkdf::{hkdf_extract,hkdf_expand};
1717

1818
use util::sha2::Sha256;
1919
use util::logger::Logger;
20+
use util::rng;
21+
use util::byte_utils;
2022

23+
use std::time::{SystemTime, UNIX_EPOCH};
2124
use std::sync::Arc;
25+
use std::sync::atomic::{AtomicUsize, Ordering};
2226

2327
/// When on-chain outputs are created by rust-lightning an event is generated which informs the
2428
/// user thereof. This enum describes the format of the output and provides the OutPoint.
@@ -39,7 +43,7 @@ pub enum SpendableOutputDescriptor {
3943
DynamicOutput {
4044
/// Outpoint spendable by user wallet
4145
outpoint: OutPoint,
42-
/// local_delayedkey = delayed_payment_basepoint_secret + SHA256(per_commitment_point || delayed_payment_basepoint
46+
/// local_delayedkey = delayed_payment_basepoint_secret + SHA256(per_commitment_point || delayed_payment_basepoint)
4347
local_delayedkey: SecretKey,
4448
/// witness redeemScript encumbering output
4549
witness_script: Script,
@@ -137,6 +141,7 @@ pub struct KeysManager {
137141
destination_script: Script,
138142
shutdown_pubkey: PublicKey,
139143
channel_master_key: ExtendedPrivKey,
144+
channel_child_index: AtomicUsize,
140145

141146
logger: Arc<Logger>,
142147
}
@@ -169,6 +174,7 @@ impl KeysManager {
169174
destination_script,
170175
shutdown_pubkey,
171176
channel_master_key,
177+
channel_child_index: AtomicUsize::new(0),
172178

173179
logger,
174180
}
@@ -192,11 +198,18 @@ impl KeysInterface for KeysManager {
192198
}
193199

194200
fn get_channel_keys(&self, _inbound: bool) -> ChannelKeys {
195-
let channel_pubkey = ExtendedPubKey::from_private(&self.secp_ctx, &self. channel_master_key);
196-
let mut seed = [0; 32];
197-
for (arr, slice) in seed.iter_mut().zip((&channel_pubkey.public_key.serialize()[0..32]).iter()) {
198-
*arr = *slice;
199-
}
200-
ChannelKeys::new_from_seed(&seed)
201+
// We only seriously intend to rely on the channel_master_key for true secure entropy,
202+
// everything else just ensures uniqueness. We generally don't expect all clients
203+
// to have non-broken RNGs here, so we also include the current time as a fallback to get uniqueness."
204+
let mut seed = [0u8; 32 + 4 + 8 + 32];
205+
rng::fill_bytes(&mut seed[..32]);
206+
let now = SystemTime::now().duration_since(UNIX_EPOCH).expect("Time went backwards");
207+
seed[32..32+4].copy_from_slice(&byte_utils::be32_to_array(now.subsec_nanos()));
208+
seed[32+4..32+4+8].copy_from_slice(&byte_utils::be64_to_array(now.as_secs()));
209+
210+
let child_ix = self.channel_child_index.fetch_add(1, Ordering::AcqRel);
211+
let child_privkey = self.channel_master_key.ckd_priv(&self.secp_ctx, ChildNumber::from_hardened_idx(child_ix as u32)).expect("Your RNG is busted");
212+
seed[32+4+8..32+4+8+32].copy_from_slice(&child_privkey.secret_key[..32]);
213+
ChannelKeys::new_from_seed(&Sha256dHash::from_data(&seed).as_bytes())
201214
}
202215
}

0 commit comments

Comments
 (0)