Skip to content

Commit

Permalink
md5: minor code tweaks, bump to v0.10.6 (#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov authored Sep 22, 2023
1 parent a24e8b7 commit 026b0e8
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 22 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/md5.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ jobs:
- uses: RustCrypto/actions/cargo-hack-install@master
- run: cargo hack test --feature-powerset

# Build-only test of the LoongArch64 assembly backend
loongarch64_asm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: RustCrypto/actions/cargo-cache@master
- uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.72
targets: loongarch64-unknown-linux-gnu
- run: cargo build --target loongarch64-unknown-linux-gnu --features loongarch64_asm

minimal-versions:
uses: RustCrypto/actions/.github/workflows/minimal-versions.yml@master
with:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions md5/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.10.6 (2023-09-22)
### Added
- `asm!`-based backend for LoongArch64 targets gated behind `loongarch64_asm` feature [#505]

[#505]: https://github.com/RustCrypto/hashes/pull/505

## 0.10.5 (2022-09-22)
### Added
- Feature-gated OID support ([#413])
Expand Down
2 changes: 1 addition & 1 deletion md5/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "md-5"
version = "0.10.5"
version = "0.10.6"
description = "MD5 hash function"
authors = ["RustCrypto Developers"]
license = "MIT OR Apache-2.0"
Expand Down
18 changes: 7 additions & 11 deletions md5/src/compress/loongarch64_asm.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
//! LoongArch64 assembly backend
use core::arch::asm;

#[path = "consts.rs"]
mod consts;
use consts::*;

macro_rules! c {
($($l:expr)*) => {
concat!($($l ,)*)
Expand Down Expand Up @@ -74,7 +68,7 @@ pub fn compress(state: &mut [u32; 4], blocks: &[[u8; 64]]) {
}

unsafe {
asm!(
core::arch::asm!(
"42:",

"move $t0, $a4",
Expand All @@ -99,6 +93,7 @@ pub fn compress(state: &mut [u32; 4], blocks: &[[u8; 64]]) {
round0!("$t3", "$t0", "$t1", "$t2", 13, 12, 13),
round0!("$t2", "$t3", "$t0", "$t1", 14, 17, 14),
round0!("$t1", "$t2", "$t3", "$t0", 15, 22, 15),

round1!("$t0", "$t1", "$t2", "$t3", 1, 5, 16),
round1!("$t3", "$t0", "$t1", "$t2", 6, 9, 17),
round1!("$t2", "$t3", "$t0", "$t1", 11, 14, 18),
Expand All @@ -114,6 +109,7 @@ pub fn compress(state: &mut [u32; 4], blocks: &[[u8; 64]]) {
round1!("$t0", "$t1", "$t2", "$t3", 13, 5, 28),
round1!("$t3", "$t0", "$t1", "$t2", 2, 9, 29),
round1!("$t2", "$t3", "$t0", "$t1", 7, 14, 30),

round1!("$t1", "$t2", "$t3", "$t0", 12, 20, 31),
round2!("$t0", "$t1", "$t2", "$t3", 5, 4, 32),
round2!("$t3", "$t0", "$t1", "$t2", 8, 11, 33),
Expand All @@ -131,6 +127,7 @@ pub fn compress(state: &mut [u32; 4], blocks: &[[u8; 64]]) {
round2!("$t3", "$t0", "$t1", "$t2", 12, 11, 45),
round2!("$t2", "$t3", "$t0", "$t1", 15, 16, 46),
round2!("$t1", "$t2", "$t3", "$t0", 2, 23, 47),

round3!("$t0", "$t1", "$t2", "$t3", 0, 6, 48),
round3!("$t3", "$t0", "$t1", "$t2", 7, 10, 49),
round3!("$t2", "$t3", "$t0", "$t1", 14, 15, 50),
Expand Down Expand Up @@ -158,14 +155,13 @@ pub fn compress(state: &mut [u32; 4], blocks: &[[u8; 64]]) {
"addi.d $a2, $a2, -1",
"bnez $a2, 42b",

inout("$a1") blocks.as_ptr() => _,
inout("$a2") blocks.len() => _,
in("$a3") crate::consts::RC.as_ptr(),
inout("$a4") state[0],
inout("$a5") state[1],
inout("$a6") state[2],
inout("$a7") state[3],
inout("$a1") blocks.as_ptr() => _,
inout("$a2") blocks.len() => _,

in("$a3") RC.as_ptr(),

// Clobbers
out("$t0") _,
Expand Down
5 changes: 1 addition & 4 deletions md5/src/compress/soft.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#![allow(clippy::many_single_char_names, clippy::unreadable_literal)]
use crate::consts::RC;
use core::convert::TryInto;

#[path = "consts.rs"]
mod consts;
use consts::*;

#[inline(always)]
fn op_f(w: u32, x: u32, y: u32, z: u32, m: u32, c: u32, s: u32) -> u32 {
((x & y) | (!x & z))
Expand Down
6 changes: 5 additions & 1 deletion md5/src/compress/consts.rs → md5/src/consts.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
pub const RC: [u32; 64] = [
pub(crate) const STATE_INIT: [u32; 4] = [0x6745_2301, 0xEFCD_AB89, 0x98BA_DCFE, 0x1032_5476];

// TODO: remove `allow` on deprecation of `md5-asm`
#[allow(dead_code)]
pub(crate) static RC: [u32; 64] = [
// round 1
0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
Expand Down
11 changes: 7 additions & 4 deletions md5/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
pub use digest::{self, Digest};

mod compress;
use compress::compress;
pub(crate) mod consts;

use core::{fmt, slice::from_ref};
#[cfg(feature = "oid")]
Expand All @@ -47,6 +47,7 @@ use digest::{
typenum::{Unsigned, U16, U64},
HashMarker, Output,
};

/// Core MD5 hasher state.
#[derive(Clone)]
pub struct Md5Core {
Expand All @@ -72,7 +73,7 @@ impl UpdateCore for Md5Core {
#[inline]
fn update_blocks(&mut self, blocks: &[Block<Self>]) {
self.block_len = self.block_len.wrapping_add(blocks.len() as u64);
compress(&mut self.state, convert(blocks))
compress::compress(&mut self.state, convert(blocks))
}
}

Expand All @@ -85,7 +86,9 @@ impl FixedOutputCore for Md5Core {
.wrapping_add(buffer.get_pos() as u64)
.wrapping_mul(8);
let mut s = self.state;
buffer.len64_padding_le(bit_len, |b| compress(&mut s, convert(from_ref(b))));
buffer.len64_padding_le(bit_len, |b| {
compress::compress(&mut s, convert(from_ref(b)))
});
for (chunk, v) in out.chunks_exact_mut(4).zip(s.iter()) {
chunk.copy_from_slice(&v.to_le_bytes());
}
Expand All @@ -97,7 +100,7 @@ impl Default for Md5Core {
fn default() -> Self {
Self {
block_len: 0,
state: [0x6745_2301, 0xEFCD_AB89, 0x98BA_DCFE, 0x1032_5476],
state: consts::STATE_INIT,
}
}
}
Expand Down

0 comments on commit 026b0e8

Please sign in to comment.