Skip to content

Commit 7de5b9d

Browse files
committed
rust: add default implementation for Mac::verify_mac
1 parent e965e0a commit 7de5b9d

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

rust/tink/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ prost = "*"
1515
rand = "*"
1616
sha-1 = "*"
1717
sha2 = "*"
18+
subtle = "*"
1819

1920
[build-dependencies]
2021
prost-build = "*"

rust/tink/src/mac.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,12 @@ pub trait Mac {
2121

2222
// Returns `()` if `mac` is a correct authentication code (MAC) for `data`,
2323
// otherwise it returns an error.
24-
fn verify_mac(&self, mac: &[u8], data: &[u8]) -> Result<(), crate::TinkError>;
24+
fn verify_mac(&self, mac: &[u8], data: &[u8]) -> Result<(), crate::TinkError> {
25+
let computed = self.compute_mac(data)?;
26+
if crate::subtle::constant_time_compare(mac, &computed) {
27+
Ok(())
28+
} else {
29+
Err("Invalid MAC".into())
30+
}
31+
}
2532
}

0 commit comments

Comments
 (0)