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 9 #56

Merged
merged 10 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/target
/.vscode

.DS_Store
drcapybara marked this conversation as resolved.
Show resolved Hide resolved
39 changes: 37 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
/// Elliptic curve backend
use tiny_ed448_goldilocks::curve::{extended_edwards::ExtendedPoint, field::scalar::Scalar};

/// Module for sha3 primitives.
/// Module for SHA-3 primitives
pub mod sha3 {

// Submodule that implements NIST 800-185 compliant functions
pub mod aux_functions;

// Submodule that implements the Keccak-f[1600] permutation
pub mod keccakf;

// Submodule that implements the sponge construction
pub mod sponge;
}

Expand Down Expand Up @@ -61,18 +67,26 @@ pub struct KeyPair {
}

#[derive(Debug)]
/// Message type for which cryptographic traits are defined.
/// Message struct for which cryptographic traits are defined.
pub struct Message {
// Input message
drcapybara marked this conversation as resolved.
Show resolved Hide resolved
pub msg: Box<Vec<u8>>,
// The digest lengths in FIPS-approved hash functions
pub d: Option<SecParam>,
// Nonce used in symmetric encryption
pub sym_nonce: Option<Vec<u8>>,
// Nonce used in asymmetric encryption
pub asym_nonce: Option<ExtendedPoint>,
// Hash value (also known as message digest)
pub digest: Result<Vec<u8>, OperationError>,
// Result of the cryptographic trait
pub op_result: Result<(), OperationError>,
// Schnorr signatures on the input message
pub sig: Option<Signature>,
}

impl Message {
// Returns a new Message instance
pub fn new(data: Vec<u8>) -> Message {
Message {
msg: Box::new(data),
Expand All @@ -92,10 +106,15 @@ impl Message {
// }
// }
#[derive(Debug, Clone, Copy)]
/// An enum representing standard digest lengths based on FIPS PUB 202
pub enum SecParam {
/// Digest length of 224 bits, also known as SHA3-224
D224 = 224,
/// Digest length of 256 bits, also known as SHA3-256
D256 = 256,
/// Digest length of 384 bits, also known as SHA3-384
D384 = 384,
/// Digest length of 512 bits, also known as SHA3-512
D512 = 512,
}

Expand All @@ -117,14 +136,21 @@ impl SecParam {
}

#[derive(Debug, Clone, Copy)]
/// An enum representing standard capacity valuess based on FIPS PUB 202.
/// (The capacity of a sponge function) = 2 * (digest length)
pub(crate) enum Capacity {
// 2 * SecParam.D224
drcapybara marked this conversation as resolved.
Show resolved Hide resolved
C448 = 448,
// 2 * SecParam.D256
C512 = 512,
// 2 * SecParam.D384
C768 = 768,
// 2 * SecParam.D512
C1024 = 1024,
}

impl Capacity {
/// This function effectively maps a given bit length to the appropriate capacity value enum variant,
fn from_bit_length(bit_length: u64) -> Self {
match bit_length * 2 {
x if x <= 448 => Capacity::C448,
Expand All @@ -135,6 +161,7 @@ impl Capacity {
}
}

/// OutputLength struct for storing the output length.
pub struct OutputLength {
value: u64,
}
Expand All @@ -155,11 +182,14 @@ impl OutputLength {
}
}

/// Rate struct for storing the rate value.
/// Rate is the number of input bits processed per invocation of the underlying function in sponge construction.
pub struct Rate {
value: u64,
}

impl Rate {
// Rate = (Permutation width) - (Capacity)
drcapybara marked this conversation as resolved.
Show resolved Hide resolved
pub fn from<R: BitLength + ?Sized>(sec_param: &R) -> Self {
Rate {
value: (1600 - sec_param.bit_length()),
Expand Down Expand Up @@ -207,6 +237,11 @@ pub trait BitLength {
}

pub trait Hashable {
/// # Message Digest
/// Computes SHA3-d hash of input.
/// ## Arguments:
/// * `d: u64`: requested security strength in bits. Supported
/// bitstrengths are 224, 256, 384, or 512.
drcapybara marked this conversation as resolved.
Show resolved Hide resolved
fn compute_hash_sha3(&mut self, d: &SecParam) -> Result<(), OperationError>;
fn compute_tagged_hash(&mut self, pw: &[u8], s: &str, d: &SecParam);
}
Expand Down