// Copyright 2016 chacha20-poly1305-aead Developers // // Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be // copied, modified, or distributed except according to those terms. use crate::as_bytes::AsBytes; use crate::simd::{Vector4, u32x4}; #[derive(Clone, Debug)] pub struct ChaCha20 { state: [u32x4; 3], } #[cfg_attr(feature = "clippy", allow(should_implement_trait))] impl ChaCha20 { pub fn new(key: &[u8], nonce: &[u8]) -> Self { Self::with_counter(key, nonce, 0) } pub fn with_counter(key: &[u8], nonce: &[u8], counter: u32) -> Self { assert!(key.len() == 32); assert!(nonce.len() == 12); let mut k = [u32x4::default(); 2]; k.as_mut_bytes().clone_from_slice(key); let mut n = [0; 3]; n.as_mut_bytes().clone_from_slice(nonce); ChaCha20 { state: [ k[0].from_le(), k[1].from_le(), u32x4::new(counter.to_le(), n[0], n[1], n[2]).from_le(), ] } } fn round(state: &mut [u32x4; 4]) { state[0] = state[0].wrapping_add(state[1]); state[3] = (state[3] ^ state[0]).rotate_left_const(16); state[2] = state[2].wrapping_add(state[3]); state[1] = (state[1] ^ state[2]).rotate_left_const(12); state[0] = state[0].wrapping_add(state[1]); state[3] = (state[3] ^ state[0]).rotate_left_const(8); state[2] = state[2].wrapping_add(state[3]); state[1] = (state[1] ^ state[2]).rotate_left_const(7); } fn shuffle(state: &mut [u32x4; 4]) { state[1] = state[1].shuffle_left_1(); state[2] = state[2].shuffle_left_2(); state[3] = state[3].shuffle_left_3(); } fn unshuffle(state: &mut [u32x4; 4]) { state[1] = state[1].shuffle_right_1(); state[2] = state[2].shuffle_right_2(); state[3] = state[3].shuffle_right_3(); } fn round_pair(state: &mut [u32x4; 4]) { ChaCha20::round(state); ChaCha20::shuffle(state); ChaCha20::round(state); ChaCha20::unshuffle(state); } fn block(&self) -> [u32x4; 4] { let c = u32x4::new(0x61707865, 0x3320646e, 0x79622d32, 0x6b206574); let mut state = [c, self.state[0], self.state[1], self.state[2]]; ChaCha20::round_pair(&mut state); ChaCha20::round_pair(&mut state); ChaCha20::round_pair(&mut state); ChaCha20::round_pair(&mut state); ChaCha20::round_pair(&mut state); ChaCha20::round_pair(&mut state); ChaCha20::round_pair(&mut state); ChaCha20::round_pair(&mut state); ChaCha20::round_pair(&mut state); ChaCha20::round_pair(&mut state); [ state[0].wrapping_add(c).to_le(), state[1].wrapping_add(self.state[0]).to_le(), state[2].wrapping_add(self.state[1]).to_le(), state[3].wrapping_add(self.state[2]).to_le(), ] } pub fn next(&mut self) -> [u32x4; 4] { let block = self.block(); self.state[2].0 = self.state[2].0.wrapping_add(1); block } } /// Runs the self-test for the chacha20 block function. #[cold] pub fn selftest() { let key = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f]; let nonce = [0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00, 0x00]; let expected = [0x10, 0xf1, 0xe7, 0xe4, 0xd1, 0x3b, 0x59, 0x15, 0x50, 0x0f, 0xdd, 0x1f, 0xa3, 0x20, 0x71, 0xc4, 0xc7, 0xd1, 0xf4, 0xc7, 0x33, 0xc0, 0x68, 0x03, 0x04, 0x22, 0xaa, 0x9a, 0xc3, 0xd4, 0x6c, 0x4e, 0xd2, 0x82, 0x64, 0x46, 0x07, 0x9f, 0xaa, 0x09, 0x14, 0xc2, 0xd7, 0x05, 0xd9, 0x8b, 0x02, 0xa2, 0xb5, 0x12, 0x9c, 0xd1, 0xde, 0x16, 0x4e, 0xb9, 0xcb, 0xd0, 0x83, 0xe8, 0xa2, 0x50, 0x3c, 0x4e]; let mut state = ChaCha20::with_counter(&key, &nonce, 1); let block = state.next(); assert_eq!(block.as_bytes(), &expected[..]); } #[cfg(test)] mod tests { use crate::as_bytes::AsBytes; use super::ChaCha20; #[test] fn selftest() { super::selftest(); } #[test] fn test_vector_1_and_2() { let mut state = ChaCha20::new(&[0; 32], &[0; 12]); assert_eq!(state.next().as_bytes(), &[0x76, 0xb8, 0xe0, 0xad, 0xa0, 0xf1, 0x3d, 0x90, 0x40, 0x5d, 0x6a, 0xe5, 0x53, 0x86, 0xbd, 0x28, 0xbd, 0xd2, 0x19, 0xb8, 0xa0, 0x8d, 0xed, 0x1a, 0xa8, 0x36, 0xef, 0xcc, 0x8b, 0x77, 0x0d, 0xc7, 0xda, 0x41, 0x59, 0x7c, 0x51, 0x57, 0x48, 0x8d, 0x77, 0x24, 0xe0, 0x3f, 0xb8, 0xd8, 0x4a, 0x37, 0x6a, 0x43, 0xb8, 0xf4, 0x15, 0x18, 0xa1, 0x1c, 0xc3, 0x87, 0xb6, 0x69, 0xb2, 0xee, 0x65, 0x86][..]); assert_eq!(state.next().as_bytes(), &[0x9f, 0x07, 0xe7, 0xbe, 0x55, 0x51, 0x38, 0x7a, 0x98, 0xba, 0x97, 0x7c, 0x73, 0x2d, 0x08, 0x0d, 0xcb, 0x0f, 0x29, 0xa0, 0x48, 0xe3, 0x65, 0x69, 0x12, 0xc6, 0x53, 0x3e, 0x32, 0xee, 0x7a, 0xed, 0x29, 0xb7, 0x21, 0x76, 0x9c, 0xe6, 0x4e, 0x43, 0xd5, 0x71, 0x33, 0xb0, 0x74, 0xd8, 0x39, 0xd5, 0x31, 0xed, 0x1f, 0x28, 0x51, 0x0a, 0xfb, 0x45, 0xac, 0xe1, 0x0a, 0x1f, 0x4b, 0x79, 0x4d, 0x6f][..]); } #[test] fn test_vector_3() { let key = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]; let mut state = ChaCha20::with_counter(&key, &[0; 12], 1); assert_eq!(state.next().as_bytes(), &[0x3a, 0xeb, 0x52, 0x24, 0xec, 0xf8, 0x49, 0x92, 0x9b, 0x9d, 0x82, 0x8d, 0xb1, 0xce, 0xd4, 0xdd, 0x83, 0x20, 0x25, 0xe8, 0x01, 0x8b, 0x81, 0x60, 0xb8, 0x22, 0x84, 0xf3, 0xc9, 0x49, 0xaa, 0x5a, 0x8e, 0xca, 0x00, 0xbb, 0xb4, 0xa7, 0x3b, 0xda, 0xd1, 0x92, 0xb5, 0xc4, 0x2f, 0x73, 0xf2, 0xfd, 0x4e, 0x27, 0x36, 0x44, 0xc8, 0xb3, 0x61, 0x25, 0xa6, 0x4a, 0xdd, 0xeb, 0x00, 0x6c, 0x13, 0xa0][..]); } #[test] fn test_vector_4() { let key = [0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; let mut state = ChaCha20::with_counter(&key, &[0; 12], 2); assert_eq!(state.next().as_bytes(), &[0x72, 0xd5, 0x4d, 0xfb, 0xf1, 0x2e, 0xc4, 0x4b, 0x36, 0x26, 0x92, 0xdf, 0x94, 0x13, 0x7f, 0x32, 0x8f, 0xea, 0x8d, 0xa7, 0x39, 0x90, 0x26, 0x5e, 0xc1, 0xbb, 0xbe, 0xa1, 0xae, 0x9a, 0xf0, 0xca, 0x13, 0xb2, 0x5a, 0xa2, 0x6c, 0xb4, 0xa6, 0x48, 0xcb, 0x9b, 0x9d, 0x1b, 0xe6, 0x5b, 0x2c, 0x09, 0x24, 0xa6, 0x6c, 0x54, 0xd5, 0x45, 0xec, 0x1b, 0x73, 0x74, 0xf4, 0x87, 0x2e, 0x99, 0xf0, 0x96][..]); } #[test] fn test_vector_5() { let nonce = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02]; let mut state = ChaCha20::with_counter(&[0; 32], &nonce, 0); assert_eq!(state.next().as_bytes(), &[0xc2, 0xc6, 0x4d, 0x37, 0x8c, 0xd5, 0x36, 0x37, 0x4a, 0xe2, 0x04, 0xb9, 0xef, 0x93, 0x3f, 0xcd, 0x1a, 0x8b, 0x22, 0x88, 0xb3, 0xdf, 0xa4, 0x96, 0x72, 0xab, 0x76, 0x5b, 0x54, 0xee, 0x27, 0xc7, 0x8a, 0x97, 0x0e, 0x0e, 0x95, 0x5c, 0x14, 0xf3, 0xa8, 0x8e, 0x74, 0x1b, 0x97, 0xc2, 0x86, 0xf7, 0x5f, 0x8f, 0xc2, 0x99, 0xe8, 0x14, 0x83, 0x62, 0xfa, 0x19, 0x8a, 0x39, 0x53, 0x1b, 0xed, 0x6d][..]); } } #[cfg(all(feature = "bench", test))] mod bench { use test::{Bencher, black_box}; use super::ChaCha20; #[bench] fn bench_new(b: &mut Bencher) { let key = [!0; 32]; let nonce = [!0; 12]; let mut counter = 0; b.bytes = 48; b.iter(|| { counter += 1; ChaCha20::with_counter(black_box(&key), black_box(&nonce), counter) }) } #[bench] fn bench_block(b: &mut Bencher) { let mut state = ChaCha20::new(&[!0; 32], &[!0; 12]); b.bytes = 64; b.iter(|| { state.next() }) } }