Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/issue 18 #66

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ pub trait Signable {
fn verify(&mut self, pub_key: &ExtendedPoint) -> Result<(), OperationError>;
}

pub trait UpdateFinalize {
fn update(&mut self, data: &[u8]);
fn finalize(&mut self, output_length: u64) -> Result<(), OperationError>;
}
const RATE_IN_BYTES: usize = 136; // SHA3-256 r = 1088 / 8 = 136

#[cfg(test)]
Expand Down
49 changes: 48 additions & 1 deletion src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -883,6 +883,36 @@ impl AesEncryptable for Message {
}
}

impl UpdateFinalize for Message {
fn update(&mut self, data: &[u8]) {
self.msg.append(&mut data.to_vec());
}
/// # Finalizes the message by computing the keyed hash of the message.
/// ## Replaces:
/// ## Usage:
/// ```
/// use capycrypt::{Message, SecParam,UpdateFinalize};
/// let mut m = Message::new(vec![]);
/// m.d = &D256;
/// m.update("foo");
/// m.update("bar");
/// m.update("baz");
/// assert_eq!(m.finalize(m.len()), vec!["foobarbaz"].compute_hash_sha3(&SecParam::D256));
/// ```
fn finalize(&mut self, output_length: u64) -> Result<(), OperationError> {
match self.d {
Some(d) => {
// Compute the SHA3 hash of accumulated message
self.compute_hash_sha3(&d)?;

self.digest = cshake(&self.msg , output_length, "", "", &d);
Ok(())
},
None => Err(OperationError::UnsupportedSecurityParameter)
}

}
}
///
/// TESTS
///
Expand Down Expand Up @@ -1165,3 +1195,20 @@ mod shake_tests {
.unwrap_or(false));
}
}

#[cfg(test)]
mod update_finalize_tests {
use crate::{ops::UpdateFinalize, Hashable, Message, SecParam::D256};
#[test]
fn test_updated_message_256() {
let mut m = Message::new(vec![]);
m.d = Some(D256);
m.update(b"foo");
m.update(b"bar");
m.update(b"baz");

let mut expected = Message::new(b"foobarbaz".to_vec());
let output_length = m.msg.len() as u64 * 8;
assert_eq!(m.finalize(output_length), expected.compute_hash_sha3(&D256));
}
}