From: Matt Corallo Date: Wed, 21 Mar 2018 22:48:41 +0000 (-0400) Subject: Remove useless Sha256 copies X-Git-Tag: v0.0.12~418^2~3 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=d14e31f51222ad0f9b1cef9dc7344dfce2f54494;p=rust-lightning Remove useless Sha256 copies --- diff --git a/src/ln/channel.rs b/src/ln/channel.rs index ef73db8eb..59a5797bf 100644 --- a/src/ln/channel.rs +++ b/src/ln/channel.rs @@ -43,34 +43,33 @@ pub struct ChannelKeys { impl ChannelKeys { pub fn new_from_seed(seed: &[u8; 32]) -> Result { - let sha = Sha256::new(); let mut prk = [0; 32]; - hkdf_extract(sha, b"rust-lightning key gen salt", seed, &mut prk); + hkdf_extract(Sha256::new(), b"rust-lightning key gen salt", seed, &mut prk); let secp_ctx = Secp256k1::new(); let mut okm = [0; 32]; - hkdf_expand(sha, &prk, b"rust-lightning funding key info", &mut okm); + hkdf_expand(Sha256::new(), &prk, b"rust-lightning funding key info", &mut okm); let funding_key = SecretKey::from_slice(&secp_ctx, &okm)?; - hkdf_expand(sha, &prk, b"rust-lightning revocation base key info", &mut okm); + hkdf_expand(Sha256::new(), &prk, b"rust-lightning revocation base key info", &mut okm); let revocation_base_key = SecretKey::from_slice(&secp_ctx, &okm)?; - hkdf_expand(sha, &prk, b"rust-lightning payment base key info", &mut okm); + hkdf_expand(Sha256::new(), &prk, b"rust-lightning payment base key info", &mut okm); let payment_base_key = SecretKey::from_slice(&secp_ctx, &okm)?; - hkdf_expand(sha, &prk, b"rust-lightning delayed payment base key info", &mut okm); + hkdf_expand(Sha256::new(), &prk, b"rust-lightning delayed payment base key info", &mut okm); let delayed_payment_base_key = SecretKey::from_slice(&secp_ctx, &okm)?; - hkdf_expand(sha, &prk, b"rust-lightning htlc base key info", &mut okm); + hkdf_expand(Sha256::new(), &prk, b"rust-lightning htlc base key info", &mut okm); let htlc_base_key = SecretKey::from_slice(&secp_ctx, &okm)?; - hkdf_expand(sha, &prk, b"rust-lightning channel close key info", &mut okm); + hkdf_expand(Sha256::new(), &prk, b"rust-lightning channel close key info", &mut okm); let channel_close_key = SecretKey::from_slice(&secp_ctx, &okm)?; - hkdf_expand(sha, &prk, b"rust-lightning channel monitor claim key info", &mut okm); + hkdf_expand(Sha256::new(), &prk, b"rust-lightning channel monitor claim key info", &mut okm); let channel_monitor_claim_key = SecretKey::from_slice(&secp_ctx, &okm)?; - hkdf_expand(sha, &prk, b"rust-lightning local commitment seed info", &mut okm); + hkdf_expand(Sha256::new(), &prk, b"rust-lightning local commitment seed info", &mut okm); Ok(ChannelKeys { funding_key: funding_key, diff --git a/src/ln/peer_channel_encryptor.rs b/src/ln/peer_channel_encryptor.rs index 2de38e68f..9c400ba2b 100644 --- a/src/ln/peer_channel_encryptor.rs +++ b/src/ln/peer_channel_encryptor.rs @@ -157,12 +157,11 @@ impl PeerChannelEncryptor { #[inline] fn hkdf(state: &mut BidirectionalNoiseState, ss: SharedSecret) -> [u8; 32] { - let sha = Sha256::new(); let mut hkdf = [0; 64]; { let mut prk = [0; 32]; - hkdf_extract(sha, &state.ck, &ss[..], &mut prk); - hkdf_expand(sha, &prk, &[0;0], &mut hkdf); + hkdf_extract(Sha256::new(), &state.ck, &ss[..], &mut prk); + hkdf_expand(Sha256::new(), &prk, &[0;0], &mut hkdf); } state.ck.copy_from_slice(&hkdf[0..32]); let mut res = [0; 32]; @@ -313,10 +312,9 @@ impl PeerChannelEncryptor { PeerChannelEncryptor::encrypt_with_ad(&mut res[50..], 0, &temp_k, &bidirectional_state.h, &[0; 0]); - sha.reset(); let mut prk = [0; 32]; - hkdf_extract(sha, &bidirectional_state.ck, &[0; 0], &mut prk); - hkdf_expand(sha, &prk, &[0;0], &mut final_hkdf); + hkdf_extract(Sha256::new(), &bidirectional_state.ck, &[0; 0], &mut prk); + hkdf_expand(Sha256::new(), &prk, &[0;0], &mut final_hkdf); ck = bidirectional_state.ck.clone(); res }, @@ -375,10 +373,9 @@ impl PeerChannelEncryptor { PeerChannelEncryptor::decrypt_with_ad(&mut [0; 0], 0, &temp_k, &bidirectional_state.h, &act_three[50..])?; - sha.reset(); let mut prk = [0; 32]; - hkdf_extract(sha, &bidirectional_state.ck, &[0; 0], &mut prk); - hkdf_expand(sha, &prk, &[0;0], &mut final_hkdf); + hkdf_extract(Sha256::new(), &bidirectional_state.ck, &[0; 0], &mut prk); + hkdf_expand(Sha256::new(), &prk, &[0;0], &mut final_hkdf); ck = bidirectional_state.ck.clone(); }, _ => panic!("Wrong direction for act"), @@ -416,11 +413,10 @@ impl PeerChannelEncryptor { match self.noise_state { NoiseState::Finished { ref mut sk, ref mut sn, ref mut sck, rk: _, rn: _, rck: _ } => { if *sn >= 1000 { - let mut sha = Sha256::new(); let mut prk = [0; 32]; - hkdf_extract(sha, sck, sk, &mut prk); + hkdf_extract(Sha256::new(), sck, sk, &mut prk); let mut hkdf = [0; 64]; - hkdf_expand(sha, &prk, &[0;0], &mut hkdf); + hkdf_expand(Sha256::new(), &prk, &[0;0], &mut hkdf); sck[..].copy_from_slice(&hkdf[0..32]); sk[..].copy_from_slice(&hkdf[32..]); @@ -447,11 +443,10 @@ impl PeerChannelEncryptor { match self.noise_state { NoiseState::Finished { sk: _, sn: _, sck: _, ref mut rk, ref mut rn, ref mut rck } => { if *rn >= 1000 { - let mut sha = Sha256::new(); let mut prk = [0; 32]; - hkdf_extract(sha, rck, rk, &mut prk); + hkdf_extract(Sha256::new(), rck, rk, &mut prk); let mut hkdf = [0; 64]; - hkdf_expand(sha, &prk, &[0;0], &mut hkdf); + hkdf_expand(Sha256::new(), &prk, &[0;0], &mut hkdf); rck[..].copy_from_slice(&hkdf[0..32]); rk[..].copy_from_slice(&hkdf[32..]);