Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,6 @@ dirs-next = "2.0.0"
dyn-clone = "1.0.17"
eyre = "0.6"
fdlimit = "0.3.0"
# pinned until downstream crypto libs migrate to 1.0 because 0.14.8 marks all types as deprecated
generic-array = "=0.14.7"
humantime = "2.1"
humantime-serde = "1.1"
itertools = { version = "0.14", default-features = false }
Expand Down
6 changes: 3 additions & 3 deletions crates/net/ecies/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ pin-project.workspace = true

tracing = { workspace = true, features = ["attributes"] }

# HeaderBytes
generic-array.workspace = true
typenum.workspace = true
byteorder.workspace = true

# crypto
Expand All @@ -42,3 +39,6 @@ aes.workspace = true
hmac.workspace = true
block-padding.workspace = true
cipher = { workspace = true, features = ["block-padding"] }

[dev-dependencies]
tokio = { workspace = true, features = ["net", "rt", "macros"] }
9 changes: 4 additions & 5 deletions crates/net/ecies/src/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::{
error::ECIESErrorImpl,
mac::{HeaderBytes, MAC},
mac::MAC,
util::{hmac_sha256, sha256},
ECIESError,
};
Expand Down Expand Up @@ -639,7 +639,6 @@ impl ECIES {
header[..3].copy_from_slice(&buf[..3]);
header[3..6].copy_from_slice(&[194, 128, 128]);

let mut header = HeaderBytes::from(header);
self.egress_aes.as_mut().unwrap().apply_keystream(&mut header);
self.egress_mac.as_mut().unwrap().update_header(&header);
let tag = self.egress_mac.as_mut().unwrap().digest();
Expand All @@ -660,7 +659,7 @@ impl ECIES {
}

let (header_bytes, mac_bytes) = split_at_mut(data, 16)?;
let header = HeaderBytes::from_mut_slice(header_bytes);
let header: &mut [u8; 16] = header_bytes.try_into().unwrap();
let mac = B128::from_slice(&mac_bytes[..16]);

self.ingress_mac.as_mut().unwrap().update_header(header);
Expand All @@ -670,11 +669,11 @@ impl ECIES {
}

self.ingress_aes.as_mut().unwrap().apply_keystream(header);
if header.as_slice().len() < 3 {
if header.len() < 3 {
return Err(ECIESErrorImpl::InvalidHeader.into())
}

let body_size = usize::try_from(header.as_slice().read_uint::<BigEndian>(3)?)?;
let body_size = usize::try_from((&header[..]).read_uint::<BigEndian>(3)?)?;

self.body_size = Some(body_size);

Expand Down
13 changes: 2 additions & 11 deletions crates/net/ecies/src/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,7 @@ use alloy_primitives::{B128, B256};
use block_padding::NoPadding;
use cipher::BlockEncrypt;
use digest::KeyInit;
use generic_array::GenericArray;
use sha3::{Digest, Keccak256};
use typenum::U16;

/// Type alias for a fixed-size array of 16 bytes used as headers.
///
/// This type is defined as [`GenericArray<u8, U16>`] and is commonly employed in Ethereum `RLPx`
/// protocol-related structures for headers. It represents 16 bytes of data used in various
/// cryptographic operations, such as MAC (Message Authentication Code) computation.
pub type HeaderBytes = GenericArray<u8, U16>;

/// [`Ethereum MAC`](https://github.com/ethereum/devp2p/blob/master/rlpx.md#mac) state.
///
Expand All @@ -49,8 +40,8 @@ impl MAC {
self.hasher.update(data)
}

/// Accumulate the given [`HeaderBytes`] into the MAC's internal state.
pub fn update_header(&mut self, data: &HeaderBytes) {
/// Accumulate the given header bytes into the MAC's internal state.
pub fn update_header(&mut self, data: &[u8; 16]) {
let aes = Aes256Enc::new_from_slice(self.secret.as_ref()).unwrap();
let mut encrypted = self.digest().0;

Expand Down
Loading