1
0
mirror of synced 2025-01-11 00:05:48 +00:00

apply feedbacks

This commit is contained in:
Youngjoon Lee 2024-11-20 18:03:57 +09:00
parent 94dbd35fea
commit 3d32942725
No known key found for this signature in database
GPG Key ID: 303963A54A81DD4D
2 changed files with 15 additions and 9 deletions

View File

@ -6,7 +6,7 @@ pub enum Error {
InvalidPacketBytes,
#[error("Invalid routing flag: {0}")]
InvalidRoutingFlag(u8),
#[error("Invalid routing length: {0}")]
#[error("Invalid routing length: {0} bytes")]
InvalidEncryptedRoutingInfoLength(usize),
#[error("ConsistentLengthLayeredEncryptionError: {0}")]
ConsistentLengthLayeredEncryptionError(#[from] super::layered_cipher::Error),

View File

@ -44,10 +44,10 @@ type Result<T> = std::result::Result<T, Error>;
/// the same as the length of the original data.
/// For example:
/// len(encrypt(k0, [data0, encrypt(k1, [data1])])) == len(encrypt(k1, [data1]))
pub struct ConsistentLengthLayeredCipher<D: ConsistentLengthLayeredCipherData> {
pub struct ConsistentLengthLayeredCipher<D> {
/// All encrypted data produced by the cipher has the same size according to the `max_layers`.
pub max_layers: usize,
_phantom_data: PhantomData<D>,
_data: PhantomData<D>,
}
pub trait ConsistentLengthLayeredCipherData {
@ -77,7 +77,7 @@ impl<D: ConsistentLengthLayeredCipherData> ConsistentLengthLayeredCipher<D> {
pub fn new(max_layers: usize) -> Self {
Self {
max_layers,
_phantom_data: Default::default(),
_data: Default::default(),
}
}
@ -180,14 +180,20 @@ impl<D: ConsistentLengthLayeredCipherData> ConsistentLengthLayeredCipher<D> {
/// Build as many fillers as the number of keys provided.
/// Fillers are encrypted in accumulated manner by keys.
fn build_fillers(&self, params: &[EncryptionParam<D>]) -> Vec<u8> {
let single_layer_size = Self::single_layer_size();
let mut fillers = vec![0u8; single_layer_size * params.len()];
params
.iter()
.map(|param| &param.key.stream_cipher_key)
.fold(Vec::new(), |mut fillers, key| {
fillers.extend(vec![0u8; Self::single_layer_size()]);
self.apply_streamcipher(&mut fillers, key, StreamCipherOption::FromBack);
fillers
})
.enumerate()
.for_each(|(i, key)| {
self.apply_streamcipher(
&mut fillers[0..(i + 1) * single_layer_size],
key,
StreamCipherOption::FromBack,
)
});
fillers
}
/// Unpack one layer of encryption by performing the length-preserved decryption.