Merge pull request #259 from TheBlueMatt/2018-11-256-redux
[rust-lightning] / src / ln / peer_channel_encryptor.rs
index d0faeabeed0c37a30508e057de7c6e299c03c844..eaf2f683279efe83ad1a84c2f37b9c2d9df76dae 100644 (file)
@@ -4,6 +4,7 @@ use ln::msgs;
 use secp256k1::Secp256k1;
 use secp256k1::key::{PublicKey,SecretKey};
 use secp256k1::ecdh::SharedSecret;
+use secp256k1;
 
 use crypto::digest::Digest;
 use crypto::hkdf::{hkdf_extract,hkdf_expand};
@@ -65,7 +66,7 @@ enum NoiseState {
 }
 
 pub struct PeerChannelEncryptor {
-       secp_ctx: Secp256k1,
+       secp_ctx: Secp256k1<secp256k1::SignOnly>,
        their_node_id: Option<PublicKey>, // filled in for outbound, or inbound after noise_state is Finished
 
        noise_state: NoiseState,
@@ -76,7 +77,7 @@ impl PeerChannelEncryptor {
                let mut key = [0u8; 32];
                rng::fill_bytes(&mut key);
 
-               let secp_ctx = Secp256k1::new();
+               let secp_ctx = Secp256k1::signing_only();
                let sec_key = SecretKey::from_slice(&secp_ctx, &key).unwrap(); //TODO: nicer rng-is-bad error message
 
                let mut sha = Sha256::new();
@@ -102,11 +103,11 @@ impl PeerChannelEncryptor {
        }
 
        pub fn new_inbound(our_node_secret: &SecretKey) -> PeerChannelEncryptor {
-               let secp_ctx = Secp256k1::new();
+               let secp_ctx = Secp256k1::signing_only();
 
                let mut sha = Sha256::new();
                sha.input(&NOISE_H);
-               let our_node_id = PublicKey::from_secret_key(&secp_ctx, our_node_secret).unwrap(); //TODO: nicer bad-node_secret error message
+               let our_node_id = PublicKey::from_secret_key(&secp_ctx, our_node_secret);
                sha.input(&our_node_id.serialize()[..]);
                let mut h = [0; 32];
                sha.result(&mut h);
@@ -167,8 +168,8 @@ impl PeerChannelEncryptor {
        }
 
        #[inline]
-       fn outbound_noise_act(secp_ctx: &Secp256k1, state: &mut BidirectionalNoiseState, our_key: &SecretKey, their_key: &PublicKey) -> ([u8; 50], [u8; 32]) {
-               let our_pub = PublicKey::from_secret_key(secp_ctx, &our_key).unwrap(); //TODO: nicer rng-is-bad error message
+       fn outbound_noise_act<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, state: &mut BidirectionalNoiseState, our_key: &SecretKey, their_key: &PublicKey) -> ([u8; 50], [u8; 32]) {
+               let our_pub = PublicKey::from_secret_key(secp_ctx, &our_key);
 
                let mut sha = Sha256::new();
                sha.input(&state.h);
@@ -191,7 +192,7 @@ impl PeerChannelEncryptor {
        }
 
        #[inline]
-       fn inbound_noise_act(secp_ctx: &Secp256k1, state: &mut BidirectionalNoiseState, act: &[u8], our_key: &SecretKey) -> Result<(PublicKey, [u8; 32]), HandleError> {
+       fn inbound_noise_act<T>(secp_ctx: &Secp256k1<T>, state: &mut BidirectionalNoiseState, act: &[u8], our_key: &SecretKey) -> Result<(PublicKey, [u8; 32]), HandleError> {
                assert_eq!(act.len(), 50);
 
                if act[0] != 0 {
@@ -278,7 +279,7 @@ impl PeerChannelEncryptor {
                self.process_act_one_with_ephemeral_key(act_one, our_node_secret, our_ephemeral_key)
        }
 
-       pub fn process_act_two(&mut self, act_two: &[u8], our_node_secret: &SecretKey) -> Result<[u8; 66], HandleError> {
+       pub fn process_act_two(&mut self, act_two: &[u8], our_node_secret: &SecretKey) -> Result<([u8; 66], PublicKey), HandleError> {
                assert_eq!(act_two.len(), 50);
 
                let mut final_hkdf = [0; 64];
@@ -294,7 +295,7 @@ impl PeerChannelEncryptor {
                                                let (re, temp_k2) = PeerChannelEncryptor::inbound_noise_act(&self.secp_ctx, bidirectional_state, act_two, &ie)?;
 
                                                let mut res = [0; 66];
-                                               let our_node_id = PublicKey::from_secret_key(&self.secp_ctx, &our_node_secret).unwrap(); //TODO: nicer rng-is-bad error message
+                                               let our_node_id = PublicKey::from_secret_key(&self.secp_ctx, &our_node_secret);
 
                                                PeerChannelEncryptor::encrypt_with_ad(&mut res[1..50], 1, &temp_k2, &bidirectional_state.h, &our_node_id.serialize()[..]);
 
@@ -333,7 +334,7 @@ impl PeerChannelEncryptor {
                        rck: ck,
                };
 
-               Ok(res)
+               Ok((res, self.their_node_id.unwrap().clone()))
        }
 
        pub fn process_act_three(&mut self, act_three: &[u8]) -> Result<PublicKey, HandleError> {
@@ -536,7 +537,7 @@ mod tests {
                        let mut outbound_peer = get_outbound_peer_for_initiator_test_vectors();
 
                        let act_two = hex::decode("0002466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f276e2470b93aac583c9ef6eafca3f730ae").unwrap().to_vec();
-                       assert_eq!(outbound_peer.process_act_two(&act_two[..], &our_node_id).unwrap()[..], hex::decode("00b9e3a702e93e3a9948c2ed6e5fd7590a6e1c3a0344cfc9d5b57357049aa22355361aa02e55a8fc28fef5bd6d71ad0c38228dc68b1c466263b47fdf31e560e139ba").unwrap()[..]);
+                       assert_eq!(outbound_peer.process_act_two(&act_two[..], &our_node_id).unwrap().0[..], hex::decode("00b9e3a702e93e3a9948c2ed6e5fd7590a6e1c3a0344cfc9d5b57357049aa22355361aa02e55a8fc28fef5bd6d71ad0c38228dc68b1c466263b47fdf31e560e139ba").unwrap()[..]);
 
                        match outbound_peer.noise_state {
                                NoiseState::Finished { sk, sn, sck, rk, rn, rck } => {
@@ -693,7 +694,7 @@ mod tests {
                        let our_node_id = SecretKey::from_slice(&secp_ctx, &hex::decode("1111111111111111111111111111111111111111111111111111111111111111").unwrap()[..]).unwrap();
 
                        let act_two = hex::decode("0002466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f276e2470b93aac583c9ef6eafca3f730ae").unwrap().to_vec();
-                       assert_eq!(outbound_peer.process_act_two(&act_two[..], &our_node_id).unwrap()[..], hex::decode("00b9e3a702e93e3a9948c2ed6e5fd7590a6e1c3a0344cfc9d5b57357049aa22355361aa02e55a8fc28fef5bd6d71ad0c38228dc68b1c466263b47fdf31e560e139ba").unwrap()[..]);
+                       assert_eq!(outbound_peer.process_act_two(&act_two[..], &our_node_id).unwrap().0[..], hex::decode("00b9e3a702e93e3a9948c2ed6e5fd7590a6e1c3a0344cfc9d5b57357049aa22355361aa02e55a8fc28fef5bd6d71ad0c38228dc68b1c466263b47fdf31e560e139ba").unwrap()[..]);
 
                        match outbound_peer.noise_state {
                                NoiseState::Finished { sk, sn, sck, rk, rn, rck } => {