Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(net): add Framed ECIES implementation #80

Merged
merged 15 commits into from
Oct 16, 2022
Prev Previous commit
chore: clippy / fmt
  • Loading branch information
gakonst committed Oct 16, 2022
commit 4e4a655c2334ea44026dd53a8eb33ba86623f917
2 changes: 1 addition & 1 deletion crates/net/ecies/src/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn kdf(secret: H256, s1: &[u8], dest: &mut [u8]) {
while written < dest.len() {
let mut hasher = Sha256::default();
let ctrs = [(ctr >> 24) as u8, (ctr >> 16) as u8, (ctr >> 8) as u8, ctr as u8];
hasher.update(&ctrs);
hasher.update(ctrs);
hasher.update(secret.as_bytes());
hasher.update(s1);
let d = hasher.finalize();
Expand Down
8 changes: 4 additions & 4 deletions crates/net/ecies/src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Decoder for ECIESCodec {
return Ok(None)
}

self.ecies.read_auth(&mut *buf.split_to(total_size))?;
self.ecies.read_auth(&mut buf.split_to(total_size))?;

self.state = ECIESState::Header;
return Ok(Some(IngressECIESValue::AuthReceive(self.ecies.remote_id())))
Expand All @@ -84,7 +84,7 @@ impl Decoder for ECIESCodec {
return Ok(None)
}

self.ecies.read_ack(&mut *buf.split_to(total_size))?;
self.ecies.read_ack(&mut buf.split_to(total_size))?;

self.state = ECIESState::Header;
return Ok(Some(IngressECIESValue::Ack))
Expand All @@ -95,7 +95,7 @@ impl Decoder for ECIESCodec {
return Ok(None)
}

self.ecies.read_header(&mut *buf.split_to(ECIES::header_len()))?;
self.ecies.read_header(&mut buf.split_to(ECIES::header_len()))?;

self.state = ECIESState::Body;
}
Expand All @@ -105,7 +105,7 @@ impl Decoder for ECIESCodec {
}

let mut data = buf.split_to(self.ecies.body_len());
let ret = Bytes::copy_from_slice(self.ecies.read_body(&mut *data)?);
let ret = Bytes::copy_from_slice(self.ecies.read_body(&mut data)?);

self.state = ECIESState::Header;
return Ok(Some(IngressECIESValue::Message(ret)))
Expand Down
3 changes: 2 additions & 1 deletion crates/net/ecies/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ mod tests {

let client_key = SecretKey::new(&mut rand::thread_rng());
let outgoing = TcpStream::connect("127.0.0.1:8080").await.unwrap();
let mut client_stream = ECIESStream::connect(outgoing, client_key, server_id).await.unwrap();
let mut client_stream =
ECIESStream::connect(outgoing, client_key, server_id).await.unwrap();
client_stream.send(Bytes::from("hello")).await.unwrap();

// make sure the server receives the message and asserts before ending the test
Expand Down
2 changes: 1 addition & 1 deletion crates/net/ecies/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub(crate) fn hmac_sha256(key: &[u8], input: &[&[u8]], auth_data: &[u8]) -> H256
hmac.update(input);
}
hmac.update(auth_data);
H256::from_slice(&*hmac.finalize().into_bytes())
H256::from_slice(&hmac.finalize().into_bytes())
}

/// Converts a [secp256k1::PublicKey] to a [PeerId] by stripping the
Expand Down