diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/constant_time_eq | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/constant_time_eq')
-rw-r--r-- | third_party/rust/constant_time_eq/.cargo-checksum.json | 1 | ||||
-rw-r--r-- | third_party/rust/constant_time_eq/Cargo.toml | 14 | ||||
-rw-r--r-- | third_party/rust/constant_time_eq/README | 3 | ||||
-rw-r--r-- | third_party/rust/constant_time_eq/benches/bench.rs | 29 | ||||
-rw-r--r-- | third_party/rust/constant_time_eq/src/lib.rs | 40 |
5 files changed, 87 insertions, 0 deletions
diff --git a/third_party/rust/constant_time_eq/.cargo-checksum.json b/third_party/rust/constant_time_eq/.cargo-checksum.json new file mode 100644 index 0000000000..697edd4fcc --- /dev/null +++ b/third_party/rust/constant_time_eq/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.toml":"2b23e4da7cb19c4d2e7a0816510558d369dc4f731ac83d5cb8fcce5534825213","README":"4f0deec2ec32eeabaa065fef2ddd7816a32550b8395da5c47fc458bd45143bea","benches/bench.rs":"ffc599703d114cc4943db322f433b0819787db0c1ff41d7be3efc5c0940e0001","src/lib.rs":"7abf980ba75361598e0250935c1de344c2d792ccccb4a3cf9243bbb9d3ad756e"},"package":"8ff012e225ce166d4422e0e78419d901719760f62ae2b7969ca6b564d1b54a9e"}
\ No newline at end of file diff --git a/third_party/rust/constant_time_eq/Cargo.toml b/third_party/rust/constant_time_eq/Cargo.toml new file mode 100644 index 0000000000..ae92925e9e --- /dev/null +++ b/third_party/rust/constant_time_eq/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "constant_time_eq" +version = "0.1.3" +authors = ["Cesar Eduardo Barros <cesarb@cesarb.eti.br>"] +description = "Compares two equal-sized byte strings in constant time." +documentation = "https://docs.rs/constant_time_eq" +repository = "https://github.com/cesarb/constant_time_eq" +readme = "README" +keywords = ["constant_time"] +categories = ["cryptography", "no-std"] +license = "CC0-1.0" + +[badges] +travis-ci = { repository = "cesarb/constant_time_eq" } diff --git a/third_party/rust/constant_time_eq/README b/third_party/rust/constant_time_eq/README new file mode 100644 index 0000000000..98cdaf04ca --- /dev/null +++ b/third_party/rust/constant_time_eq/README @@ -0,0 +1,3 @@ +Compares two equal-sized byte strings in constant time. + +Inspired by the Linux kernel's crypto_memneq. diff --git a/third_party/rust/constant_time_eq/benches/bench.rs b/third_party/rust/constant_time_eq/benches/bench.rs new file mode 100644 index 0000000000..4b3b790a5d --- /dev/null +++ b/third_party/rust/constant_time_eq/benches/bench.rs @@ -0,0 +1,29 @@ +#![feature(test)] + +extern crate constant_time_eq; +extern crate test; + +use constant_time_eq::constant_time_eq; +use test::{Bencher, black_box}; + +fn bench(b: &mut Bencher, left: &[u8], right: &[u8]) { + b.bytes = (left.len() + right.len()) as u64; + b.iter(|| { + constant_time_eq(black_box(left), black_box(right)) + }) +} + +#[bench] +fn bench_16(b: &mut Bencher) { + bench(b, &[0; 16], &[0; 16]) +} + +#[bench] +fn bench_4096(b: &mut Bencher) { + bench(b, &[0; 4096], &[0; 4096]) +} + +#[bench] +fn bench_65536(b: &mut Bencher) { + bench(b, &[0; 65536], &[0; 65536]) +} diff --git a/third_party/rust/constant_time_eq/src/lib.rs b/third_party/rust/constant_time_eq/src/lib.rs new file mode 100644 index 0000000000..27a2d27a1a --- /dev/null +++ b/third_party/rust/constant_time_eq/src/lib.rs @@ -0,0 +1,40 @@ +#![no_std] + +// This function is non-inline to prevent the optimizer from looking inside it. +#[inline(never)] +fn constant_time_ne(a: &[u8], b: &[u8]) -> u8 { + assert!(a.len() == b.len()); + + // These useless slices make the optimizer elide the bounds checks. + // See the comment in clone_from_slice() added on Rust commit 6a7bc47. + let len = a.len(); + let a = &a[..len]; + let b = &b[..len]; + + let mut tmp = 0; + for i in 0..len { + tmp |= a[i] ^ b[i]; + } + tmp // The compare with 0 must happen outside this function. +} + +/// Compares two equal-sized byte strings in constant time. +/// +/// # Examples +/// +/// ``` +/// use constant_time_eq::constant_time_eq; +/// +/// assert!(constant_time_eq(b"foo", b"foo")); +/// assert!(!constant_time_eq(b"foo", b"bar")); +/// assert!(!constant_time_eq(b"bar", b"baz")); +/// # assert!(constant_time_eq(b"", b"")); +/// +/// // Not equal-sized, so won't take constant time. +/// assert!(!constant_time_eq(b"foo", b"")); +/// assert!(!constant_time_eq(b"foo", b"quux")); +/// ``` +#[inline] +pub fn constant_time_eq(a: &[u8], b: &[u8]) -> bool { + a.len() == b.len() && constant_time_ne(a, b) == 0 +} |