ferritls_core/ct.rs
1//! 常数时间工具与边界内标准 crate 的统一出口。
2//!
3//! 边界内所有“秘密影响控制流或内存访问”的操作必须经由本模块(或直接使用
4//! [`subtle`]),禁止手写可能被优化器破坏的尝试(如 `if a == b`、逐字节短路
5//! 比较)。见 AGENTS.md“安全注意事项”。
6
7pub use subtle;
8pub use zeroize;
9
10use subtle::ConstantTimeEq;
11
12/// 常数时间字节串相等比较。长度不同直接返回 `false`(长度本身不是秘密)。
13pub fn ct_eq(a: &[u8], b: &[u8]) -> bool {
14 a.ct_eq(b).into()
15}
16
17/// 常数时间 MAC/标签验证:相等返回 `Ok(())`,否则 [`crate::Error::VerificationFailed`]。
18pub fn verify_tag(computed: &[u8], received: &[u8]) -> Result<(), crate::Error> {
19 if ct_eq(computed, received) {
20 Ok(())
21 } else {
22 Err(crate::Error::VerificationFailed)
23 }
24}