From 68a3cdff87d1043ea31930623901dab24766758a Mon Sep 17 00:00:00 2001 From: hyunggilwoo Date: Sun, 3 Mar 2024 21:29:28 -0800 Subject: [PATCH 1/5] `finalize()` internally uses `cshake` and handles errors with Err() instead of eprintln! What is next: `finalize()` should keep track of the state of a block and produce a single block. --- src/lib.rs | 5 +++++ src/ops.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index e792e09..0e6d21e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -226,6 +226,11 @@ pub trait Signable { fn verify(&mut self, pub_key: &ExtendedPoint) -> Result<(), OperationError>; } +pub trait UpdateFinalize { + fn update(&mut self, write_data: &[u8]); + fn finalize(self) -> Result, OperationError>; +} + const RATE_IN_BYTES: usize = 136; // SHA3-256 r = 1088 / 8 = 136 #[cfg(test)] diff --git a/src/ops.rs b/src/ops.rs index c29b1d6..721b3fc 100644 --- a/src/ops.rs +++ b/src/ops.rs @@ -19,7 +19,7 @@ use crate::{ }, AesEncryptable, BitLength, Capacity, Hashable, KeyEncryptable, KeyPair, Message, OperationError, OutputLength, Rate, SecParam, Signable, Signature, SpongeEncryptable, - RATE_IN_BYTES, + UpdateFinalize, RATE_IN_BYTES, }; use rayon::prelude::*; use tiny_ed448_goldilocks::curve::{extended_edwards::ExtendedPoint, field::scalar::Scalar}; @@ -883,10 +883,60 @@ impl AesEncryptable for Message { } } +impl UpdateFinalize for Message { + /// Returns nothing and simply appends the write data into self.data + /// + fn update(&mut self, write_data: &[u8]) { + self.msg.append(&mut write_data.to_owned()); + } + /// Internally, this calls cshake, then + /// passes self.data and returns the result + fn finalize(self) -> Result, OperationError> { + if let Some(d) = self.d { + let value = d as u64; + cshake(&self.msg, value, "", "", &d) + } else { + Err(OperationError::SecurityParameterNotSet) + } + } +} /// /// TESTS /// #[cfg(test)] +mod message_tests { + use crate::{Message, UpdateFinalize, SecParam::{D256}, ops::cshake}; + + #[test] + #[allow(non_snake_case)] + fn test_UpdateFinalize_initial_message() { + let mut m = Message::new("Initial data".as_bytes().to_vec()); + m.d = Some(D256); + m.update("More data".as_bytes()); + m.update("Even more data".as_bytes()); + + let expected_hash_result = cshake( + "Initial dataMore dataEven more data".as_bytes(), 256, "", "", &D256); + + assert_eq!(m.finalize(), expected_hash_result, + " The computed hash does not match the expected hash"); + } + #[test] + #[allow(non_snake_case)] + fn test_UpdateFinalize_empty_message() { + let mut m = Message::new("".as_bytes().to_vec()); + m.d = Some(D256); + m.update("foo".as_bytes()); + m.update("bar".as_bytes()); + m.update("baz".as_bytes()); + + let expected_hash_result = cshake( + "foobarbaz".as_bytes(), 256, "", "", &D256); + assert_eq!(m.finalize(), expected_hash_result, + " The computed hash does not match the expected hash"); + } +} +#[cfg(test)] mod cshake_tests { use crate::{ops::cshake, SecParam, NIST_DATA_SPONGE_INIT}; From 494915c74832b127aa6e0fa56e76b91cbaf698ec Mon Sep 17 00:00:00 2001 From: hyunggilwoo Date: Mon, 4 Mar 2024 16:53:08 -0800 Subject: [PATCH 2/5] Formatted the code. --- src/ops.rs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/ops.rs b/src/ops.rs index 721b3fc..9736083 100644 --- a/src/ops.rs +++ b/src/ops.rs @@ -905,7 +905,7 @@ impl UpdateFinalize for Message { /// #[cfg(test)] mod message_tests { - use crate::{Message, UpdateFinalize, SecParam::{D256}, ops::cshake}; + use crate::{ops::cshake, Message, SecParam::D256, UpdateFinalize}; #[test] #[allow(non_snake_case)] @@ -916,10 +916,18 @@ mod message_tests { m.update("Even more data".as_bytes()); let expected_hash_result = cshake( - "Initial dataMore dataEven more data".as_bytes(), 256, "", "", &D256); + "Initial dataMore dataEven more data".as_bytes(), + 256, + "", + "", + &D256, + ); - assert_eq!(m.finalize(), expected_hash_result, - " The computed hash does not match the expected hash"); + assert_eq!( + m.finalize(), + expected_hash_result, + " The computed hash does not match the expected hash" + ); } #[test] #[allow(non_snake_case)] @@ -930,10 +938,12 @@ mod message_tests { m.update("bar".as_bytes()); m.update("baz".as_bytes()); - let expected_hash_result = cshake( - "foobarbaz".as_bytes(), 256, "", "", &D256); - assert_eq!(m.finalize(), expected_hash_result, - " The computed hash does not match the expected hash"); + let expected_hash_result = cshake("foobarbaz".as_bytes(), 256, "", "", &D256); + assert_eq!( + m.finalize(), + expected_hash_result, + " The computed hash does not match the expected hash" + ); } } #[cfg(test)] From e07c3a091165f14bc4419fedf0c8acf2219606de Mon Sep 17 00:00:00 2001 From: hyunggilwoo Date: Mon, 4 Mar 2024 16:58:01 -0800 Subject: [PATCH 3/5] Removed a test case for an message that begins with an empty message. --- src/ops.rs | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/ops.rs b/src/ops.rs index 9736083..a58c75e 100644 --- a/src/ops.rs +++ b/src/ops.rs @@ -929,22 +929,6 @@ mod message_tests { " The computed hash does not match the expected hash" ); } - #[test] - #[allow(non_snake_case)] - fn test_UpdateFinalize_empty_message() { - let mut m = Message::new("".as_bytes().to_vec()); - m.d = Some(D256); - m.update("foo".as_bytes()); - m.update("bar".as_bytes()); - m.update("baz".as_bytes()); - - let expected_hash_result = cshake("foobarbaz".as_bytes(), 256, "", "", &D256); - assert_eq!( - m.finalize(), - expected_hash_result, - " The computed hash does not match the expected hash" - ); - } } #[cfg(test)] mod cshake_tests { From e3a9d1d46aa895b5a37fe2d9af4dcb4567f5f956 Mon Sep 17 00:00:00 2001 From: Dustin Ray <40841027+drcapybara@users.noreply.github.com> Date: Mon, 4 Mar 2024 17:54:32 -0800 Subject: [PATCH 4/5] Update ops.rs --- src/ops.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ops.rs b/src/ops.rs index a58c75e..ee161d9 100644 --- a/src/ops.rs +++ b/src/ops.rs @@ -885,7 +885,6 @@ impl AesEncryptable for Message { impl UpdateFinalize for Message { /// Returns nothing and simply appends the write data into self.data - /// fn update(&mut self, write_data: &[u8]) { self.msg.append(&mut write_data.to_owned()); } From 38b51290f0d98d2c6d3437d338c31d98932eca00 Mon Sep 17 00:00:00 2001 From: hyunggilwoo Date: Mon, 4 Mar 2024 19:40:45 -0800 Subject: [PATCH 5/5] finalize() 1st format. --- src/lib.rs | 2 +- src/ops.rs | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0e6d21e..9862d34 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -228,7 +228,7 @@ pub trait Signable { pub trait UpdateFinalize { fn update(&mut self, write_data: &[u8]); - fn finalize(self) -> Result, OperationError>; + fn finalize(self, output_length: &u64) -> Result>, OperationError>; } const RATE_IN_BYTES: usize = 136; // SHA3-256 r = 1088 / 8 = 136 diff --git a/src/ops.rs b/src/ops.rs index a58c75e..62ffdc7 100644 --- a/src/ops.rs +++ b/src/ops.rs @@ -889,12 +889,21 @@ impl UpdateFinalize for Message { fn update(&mut self, write_data: &[u8]) { self.msg.append(&mut write_data.to_owned()); } + /// Used in a sliding window /// Internally, this calls cshake, then /// passes self.data and returns the result - fn finalize(self) -> Result, OperationError> { + /// + fn finalize(self, output_length: &u64) -> Result>, OperationError> { if let Some(d) = self.d { - let value = d as u64; - cshake(&self.msg, value, "", "", &d) + match cshake(&self.msg, *output_length, "", "", &d) { + Ok(new_msg) => { + self.msg = Box::new(new_msg); + Ok(self.msg) + } + }, + Err(_) => { + Err(OperationError::CShakeError) + } } else { Err(OperationError::SecurityParameterNotSet) }