Skip to content

Commit e4f1350

Browse files
committed
security:implemented const. time hash comparison to prevent timing side channel attack
1 parent 0b2ca10 commit e4f1350

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

src/provenance.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,13 @@ pub fn provenance_hash(data: &[u8]) -> [u8; 32] {
3232
pub fn tamper(_data: &[u8]) -> &'static [u8] {
3333
b"TAMPERED: Journalist report from Jharkhand"
3434
}
35+
36+
/// Constant-time byte comparison — never short-circuits
37+
/// Prevents timing side-channel attacks on hash comparison
38+
pub fn constant_time_eq(a: &[u8; 32], b: &[u8; 32]) -> bool {
39+
let mut result = 0u8;
40+
for i in 0..32 {
41+
result |= a[i] ^ b[i];
42+
}
43+
result == 0
44+
}

src/vfs.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ impl FileNode {
3838
}
3939

4040
pub fn verify(&self) -> bool {
41-
provenance_hash(&self.data) == self.provenance_hash
41+
crate::provenance::constant_time_eq(
42+
&provenance_hash(&self.data),
43+
&self.provenance_hash
44+
)
4245
}
4346
}
4447

0 commit comments

Comments
 (0)