cargo fmt

This commit is contained in:
2026-09-23 11:16:13 +02:00
parent 30c906667c
commit 44fe608942
+61 -194
View File
@@ -31,9 +31,7 @@ impl KeccakAead {
CAPACITY_SIZE
);
let mut input = Vec::with_capacity(
iv.len() + key.len() + nonce.len(),
);
let mut input = Vec::with_capacity(iv.len() + key.len() + nonce.len());
input.extend_from_slice(iv);
input.extend_from_slice(key);
@@ -53,22 +51,17 @@ impl KeccakAead {
/// Process associated data.
/// Data is processed in 16-byte chunks.
pub fn associated_data_processing(
&mut self,
associated_data: &[u8],
) {
pub fn associated_data_processing(&mut self, associated_data: &[u8]) {
for chunk in associated_data.chunks(CHUNK_SIZE) {
let mut input = [0u8; STATE_SIZE];
// r = chunk XOR state[0..16]
for i in 0..chunk.len() {
input[i] =
chunk[i] ^ self.state[i];
input[i] = chunk[i] ^ self.state[i];
}
// c = state[16..40]
input[RATE_SIZE..STATE_SIZE]
.copy_from_slice(&self.state[RATE_SIZE..STATE_SIZE]);
input[RATE_SIZE..STATE_SIZE].copy_from_slice(&self.state[RATE_SIZE..STATE_SIZE]);
self.state = shake256_state(&input);
}
@@ -78,12 +71,8 @@ impl KeccakAead {
/// Encrypt plaintext.
/// Returns the ciphertext split logically into 16-byte chunks, then concatenated
pub fn plaintext_processing(
&mut self,
plaintext: &[u8],
) -> Vec<u8> {
let mut ciphertext =
Vec::with_capacity(plaintext.len());
pub fn plaintext_processing(&mut self, plaintext: &[u8]) -> Vec<u8> {
let mut ciphertext = Vec::with_capacity(plaintext.len());
for chunk in plaintext.chunks(CHUNK_SIZE) {
let mut input = [0u8; STATE_SIZE];
@@ -98,11 +87,9 @@ impl KeccakAead {
// The rate portion must be the generated ciphertext.
let start = ciphertext.len() - chunk.len();
input[..chunk.len()]
.copy_from_slice(&ciphertext[start..]);
input[..chunk.len()].copy_from_slice(&ciphertext[start..]);
input[RATE_SIZE..STATE_SIZE]
.copy_from_slice(&self.state[RATE_SIZE..STATE_SIZE]);
input[RATE_SIZE..STATE_SIZE].copy_from_slice(&self.state[RATE_SIZE..STATE_SIZE]);
self.state = shake256_state(&input);
}
@@ -111,12 +98,8 @@ impl KeccakAead {
}
/// Decrypt ciphertext.
pub fn ciphertext_processing(
&mut self,
ciphertext: &[u8],
) -> Vec<u8> {
let mut plaintext =
Vec::with_capacity(ciphertext.len());
pub fn ciphertext_processing(&mut self, ciphertext: &[u8]) -> Vec<u8> {
let mut plaintext = Vec::with_capacity(ciphertext.len());
for chunk in ciphertext.chunks(CHUNK_SIZE) {
let mut input = [0u8; STATE_SIZE];
@@ -127,11 +110,9 @@ impl KeccakAead {
}
// input = ciphertext || c
input[..chunk.len()]
.copy_from_slice(chunk);
input[..chunk.len()].copy_from_slice(chunk);
input[RATE_SIZE..STATE_SIZE]
.copy_from_slice(&self.state[RATE_SIZE..STATE_SIZE]);
input[RATE_SIZE..STATE_SIZE].copy_from_slice(&self.state[RATE_SIZE..STATE_SIZE]);
self.state = shake256_state(&input);
}
@@ -151,11 +132,9 @@ impl KeccakAead {
// Construct padded key:
let mut padded_key = [0u8; CAPACITY_SIZE];
let padding_len =
CAPACITY_SIZE - key.len();
let padding_len = CAPACITY_SIZE - key.len();
padded_key[padding_len..]
.copy_from_slice(key);
padded_key[padding_len..].copy_from_slice(key);
// XOR padded key into c.
for i in 0..CAPACITY_SIZE {
@@ -163,22 +142,14 @@ impl KeccakAead {
}
// SHAKE256(state, 40)
let output = shake256(
&self.state,
STATE_SIZE,
);
let output = shake256(&self.state, STATE_SIZE);
let tag_start =
STATE_SIZE - key.len();
let tag_start = STATE_SIZE - key.len();
let mut tag =
Vec::with_capacity(key.len());
let mut tag = Vec::with_capacity(key.len());
for i in 0..key.len() {
tag.push(
key[i]
^ output[tag_start + i],
);
tag.push(key[i] ^ output[tag_start + i]);
}
tag
@@ -197,23 +168,15 @@ impl KeccakAead {
associated_data: &[u8],
nonce: &[u8],
) -> EncryptionResult {
let mut sponge =
Self::new(iv, key, nonce);
let mut sponge = Self::new(iv, key, nonce);
sponge.associated_data_processing(
associated_data,
);
sponge.associated_data_processing(associated_data);
let cipher =
sponge.plaintext_processing(plaintext);
let cipher = sponge.plaintext_processing(plaintext);
let tag =
sponge.finalize(key);
let tag = sponge.finalize(key);
EncryptionResult {
cipher,
tag,
}
EncryptionResult { cipher, tag }
}
/// Decrypt.
@@ -224,36 +187,24 @@ impl KeccakAead {
associated_data: &[u8],
nonce: &[u8],
) -> DecryptionResult {
let mut sponge =
Self::new(iv, key, nonce);
let mut sponge = Self::new(iv, key, nonce);
sponge.associated_data_processing(
associated_data,
);
sponge.associated_data_processing(associated_data);
let plaintext =
sponge.ciphertext_processing(ciphertext);
let plaintext = sponge.ciphertext_processing(ciphertext);
let tag =
sponge.finalize(key);
let tag = sponge.finalize(key);
DecryptionResult {
plaintext,
tag,
}
DecryptionResult { plaintext, tag }
}
}
/// SHAKE256 wrapper returning exactly a 40-byte AEAD state.
#[inline]
fn shake256_state(
state: &[u8; STATE_SIZE],
) -> [u8; STATE_SIZE] {
let output =
shake256(state, STATE_SIZE);
fn shake256_state(state: &[u8; STATE_SIZE]) -> [u8; STATE_SIZE] {
let output = shake256(state, STATE_SIZE);
let mut result =
[0u8; STATE_SIZE];
let mut result = [0u8; STATE_SIZE];
result.copy_from_slice(&output);
@@ -270,37 +221,17 @@ mod tests {
let iv = [0x11u8; 16];
let nonce = [0x22u8; 16];
let associated_data =
b"associated data";
let associated_data = b"associated data";
let plaintext =
b"Hello, Keccak AEAD!";
let plaintext = b"Hello, Keccak AEAD!";
let encrypted = KeccakAead::encrypt(
&key,
plaintext,
&iv,
associated_data,
&nonce,
);
let encrypted = KeccakAead::encrypt(&key, plaintext, &iv, associated_data, &nonce);
let decrypted = KeccakAead::decrypt(
&key,
&encrypted.cipher,
&iv,
associated_data,
&nonce,
);
let decrypted = KeccakAead::decrypt(&key, &encrypted.cipher, &iv, associated_data, &nonce);
assert_eq!(
decrypted.plaintext,
plaintext
);
assert_eq!(decrypted.plaintext, plaintext);
assert_eq!(
decrypted.tag,
encrypted.tag
);
assert_eq!(decrypted.tag, encrypted.tag);
}
#[test]
@@ -309,13 +240,7 @@ mod tests {
let iv = [0x11u8; 16];
let nonce = [0x22u8; 16];
let result = KeccakAead::encrypt(
&key,
&[],
&iv,
b"test",
&nonce,
);
let result = KeccakAead::encrypt(&key, &[], &iv, b"test", &nonce);
assert_eq!(result.cipher.len(), 0);
assert_eq!(result.tag.len(), key.len());
@@ -327,35 +252,15 @@ mod tests {
let iv = [0x11u8; 16];
let nonce = [0x22u8; 16];
let plaintext =
b"test plaintext";
let plaintext = b"test plaintext";
let result = KeccakAead::encrypt(
&key,
plaintext,
&iv,
&[],
&nonce,
);
let result = KeccakAead::encrypt(&key, plaintext, &iv, &[], &nonce);
let decrypted =
KeccakAead::decrypt(
&key,
&result.cipher,
&iv,
&[],
&nonce,
);
let decrypted = KeccakAead::decrypt(&key, &result.cipher, &iv, &[], &nonce);
assert_eq!(
decrypted.plaintext,
plaintext
);
assert_eq!(decrypted.plaintext, plaintext);
assert_eq!(
decrypted.tag,
result.tag
);
assert_eq!(decrypted.tag, result.tag);
}
#[test]
@@ -365,44 +270,25 @@ mod tests {
let nonce = [0x22u8; 16];
// > 16 bytes for multiple chunks.
let plaintext =
b"0123456789abcdef0123456789abcdef0123";
let plaintext = b"0123456789abcdef0123456789abcdef0123";
let associated_data =
b"abcdefghijklmnopqrstuvwxyz";
let associated_data = b"abcdefghijklmnopqrstuvwxyz";
let encrypted =
KeccakAead::encrypt(
&key,
plaintext,
&iv,
associated_data,
&nonce,
);
let encrypted = KeccakAead::encrypt(&key, plaintext, &iv, associated_data, &nonce);
assert_eq!(
encrypted.cipher.len(),
plaintext.len()
assert_eq!(encrypted.cipher.len(), plaintext.len());
let decrypted = KeccakAead::decrypt(
&key,
&encrypted.cipher,
iv.as_slice(),
associated_data,
nonce.as_slice(),
);
let decrypted =
KeccakAead::decrypt(
&key,
&encrypted.cipher,
iv.as_slice(),
associated_data,
nonce.as_slice(),
);
assert_eq!(decrypted.plaintext, plaintext);
assert_eq!(
decrypted.plaintext,
plaintext
);
assert_eq!(
decrypted.tag,
encrypted.tag
);
assert_eq!(decrypted.tag, encrypted.tag);
}
#[test]
@@ -411,35 +297,16 @@ mod tests {
let iv = [0x11u8; 16];
let nonce = [0x22u8; 16];
let plaintext =
b"test plaintext";
let plaintext = b"test plaintext";
let encrypted =
KeccakAead::encrypt(
&key,
plaintext,
&iv,
b"ad",
&nonce,
);
let encrypted = KeccakAead::encrypt(&key, plaintext, &iv, b"ad", &nonce);
let mut modified =
encrypted.cipher.clone();
let mut modified = encrypted.cipher.clone();
modified[0] ^= 1;
let decrypted =
KeccakAead::decrypt(
&key,
&modified,
&iv,
b"ad",
&nonce,
);
let decrypted = KeccakAead::decrypt(&key, &modified, &iv, b"ad", &nonce);
assert_ne!(
decrypted.tag,
encrypted.tag
);
assert_ne!(decrypted.tag, encrypted.tag);
}
}