]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Remove useless Sha256 copies
authorMatt Corallo <git@bluematt.me>
Wed, 21 Mar 2018 22:48:41 +0000 (18:48 -0400)
committerMatt Corallo <git@bluematt.me>
Thu, 22 Mar 2018 21:42:12 +0000 (17:42 -0400)
src/ln/channel.rs
src/ln/peer_channel_encryptor.rs

index ef73db8eb813b2dea164cc00a4b245924c1898a1..59a5797bfd5e192d5e3c72ac81e5f82d7e59a96c 100644 (file)
@@ -43,34 +43,33 @@ pub struct ChannelKeys {
 
 impl ChannelKeys {
        pub fn new_from_seed(seed: &[u8; 32]) -> Result<ChannelKeys, secp256k1::Error> {
-               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,
index 2de38e68f8a0dd7ff297cd2e63a23fc43dde559c..9c400ba2b7057986c8a44bced868378f3e3a3213 100644 (file)
@@ -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..]);