summaryrefslogtreecommitdiffstats
path: root/vendor/digest/src/dev
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /vendor/digest/src/dev
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/digest/src/dev')
-rw-r--r--vendor/digest/src/dev/fixed.rs65
-rw-r--r--vendor/digest/src/dev/mac.rs159
-rw-r--r--vendor/digest/src/dev/rng.rs38
-rw-r--r--vendor/digest/src/dev/variable.rs82
-rw-r--r--vendor/digest/src/dev/xof.rs51
5 files changed, 395 insertions, 0 deletions
diff --git a/vendor/digest/src/dev/fixed.rs b/vendor/digest/src/dev/fixed.rs
new file mode 100644
index 000000000..24f380112
--- /dev/null
+++ b/vendor/digest/src/dev/fixed.rs
@@ -0,0 +1,65 @@
+use crate::{Digest, FixedOutput, FixedOutputReset, HashMarker, Update};
+use core::fmt::Debug;
+
+/// Fixed-output resettable digest test via the `Digest` trait
+pub fn fixed_reset_test<D>(input: &[u8], output: &[u8]) -> Option<&'static str>
+where
+ D: FixedOutputReset + Debug + Clone + Default + Update + HashMarker,
+{
+ let mut hasher = D::new();
+ // Test that it works when accepting the message all at once
+ hasher.update(input);
+ let mut hasher2 = hasher.clone();
+ if hasher.finalize()[..] != output[..] {
+ return Some("whole message");
+ }
+
+ // Test if reset works correctly
+ hasher2.reset();
+ hasher2.update(input);
+ if hasher2.finalize_reset()[..] != output[..] {
+ return Some("whole message after reset");
+ }
+
+ // Test that it works when accepting the message in chunks
+ for n in 1..core::cmp::min(17, input.len()) {
+ let mut hasher = D::new();
+ for chunk in input.chunks(n) {
+ hasher.update(chunk);
+ hasher2.update(chunk);
+ }
+ if hasher.finalize()[..] != output[..] {
+ return Some("message in chunks");
+ }
+ if hasher2.finalize_reset()[..] != output[..] {
+ return Some("message in chunks");
+ }
+ }
+
+ None
+}
+
+/// Variable-output resettable digest test
+pub fn fixed_test<D>(input: &[u8], output: &[u8]) -> Option<&'static str>
+where
+ D: FixedOutput + Default + Debug + Clone,
+{
+ let mut hasher = D::default();
+ // Test that it works when accepting the message all at once
+ hasher.update(input);
+ if hasher.finalize_fixed()[..] != output[..] {
+ return Some("whole message");
+ }
+
+ // Test that it works when accepting the message in chunks
+ for n in 1..core::cmp::min(17, input.len()) {
+ let mut hasher = D::default();
+ for chunk in input.chunks(n) {
+ hasher.update(chunk);
+ }
+ if hasher.finalize_fixed()[..] != output[..] {
+ return Some("message in chunks");
+ }
+ }
+ None
+}
diff --git a/vendor/digest/src/dev/mac.rs b/vendor/digest/src/dev/mac.rs
new file mode 100644
index 000000000..0d4a37dfc
--- /dev/null
+++ b/vendor/digest/src/dev/mac.rs
@@ -0,0 +1,159 @@
+/// Define MAC test
+#[macro_export]
+#[cfg(feature = "mac")]
+#[cfg_attr(docsrs, doc(cfg(all(feature = "dev", feature = "mac"))))]
+macro_rules! new_mac_test {
+ ($name:ident, $test_name:expr, $mac:ty $(,)?) => {
+ digest::new_mac_test!($name, $test_name, $mac, "");
+ };
+ ($name:ident, $test_name:expr, $mac:ty, trunc_left $(,)?) => {
+ digest::new_mac_test!($name, $test_name, $mac, "left");
+ };
+ ($name:ident, $test_name:expr, $mac:ty, trunc_right $(,)?) => {
+ digest::new_mac_test!($name, $test_name, $mac, "right");
+ };
+ ($name:ident, $test_name:expr, $mac:ty, $trunc:expr $(,)?) => {
+ #[test]
+ fn $name() {
+ use core::cmp::min;
+ use digest::dev::blobby::Blob3Iterator;
+ use digest::Mac;
+
+ fn run_test(key: &[u8], input: &[u8], tag: &[u8]) -> Option<&'static str> {
+ let mac0 = <$mac as Mac>::new_from_slice(key).unwrap();
+
+ let mut mac = mac0.clone();
+ mac.update(input);
+ let result = mac.finalize().into_bytes();
+ let n = tag.len();
+ let result_bytes = match $trunc {
+ "left" => &result[..n],
+ "right" => &result[result.len() - n..],
+ _ => &result[..],
+ };
+ if result_bytes != tag {
+ return Some("whole message");
+ }
+
+ // test reading different chunk sizes
+ for chunk_size in 1..min(64, input.len()) {
+ let mut mac = mac0.clone();
+ for chunk in input.chunks(chunk_size) {
+ mac.update(chunk);
+ }
+ let res = match $trunc {
+ "left" => mac.verify_truncated_left(tag),
+ "right" => mac.verify_truncated_right(tag),
+ _ => mac.verify_slice(tag),
+ };
+ if res.is_err() {
+ return Some("chunked message");
+ }
+ }
+
+ None
+ }
+
+ let data = include_bytes!(concat!("data/", $test_name, ".blb"));
+
+ for (i, row) in Blob3Iterator::new(data).unwrap().enumerate() {
+ let [key, input, tag] = row.unwrap();
+ if let Some(desc) = run_test(key, input, tag) {
+ panic!(
+ "\n\
+ Failed test №{}: {}\n\
+ key:\t{:?}\n\
+ input:\t{:?}\n\
+ tag:\t{:?}\n",
+ i, desc, key, input, tag,
+ );
+ }
+ }
+ }
+ };
+}
+
+/// Define resettable MAC test
+#[macro_export]
+#[cfg(feature = "mac")]
+#[cfg_attr(docsrs, doc(cfg(all(feature = "dev", feature = "mac"))))]
+macro_rules! new_resettable_mac_test {
+ ($name:ident, $test_name:expr, $mac:ty $(,)?) => {
+ digest::new_resettable_mac_test!($name, $test_name, $mac, "");
+ };
+ ($name:ident, $test_name:expr, $mac:ty, trunc_left $(,)?) => {
+ digest::new_resettable_mac_test!($name, $test_name, $mac, "left");
+ };
+ ($name:ident, $test_name:expr, $mac:ty, trunc_right $(,)?) => {
+ digest::new_resettable_mac_test!($name, $test_name, $mac, "right");
+ };
+ ($name:ident, $test_name:expr, $mac:ty, $trunc:expr $(,)?) => {
+ #[test]
+ fn $name() {
+ use core::cmp::min;
+ use digest::dev::blobby::Blob3Iterator;
+ use digest::Mac;
+
+ fn run_test(key: &[u8], input: &[u8], tag: &[u8]) -> Option<&'static str> {
+ let mac0 = <$mac as Mac>::new_from_slice(key).unwrap();
+
+ let mut mac = mac0.clone();
+ mac.update(input);
+ let result = mac.finalize_reset().into_bytes();
+ let n = tag.len();
+ let result_bytes = match $trunc {
+ "left" => &result[..n],
+ "right" => &result[result.len() - n..],
+ _ => &result[..],
+ };
+ if result_bytes != tag {
+ return Some("whole message");
+ }
+
+ // test if reset worked correctly
+ mac.update(input);
+ let res = match $trunc {
+ "left" => mac.verify_truncated_left(tag),
+ "right" => mac.verify_truncated_right(tag),
+ _ => mac.verify_slice(tag),
+ };
+ if res.is_err() {
+ return Some("after reset");
+ }
+
+ // test reading different chunk sizes
+ for chunk_size in 1..min(64, input.len()) {
+ let mut mac = mac0.clone();
+ for chunk in input.chunks(chunk_size) {
+ mac.update(chunk);
+ }
+ let res = match $trunc {
+ "left" => mac.verify_truncated_left(tag),
+ "right" => mac.verify_truncated_right(tag),
+ _ => mac.verify_slice(tag),
+ };
+ if res.is_err() {
+ return Some("chunked message");
+ }
+ }
+ None
+ }
+
+ let data = include_bytes!(concat!("data/", $test_name, ".blb"));
+
+ for (i, row) in Blob3Iterator::new(data).unwrap().enumerate() {
+ let [key, input, tag] = row.unwrap();
+ if let Some(desc) = run_test(key, input, tag) {
+ panic!(
+ "\n\
+ Failed test №{}: {}\n\
+ key:\t{:?}\n\
+ input:\t{:?}\n\
+ tag:\t{:?}\n",
+ i, desc, key, input, tag,
+ );
+ }
+ }
+ }
+ };
+}
diff --git a/vendor/digest/src/dev/rng.rs b/vendor/digest/src/dev/rng.rs
new file mode 100644
index 000000000..8b233aafb
--- /dev/null
+++ b/vendor/digest/src/dev/rng.rs
@@ -0,0 +1,38 @@
+//! Xorshift RNG used for tests. Based on the `rand_xorshift` crate.
+use core::num::Wrapping;
+
+/// Initial RNG state used in tests.
+// choosen by fair dice roll. guaranteed to be random.
+pub(crate) const RNG: XorShiftRng = XorShiftRng {
+ x: Wrapping(0x0787_3B4A),
+ y: Wrapping(0xFAAB_8FFE),
+ z: Wrapping(0x1745_980F),
+ w: Wrapping(0xB0AD_B4F3),
+};
+
+/// Xorshift RNG instance/
+pub(crate) struct XorShiftRng {
+ x: Wrapping<u32>,
+ y: Wrapping<u32>,
+ z: Wrapping<u32>,
+ w: Wrapping<u32>,
+}
+
+impl XorShiftRng {
+ pub(crate) fn fill(&mut self, buf: &mut [u8; 1024]) {
+ for chunk in buf.chunks_exact_mut(4) {
+ chunk.copy_from_slice(&self.next_u32().to_le_bytes());
+ }
+ }
+
+ fn next_u32(&mut self) -> u32 {
+ let x = self.x;
+ let t = x ^ (x << 11);
+ self.x = self.y;
+ self.y = self.z;
+ self.z = self.w;
+ let w = self.w;
+ self.w = w ^ (w >> 19) ^ (t ^ (t >> 8));
+ self.w.0
+ }
+}
diff --git a/vendor/digest/src/dev/variable.rs b/vendor/digest/src/dev/variable.rs
new file mode 100644
index 000000000..ed8ff8828
--- /dev/null
+++ b/vendor/digest/src/dev/variable.rs
@@ -0,0 +1,82 @@
+use crate::{VariableOutput, VariableOutputReset};
+use core::fmt::Debug;
+
+/// Variable-output resettable digest test
+pub fn variable_reset_test<D>(input: &[u8], output: &[u8]) -> Option<&'static str>
+where
+ D: VariableOutputReset + Debug + Clone,
+{
+ let mut hasher = D::new(output.len()).unwrap();
+ let mut buf = [0u8; 128];
+ let buf = &mut buf[..output.len()];
+ // Test that it works when accepting the message all at once
+ hasher.update(input);
+ let mut hasher2 = hasher.clone();
+ hasher.finalize_variable(buf).unwrap();
+ if buf != output {
+ return Some("whole message");
+ }
+ buf.iter_mut().for_each(|b| *b = 0);
+
+ // Test if reset works correctly
+ hasher2.reset();
+ hasher2.update(input);
+ hasher2.finalize_variable_reset(buf).unwrap();
+ if buf != output {
+ return Some("whole message after reset");
+ }
+ buf.iter_mut().for_each(|b| *b = 0);
+
+ // Test that it works when accepting the message in chunks
+ for n in 1..core::cmp::min(17, input.len()) {
+ let mut hasher = D::new(output.len()).unwrap();
+ for chunk in input.chunks(n) {
+ hasher.update(chunk);
+ hasher2.update(chunk);
+ }
+ hasher.finalize_variable(buf).unwrap();
+ if buf != output {
+ return Some("message in chunks");
+ }
+ buf.iter_mut().for_each(|b| *b = 0);
+
+ hasher2.finalize_variable_reset(buf).unwrap();
+ if buf != output {
+ return Some("message in chunks");
+ }
+ buf.iter_mut().for_each(|b| *b = 0);
+ }
+
+ None
+}
+
+/// Variable-output resettable digest test
+pub fn variable_test<D>(input: &[u8], output: &[u8]) -> Option<&'static str>
+where
+ D: VariableOutput + Debug + Clone,
+{
+ let mut hasher = D::new(output.len()).unwrap();
+ let mut buf = [0u8; 128];
+ let buf = &mut buf[..output.len()];
+ // Test that it works when accepting the message all at once
+ hasher.update(input);
+ hasher.finalize_variable(buf).unwrap();
+ if buf != output {
+ return Some("whole message");
+ }
+ buf.iter_mut().for_each(|b| *b = 0);
+
+ // Test that it works when accepting the message in chunks
+ for n in 1..core::cmp::min(17, input.len()) {
+ let mut hasher = D::new(output.len()).unwrap();
+ for chunk in input.chunks(n) {
+ hasher.update(chunk);
+ }
+ hasher.finalize_variable(buf).unwrap();
+ if buf != output {
+ return Some("message in chunks");
+ }
+ buf.iter_mut().for_each(|b| *b = 0);
+ }
+ None
+}
diff --git a/vendor/digest/src/dev/xof.rs b/vendor/digest/src/dev/xof.rs
new file mode 100644
index 000000000..9e5d07a09
--- /dev/null
+++ b/vendor/digest/src/dev/xof.rs
@@ -0,0 +1,51 @@
+use crate::ExtendableOutputReset;
+use core::fmt::Debug;
+
+/// Resettable XOF test
+pub fn xof_reset_test<D>(input: &[u8], output: &[u8]) -> Option<&'static str>
+where
+ D: ExtendableOutputReset + Default + Debug + Clone,
+{
+ let mut hasher = D::default();
+ let mut buf = [0u8; 1024];
+ let buf = &mut buf[..output.len()];
+ // Test that it works when accepting the message all at once
+ hasher.update(input);
+ let mut hasher2 = hasher.clone();
+ hasher.finalize_xof_into(buf);
+ if buf != output {
+ return Some("whole message");
+ }
+ buf.iter_mut().for_each(|b| *b = 0);
+
+ // Test if reset works correctly
+ hasher2.reset();
+ hasher2.update(input);
+ hasher2.finalize_xof_reset_into(buf);
+ if buf != output {
+ return Some("whole message after reset");
+ }
+ buf.iter_mut().for_each(|b| *b = 0);
+
+ // Test that it works when accepting the message in chunks
+ for n in 1..core::cmp::min(17, input.len()) {
+ let mut hasher = D::default();
+ for chunk in input.chunks(n) {
+ hasher.update(chunk);
+ hasher2.update(chunk);
+ }
+ hasher.finalize_xof_into(buf);
+ if buf != output {
+ return Some("message in chunks");
+ }
+ buf.iter_mut().for_each(|b| *b = 0);
+
+ hasher2.finalize_xof_reset_into(buf);
+ if buf != output {
+ return Some("message in chunks");
+ }
+ buf.iter_mut().for_each(|b| *b = 0);
+ }
+
+ None
+}