1
0

feat: finish p18, implment aes ctr mode

This commit is contained in:
2025-08-24 22:28:40 +08:00
parent e400b87e9f
commit b2424c1fba
6 changed files with 165 additions and 4 deletions

View File

@@ -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)
}