feat: finish p18, implment aes ctr mode
This commit is contained in:
@@ -482,3 +482,23 @@ pub fn gen_random_key() -> [u8; 16] {
|
||||
rng.fill(&mut key);
|
||||
key
|
||||
}
|
||||
|
||||
pub fn aes_ctr_enc(input: &[u8], key: &[u8; 16], nonce: u64) -> Result<Vec<u8>> {
|
||||
let mut key_stream = Vec::new();
|
||||
for round in 0..=(input.len() / 16) as u64 {
|
||||
let input: Vec<u8> = nonce
|
||||
.to_le_bytes()
|
||||
.into_iter()
|
||||
.chain(round.to_le_bytes())
|
||||
.collect();
|
||||
let stream_block = aes_ecb_enc(&input, key)?;
|
||||
key_stream.extend(stream_block);
|
||||
}
|
||||
let output: Vec<u8> = input.iter().zip(key_stream).map(|(&a, b)| a ^ b).collect();
|
||||
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
pub fn aes_ctr_dec(input: &[u8], key: &[u8; 16], nonce: u64) -> Result<Vec<u8>> {
|
||||
aes_ctr_enc(input, key, nonce)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user