From e120c75d174e665c3aca1d791732926b0183bf0b Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 30 Aug 2017 17:14:52 +0200 Subject: [PATCH] Hashable::sha3 -> fn keccak for ethcore-network --- Cargo.lock | 1 + util/network/Cargo.toml | 1 + util/network/src/connection.rs | 10 +++++----- util/network/src/discovery.rs | 22 +++++++++++----------- util/network/src/handshake.rs | 4 ++-- util/network/src/host.rs | 8 ++++---- util/network/src/lib.rs | 3 ++- util/src/lib.rs | 1 - util/src/trie/triedb.rs | 2 +- 9 files changed, 27 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c28246791..bd3469589 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -693,6 +693,7 @@ dependencies = [ "ethcore-util 1.8.0", "ethcrypto 0.1.0", "ethkey 0.2.0", + "hash 0.1.0", "igd 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "ipnetwork 0.12.6 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/util/network/Cargo.toml b/util/network/Cargo.toml index 2d571d632..86f7867f4 100644 --- a/util/network/Cargo.toml +++ b/util/network/Cargo.toml @@ -31,6 +31,7 @@ rlp = { path = "../rlp" } path = { path = "../path" } ethcore-logger = { path ="../../logger" } ipnetwork = "0.12.6" +hash = { path = "../hash" } [features] default = [] diff --git a/util/network/src/connection.rs b/util/network/src/connection.rs index a9cb05c00..b51813100 100644 --- a/util/network/src/connection.rs +++ b/util/network/src/connection.rs @@ -18,11 +18,11 @@ use std::sync::Arc; use std::collections::VecDeque; use std::net::SocketAddr; use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering}; +use hash::{keccak, keccak_into}; use mio::{Token, Ready, PollOpt}; use mio::deprecated::{Handler, EventLoop, TryRead, TryWrite}; use mio::tcp::*; use util::hash::*; -use util::sha3::*; use util::bytes::*; use rlp::*; use std::io::{self, Cursor, Read, Write}; @@ -312,16 +312,16 @@ impl EncryptedConnection { } let mut key_material = H512::new(); shared.copy_to(&mut key_material[0..32]); - nonce_material.sha3_into(&mut key_material[32..64]); - key_material.sha3().copy_to(&mut key_material[32..64]); - key_material.sha3().copy_to(&mut key_material[32..64]); + keccak_into(&nonce_material, &mut key_material[32..64]); + keccak(&key_material).copy_to(&mut key_material[32..64]); + keccak(&key_material).copy_to(&mut key_material[32..64]); let iv = vec![0u8; 16]; let encoder = CtrMode::new(AesSafe256Encryptor::new(&key_material[32..64]), iv); let iv = vec![0u8; 16]; let decoder = CtrMode::new(AesSafe256Encryptor::new(&key_material[32..64]), iv); - key_material.sha3().copy_to(&mut key_material[32..64]); + keccak(&key_material).copy_to(&mut key_material[32..64]); let mac_encoder = EcbEncryptor::new(AesSafe256Encryptor::new(&key_material[32..64]), NoPadding); let mut egress_mac = Keccak::new_keccak256(); diff --git a/util/network/src/discovery.rs b/util/network/src/discovery.rs index 3dcd8548d..f4025b26f 100644 --- a/util/network/src/discovery.rs +++ b/util/network/src/discovery.rs @@ -22,7 +22,7 @@ use std::default::Default; use mio::*; use mio::deprecated::{Handler, EventLoop}; use mio::udp::*; -use util::sha3::*; +use hash::keccak; use time; use util::hash::*; use rlp::*; @@ -112,7 +112,7 @@ impl Discovery { let socket = UdpSocket::bind(&listen).expect("Error binding UDP socket"); Discovery { id: key.public().clone(), - id_hash: key.public().sha3(), + id_hash: keccak(key.public()), secret: key.secret().clone(), public_endpoint: public, token: token, @@ -154,7 +154,7 @@ impl Discovery { fn update_node(&mut self, e: NodeEntry) { trace!(target: "discovery", "Inserting {:?}", &e); - let id_hash = e.id.sha3(); + let id_hash = keccak(e.id); let ping = { let mut bucket = &mut self.node_buckets[Discovery::distance(&self.id_hash, &id_hash) as usize]; let updated = if let Some(node) = bucket.nodes.iter_mut().find(|n| n.address.id == e.id) { @@ -180,7 +180,7 @@ impl Discovery { } fn clear_ping(&mut self, id: &NodeId) { - let mut bucket = &mut self.node_buckets[Discovery::distance(&self.id_hash, &id.sha3()) as usize]; + let mut bucket = &mut self.node_buckets[Discovery::distance(&self.id_hash, &keccak(id)) as usize]; if let Some(node) = bucket.nodes.iter_mut().find(|n| &n.address.id == id) { node.timeout = None; } @@ -264,7 +264,7 @@ impl Discovery { rlp.append(×tamp); let bytes = rlp.drain(); - let hash = bytes.as_ref().sha3(); + let hash = keccak(bytes.as_ref()); let signature = match sign(&self.secret, &hash) { Ok(s) => s, Err(_) => { @@ -276,7 +276,7 @@ impl Discovery { packet.extend(hash.iter()); packet.extend(signature.iter()); packet.extend(bytes.iter()); - let signed_hash = (&packet[32..]).sha3(); + let signed_hash = keccak(&packet[32..]); packet[0..32].clone_from_slice(&signed_hash); self.send_to(packet, address.clone()); } @@ -285,7 +285,7 @@ impl Discovery { fn nearest_node_entries(target: &NodeId, buckets: &[NodeBucket]) -> Vec { let mut found: BTreeMap> = BTreeMap::new(); let mut count = 0; - let target_hash = target.sha3(); + let target_hash = keccak(target); // Sort nodes by distance to target for bucket in buckets { @@ -368,14 +368,14 @@ impl Discovery { return Err(NetworkError::BadProtocol); } - let hash_signed = (&packet[32..]).sha3(); + let hash_signed = keccak(&packet[32..]); if hash_signed[..] != packet[0..32] { return Err(NetworkError::BadProtocol); } let signed = &packet[(32 + 65)..]; let signature = H520::from_slice(&packet[32..(32 + 65)]); - let node_id = recover(&signature.into(), &signed.sha3())?; + let node_id = recover(&signature.into(), &keccak(signed))?; let packet_id = signed[0]; let rlp = UntrustedRlp::new(&signed[1..]); @@ -419,7 +419,7 @@ impl Discovery { self.update_node(entry.clone()); added_map.insert(node.clone(), entry); } - let hash = rlp.as_raw().sha3(); + let hash = keccak(rlp.as_raw()); let mut response = RlpStream::new_list(2); dest.to_rlp_list(&mut response); response.append(&hash); @@ -636,7 +636,7 @@ mod tests { buckets[0].nodes.push_back(BucketEntry { address: NodeEntry { id: NodeId::new(), endpoint: ep.clone() }, timeout: None, - id_hash: NodeId::new().sha3(), + id_hash: keccak(NodeId::new()), }); } let nearest = Discovery::nearest_node_entries(&NodeId::new(), &buckets); diff --git a/util/network/src/handshake.rs b/util/network/src/handshake.rs index f5c4e118a..ca38d4360 100644 --- a/util/network/src/handshake.rs +++ b/util/network/src/handshake.rs @@ -16,9 +16,9 @@ use std::sync::Arc; use rand::random; +use hash::keccak_into; use mio::tcp::*; use util::hash::*; -use util::sha3::Hashable; use util::bytes::Bytes; use rlp::*; use connection::{Connection}; @@ -273,7 +273,7 @@ impl Handshake { // E(remote-pubk, S(ecdhe-random, ecdh-shared-secret^nonce) || H(ecdhe-random-pubk) || pubk || nonce || 0x0) let shared = *ecdh::agree(secret, &self.id)?; sig.copy_from_slice(&*sign(self.ecdhe.secret(), &(&shared ^ &self.nonce))?); - self.ecdhe.public().sha3_into(hepubk); + keccak_into(self.ecdhe.public(), hepubk); pubk.copy_from_slice(public); nonce.copy_from_slice(&self.nonce); } diff --git a/util/network/src/host.rs b/util/network/src/host.rs index d74b2fa6e..caefc6738 100644 --- a/util/network/src/host.rs +++ b/util/network/src/host.rs @@ -25,11 +25,11 @@ use std::path::{Path, PathBuf}; use std::io::{Read, Write, ErrorKind}; use std::fs; use ethkey::{KeyPair, Secret, Random, Generator}; +use hash::keccak; use mio::*; use mio::deprecated::{EventLoop}; use mio::tcp::*; use util::hash::*; -use util::Hashable; use util::version; use rlp::*; use session::{Session, SessionInfo, SessionData}; @@ -354,8 +354,8 @@ impl HostInfo { /// Increments and returns connection nonce. pub fn next_nonce(&mut self) -> H256 { - self.nonce = self.nonce.sha3(); - self.nonce.clone() + self.nonce = keccak(&self.nonce); + self.nonce } } @@ -694,7 +694,7 @@ impl Host { let max_handshakes_per_round = max_handshakes / 2; let mut started: usize = 0; - for id in nodes.filter(|id| + for id in nodes.filter(|id| !self.have_session(id) && !self.connecting_to(id) && *id != self_id && diff --git a/util/network/src/lib.rs b/util/network/src/lib.rs index 5695b8196..accd6b0a2 100644 --- a/util/network/src/lib.rs +++ b/util/network/src/lib.rs @@ -78,6 +78,7 @@ extern crate bytes; extern crate path; extern crate ethcore_logger; extern crate ipnetwork; +extern crate hash; #[macro_use] extern crate log; @@ -156,7 +157,7 @@ pub struct IpFilter { pub predefined: AllowIP, pub custom_allow: Vec, pub custom_block: Vec, -} +} impl Default for IpFilter { fn default() -> Self { diff --git a/util/src/lib.rs b/util/src/lib.rs index def0b17c8..911df1cc6 100644 --- a/util/src/lib.rs +++ b/util/src/lib.rs @@ -118,7 +118,6 @@ pub mod error; pub mod bytes; pub mod misc; pub mod vector; -//pub mod sha3; pub mod hashdb; pub mod memorydb; pub mod migration; diff --git a/util/src/trie/triedb.rs b/util/src/trie/triedb.rs index 3e62550ba..0398aed51 100644 --- a/util/src/trie/triedb.rs +++ b/util/src/trie/triedb.rs @@ -130,7 +130,7 @@ impl<'db> TrieDB<'db> { /// This could be a simple identity operation in the case that the node is sufficiently small, but /// may require a database lookup. fn get_raw_or_lookup(&'db self, node: &'db [u8]) -> super::Result { - // check if its sha3 + len + // check if its keccak + len let r = Rlp::new(node); match r.is_data() && r.size() == 32 { true => {