X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fln%2Fpeer_channel_encryptor.rs;h=3ec6241fb373fd58b415fbec2f548912a0cf9b96;hb=df9367adaffd1385034e123c37cf971d7e102105;hp=e22d8bd3574969cdee2ff1470a1207b5f4607480;hpb=bceb952b1270eb114a6268fcea9c69dc9c2e68c3;p=rust-lightning diff --git a/src/ln/peer_channel_encryptor.rs b/src/ln/peer_channel_encryptor.rs index e22d8bd3..3ec6241f 100644 --- a/src/ln/peer_channel_encryptor.rs +++ b/src/ln/peer_channel_encryptor.rs @@ -5,16 +5,14 @@ use secp256k1::Secp256k1; use secp256k1::key::{PublicKey,SecretKey}; use secp256k1::ecdh::SharedSecret; -use rand::{thread_rng,Rng}; - use crypto::digest::Digest; use crypto::hkdf::{hkdf_extract,hkdf_expand}; -use crypto::sha2::Sha256; use crypto::aead::{AeadEncryptor, AeadDecryptor}; use util::chacha20poly1305rfc::ChaCha20Poly1305RFC; -use util::byte_utils; +use util::{byte_utils,rng}; +use util::sha2::Sha256; // Sha256("Noise_XK_secp256k1_ChaChaPoly_SHA256") const NOISE_CK: [u8; 32] = [0x26, 0x40, 0xf5, 0x2e, 0xeb, 0xcd, 0x9e, 0x88, 0x29, 0x58, 0x95, 0x1c, 0x79, 0x42, 0x50, 0xee, 0xdb, 0x28, 0x00, 0x2c, 0x05, 0xd7, 0xdc, 0x2e, 0xa0, 0xf1, 0x95, 0x40, 0x60, 0x42, 0xca, 0xf1]; @@ -75,9 +73,8 @@ pub struct PeerChannelEncryptor { impl PeerChannelEncryptor { pub fn new_outbound(their_node_id: PublicKey) -> PeerChannelEncryptor { - let mut rng = thread_rng(); let mut key = [0u8; 32]; - rng.fill_bytes(&mut key); + rng::fill_bytes(&mut key); let secp_ctx = Secp256k1::new(); let sec_key = SecretKey::from_slice(&secp_ctx, &key).unwrap(); //TODO: nicer rng-is-bad error message @@ -150,7 +147,7 @@ impl PeerChannelEncryptor { let mut chacha = ChaCha20Poly1305RFC::new(key, &nonce, h); if !chacha.decrypt(&cyphertext[0..cyphertext.len() - 16], res, &cyphertext[cyphertext.len() - 16..]) { - return Err(HandleError{err: "Bad MAC", msg: Some(msgs::ErrorMessage::DisconnectPeer{})}); + return Err(HandleError{err: "Bad MAC", msg: Some(msgs::ErrorAction::DisconnectPeer{})}); } Ok(()) } @@ -198,11 +195,11 @@ impl PeerChannelEncryptor { assert_eq!(act.len(), 50); if act[0] != 0 { - return Err(HandleError{err: "Unknown handshake version number", msg: Some(msgs::ErrorMessage::DisconnectPeer{})}); + return Err(HandleError{err: "Unknown handshake version number", msg: Some(msgs::ErrorAction::DisconnectPeer{})}); } let their_pub = match PublicKey::from_slice(secp_ctx, &act[1..34]) { - Err(_) => return Err(HandleError{err: "Invalid public key", msg: Some(msgs::ErrorMessage::DisconnectPeer{})}), + Err(_) => return Err(HandleError{err: "Invalid public key", msg: Some(msgs::ErrorAction::DisconnectPeer{})}), Ok(key) => key, }; @@ -275,9 +272,8 @@ impl PeerChannelEncryptor { pub fn process_act_one_with_key(&mut self, act_one: &[u8], our_node_secret: &SecretKey) -> Result<[u8; 50], HandleError> { assert_eq!(act_one.len(), 50); - let mut rng = thread_rng(); let mut key = [0u8; 32]; - rng.fill_bytes(&mut key); + rng::fill_bytes(&mut key); let our_ephemeral_key = SecretKey::from_slice(&self.secp_ctx, &key).unwrap(); //TODO: nicer rng-is-bad error message self.process_act_one_with_ephemeral_key(act_one, our_node_secret, our_ephemeral_key) } @@ -353,14 +349,14 @@ impl PeerChannelEncryptor { panic!("Requested act at wrong step"); } if act_three[0] != 0 { - return Err(HandleError{err: "Unknown handshake version number", msg: Some(msgs::ErrorMessage::DisconnectPeer{})}); + return Err(HandleError{err: "Unknown handshake version number", msg: Some(msgs::ErrorAction::DisconnectPeer{})}); } let mut their_node_id = [0; 33]; PeerChannelEncryptor::decrypt_with_ad(&mut their_node_id, 1, &temp_k2.unwrap(), &bidirectional_state.h, &act_three[1..50])?; self.their_node_id = Some(match PublicKey::from_slice(&self.secp_ctx, &their_node_id) { Ok(key) => key, - Err(_) => return Err(HandleError{err: "Bad node_id from peer", msg: Some(msgs::ErrorMessage::DisconnectPeer{})}), + Err(_) => return Err(HandleError{err: "Bad node_id from peer", msg: Some(msgs::ErrorAction::DisconnectPeer{})}), }); let mut sha = Sha256::new();