feat: add __crypto/aes_ctr_test/

This commit is contained in:
2024-08-31 17:11:16 +08:00
parent 22344b6d96
commit 7f0e18bbe4
4 changed files with 180 additions and 1 deletions

100
__crypto/aes_ctr_test/Cargo.lock generated Normal file
View File

@@ -0,0 +1,100 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aes"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0"
dependencies = [
"cfg-if",
"cipher",
"cpufeatures",
]
[[package]]
name = "aes_ctr_test"
version = "0.1.0"
dependencies = [
"aes",
"hex",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cipher"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad"
dependencies = [
"crypto-common",
"inout",
]
[[package]]
name = "cpufeatures"
version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51e852e6dc9a5bed1fae92dd2375037bf2b768725bf3be87811edee3249d09ad"
dependencies = [
"libc",
]
[[package]]
name = "crypto-common"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "inout"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0c10553d664a4d0bcff9f4215d0aac67a639cc68ef660840afe309b807bc9f5"
dependencies = [
"generic-array",
]
[[package]]
name = "libc"
version = "0.2.158"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439"
[[package]]
name = "typenum"
version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"

View File

@@ -0,0 +1,8 @@
[package]
name = "aes_ctr_test"
version = "0.1.0"
edition = "2021"
[dependencies]
aes = "0.8.4"
hex = "0.4.3"

View File

@@ -0,0 +1,70 @@
use aes::cipher::generic_array::GenericArray;
use aes::cipher::{Block, BlockEncryptMut, KeyInit};
use aes::Aes128;
fn main() {
let key = [0_u8; 16];
let iv = [0_u8; 16];
let plaintext = b"hello world";
let ciphertext = encrypt_ctr(&key, &iv, plaintext);
assert_eq!("0e8c27b880aa5b54fa209e", hex::encode(&ciphertext));
let plaintext = b"hello world.hello world.hello world.hello world.hello world.hello world.";
let ciphertext = encrypt_ctr(&key, &iv, plaintext);
assert_eq!("0e8c27b880aa5b54fa209e77a251474237c28ba18812544f5e1a713bcbc7323571e4bee008d3cf\
fe9c08b5d603de9a569ff0c6c7266b2e4c8591edd1fceead8c4f22664e3c1ff0f4", hex::encode(&ciphertext));
let key = [1_u8; 16];
let mut iv = [0_u8; 16];
for i in 0..8 {
iv[i] = i as u8 + 1;
}
let plaintext = b"hello world.hello world.hello world.hello world.hello world.hello world.";
let ciphertext = encrypt_ctr(&key, &iv, plaintext);
assert_eq!("84ad8d80732490c061177a58bd26d032d6fcff2e66f9afe3cf95717485d3a4485d4a2a7bd835df\
3d0756b8192e3bf5a287ad8dd81942c43bc812c82d666ebbb34df4e2a5069467d9", hex::encode(&ciphertext));
}
fn encrypt_ctr(key: &[u8; 16], iv: &[u8; 16], plaintext: &[u8]) -> Vec<u8> {
let mut aes128 = Aes128::new_from_slice(key).unwrap();
let plaintext_len = plaintext.len();
let blocks_count = (plaintext_len / 16) + if plaintext_len % 16 == 0 { 0 } else { 1 };
let mut iv = iv.clone();
let mut cipher_text = vec![0_u8; plaintext_len];
for i in 0..blocks_count {
update_ctr(&mut iv, i as u64);
let mut block: Block<Aes128> = GenericArray::from_slice(&iv).clone();
aes128.encrypt_block_mut(&mut block);
let block_slice = block.as_slice();
for j in 0..block_slice.len() {
let oi = i * 16 + j;
if oi < plaintext_len {
cipher_text[oi] = block_slice[j] ^ plaintext[oi];
}
}
}
cipher_text
}
fn update_ctr(iv: &mut [u8; 16], counter: u64) {
let counter_be_bytes = counter.to_be_bytes();
for i in 8..iv.len() {
iv[i] = counter_be_bytes[i - 8];
}
}
#[test]
fn test_aes() {
let mut aes128 = Aes128::new_from_slice(
&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
).unwrap();
let mut a: Block<Aes128> = GenericArray::from_slice(&[0_u8; 16]).clone();
aes128.encrypt_block_mut(&mut a);
println!("{}", hex::encode(a.as_slice()));
assert_eq!("66e94bd4ef8a2c3b884cfa59ca342b2e", hex::encode(a.as_slice()));
}