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

sha1: 2018 edition and digest crate upgrade #132

Merged
merged 1 commit into from
May 28, 2020
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
7 changes: 4 additions & 3 deletions sha1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ description = "SHA-1 hash function"
authors = ["RustCrypto Developers"]
license = "MIT OR Apache-2.0"
readme = "README.md"
edition = "2018"
documentation = "https://docs.rs/sha-1"
repository = "https://github.com/RustCrypto/hashes"
keywords = ["crypto", "sha1", "hash", "digest"]
Expand All @@ -14,15 +15,15 @@ categories = ["cryptography", "no-std"]
name = "sha1"

[dependencies]
digest = "0.8"
block-buffer = "0.7"
digest = { version = "0.9.0-pre", git = "https://github.com/RustCrypto/traits" }
block-buffer = { version = "0.7", git = "https://github.com/RustCrypto/utils" }
fake-simd = "0.1"
sha1-asm = { version = "0.4", optional = true }
opaque-debug = "0.2"
libc = { version = "0.2.68", optional = true }

[dev-dependencies]
digest = { version = "0.8", features = ["dev"] }
digest = { version = "0.9.0-pre", features = ["dev"], git = "https://github.com/RustCrypto/traits" }
hex-literal = "0.1"

[features]
Expand Down
4 changes: 1 addition & 3 deletions sha1/benches/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#![no_std]
#![feature(test)]
#[macro_use]
extern crate digest;
extern crate sha1;

use digest::bench;
bench!(sha1::Sha1);
4 changes: 1 addition & 3 deletions sha1/examples/sha1sum.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate sha1;

use sha1::{Digest, Sha1};
use std::env;
use std::fs;
Expand All @@ -25,7 +23,7 @@ fn process<D: Digest + Default, R: Read>(reader: &mut R, name: &str) {
Ok(n) => n,
Err(_) => return,
};
sh.input(&buffer[..n]);
sh.update(&buffer[..n]);
if n == 0 || n < BUFFER_SIZE {
break;
}
Expand Down
1 change: 1 addition & 0 deletions sha1/src/aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use libc::{getauxval, AT_HWCAP, HWCAP_SHA1};

#[inline(always)]
pub fn sha1_supported() -> bool {
#[allow(unsafe_code)]
let hwcaps: u64 = unsafe { getauxval(AT_HWCAP) };
(hwcaps & HWCAP_SHA1) != 0
}
18 changes: 11 additions & 7 deletions sha1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//! let mut hasher = Sha1::new();
//!
//! // process input message
//! hasher.input(b"hello world");
//! hasher.update(b"hello world");
//!
//! // acquire hash digest in the form of GenericArray,
//! // which in this case is equivalent to [u8; 20]
Expand All @@ -25,8 +25,11 @@
//!
//! [1]: https://en.wikipedia.org/wiki/SHA-1
//! [2]: https://github.com/RustCrypto/hashes

#![no_std]
#![doc(html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo_small.png")]
#![deny(unsafe_code)]
#![warn(missing_docs, rust_2018_idioms)]

// Give relevant error messages if the user tries to enable AArch64 asm on unsupported platforms.
#[cfg(all(
Expand All @@ -52,7 +55,6 @@ compile_error!("Enable the \"asm\" feature instead of \"asm-aarch64\" when build
))]
compile_error!("Enable the \"asm-aarch64\" feature on AArch64 if you want to use asm detected at runtime, or build with the crypto extensions support, for instance with RUSTFLAGS='-C target-cpu=native' on a compatible CPU.");

extern crate block_buffer;
#[macro_use]
extern crate opaque_debug;
#[macro_use]
Expand All @@ -69,6 +71,7 @@ extern crate sha1_asm;
#[cfg(all(feature = "asm", not(feature = "asm-aarch64")))]
#[inline(always)]
fn compress(state: &mut [u32; 5], block: &GenericArray<u8, U64>) {
#[allow(unsafe_code)]
let block: &[u8; 64] = unsafe { core::mem::transmute(block) };
sha1_asm::compress(state, block);
}
Expand All @@ -81,6 +84,7 @@ fn compress(state: &mut [u32; 5], block: &GenericArray<u8, U64>) {
// that macro is stabilised and https://github.com/rust-lang/rfcs/pull/2725 is implemented
// to let us use it on no_std.
if aarch64::sha1_supported() {
#[allow(unsafe_code)]
let block: &[u8; 64] = unsafe { core::mem::transmute(block) };
sha1_asm::compress(state, block);
} else {
Expand All @@ -91,17 +95,17 @@ fn compress(state: &mut [u32; 5], block: &GenericArray<u8, U64>) {
#[cfg(any(not(feature = "asm"), feature = "asm-aarch64"))]
mod utils;
#[cfg(not(feature = "asm"))]
use utils::compress;
use crate::utils::compress;

use block_buffer::byteorder::{ByteOrder, BE};
use block_buffer::BlockBuffer;
use digest::generic_array::typenum::{U20, U64};
use digest::generic_array::GenericArray;
pub use digest::Digest;
use digest::{BlockInput, FixedOutput, Input, Reset};
use digest::{BlockInput, FixedOutput, Reset, Update};

mod consts;
use consts::{H, STATE_LEN};
use crate::consts::{H, STATE_LEN};

/// Structure representing the state of a SHA-1 computation
#[derive(Clone)]
Expand All @@ -125,8 +129,8 @@ impl BlockInput for Sha1 {
type BlockSize = U64;
}

impl Input for Sha1 {
fn input<B: AsRef<[u8]>>(&mut self, input: B) {
impl Update for Sha1 {
fn update(&mut self, input: impl AsRef<[u8]>) {
let input = input.as_ref();
// Assumes that `length_bits<<3` will not overflow
self.len += input.len() as u64;
Expand Down
4 changes: 2 additions & 2 deletions sha1/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#![cfg_attr(feature = "cargo-clippy", allow(clippy::many_single_char_names))]

use crate::consts::{BLOCK_LEN, K0, K1, K2, K3};
use crate::simd::u32x4;
use block_buffer::byteorder::{ByteOrder, BE};
use consts::{BLOCK_LEN, K0, K1, K2, K3};
use digest::generic_array::typenum::U64;
use digest::generic_array::GenericArray;
use simd::u32x4;

type Block = GenericArray<u8, U64>;

Expand Down
2 changes: 1 addition & 1 deletion sha1/tests/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![no_std]
#[macro_use]
extern crate digest;
extern crate sha1;
use sha1;

use digest::dev::{digest_test, one_million_a};

Expand Down