split files, keccak and keccak_aead
This commit is contained in:
1 parent
8f7e465c34
commit
208e8eda77
8 files changed
+575
-590
No files matched your search
@@ -0,0 +1,217 @@
|
||||
use crate::keccak::shake256;
|
||||
|
||||
const RATE_SIZE: usize = 16;
|
||||
const CAPACITY_SIZE: usize = 64;
|
||||
const STATE_SIZE: usize = RATE_SIZE + CAPACITY_SIZE;
|
||||
const CHUNK_SIZE: usize = 16;
|
||||
const MIN_KEY_SIZE: usize = 16;
|
||||
const MAX_KEY_SIZE: usize = 64;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct EncryptionResult {
|
||||
pub cipher: Vec<u8>,
|
||||
pub tag: Vec<u8>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct DecryptionResult {
|
||||
pub plaintext: Vec<u8>,
|
||||
pub tag: Vec<u8>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct KeccakAead {
|
||||
state: [u8; STATE_SIZE],
|
||||
}
|
||||
|
||||
impl KeccakAead {
|
||||
#[inline]
|
||||
fn assert_valid_key_size(key: &[u8]) {
|
||||
assert!(
|
||||
(MIN_KEY_SIZE..=MAX_KEY_SIZE).contains(&key.len()),
|
||||
"key must be between {} and {} bytes",
|
||||
MIN_KEY_SIZE,
|
||||
MAX_KEY_SIZE
|
||||
);
|
||||
}
|
||||
|
||||
/// Initialize the AEAD state.
|
||||
pub fn new(iv: &[u8], key: &[u8], nonce: &[u8]) -> Self {
|
||||
Self::assert_valid_key_size(key);
|
||||
|
||||
let mut input = Vec::with_capacity(iv.len() + key.len() + nonce.len());
|
||||
|
||||
input.extend_from_slice(iv);
|
||||
input.extend_from_slice(key);
|
||||
input.extend_from_slice(nonce);
|
||||
|
||||
let squeezed = shake256(&input, STATE_SIZE);
|
||||
|
||||
let mut state = [0u8; STATE_SIZE];
|
||||
state.copy_from_slice(&squeezed);
|
||||
|
||||
for (i, &byte) in key.iter().enumerate() {
|
||||
state[RATE_SIZE + i] ^= byte;
|
||||
}
|
||||
|
||||
Self { state }
|
||||
}
|
||||
|
||||
/// Process associated data.
|
||||
/// Data is processed in 16-byte chunks.
|
||||
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, &byte) in chunk.iter().enumerate() {
|
||||
input[i] = byte ^ self.state[i];
|
||||
}
|
||||
|
||||
// c = state[16..48]
|
||||
input[RATE_SIZE..STATE_SIZE].copy_from_slice(&self.state[RATE_SIZE..STATE_SIZE]);
|
||||
|
||||
self.state = shake256_state(&input);
|
||||
}
|
||||
|
||||
self.state[STATE_SIZE - 1] ^= 1;
|
||||
}
|
||||
|
||||
/// 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());
|
||||
|
||||
for chunk in plaintext.chunks(CHUNK_SIZE) {
|
||||
let mut input = [0u8; STATE_SIZE];
|
||||
|
||||
// r = chunk XOR state[0..16]
|
||||
for (i, &byte) in chunk.iter().enumerate() {
|
||||
ciphertext.push(byte ^ self.state[i]);
|
||||
}
|
||||
|
||||
// input = r || c
|
||||
//
|
||||
// The rate portion must be the generated ciphertext.
|
||||
let start = ciphertext.len() - chunk.len();
|
||||
|
||||
input[..chunk.len()].copy_from_slice(&ciphertext[start..]);
|
||||
|
||||
input[RATE_SIZE..STATE_SIZE].copy_from_slice(&self.state[RATE_SIZE..STATE_SIZE]);
|
||||
|
||||
self.state = shake256_state(&input);
|
||||
}
|
||||
|
||||
ciphertext
|
||||
}
|
||||
|
||||
/// Decrypt ciphertext.
|
||||
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];
|
||||
|
||||
// plaintext = ciphertext XOR state[0..16]
|
||||
for (i, &byte) in chunk.iter().enumerate() {
|
||||
plaintext.push(byte ^ self.state[i]);
|
||||
}
|
||||
|
||||
// input = ciphertext || c
|
||||
input[..chunk.len()].copy_from_slice(chunk);
|
||||
|
||||
input[RATE_SIZE..STATE_SIZE].copy_from_slice(&self.state[RATE_SIZE..STATE_SIZE]);
|
||||
|
||||
self.state = shake256_state(&input);
|
||||
}
|
||||
|
||||
plaintext
|
||||
}
|
||||
|
||||
/// Finalize and generate the authentication tag.
|
||||
/// The tag has the same length as the key.
|
||||
pub fn finalize(&mut self, key: &[u8]) -> Vec<u8> {
|
||||
Self::assert_valid_key_size(key);
|
||||
|
||||
// Construct padded key:
|
||||
let mut padded_key = [0u8; CAPACITY_SIZE];
|
||||
|
||||
let padding_len = CAPACITY_SIZE - key.len();
|
||||
|
||||
padded_key[padding_len..].copy_from_slice(key);
|
||||
|
||||
// XOR padded key into c.
|
||||
for (state_byte, &key_byte) in self.state[RATE_SIZE..STATE_SIZE]
|
||||
.iter_mut()
|
||||
.zip(padded_key.iter())
|
||||
{
|
||||
*state_byte ^= key_byte;
|
||||
}
|
||||
|
||||
// SHAKE256(state, 48)
|
||||
let output = shake256(&self.state, STATE_SIZE);
|
||||
|
||||
// Take the last `key.len()` bytes from the output.
|
||||
let tag_start = STATE_SIZE - key.len();
|
||||
|
||||
key.iter()
|
||||
.zip(&output[tag_start..])
|
||||
.map(|(&key_byte, &output_byte)| key_byte ^ output_byte)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Encrypt:
|
||||
///
|
||||
/// sponge = KeccakAead(iv, key, nonce)
|
||||
/// sponge.associated_data_processing(ad)
|
||||
/// ciphertext = sponge.plaintext_processing(plaintext)
|
||||
/// tag = sponge.finalize(key)
|
||||
pub fn encrypt(
|
||||
key: &[u8],
|
||||
plaintext: &[u8],
|
||||
iv: &[u8],
|
||||
associated_data: &[u8],
|
||||
nonce: &[u8],
|
||||
) -> EncryptionResult {
|
||||
let mut sponge = Self::new(iv, key, nonce);
|
||||
|
||||
sponge.associated_data_processing(associated_data);
|
||||
|
||||
let cipher = sponge.plaintext_processing(plaintext);
|
||||
|
||||
let tag = sponge.finalize(key);
|
||||
|
||||
EncryptionResult { cipher, tag }
|
||||
}
|
||||
|
||||
/// Decrypt.
|
||||
pub fn decrypt(
|
||||
key: &[u8],
|
||||
ciphertext: &[u8],
|
||||
iv: &[u8],
|
||||
associated_data: &[u8],
|
||||
nonce: &[u8],
|
||||
) -> DecryptionResult {
|
||||
let mut sponge = Self::new(iv, key, nonce);
|
||||
|
||||
sponge.associated_data_processing(associated_data);
|
||||
|
||||
let plaintext = sponge.ciphertext_processing(ciphertext);
|
||||
|
||||
let tag = sponge.finalize(key);
|
||||
|
||||
DecryptionResult { plaintext, tag }
|
||||
}
|
||||
}
|
||||
|
||||
/// SHAKE256 wrapper returning exactly a 48-byte AEAD state.
|
||||
#[inline]
|
||||
fn shake256_state(state: &[u8; STATE_SIZE]) -> [u8; STATE_SIZE] {
|
||||
let output = shake256(state, STATE_SIZE);
|
||||
|
||||
let mut result = [0u8; STATE_SIZE];
|
||||
|
||||
result.copy_from_slice(&output);
|
||||
|
||||
result
|
||||
}
|
||||
Reference in new issue
Block a user