test(cipher): 钉住 PKCS7 严格解填充 + 上游向量反向断言
- 补 invalid_pkcs7_padding_yields_none:改密文末字节(长度字节 0x10→0x11)
与填充块中间字节(0x10→0x0f)两条路径,均须解密失败 ⇒ None。
第二条是「严格性」的证据:宽松解填充会照单全收。
- 补 decrypt_matches_upstream_vector:上游密文 ⇒ Some(md5("123456"))。
- 补 uppercase_hex_input_is_accepted:十六进制大小写都接受。
- encrypt_password 加 debug_assert_eq!(src.len(), 32)。
Co-Authored-By: Claude Code <[email protected]>
This commit is contained in:
1 parent
349aeef06f
commit
d73b1617b2
1 file changed
+56
@@ -50,6 +50,7 @@ fn cipher_key_bytes() -> [u8; 32] {
|
||||
pub fn encrypt_password(plain: &str) -> String {
|
||||
// src = md5(明文) 的 32 个 ASCII 字节。
|
||||
let src = md5_hex(plain).into_bytes();
|
||||
debug_assert_eq!(src.len(), 32, "md5_hex 恒为 32 个 ASCII 字符");
|
||||
|
||||
// 32 字节明文 + PKCS7 补满一整块 ⇒ 48 字节密文。
|
||||
let mut buf = [0u8; 48];
|
||||
@@ -91,6 +92,30 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
/// 上游检验向量反向:上游密文解出来必须等于 `md5("123456")`。
|
||||
#[test]
|
||||
fn decrypt_matches_upstream_vector() {
|
||||
assert_eq!(
|
||||
decrypt_to_md5(
|
||||
"d4958348e22108119b3ef57b022c83f4bf857dd0b247eaab15b9a43d47ee3c503900f7135bf0de255bc33620c3cdc02b"
|
||||
),
|
||||
Some(md5_hex("123456"))
|
||||
);
|
||||
assert_eq!(
|
||||
decrypt_to_md5(
|
||||
"d4958348e22108119b3ef57b022c83f4bf857dd0b247eaab15b9a43d47ee3c503900f7135bf0de255bc33620c3cdc02b"
|
||||
),
|
||||
Some("e10adc3949ba59abbe56e057f20f883e".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
/// 十六进制大小写都接受(前端 CryptoJS `Hex.parse` 产出小写,但别把大小写当契约)。
|
||||
#[test]
|
||||
fn uppercase_hex_input_is_accepted() {
|
||||
let lower = encrypt_password("123456");
|
||||
assert_eq!(decrypt_to_md5(&lower.to_uppercase()), decrypt_to_md5(&lower));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn md5_hex_of_empty_string() {
|
||||
assert_eq!(md5_hex(""), "d41d8cd98f00b204e9800998ecf8427e");
|
||||
@@ -139,4 +164,35 @@ mod tests {
|
||||
assert_eq!(decrypt_to_md5("00"), None);
|
||||
assert_eq!(decrypt_to_md5(""), None);
|
||||
}
|
||||
|
||||
/// 规格 §3:**PKCS7 填充不合法 ⇒ `None`**(钉住 `block-padding 0.3.3` 的严格解填充)。
|
||||
///
|
||||
/// `block-padding-0.3.3/src/lib.rs:216-218` 的 `Pkcs7::raw_unpad` 传 `strict = true`
|
||||
/// ⇒ 填充字节不全等于长度字节 n 时解填充失败 ⇒ `None`。
|
||||
///
|
||||
/// 构造(确定性,不靠概率):上游向量是 48 字节密文 = 32 字节明文 + **整块 16 字节填充**
|
||||
/// ⇒ 最后一个密文块解出来是 16 个 `0x10`。ECB 无扩散 ⇒ 改**密文**的某字节,
|
||||
/// 只改对应明文字节。
|
||||
#[test]
|
||||
fn invalid_pkcs7_padding_yields_none() {
|
||||
let good = encrypt_password("123456");
|
||||
let bytes = hex::decode(&good).unwrap();
|
||||
assert_eq!(bytes.len(), 48, "32 字节明文 + 整块 16 字节填充");
|
||||
|
||||
// ① 改填充块的**长度字节**(末字节):0x10 → 0x11(= 17 > 块长 16)⇒ 填充非法。
|
||||
let mut a = bytes.clone();
|
||||
a[47] ^= 0x01;
|
||||
let corrupted_len_byte = hex::encode(&a);
|
||||
assert_ne!(corrupted_len_byte, good);
|
||||
assert_eq!(decrypt_to_md5(&corrupted_len_byte), None);
|
||||
|
||||
// ② 改填充块**中间某个填充字节**(第 2 个):0x10 → 0x0f。
|
||||
// 长度字节仍是 0x10(= 16,合法范围),但填充字节不自洽
|
||||
// ⇒ **严格模式**才拒;宽松模式会照单全收 ⇒ 这条断言正是「严格性」的证据。
|
||||
let mut b = bytes.clone();
|
||||
b[32 + 1] = 0x0f;
|
||||
let corrupted_fill_byte = hex::encode(&b);
|
||||
assert_ne!(corrupted_fill_byte, good);
|
||||
assert_eq!(decrypt_to_md5(&corrupted_fill_byte), None);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user