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

digest internals: Refactor format_output. #2191

Merged
merged 1 commit into from
Dec 18, 2024
Merged
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
9 changes: 1 addition & 8 deletions src/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl BlockContext {

Ok(Digest {
algorithm: self.algorithm,
value: (self.algorithm.format_output)(self.state),
value: self.state.format_output(),
})
}

Expand Down Expand Up @@ -348,8 +348,6 @@ pub struct Algorithm {
cpu_features: cpu::Features,
) -> (usize, &'d [u8]),

format_output: fn(input: DynState) -> Output,

initial_state: DynState,

id: AlgorithmID,
Expand Down Expand Up @@ -405,7 +403,6 @@ pub static SHA1_FOR_LEGACY_USE_ONLY: Algorithm = Algorithm {
chaining_len: sha1::CHAINING_LEN,
block_len: sha1::BLOCK_LEN,
block_data_order: dynstate::sha1_block_data_order,
format_output: dynstate::sha256_format_output,
initial_state: DynState::new32([
Wrapping(0x67452301u32),
Wrapping(0xefcdab89u32),
Expand All @@ -427,7 +424,6 @@ pub static SHA256: Algorithm = Algorithm {
chaining_len: SHA256_OUTPUT_LEN,
block_len: SHA256_BLOCK_LEN,
block_data_order: dynstate::sha256_block_data_order,
format_output: dynstate::sha256_format_output,
initial_state: DynState::new32([
Wrapping(0x6a09e667u32),
Wrapping(0xbb67ae85u32),
Expand All @@ -449,7 +445,6 @@ pub static SHA384: Algorithm = Algorithm {
chaining_len: SHA512_OUTPUT_LEN,
block_len: SHA512_BLOCK_LEN,
block_data_order: dynstate::sha512_block_data_order,
format_output: dynstate::sha512_format_output,
initial_state: DynState::new64([
Wrapping(0xcbbb9d5dc1059ed8),
Wrapping(0x629a292a367cd507),
Expand All @@ -471,7 +466,6 @@ pub static SHA512: Algorithm = Algorithm {
chaining_len: SHA512_OUTPUT_LEN,
block_len: SHA512_BLOCK_LEN,
block_data_order: dynstate::sha512_block_data_order,
format_output: dynstate::sha512_format_output,
initial_state: DynState::new64([
Wrapping(0x6a09e667f3bcc908),
Wrapping(0xbb67ae8584caa73b),
Expand All @@ -497,7 +491,6 @@ pub static SHA512_256: Algorithm = Algorithm {
chaining_len: SHA512_OUTPUT_LEN,
block_len: SHA512_BLOCK_LEN,
block_data_order: dynstate::sha512_block_data_order,
format_output: dynstate::sha512_format_output,
initial_state: DynState::new64([
Wrapping(0x22312194fc2bf72c),
Wrapping(0x9f555fa3c84c64c2),
Expand Down
31 changes: 11 additions & 20 deletions src/digest/dynstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ impl DynState {
pub const fn new64(initial_state: sha2::State64) -> Self {
Self::As64(initial_state)
}

pub fn format_output(self) -> Output {
match self {
Self::As64(state) => {
format_output::<_, _, { size_of::<u64>() }>(state, u64::to_be_bytes)
}
Self::As32(state) => {
format_output::<_, _, { size_of::<u32>() }>(state, u32::to_be_bytes)
}
}
}
}

pub(super) fn sha1_block_data_order<'d>(
Expand Down Expand Up @@ -85,23 +96,3 @@ pub(super) fn sha512_block_data_order<'d>(
sha2::block_data_order_64(state, full_blocks, cpu_features);
(full_blocks.len() * sha2::SHA512_BLOCK_LEN.into(), leftover)
}

pub(super) fn sha256_format_output(state: DynState) -> Output {
let state = match state {
DynState::As32(state) => state,
_ => {
unreachable!();
}
};
format_output::<_, _, { size_of::<u32>() }>(state, u32::to_be_bytes)
}

pub(super) fn sha512_format_output(state: DynState) -> Output {
let state = match state {
DynState::As64(state) => state,
_ => {
unreachable!();
}
};
format_output::<_, _, { size_of::<u64>() }>(state, u64::to_be_bytes)
}