Skip to content

Commit

Permalink
Provide output size constants for hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
ctz committed Nov 22, 2024
1 parent cffb342 commit 1528f11
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
12 changes: 6 additions & 6 deletions graviola/src/high/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use core::ops::{Deref, DerefMut};
#[derive(Clone, Debug)]
pub enum HashOutput {
/// Output from SHA256
Sha256([u8; 32]),
Sha256([u8; Sha256Context::OUTPUT_SZ]),
/// Output from SHA384
Sha384([u8; 48]),
Sha384([u8; Sha384Context::OUTPUT_SZ]),
/// Output from SHA512
Sha512([u8; 64]),
Sha512([u8; Sha512Context::OUTPUT_SZ]),
}

impl HashOutput {
Expand Down Expand Up @@ -160,7 +160,7 @@ impl Hash for Sha256 {
}

fn zeroed_output() -> HashOutput {
HashOutput::Sha256([0u8; 32])
HashOutput::Sha256([0u8; Sha256Context::OUTPUT_SZ])
}
}

Expand Down Expand Up @@ -198,7 +198,7 @@ impl Hash for Sha384 {
}

fn zeroed_output() -> HashOutput {
HashOutput::Sha384([0u8; 48])
HashOutput::Sha384([0u8; Sha384Context::OUTPUT_SZ])
}
}

Expand Down Expand Up @@ -235,7 +235,7 @@ impl Hash for Sha512 {
}

fn zeroed_output() -> HashOutput {
HashOutput::Sha512([0u8; 64])
HashOutput::Sha512([0u8; Sha512Context::OUTPUT_SZ])
}
}

Expand Down
25 changes: 17 additions & 8 deletions graviola/src/mid/sha2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::low::Blockwise;
#[derive(Clone)]
pub struct Sha256Context {
h: [u32; 8],
blockwise: Blockwise<64>,
blockwise: Blockwise<{ Sha256Context::BLOCK_SZ }>,
nblocks: usize,
}

Expand Down Expand Up @@ -48,7 +48,7 @@ impl Sha256Context {
}

/// Complete the SHA256 computation, returning the hash output.
pub fn finish(mut self) -> [u8; 32] {
pub fn finish(mut self) -> [u8; Self::OUTPUT_SZ] {
let bytes = self
.nblocks
.checked_mul(Self::BLOCK_SZ)
Expand All @@ -64,7 +64,7 @@ impl Sha256Context {
self.update(&(bits as u64).to_be_bytes());
debug_assert_eq!(self.blockwise.used(), 0);

let mut r = [0u8; 32];
let mut r = [0u8; Self::OUTPUT_SZ];
for (out, state) in r.chunks_exact_mut(4).zip(self.h.iter()) {
out.copy_from_slice(&state.to_be_bytes());
}
Expand All @@ -81,6 +81,9 @@ impl Sha256Context {

/// The internal block size of SHA256.
pub const BLOCK_SZ: usize = 64;

/// The output size of SHA256.
pub const OUTPUT_SZ: usize = 32;
}

/// A context for incremental computation of SHA384.
Expand Down Expand Up @@ -116,18 +119,21 @@ impl Sha384Context {
}

/// Complete the SHA384 computation, returning the hash output.
pub fn finish(self) -> [u8; 48] {
pub fn finish(self) -> [u8; Self::OUTPUT_SZ] {
let inner = self.inner.finish();
// SAFETY: 48 is less than 64.
inner[..48].try_into().unwrap()
inner[..Self::OUTPUT_SZ].try_into().unwrap()
}

/// The output size of SHA384.
pub const OUTPUT_SZ: usize = 48;
}

/// A context for incremental computation of SHA512.
#[derive(Clone)]
pub struct Sha512Context {
h: [u64; 8],
blockwise: Blockwise<128>,
blockwise: Blockwise<{ Sha512Context::BLOCK_SZ }>,
nblocks: usize,
}

Expand Down Expand Up @@ -169,7 +175,7 @@ impl Sha512Context {
}

/// Complete the SHA512 computation, returning the hash output.
pub fn finish(mut self) -> [u8; 64] {
pub fn finish(mut self) -> [u8; Self::OUTPUT_SZ] {
let bytes = self
.nblocks
.checked_mul(Self::BLOCK_SZ)
Expand All @@ -185,7 +191,7 @@ impl Sha512Context {
self.update(&bits.to_be_bytes());
debug_assert_eq!(self.blockwise.used(), 0);

let mut r = [0u8; 64];
let mut r = [0u8; Self::OUTPUT_SZ];
for (out, state) in r.chunks_exact_mut(8).zip(self.h.iter()) {
out.copy_from_slice(&state.to_be_bytes());
}
Expand All @@ -202,6 +208,9 @@ impl Sha512Context {

/// The internal block size of SHA512.
pub const BLOCK_SZ: usize = 128;

/// The output size of SHA512.
pub const OUTPUT_SZ: usize = 64;
}

static MD_PADDING: [u8; 128] = [
Expand Down
4 changes: 2 additions & 2 deletions rustls-graviola/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl hash::Hash for Sha256 {
}

fn output_len(&self) -> usize {
32
sha2::Sha256Context::OUTPUT_SZ
}
}

Expand Down Expand Up @@ -64,7 +64,7 @@ impl hash::Hash for Sha384 {
}

fn output_len(&self) -> usize {
48
sha2::Sha384Context::OUTPUT_SZ
}
}

Expand Down

0 comments on commit 1528f11

Please sign in to comment.