Skip to content

Bug fix using same seed for channel keys generation #229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

yuntai
Copy link
Contributor

@yuntai yuntai commented Oct 28, 2018

No description provided.

@ariard
Copy link

ariard commented Oct 28, 2018

Great, was what I would like to do at first, but didn't in keys interface rush writing for demo :)

@TheBlueMatt
Copy link
Collaborator

TheBlueMatt commented Oct 28, 2018 via email

@yuntai
Copy link
Contributor Author

yuntai commented Oct 28, 2018

ah.. then, for channel keys, HD stuff is any helpful? hash(rng) is enough?

@TheBlueMatt
Copy link
Collaborator

If we have more stuff available, shoving it in the hash won't hurt and RNGs have been more than problematic on some platforms many times - hashing rng output plus something derived from the master key seems like a good idea. Maybe be extra doubly overkill careful and shove the current time in there as well to make sure keys can't be duplicated, even though it doesn't help with security.

@yuntai
Copy link
Contributor Author

yuntai commented Oct 29, 2018

I'm still not so sure why we need to keep around a fixed channel master key (for a fixed user seed) if we randomize the derived keys anyway. To me, hash(rng||timestamp) as a seed for generating channel_keys alone seems enough and simple(no id tracking).

@TheBlueMatt
Copy link
Collaborator

One of the long-term goals of the key interface is to move towards a world where we can function without using our own RNG dependency and just push that complexity onto the user (for, eg, a hardware wallet or some other device with shitty entropy).

@yuntai
Copy link
Contributor Author

yuntai commented Oct 30, 2018

@ariard Sorry, didn't realize you are in the middle of work for this. As this patch is a quick point fix, hope it won't conflict too much with your local work.

@ariard
Copy link

ariard commented Oct 30, 2018

Ah sorry no I didn't work further on the channel key generation, because it's only a default implementation for KeysInterface. Long term it should be up to the user to write it's own scheme.
I'm writing tests for spendable outputs events, but nothing which should conflict with yours.

Copy link
Collaborator

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Just a few nits.

let mut seed = [0u8; 32 + 4 + 8 + 33];
rng::fill_bytes(&mut seed[..32]);
let now = SystemTime::now().duration_since(UNIX_EPOCH).expect("Time went backwards");
seed[32..32+4].copy_from_slice(&byte_utils::be32_to_array(now.subsec_micros()));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want nanos.

seed[32..32+4].copy_from_slice(&byte_utils::be32_to_array(now.subsec_micros()));
seed[32+4..32+4+8].copy_from_slice(&byte_utils::be64_to_array(now.as_secs()));

let child_ix = self.channel_child_index.fetch_add(1, Ordering::SeqCst);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RelAcq should be fine here.


let child_ix = self.channel_child_index.fetch_add(1, Ordering::SeqCst);
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");
let child_pubkey = ExtendedPubKey::from_private(&self.secp_ctx, &child_privkey);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why convert to pub? Just hash the secret key?

ChannelKeys::new_from_seed(&seed)
let mut seed = [0u8; 32 + 4 + 8 + 33];
rng::fill_bytes(&mut seed[..32]);
let now = SystemTime::now().duration_since(UNIX_EPOCH).expect("Time went backwards");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment here something like:

"We only seriously intend to rely on the channel_master_key for true secure entropy, everything else just ensures uniqueness. We generally don't expect all clients to have non-broken RNGs here, so we also include the current time as a fallback to get uniqueness."

Copy link
Collaborator

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will take as #235.

let child_ix = self.channel_child_index.fetch_add(1, Ordering::AcqRel);
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");
seed[32+4+8..32+4+8+32].copy_from_slice(&child_privkey.secret_key[..32]);
ChannelKeys::new_from_seed(&Sha256dHash::from_data(&seed).as_bytes())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to double-sha here.


let child_ix = self.channel_child_index.fetch_add(1, Ordering::AcqRel);
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");
seed[32+4+8..32+4+8+32].copy_from_slice(&child_privkey.secret_key[..32]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: secret_key is 32 bytes.

ChannelKeys::new_from_seed(&seed)
// We only seriously intend to rely on the channel_master_key for true secure entropy,
// everything else just ensures uniqueness. We generally don't expect all clients
// to have non-broken RNGs here, so we also include the current time as a fallback to get uniqueness."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Oops, you left the " at the end here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants