diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/accessor.rs | 4 | ||||
-rw-r--r-- | src/block.rs | 4 | ||||
-rw-r--r-- | src/lib.rs | 88 |
3 files changed, 60 insertions, 36 deletions
diff --git a/src/accessor.rs b/src/accessor.rs index 24905af..35d2890 100644 --- a/src/accessor.rs +++ b/src/accessor.rs @@ -71,9 +71,7 @@ impl<'a> BlockAccessor<'a> { self.data[start + 16..start + 32].copy_from_slice(&m_y.bytes()); } - pub fn pairs_mut<'b>( - &'b mut self, - ) -> impl Iterator<Item = (&'b mut [u8; 16], &'b mut [u8; 16])> { + pub fn pairs_mut(&mut self) -> impl Iterator<Item = (&mut [u8; 16], &mut [u8; 16])> { let stop = self.suffix_start(); self.data[..stop] .chunks_exact_mut(32) diff --git a/src/block.rs b/src/block.rs index ce2d22c..3425242 100644 --- a/src/block.rs +++ b/src/block.rs @@ -61,7 +61,7 @@ impl Block { Block(value.into().to_be_bytes().into()) } - pub fn to_int(&self) -> u128 { + pub fn to_int(self) -> u128 { u128::from_be_bytes(self.0.into()) } @@ -226,7 +226,7 @@ impl Mul<u32> for Block { result } _ if rhs % 2 == 0 => self * 2 * (rhs / 2), - _ => self * (rhs - 1) ^ self, + _ => (self * (rhs - 1)) ^ self, } } } @@ -147,6 +147,7 @@ impl Aez { let key = extract(key); let (key_i, key_j, key_l) = split_key(&key); let aes = aesround::AesImpl::new(key_i, key_j, key_l); + #[allow(clippy::erasing_op)] let key_l_multiples = [ key_l * 0, key_l * 1, @@ -214,7 +215,7 @@ impl Aez { data: &mut Vec<u8>, ) { data.resize(data.len() + tau as usize, 0); - encrypt(&self, nonce, associated_data, tau, data); + encrypt(self, nonce, associated_data, tau, data); } /// Encrypts the data inplace. @@ -233,7 +234,7 @@ impl Aez { assert!(buffer.len() >= tau as usize); let data_len = buffer.len() - tau as usize; append_auth(data_len, buffer); - encrypt(&self, nonce, associated_data, tau as u32, buffer); + encrypt(self, nonce, associated_data, tau, buffer); } /// Encrypts the data in the given buffer, writing the output to the given output buffer. @@ -253,7 +254,7 @@ impl Aez { let tau = output.len() - input.len(); output[..input.len()].copy_from_slice(input); append_auth(input.len(), output); - encrypt(&self, nonce, associated_data, tau as u32, output); + encrypt(self, nonce, associated_data, tau as u32, output); } /// Decrypts the given ciphertext. @@ -276,7 +277,7 @@ impl Aez { data: &[u8], ) -> Option<Vec<u8>> { let mut buffer = Vec::from(data); - let len = match decrypt(&self, nonce, associated_data, tau, &mut buffer) { + let len = match decrypt(self, nonce, associated_data, tau, &mut buffer) { None => return None, Some(m) => m.len(), }; @@ -296,7 +297,7 @@ impl Aez { tau: u32, data: &'a mut [u8], ) -> Option<&'a [u8]> { - decrypt(&self, nonce, associated_data, tau, data) + decrypt(self, nonce, associated_data, tau, data) } } @@ -342,9 +343,9 @@ fn encrypt(aez: &Aez, nonce: &[u8], ad: &[&[u8]], tau: u32, buffer: &mut [u8]) { if buffer.len() == tau as usize { // As aez_prf only xor's the input in, we have to clear the buffer first buffer.fill(0); - aez_prf(aez, &tweaks, buffer); + aez_prf(aez, tweaks, buffer); } else { - encipher(aez, &tweaks, buffer); + encipher(aez, tweaks, buffer); } } @@ -373,19 +374,19 @@ fn decrypt<'a>( }; if ciphertext.len() == tau as usize { - aez_prf(aez, &tweaks, ciphertext); - if is_zeroes(&ciphertext) { + aez_prf(aez, tweaks, ciphertext); + if is_zeroes(ciphertext) { return Some(&[]); } else { return None; } } - decipher(aez, &tweaks, ciphertext); + decipher(aez, tweaks, ciphertext); let (m, auth) = ciphertext.split_at(ciphertext.len() - tau as usize); assert!(auth.len() == tau as usize); - if is_zeroes(&auth) { Some(m) } else { None } + if is_zeroes(auth) { Some(m) } else { None } } fn is_zeroes(data: &[u8]) -> bool { @@ -474,7 +475,7 @@ fn cipher_aez_tiny(mode: Mode, aez: &Aez, tweaks: Tweak, message: &mut [u8]) { } if mode == Mode::Encipher && mu < 128 { - let mut c = Block::from_slice(&message); + let mut c = Block::from_slice(message); c = c ^ (e(0, 3, aez, delta ^ (c | Block::one())) & Block::one()); message.copy_from_slice(&c.bytes()[..mu / 8]); } @@ -543,11 +544,11 @@ fn cipher_aez_core(mode: Mode, aez: &Aez, tweaks: Tweak, message: &mut [u8]) { match d { 0 => (), _ if d <= 127 => { - x = x ^ e(0, 4, aez, m_u.pad(d.into())); + x = x ^ e(0, 4, aez, m_u.pad(d)); } _ => { x = x ^ e(0, 4, aez, m_u); - x = x ^ e(0, 5, aez, m_v.pad(len_v.into())); + x = x ^ e(0, 5, aez, m_v.pad(len_v)); } } @@ -572,14 +573,14 @@ fn cipher_aez_core(mode: Mode, aez: &Aez, tweaks: Tweak, message: &mut [u8]) { match d { 0 => (), _ if d <= 127 => { - c_u = (m_u ^ e(-1, 4, aez, s)).clip(d.into()); - y = y ^ e(0, 4, aez, c_u.pad(d.into())); + c_u = (m_u ^ e(-1, 4, aez, s)).clip(d); + y = y ^ e(0, 4, aez, c_u.pad(d)); } _ => { c_u = m_u ^ e(-1, 4, aez, s); - c_v = (m_v ^ e(-1, 5, aez, s)).clip(len_v.into()); + c_v = (m_v ^ e(-1, 5, aez, s)).clip(len_v); y = y ^ e(0, 4, aez, c_u); - y = y ^ e(0, 5, aez, c_v.pad(len_v.into())); + y = y ^ e(0, 5, aez, c_v.pad(len_v)); } } @@ -601,13 +602,14 @@ fn cipher_aez_core(mode: Mode, aez: &Aez, tweaks: Tweak, message: &mut [u8]) { blocks.set_m_y(c_y); } -fn pad_to_blocks(value: &[u8]) -> impl Iterator<Item=Block> { - value.chunks(16) - .map(|chunk| if chunk.len() == 16 { +fn pad_to_blocks(value: &[u8]) -> impl Iterator<Item = Block> { + value.chunks(16).map(|chunk| { + if chunk.len() == 16 { Block::from_slice(chunk) } else { Block::from_slice(chunk).pad(chunk.len() * 8) - }) + } + }) } fn aez_hash(aez: &Aez, tweaks: Tweak) -> Block { @@ -690,7 +692,7 @@ impl<'a> E<'a> { self.aez.aes.aes4(block ^ delta) } - fn evals_for(self, block: Block) -> impl Iterator<Item=Block> { + fn evals_for(self, block: Block) -> impl Iterator<Item = Block> { Eiter::new(self, block) } @@ -730,14 +732,38 @@ impl<'a> Eiter<'a> { self.e.ki_p_i = self.e.ki_p_i * 2; let pre_xored = self.value ^ self.e.kj_t_j ^ self.e.ki_p_i; self.blocks = [ - self.e.aez.aes.aes4(pre_xored ^ self.e.aez.key_l_multiples[1]), - self.e.aez.aes.aes4(pre_xored ^ self.e.aez.key_l_multiples[2]), - self.e.aez.aes.aes4(pre_xored ^ self.e.aez.key_l_multiples[3]), - self.e.aez.aes.aes4(pre_xored ^ self.e.aez.key_l_multiples[4]), - self.e.aez.aes.aes4(pre_xored ^ self.e.aez.key_l_multiples[5]), - self.e.aez.aes.aes4(pre_xored ^ self.e.aez.key_l_multiples[6]), - self.e.aez.aes.aes4(pre_xored ^ self.e.aez.key_l_multiples[7]), - self.e.aez.aes.aes4(pre_xored ^ self.e.aez.key_l_multiples[0]), + self.e + .aez + .aes + .aes4(pre_xored ^ self.e.aez.key_l_multiples[1]), + self.e + .aez + .aes + .aes4(pre_xored ^ self.e.aez.key_l_multiples[2]), + self.e + .aez + .aes + .aes4(pre_xored ^ self.e.aez.key_l_multiples[3]), + self.e + .aez + .aes + .aes4(pre_xored ^ self.e.aez.key_l_multiples[4]), + self.e + .aez + .aes + .aes4(pre_xored ^ self.e.aez.key_l_multiples[5]), + self.e + .aez + .aes + .aes4(pre_xored ^ self.e.aez.key_l_multiples[6]), + self.e + .aez + .aes + .aes4(pre_xored ^ self.e.aez.key_l_multiples[7]), + self.e + .aez + .aes + .aes4(pre_xored ^ self.e.aez.key_l_multiples[0]), ]; self.len = 8; } |