script: Require HMAC comparison to be constant time#43773
Merged
Conversation
We should compare HMAC signatures when validating user-provided signatures in constant time, to prevent leaking timing information proportional to the number of matching bytes. The WebCrypto specification has also updated to require to use constant-time comparison in HMAC signatures. We update our implementation accordingly. Since we are still using the `aws-lc-rs` crate for our HMAC implementation, we use the function `verify_slices_are_equal` provided by `aws_lc_rs::constant_time` to guarantees the comparison is constant-time. Specification Update: w3c/webcrypto@c962bc7 Testing: Existing tests suffice. Signed-off-by: Kingsley Yung <kingsley@kkoyung.dev>
Taym95
reviewed
Mar 30, 2026
| let mac = hmac::sign(&sign_key, message); | ||
|
|
||
| // Step 2. Return true if mac is equal to signature and false otherwise. | ||
| Ok(mac.as_ref() == signature) |
Member
There was a problem hiding this comment.
we should cover this with WPT test
Member
There was a problem hiding this comment.
Hrm. Is it possible to test that an algorithm is run in constant time via a WPT test?
Member
Author
There was a problem hiding this comment.
I think it is pretty hard to do it as a WPT test. The time difference between constant-time comparison and naive comparison of less than a hundred bytes can be as small as a few nanosecond. Detecting the difference may need fine-tuned device and/or well-designed framework.
mrobinson
approved these changes
Mar 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We should compare HMAC signatures in constant time when validating user-provided signatures, to prevent leaking timing information proportional to the number of matching bytes. The WebCrypto specification has also updated to require to use constant-time comparison in HMAC signatures.
We update our implementation accordingly. Since we are still using the
aws-lc-rscrate for our HMAC implementation, we use the functionverify_slices_are_equalprovided byaws_lc_rs::constant_timeto guarantees the comparison is constant-time.Specification Update: w3c/webcrypto@c962bc7
Testing: Existing tests suffice.