Skip to content

Commit

Permalink
sha1: make compress consume blocks
Browse files Browse the repository at this point in the history
To align with the semantics of `block_buffer::BlockBuffer::digest_blocks`
signature which works with `&[Block]` and not `&[[u8; N]]`.
  • Loading branch information
baloo committed May 11, 2024
1 parent 470c789 commit 461de5b
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 14 deletions.
4 changes: 2 additions & 2 deletions sha1/src/compress.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::BLOCK_SIZE;
use crate::Block;

cfg_if::cfg_if! {
if #[cfg(feature = "force-soft")] {
Expand All @@ -22,6 +22,6 @@ cfg_if::cfg_if! {
}

/// SHA-1 compression function
pub fn compress(state: &mut [u32; 5], blocks: &[[u8; BLOCK_SIZE]]) {
pub fn compress(state: &mut [u32; 5], blocks: &[Block]) {
compress_inner(state, blocks);
}
4 changes: 3 additions & 1 deletion sha1/src/compress/aarch64.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
//! SHA-1 `aarch64` backend.

use crate::Block;

// Per rustc target feature docs for `aarch64-unknown-linux-gnu` and
// `aarch64-apple-darwin` platforms, the `sha2` target feature enables
// SHA-1 as well:
//
// > Enable SHA1 and SHA256 support.
cpufeatures::new!(sha1_hwcap, "sha2");

pub fn compress(state: &mut [u32; 5], blocks: &[[u8; 64]]) {
pub fn compress(state: &mut [u32; 5], blocks: &[Block]) {
// TODO: Replace with https://github.com/rust-lang/rfcs/pull/2725
// after stabilization
if sha1_hwcap::get() {
Expand Down
4 changes: 3 additions & 1 deletion sha1/src/compress/loongarch64_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use core::arch::asm;

use crate::Block;

const K: [u32; 4] = [0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6];

macro_rules! c {
Expand Down Expand Up @@ -102,7 +104,7 @@ macro_rules! roundtail {
};
}

pub fn compress(state: &mut [u32; 5], blocks: &[[u8; 64]]) {
pub fn compress(state: &mut [u32; 5], blocks: &[Block]) {
if blocks.is_empty() {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions sha1/src/compress/soft.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![allow(clippy::many_single_char_names)]
use super::BLOCK_SIZE;
use crate::{Block, BLOCK_SIZE};

const K: [u32; 4] = [0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6];

Expand Down Expand Up @@ -244,7 +244,7 @@ fn sha1_digest_block_u32(state: &mut [u32; 5], block: &[u32; 16]) {
state[4] = state[4].wrapping_add(e);
}

pub fn compress(state: &mut [u32; 5], blocks: &[[u8; BLOCK_SIZE]]) {
pub fn compress(state: &mut [u32; 5], blocks: &[Block]) {
let mut block_u32 = [0u32; BLOCK_SIZE / 4];
// since LLVM can't properly use aliasing yet it will make
// unnecessary state stores without this copy
Expand Down
6 changes: 4 additions & 2 deletions sha1/src/compress/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use core::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64::*;

use crate::Block;

macro_rules! rounds4 {
($h0:ident, $h1:ident, $wk:expr, $i:expr) => {
_mm_sha1rnds4_epu32($h0, _mm_sha1nexte_epu32($h1, $wk), $i)
Expand All @@ -31,7 +33,7 @@ macro_rules! schedule_rounds4 {
}

#[target_feature(enable = "sha,sse2,ssse3,sse4.1")]
unsafe fn digest_blocks(state: &mut [u32; 5], blocks: &[[u8; 64]]) {
unsafe fn digest_blocks(state: &mut [u32; 5], blocks: &[Block]) {
#[allow(non_snake_case)]
let MASK: __m128i = _mm_set_epi64x(0x0001_0203_0405_0607, 0x0809_0A0B_0C0D_0E0F);

Expand Down Expand Up @@ -91,7 +93,7 @@ unsafe fn digest_blocks(state: &mut [u32; 5], blocks: &[[u8; 64]]) {

cpufeatures::new!(shani_cpuid, "sha", "sse2", "ssse3", "sse4.1");

pub fn compress(state: &mut [u32; 5], blocks: &[[u8; 64]]) {
pub fn compress(state: &mut [u32; 5], blocks: &[Block]) {
// TODO: Replace with https://github.com/rust-lang/rfcs/pull/2725
// after stabilization
if shani_cpuid::get() {
Expand Down
12 changes: 6 additions & 6 deletions sha1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ pub use digest::{self, Digest};

use core::{convert::TryInto, fmt, slice::from_ref};
use digest::{
array::Array,
block_buffer::Eager,
core_api::{
AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, CoreWrapper, FixedOutputCore,
AlgorithmName, BlockSizeUser, Buffer, BufferKindUser, CoreWrapper, FixedOutputCore,
OutputSizeUser, Reset, UpdateCore,
},
crypto_common::hazmat::{DeserializeStateError, SerializableState, SerializedState},
Expand All @@ -34,6 +33,9 @@ pub use compress::compress;
const STATE_LEN: usize = 5;
const BLOCK_SIZE: usize = <Sha1Core as BlockSizeUser>::BlockSize::USIZE;

/// Block for SHA-1
pub type Block = digest::block_buffer::Block<Sha1Core>;

/// Core SHA-1 hasher state.
#[derive(Clone)]
pub struct Sha1Core {
Expand All @@ -60,9 +62,7 @@ impl OutputSizeUser for Sha1Core {

impl UpdateCore for Sha1Core {
#[inline]
fn update_blocks(&mut self, blocks: &[Block<Self>]) {
self.block_len += blocks.len() as u64;
let blocks = Array::cast_slice_to_core(blocks);
fn update_blocks(&mut self, blocks: &[Block]) {
compress(&mut self.h, blocks);
}
}
Expand All @@ -74,7 +74,7 @@ impl FixedOutputCore for Sha1Core {
let bit_len = 8 * (buffer.get_pos() as u64 + bs * self.block_len);

let mut h = self.h;
buffer.len64_padding_be(bit_len, |b| compress(&mut h, from_ref(&b.0)));
buffer.len64_padding_be(bit_len, |b| compress(&mut h, from_ref(b)));
for (chunk, v) in out.chunks_exact_mut(4).zip(h.iter()) {
chunk.copy_from_slice(&v.to_be_bytes());
}
Expand Down

0 comments on commit 461de5b

Please sign in to comment.