Skip to content

Commit d834c46

Browse files
mattsseyongkangc
authored andcommitted
refactor: replace GenericArray with regular arrays in ECIES (#19563)
1 parent 2afac18 commit d834c46

File tree

5 files changed

+9
-23
lines changed

5 files changed

+9
-23
lines changed

Cargo.lock

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,8 +549,6 @@ dirs-next = "2.0.0"
549549
dyn-clone = "1.0.17"
550550
eyre = "0.6"
551551
fdlimit = "0.3.0"
552-
# pinned until downstream crypto libs migrate to 1.0 because 0.14.8 marks all types as deprecated
553-
generic-array = "=0.14.7"
554552
humantime = "2.1"
555553
humantime-serde = "1.1"
556554
itertools = { version = "0.14", default-features = false }

crates/net/ecies/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ pin-project.workspace = true
2525

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

28-
# HeaderBytes
29-
generic-array.workspace = true
30-
typenum.workspace = true
3128
byteorder.workspace = true
3229

3330
# crypto
@@ -42,3 +39,6 @@ aes.workspace = true
4239
hmac.workspace = true
4340
block-padding.workspace = true
4441
cipher = { workspace = true, features = ["block-padding"] }
42+
43+
[dev-dependencies]
44+
tokio = { workspace = true, features = ["net", "rt", "macros"] }

crates/net/ecies/src/algorithm.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use crate::{
44
error::ECIESErrorImpl,
5-
mac::{HeaderBytes, MAC},
5+
mac::MAC,
66
util::{hmac_sha256, sha256},
77
ECIESError,
88
};
@@ -639,7 +639,6 @@ impl ECIES {
639639
header[..3].copy_from_slice(&buf[..3]);
640640
header[3..6].copy_from_slice(&[194, 128, 128]);
641641

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

662661
let (header_bytes, mac_bytes) = split_at_mut(data, 16)?;
663-
let header = HeaderBytes::from_mut_slice(header_bytes);
662+
let header: &mut [u8; 16] = header_bytes.try_into().unwrap();
664663
let mac = B128::from_slice(&mac_bytes[..16]);
665664

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

672671
self.ingress_aes.as_mut().unwrap().apply_keystream(header);
673-
if header.as_slice().len() < 3 {
672+
if header.len() < 3 {
674673
return Err(ECIESErrorImpl::InvalidHeader.into())
675674
}
676675

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

679678
self.body_size = Some(body_size);
680679

crates/net/ecies/src/mac.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,7 @@ use alloy_primitives::{B128, B256};
1414
use block_padding::NoPadding;
1515
use cipher::BlockEncrypt;
1616
use digest::KeyInit;
17-
use generic_array::GenericArray;
1817
use sha3::{Digest, Keccak256};
19-
use typenum::U16;
20-
21-
/// Type alias for a fixed-size array of 16 bytes used as headers.
22-
///
23-
/// This type is defined as [`GenericArray<u8, U16>`] and is commonly employed in Ethereum `RLPx`
24-
/// protocol-related structures for headers. It represents 16 bytes of data used in various
25-
/// cryptographic operations, such as MAC (Message Authentication Code) computation.
26-
pub type HeaderBytes = GenericArray<u8, U16>;
2718

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

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

0 commit comments

Comments
 (0)