From c43d34c12755b2e380d73c8e84b018abe8bc0b15 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Wed, 27 May 2020 20:12:05 -0700 Subject: [PATCH] sha1: 2018 edition and `digest` crate upgrade Upgrades to Rust 2018 edition and the (now 2018 edition) `digest` v0.9.0-pre crate. --- sha1/Cargo.toml | 7 ++++--- sha1/benches/lib.rs | 4 +--- sha1/examples/sha1sum.rs | 4 +--- sha1/src/aarch64.rs | 1 + sha1/src/lib.rs | 18 +++++++++++------- sha1/src/utils.rs | 4 ++-- sha1/tests/lib.rs | 2 +- 7 files changed, 21 insertions(+), 19 deletions(-) diff --git a/sha1/Cargo.toml b/sha1/Cargo.toml index b85c499c1..e27dec5a3 100644 --- a/sha1/Cargo.toml +++ b/sha1/Cargo.toml @@ -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"] @@ -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] diff --git a/sha1/benches/lib.rs b/sha1/benches/lib.rs index e204e565a..718ec6378 100644 --- a/sha1/benches/lib.rs +++ b/sha1/benches/lib.rs @@ -1,7 +1,5 @@ #![no_std] #![feature(test)] -#[macro_use] -extern crate digest; -extern crate sha1; +use digest::bench; bench!(sha1::Sha1); diff --git a/sha1/examples/sha1sum.rs b/sha1/examples/sha1sum.rs index 595f0ddc3..cbd68e96d 100644 --- a/sha1/examples/sha1sum.rs +++ b/sha1/examples/sha1sum.rs @@ -1,5 +1,3 @@ -extern crate sha1; - use sha1::{Digest, Sha1}; use std::env; use std::fs; @@ -25,7 +23,7 @@ fn process(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; } diff --git a/sha1/src/aarch64.rs b/sha1/src/aarch64.rs index 16907404c..8d1a916cc 100644 --- a/sha1/src/aarch64.rs +++ b/sha1/src/aarch64.rs @@ -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 } diff --git a/sha1/src/lib.rs b/sha1/src/lib.rs index 5baacf56c..2f05823e9 100644 --- a/sha1/src/lib.rs +++ b/sha1/src/lib.rs @@ -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] @@ -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( @@ -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] @@ -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) { + #[allow(unsafe_code)] let block: &[u8; 64] = unsafe { core::mem::transmute(block) }; sha1_asm::compress(state, block); } @@ -81,6 +84,7 @@ fn compress(state: &mut [u32; 5], block: &GenericArray) { // 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 { @@ -91,17 +95,17 @@ fn compress(state: &mut [u32; 5], block: &GenericArray) { #[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)] @@ -125,8 +129,8 @@ impl BlockInput for Sha1 { type BlockSize = U64; } -impl Input for Sha1 { - fn input>(&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; diff --git a/sha1/src/utils.rs b/sha1/src/utils.rs index b95b9c04c..8a3588225 100644 --- a/sha1/src/utils.rs +++ b/sha1/src/utils.rs @@ -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; diff --git a/sha1/tests/lib.rs b/sha1/tests/lib.rs index 542bd9ca3..b9b77734f 100644 --- a/sha1/tests/lib.rs +++ b/sha1/tests/lib.rs @@ -1,7 +1,7 @@ #![no_std] #[macro_use] extern crate digest; -extern crate sha1; +use sha1; use digest::dev::{digest_test, one_million_a};