Files
libsmx/fuzz/fuzz_targets/fuzz_sm4_roundtrip.rs
huangxt a4ca734d0d 重构:RustCrypto 兼容接口
主要变更:
- 新增 sm2/、sm3/、sm4/ 独立 crate,实现 RustCrypto traits
  - sm2:实现 signature、elliptic-curve traits
  - sm3:实现 digest、crypto-common traits
  - sm4:实现 cipher、aead traits
- 新增 fuzz/ 模糊测试目标
- 新增 tests/sm2_proptest.rs 属性测试
- 重构 src/sm4/ 使用 RustCrypto AEAD traits
- 更新 CI 工作流支持多 crate 测试
- 更新 .gitignore 忽略 fuzz/target/
2026-03-11 18:32:21 +08:00

32 lines
888 B
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Fuzz target: SM4 加密后解密必须还原原始数据
//!
//! 验证对任意 key + blockencrypt 后 decrypt 得到原始 block。
#![no_main]
use libfuzzer_sys::fuzz_target;
use sm4::{Sm4, KeyInit, BlockCipherEncrypt, BlockCipherDecrypt};
use sm4::cipher::array::Array;
fuzz_target!(|data: &[u8]| {
// 需要至少 32 字节(16 字节 key + 16 字节 block
if data.len() < 32 {
return;
}
let key: [u8; 16] = data[..16].try_into().unwrap();
let block_data: [u8; 16] = data[16..32].try_into().unwrap();
let cipher = Sm4::new(&Array::from(key));
let mut block = Array::from(block_data);
let original = block.clone();
// 加密
cipher.encrypt_block(&mut block);
// 解密
cipher.decrypt_block(&mut block);
// 还原检查
assert_eq!(block, original, "SM4 encrypt then decrypt must restore original");
});