From 86afa4465250bda33a8c523ba6ca3463acf91ebb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Thu, 10 Feb 2022 10:28:18 +0300 Subject: [PATCH] Reduce amount of unsafe and use new imports --- .github/actions/cross-tests/action.yml | 11 +++++----- .github/workflows/sha1.yml | 3 +-- .github/workflows/sha2.yml | 3 +-- .github/workflows/sha3.yml | 3 +-- Cargo.lock | 17 +++++++-------- blake2/Cargo.toml | 4 ++-- blake2/src/lib.rs | 6 ++---- fsb/Cargo.toml | 4 ++-- fsb/src/lib.rs | 3 ++- fsb/src/macros.rs | 20 +++++------------- gost94/Cargo.toml | 4 ++-- gost94/src/gost94_core.rs | 10 ++++----- gost94/src/lib.rs | 1 + groestl/Cargo.toml | 4 ++-- groestl/src/compress1024.rs | 3 +-- groestl/src/compress512.rs | 3 +-- groestl/src/lib.rs | 16 ++++++++------ md2/src/lib.rs | 2 +- md4/Cargo.toml | 4 ++-- md4/src/lib.rs | 4 ++-- md5/Cargo.toml | 4 ++-- md5/src/lib.rs | 2 +- ripemd/Cargo.toml | 4 ++-- ripemd/src/c160.rs | 3 +-- ripemd/src/c256.rs | 3 +-- ripemd/src/c320.rs | 3 +-- ripemd/src/lib.rs | 8 +++---- sha1/Cargo.toml | 4 ++-- sha1/src/compress.rs | 3 +-- sha1/src/compress/x86.rs | 1 - sha1/src/lib.rs | 2 +- sha2/Cargo.toml | 4 ++-- sha2/src/core_api.rs | 2 +- sha2/src/sha256.rs | 2 +- sha2/src/sha512.rs | 2 +- shabal/Cargo.toml | 2 +- shabal/src/lib.rs | 2 +- sm3/Cargo.toml | 4 ++-- sm3/src/lib.rs | 4 ++-- streebog/Cargo.toml | 4 ++-- streebog/src/core_api.rs | 7 +++---- streebog/src/lib.rs | 1 + tiger/Cargo.toml | 4 ++-- tiger/src/compress.rs | 29 ++++++++++++-------------- tiger/src/lib.rs | 18 +++++++++------- whirlpool/Cargo.toml | 4 ++-- whirlpool/src/lib.rs | 6 +++--- 47 files changed, 120 insertions(+), 137 deletions(-) diff --git a/.github/actions/cross-tests/action.yml b/.github/actions/cross-tests/action.yml index 0f81b040..cc63225d 100644 --- a/.github/actions/cross-tests/action.yml +++ b/.github/actions/cross-tests/action.yml @@ -22,14 +22,15 @@ runs: override: true - name: Install precompiled cross run: | - export URL=$(curl -s https://api.github.com/repos/rust-embedded/cross/releases/latest | \ + export URL=$(curl -s https://api.github.com/repos/cross-rs/cross/releases/latest | \ jq -r '.assets[] | select(.name | contains("x86_64-unknown-linux-gnu.tar.gz")) | .browser_download_url') wget -O /tmp/binaries.tar.gz $URL tar -C /tmp -xzf /tmp/binaries.tar.gz mv /tmp/cross ~/.cargo/bin shell: bash - - run: | - cd ${{ inputs.package }} - cross test --target ${{ inputs.target }} --no-default-features \ - --features ${{ inputs.features }} + - run: cross test + --package ${{ inputs.package }} + --target ${{ inputs.target }} + --no-default-features + --features ${{ inputs.features }} shell: bash diff --git a/.github/workflows/sha1.yml b/.github/workflows/sha1.yml index 6efb8e4e..e2852cf0 100644 --- a/.github/workflows/sha1.yml +++ b/.github/workflows/sha1.yml @@ -137,11 +137,10 @@ jobs: # Cross-compiled tests cross: - needs: set-msrv strategy: matrix: rust: - - ${{needs.set-msrv.outputs.msrv}} + - 1.51 # 1.41-1.50 `--features` can't be used inside virtual manifest - stable target: - aarch64-unknown-linux-gnu diff --git a/.github/workflows/sha2.yml b/.github/workflows/sha2.yml index b8b78796..5e727dcd 100644 --- a/.github/workflows/sha2.yml +++ b/.github/workflows/sha2.yml @@ -134,11 +134,10 @@ jobs: # Cross-compiled tests cross: - needs: set-msrv strategy: matrix: rust: - - ${{needs.set-msrv.outputs.msrv}} + - 1.51 # 1.41-1.50 `--features` can't be used inside virtual manifest - stable target: - aarch64-unknown-linux-gnu diff --git a/.github/workflows/sha3.yml b/.github/workflows/sha3.yml index b90b5d95..ce17381c 100644 --- a/.github/workflows/sha3.yml +++ b/.github/workflows/sha3.yml @@ -71,11 +71,10 @@ jobs: # Cross-compiled tests cross: - needs: set-msrv strategy: matrix: rust: - - ${{needs.set-msrv.outputs.msrv}} + - 1.51 # 1.41-1.50 `--features` can't be used inside virtual manifest - stable target: - aarch64-unknown-linux-gnu diff --git a/Cargo.lock b/Cargo.lock index 67e37fea..cad8d044 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -18,9 +18,9 @@ checksum = "847495c209977a90e8aad588b959d0ca9f5dc228096d29a6bd3defd53f35eaec" [[package]] name = "block-buffer" -version = "0.10.0" +version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1d36a02058e76b040de25a4464ba1c80935655595b661505c8b39b664828b95" +checksum = "0bf7fe51849ea569fd452f37822f606a5cabb684dc918707a0193fd4664ff324" dependencies = [ "generic-array", ] @@ -48,23 +48,22 @@ dependencies = [ [[package]] name = "crypto-common" -version = "0.1.1" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "683d6b536309245c849479fba3da410962a43ed8e51c26b729208ec0ac2798d0" +checksum = "a4600d695eb3f6ce1cd44e6e291adceb2cc3ab12f20a33777ecd0bf6eba34e06" dependencies = [ "generic-array", ] [[package]] name = "digest" -version = "0.10.1" +version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b697d66081d42af4fba142d56918a3cb21dc8eb63372c6b85d14f44fb9c5979b" +checksum = "8cb780dce4f9a8f5c087362b3a4595936b2019e7c8b30f2c3e9a7e94e6ae9837" dependencies = [ "blobby", "block-buffer", "crypto-common", - "generic-array", "subtle", ] @@ -138,9 +137,9 @@ checksum = "67c21572b4949434e4fc1e1978b99c5f77064153c59d998bf13ecd96fb5ecba7" [[package]] name = "libc" -version = "0.2.112" +version = "0.2.117" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125" +checksum = "e74d72e0f9b65b5b4ca49a346af3976df0f9c61d550727f349ecd559f251a26c" [[package]] name = "md-5" diff --git a/blake2/Cargo.toml b/blake2/Cargo.toml index a49b7a54..d3ca3c28 100644 --- a/blake2/Cargo.toml +++ b/blake2/Cargo.toml @@ -12,10 +12,10 @@ keywords = ["crypto", "blake2", "hash", "digest"] categories = ["cryptography", "no-std"] [dependencies] -digest = { version = "0.10", features = ["mac"] } +digest = { version = "0.10.2", features = ["mac"] } [dev-dependencies] -digest = { version = "0.10", features = ["dev"] } +digest = { version = "0.10.2", features = ["dev"] } hex-literal = "0.2" [features] diff --git a/blake2/src/lib.rs b/blake2/src/lib.rs index e92bc5ce..feebeb2a 100644 --- a/blake2/src/lib.rs +++ b/blake2/src/lib.rs @@ -94,10 +94,8 @@ use digest::{ VariableOutputCore, }, crypto_common::{InvalidLength, Key, KeyInit, KeySizeUser}, - generic_array::{ - typenum::{IsLessOrEqual, LeEq, NonZero, Unsigned}, - ArrayLength, GenericArray, - }, + generic_array::{ArrayLength, GenericArray}, + typenum::{IsLessOrEqual, LeEq, NonZero, Unsigned}, FixedOutput, HashMarker, InvalidOutputSize, MacMarker, Output, Update, }; #[cfg(feature = "reset")] diff --git a/fsb/Cargo.toml b/fsb/Cargo.toml index 96ffe604..2f29bdea 100644 --- a/fsb/Cargo.toml +++ b/fsb/Cargo.toml @@ -11,11 +11,11 @@ keywords = ["crypto", "fsb", "hash", "digest"] categories = ["cryptography", "no-std"] [dependencies] -digest = "0.10" +digest = "0.10.2" whirlpool = { version = "0.10", path = "../whirlpool", default-features = false } [dev-dependencies] -digest = { version = "0.10", features = ["dev"] } +digest = { version = "0.10.2", features = ["dev"] } hex-literal = "0.2" [features] diff --git a/fsb/src/lib.rs b/fsb/src/lib.rs index 619f2df0..292c32e6 100644 --- a/fsb/src/lib.rs +++ b/fsb/src/lib.rs @@ -43,6 +43,7 @@ html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg", html_root_url = "https://docs.rs/fsb/0.1.0" )] +#![forbid(unsafe_code)] #![warn(missing_docs, rust_2018_idioms)] #![allow(non_snake_case)] @@ -61,7 +62,7 @@ use digest::{ AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, CoreWrapper, FixedOutputCore, OutputSizeUser, Reset, UpdateCore, }, - generic_array::{typenum::Unsigned, GenericArray}, + typenum::Unsigned, HashMarker, Output, }; diff --git a/fsb/src/macros.rs b/fsb/src/macros.rs index 620cb6d2..8b1e31a8 100644 --- a/fsb/src/macros.rs +++ b/fsb/src/macros.rs @@ -31,7 +31,7 @@ macro_rules! fsb_impl { fn update_blocks(&mut self, blocks: &[Block]) { self.blocks_len += blocks.len() as u64; for block in blocks { - Self::compress(&mut self.state, Self::convert(block)); + Self::compress(&mut self.state, block); } } } @@ -42,7 +42,7 @@ macro_rules! fsb_impl { let block_bytes = self.blocks_len * Self::BlockSize::U64; let bit_len = 8 * (block_bytes + buffer.get_pos() as u64); let mut h = self.state; - buffer.len64_padding_be(bit_len, |b| Self::compress(&mut h, Self::convert(b))); + buffer.len64_padding_be(bit_len, |b| Self::compress(&mut h, b)); let res = whirlpool::Whirlpool::digest(&h[..]); let n = out.len(); @@ -111,7 +111,7 @@ macro_rules! fsb_impl { /// $W_i = i \times (n / w) + IV_i + M_i \times 2^{r / w}. fn computing_w_indices( input_vector: &[u8; Self::SIZE_OUTPUT_COMPRESS], - message: &[u8; Self::SIZE_MSG_CHUNKS], + message: &Block, ) -> [u32; $w] { let mut wind: [u32; $w] = [0; $w]; let divided_message: [u8; $w] = Self::dividing_bits(message, ($s - $r) / $w); @@ -129,10 +129,7 @@ macro_rules! fsb_impl { /// This function servers the purpose presented in table 3, of breaking a bit array into /// batches of size not multiple of 8. Note that the IV will be broken always in size 8, which /// is quite convenient. Also, the only numbers we'll have to worry for are 5 and 6. - fn dividing_bits( - input_bits: &[u8; Self::SIZE_MSG_CHUNKS], - size_batches: usize, - ) -> [u8; $w] { + fn dividing_bits(input_bits: &Block, size_batches: usize) -> [u8; $w] { if size_batches != 5usize && size_batches != 6usize { panic!( "Expecting batches of size 5 or 6. Other values do not follow \ @@ -164,10 +161,7 @@ macro_rules! fsb_impl { } /// This function outputs r bits, which are used to chain to the next iteration. - fn compress( - hash: &mut [u8; Self::SIZE_OUTPUT_COMPRESS], - message_block: &[u8; Self::SIZE_MSG_CHUNKS], - ) { + fn compress(hash: &mut [u8; Self::SIZE_OUTPUT_COMPRESS], message_block: &Block) { let mut initial_vector = [0u8; Self::SIZE_OUTPUT_COMPRESS]; let w_indices = Self::computing_w_indices(hash, message_block); @@ -284,10 +278,6 @@ macro_rules! fsb_impl { } truncated } - - fn convert(block: &GenericArray) -> &[u8; Self::SIZE_MSG_CHUNKS] { - unsafe { &*(block.as_ptr() as *const [u8; Self::SIZE_MSG_CHUNKS]) } - } } }; } diff --git a/gost94/Cargo.toml b/gost94/Cargo.toml index 057e92a0..839478cc 100644 --- a/gost94/Cargo.toml +++ b/gost94/Cargo.toml @@ -12,10 +12,10 @@ keywords = ["crypto", "gost94", "gost", "hash", "digest"] categories = ["cryptography", "no-std"] [dependencies] -digest = "0.10" +digest = "0.10.2" [dev-dependencies] -digest = { version = "0.10", features = ["dev"] } +digest = { version = "0.10.2", features = ["dev"] } hex-literal = "0.2" [features] diff --git a/gost94/src/gost94_core.rs b/gost94/src/gost94_core.rs index 569789a0..825abf61 100644 --- a/gost94/src/gost94_core.rs +++ b/gost94/src/gost94_core.rs @@ -2,12 +2,11 @@ use core::{convert::TryInto, fmt}; use digest::{ block_buffer::Eager, - consts::U32, core_api::{ AlgorithmName, Block as TBlock, BlockSizeUser, Buffer, BufferKindUser, FixedOutputCore, OutputSizeUser, Reset, UpdateCore, }, - generic_array::{typenum::Unsigned, GenericArray}, + typenum::{Unsigned, U32}, HashMarker, Output, }; @@ -191,8 +190,7 @@ impl Gost94Core

{ } #[inline(always)] - fn compress(&mut self, block: &GenericArray) { - let block = unsafe { &*(block.as_ptr() as *const [u8; 32]) }; + fn compress(&mut self, block: &[u8; 32]) { self.f(block); self.update_sigma(block); } @@ -217,7 +215,7 @@ impl UpdateCore for Gost94Core

{ fn update_blocks(&mut self, blocks: &[TBlock]) { let len = Self::BlockSize::USIZE * blocks.len(); self.update_n(len); - blocks.iter().for_each(|b| self.compress(b)); + blocks.iter().for_each(|b| self.compress(b.as_ref())); } } @@ -226,7 +224,7 @@ impl FixedOutputCore for Gost94Core

{ fn finalize_fixed_core(&mut self, buffer: &mut Buffer, out: &mut Output) { if buffer.get_pos() != 0 { self.update_n(buffer.get_pos()); - self.compress(buffer.pad_with_zeros()); + self.compress(buffer.pad_with_zeros().as_ref()); } let mut buf = Block::default(); diff --git a/gost94/src/lib.rs b/gost94/src/lib.rs index 8427b2c5..e35e48e5 100644 --- a/gost94/src/lib.rs +++ b/gost94/src/lib.rs @@ -31,6 +31,7 @@ html_root_url = "https://docs.rs/gost94/0.10.0" )] #![warn(missing_docs, rust_2018_idioms)] +#![forbid(unsafe_code)] #[cfg(feature = "std")] extern crate std; diff --git a/groestl/Cargo.toml b/groestl/Cargo.toml index 307111bd..1f67989c 100644 --- a/groestl/Cargo.toml +++ b/groestl/Cargo.toml @@ -12,10 +12,10 @@ keywords = ["crypto", "groestl", "grostl", "hash", "digest"] categories = ["cryptography", "no-std"] [dependencies] -digest = "0.10" +digest = "0.10.2" [dev-dependencies] -digest = { version = "0.10", features = ["dev"] } +digest = { version = "0.10.2", features = ["dev"] } hex-literal = "0.2" [features] diff --git a/groestl/src/compress1024.rs b/groestl/src/compress1024.rs index ebfd080f..fa142c39 100644 --- a/groestl/src/compress1024.rs +++ b/groestl/src/compress1024.rs @@ -4,7 +4,6 @@ use core::{convert::TryInto, u64}; pub(crate) const COLS: usize = 16; const ROUNDS: u64 = 14; -type Block = super::Block; #[inline(always)] fn column(x: &[u64; COLS], c: [usize; 8]) -> u64 { @@ -67,7 +66,7 @@ fn rndp(mut x: [u64; COLS], r: u64) -> [u64; COLS] { ] } -pub(crate) fn compress(h: &mut [u64; COLS], block: &Block) { +pub(crate) fn compress(h: &mut [u64; COLS], block: &[u8; 128]) { let mut q = [0u64; COLS]; for (chunk, v) in block.chunks_exact(8).zip(q.iter_mut()) { *v = u64::from_be_bytes(chunk.try_into().unwrap()); diff --git a/groestl/src/compress512.rs b/groestl/src/compress512.rs index 1db8d409..121e1a39 100644 --- a/groestl/src/compress512.rs +++ b/groestl/src/compress512.rs @@ -1,7 +1,6 @@ #![allow(clippy::needless_range_loop)] use crate::table::TABLE; use core::{convert::TryInto, u64}; -type Block = super::Block; pub(crate) const COLS: usize = 8; const ROUNDS: u64 = 10; @@ -51,7 +50,7 @@ fn rndp(mut x: [u64; COLS], r: u64) -> [u64; COLS] { ] } -pub(crate) fn compress(h: &mut [u64; COLS], block: &Block) { +pub(crate) fn compress(h: &mut [u64; COLS], block: &[u8; 64]) { let mut q = [0u64; COLS]; for (chunk, v) in block.chunks_exact(8).zip(q.iter_mut()) { *v = u64::from_be_bytes(chunk.try_into().unwrap()); diff --git a/groestl/src/lib.rs b/groestl/src/lib.rs index 8e5085a0..c37b4f27 100644 --- a/groestl/src/lib.rs +++ b/groestl/src/lib.rs @@ -31,7 +31,7 @@ html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg", html_root_url = "https://docs.rs/groestl/0.10.0" )] -#![deny(unsafe_code)] +#![forbid(unsafe_code)] #![warn(rust_2018_idioms)] pub use digest::{self, Digest}; @@ -44,7 +44,7 @@ use digest::{ CtVariableCoreWrapper, OutputSizeUser, RtVariableCoreWrapper, TruncSide, UpdateCore, VariableOutputCore, }, - generic_array::typenum::{Unsigned, U128, U28, U32, U48, U64}, + typenum::{Unsigned, U128, U28, U32, U48, U64}, HashMarker, InvalidOutputSize, Output, }; @@ -74,7 +74,7 @@ impl UpdateCore for GroestlShortVarCore { fn update_blocks(&mut self, blocks: &[Block]) { self.blocks_len += blocks.len() as u64; for block in blocks { - compress512::compress(&mut self.state, block); + compress512::compress(&mut self.state, block.as_ref()); } } } @@ -104,7 +104,9 @@ impl VariableOutputCore for GroestlShortVarCore { } else { self.blocks_len + 1 }; - buffer.len64_padding_be(blocks_len, |b| compress512::compress(&mut self.state, b)); + buffer.len64_padding_be(blocks_len, |block| { + compress512::compress(&mut self.state, block.as_ref()) + }); let res = compress512::p(&self.state); let n = compress512::COLS / 2; for (chunk, v) in out.chunks_exact_mut(8).zip(res[n..].iter()) { @@ -160,7 +162,7 @@ impl UpdateCore for GroestlLongVarCore { fn update_blocks(&mut self, blocks: &[Block]) { self.blocks_len += blocks.len() as u64; for block in blocks { - compress1024::compress(&mut self.state, block); + compress1024::compress(&mut self.state, block.as_ref()); } } } @@ -190,7 +192,9 @@ impl VariableOutputCore for GroestlLongVarCore { } else { self.blocks_len + 1 }; - buffer.len64_padding_be(blocks_len, |b| compress1024::compress(&mut self.state, b)); + buffer.len64_padding_be(blocks_len, |block| { + compress1024::compress(&mut self.state, block.as_ref()) + }); let res = compress1024::p(&self.state); let n = compress1024::COLS / 2; for (chunk, v) in out.chunks_exact_mut(8).zip(res[n..].iter()) { diff --git a/md2/src/lib.rs b/md2/src/lib.rs index d41a1178..44069fa6 100644 --- a/md2/src/lib.rs +++ b/md2/src/lib.rs @@ -29,7 +29,7 @@ html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg", html_root_url = "https://docs.rs/md2/0.10.0" )] -#![deny(unsafe_code)] +#![forbid(unsafe_code)] #![warn(missing_docs, rust_2018_idioms)] pub use digest::{self, Digest}; diff --git a/md4/Cargo.toml b/md4/Cargo.toml index 23c0b6ac..2a7417d0 100644 --- a/md4/Cargo.toml +++ b/md4/Cargo.toml @@ -12,10 +12,10 @@ keywords = ["crypto", "md4", "hash", "digest"] categories = ["cryptography", "no-std"] [dependencies] -digest = "0.10" +digest = "0.10.2" [dev-dependencies] -digest = { version = "0.10", features = ["dev"] } +digest = { version = "0.10.2", features = ["dev"] } hex-literal = "0.2" [features] diff --git a/md4/src/lib.rs b/md4/src/lib.rs index afbf0c31..1e7dcb04 100644 --- a/md4/src/lib.rs +++ b/md4/src/lib.rs @@ -29,7 +29,7 @@ html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg", html_root_url = "https://docs.rs/md4/0.10.0" )] -#![deny(unsafe_code)] +#![forbid(unsafe_code)] #![warn(rust_2018_idioms)] #![allow(clippy::many_single_char_names)] @@ -42,7 +42,7 @@ use digest::{ AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, CoreWrapper, FixedOutputCore, OutputSizeUser, Reset, UpdateCore, }, - generic_array::typenum::{Unsigned, U16, U64}, + typenum::{Unsigned, U16, U64}, HashMarker, Output, }; diff --git a/md5/Cargo.toml b/md5/Cargo.toml index 1c578e8f..6b27c2fd 100644 --- a/md5/Cargo.toml +++ b/md5/Cargo.toml @@ -15,11 +15,11 @@ categories = ["cryptography", "no-std"] name = "md5" [dependencies] -digest = "0.10" +digest = "0.10.2" md5-asm = { version = "0.5", optional = true } [dev-dependencies] -digest = { version = "0.10", features = ["dev"] } +digest = { version = "0.10.2", features = ["dev"] } hex-literal = "0.2" [features] diff --git a/md5/src/lib.rs b/md5/src/lib.rs index fadaf519..d32ff711 100644 --- a/md5/src/lib.rs +++ b/md5/src/lib.rs @@ -48,7 +48,7 @@ use digest::{ AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, CoreWrapper, FixedOutputCore, OutputSizeUser, Reset, UpdateCore, }, - generic_array::typenum::{Unsigned, U16, U64}, + typenum::{Unsigned, U16, U64}, HashMarker, Output, }; /// Core MD5 hasher state. diff --git a/ripemd/Cargo.toml b/ripemd/Cargo.toml index 9487f7fb..68170ec4 100644 --- a/ripemd/Cargo.toml +++ b/ripemd/Cargo.toml @@ -12,10 +12,10 @@ keywords = ["crypto", "ripemd", "hash", "digest"] categories = ["cryptography", "no-std"] [dependencies] -digest = "0.10" +digest = "0.10.2" [dev-dependencies] -digest = { version = "0.10", features = ["dev"] } +digest = { version = "0.10.2", features = ["dev"] } hex-literal = "0.2" [features] diff --git a/ripemd/src/c160.rs b/ripemd/src/c160.rs index 2163e848..db2e3fb1 100644 --- a/ripemd/src/c160.rs +++ b/ripemd/src/c160.rs @@ -1,4 +1,3 @@ -use super::{Block, Ripemd160Core}; use core::convert::TryInto; pub const DIGEST_BUF_LEN: usize = 5; @@ -112,7 +111,7 @@ macro_rules! process_block( }); ); -pub fn compress(h: &mut [u32; DIGEST_BUF_LEN], data: &Block) { +pub fn compress(h: &mut [u32; DIGEST_BUF_LEN], data: &[u8; 64]) { let mut w = [0u32; WORK_BUF_LEN]; for (o, chunk) in w.iter_mut().zip(data.chunks_exact(4)) { *o = u32::from_le_bytes(chunk.try_into().unwrap()); diff --git a/ripemd/src/c256.rs b/ripemd/src/c256.rs index a7d6e51a..bdcd0c39 100644 --- a/ripemd/src/c256.rs +++ b/ripemd/src/c256.rs @@ -1,4 +1,3 @@ -use super::{Block, Ripemd256Core}; use core::{convert::TryInto, mem::swap}; pub const DIGEST_BUF_LEN: usize = 8; @@ -119,7 +118,7 @@ macro_rules! process_block( }); ); -pub fn compress(h: &mut [u32; DIGEST_BUF_LEN], data: &Block) { +pub fn compress(h: &mut [u32; DIGEST_BUF_LEN], data: &[u8; 64]) { let mut w = [0u32; WORK_BUF_LEN]; for (o, chunk) in w.iter_mut().zip(data.chunks_exact(4)) { *o = u32::from_le_bytes(chunk.try_into().unwrap()); diff --git a/ripemd/src/c320.rs b/ripemd/src/c320.rs index 89b17406..af85eff2 100644 --- a/ripemd/src/c320.rs +++ b/ripemd/src/c320.rs @@ -1,4 +1,3 @@ -use super::{Block, Ripemd320Core}; use core::{convert::TryInto, mem::swap}; pub const HALF_DIGEST_BUF_LEN: usize = 5; @@ -142,7 +141,7 @@ macro_rules! process_block( }); ); -pub fn compress(h: &mut [u32; DIGEST_BUF_LEN], data: &Block) { +pub fn compress(h: &mut [u32; DIGEST_BUF_LEN], data: &[u8; 64]) { let mut w = [0u32; WORK_BUF_LEN]; for (o, chunk) in w.iter_mut().zip(data.chunks_exact(4)) { *o = u32::from_le_bytes(chunk.try_into().unwrap()); diff --git a/ripemd/src/lib.rs b/ripemd/src/lib.rs index 18a0a3b5..e83d49df 100644 --- a/ripemd/src/lib.rs +++ b/ripemd/src/lib.rs @@ -44,7 +44,7 @@ html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg", html_root_url = "https://docs.rs/ripemd/0.1.0" )] -#![deny(unsafe_code)] +#![forbid(unsafe_code)] #![warn(missing_docs, rust_2018_idioms)] pub use digest::{self, Digest}; @@ -56,7 +56,7 @@ use digest::{ AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, CoreWrapper, FixedOutputCore, OutputSizeUser, Reset, UpdateCore, }, - generic_array::typenum::{Unsigned, U20, U32, U40, U64}, + typenum::{Unsigned, U20, U32, U40, U64}, HashMarker, Output, }; @@ -98,7 +98,7 @@ macro_rules! impl_ripemd { // Assumes that `block_len` does not overflow self.block_len += blocks.len() as u64; for block in blocks { - $mod::compress(&mut self.h, block); + $mod::compress(&mut self.h, block.as_ref()); } } } @@ -109,7 +109,7 @@ macro_rules! impl_ripemd { let bs = Self::BlockSize::U64; let bit_len = 8 * (buffer.get_pos() as u64 + bs * self.block_len); let mut h = self.h; - buffer.len64_padding_le(bit_len, |block| $mod::compress(&mut h, block)); + buffer.len64_padding_le(bit_len, |block| $mod::compress(&mut h, block.as_ref())); for (chunk, v) in out.chunks_exact_mut(4).zip(h.iter()) { chunk.copy_from_slice(&v.to_le_bytes()); diff --git a/sha1/Cargo.toml b/sha1/Cargo.toml index 0ed7c60f..22dcce92 100644 --- a/sha1/Cargo.toml +++ b/sha1/Cargo.toml @@ -13,7 +13,7 @@ keywords = ["crypto", "sha1", "hash", "digest"] categories = ["cryptography", "no-std"] [dependencies] -digest = "0.10" +digest = "0.10.2" cfg-if = "1.0" sha1-asm = { version = "0.5", optional = true } @@ -21,7 +21,7 @@ sha1-asm = { version = "0.5", optional = true } cpufeatures = "0.2" [dev-dependencies] -digest = { version = "0.10", features = ["dev"] } +digest = { version = "0.10.2", features = ["dev"] } hex-literal = "0.2" [features] diff --git a/sha1/src/compress.rs b/sha1/src/compress.rs index feba6fd0..da4a10a9 100644 --- a/sha1/src/compress.rs +++ b/sha1/src/compress.rs @@ -1,5 +1,5 @@ use crate::{Block, BlockSizeUser, Sha1Core}; -use digest::generic_array::typenum::Unsigned; +use digest::typenum::Unsigned; cfg_if::cfg_if! { if #[cfg(feature = "force-soft")] { @@ -31,7 +31,6 @@ const BLOCK_SIZE: usize = ::BlockSize::USIZE; pub fn compress(state: &mut [u32; 5], blocks: &[Block]) { // SAFETY: GenericArray and [u8; 64] have // exactly the same memory layout - #[allow(unsafe_code)] let blocks: &[[u8; BLOCK_SIZE]] = unsafe { &*(blocks as *const _ as *const [[u8; BLOCK_SIZE]]) }; compress_inner(state, blocks); diff --git a/sha1/src/compress/x86.rs b/sha1/src/compress/x86.rs index dc4ca476..d3023424 100644 --- a/sha1/src/compress/x86.rs +++ b/sha1/src/compress/x86.rs @@ -1,7 +1,6 @@ //! SHA-1 `x86`/`x86_64` backend #![cfg(any(target_arch = "x86", target_arch = "x86_64"))] -#![allow(unsafe_code)] #[cfg(target_arch = "x86")] use core::arch::x86::*; diff --git a/sha1/src/lib.rs b/sha1/src/lib.rs index 96b1c8a9..e22a6004 100644 --- a/sha1/src/lib.rs +++ b/sha1/src/lib.rs @@ -58,7 +58,7 @@ use digest::{ AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, CoreWrapper, FixedOutputCore, OutputSizeUser, Reset, UpdateCore, }, - generic_array::typenum::{Unsigned, U20, U64}, + typenum::{Unsigned, U20, U64}, HashMarker, Output, }; diff --git a/sha2/Cargo.toml b/sha2/Cargo.toml index 8a0ed161..89f874cd 100644 --- a/sha2/Cargo.toml +++ b/sha2/Cargo.toml @@ -15,7 +15,7 @@ keywords = ["crypto", "sha2", "hash", "digest"] categories = ["cryptography", "no-std"] [dependencies] -digest = "0.10" +digest = "0.10.2" cfg-if = "1.0" sha2-asm = { version = "0.6.1", optional = true } @@ -23,7 +23,7 @@ sha2-asm = { version = "0.6.1", optional = true } cpufeatures = "0.2" [dev-dependencies] -digest = { version = "0.10", features = ["dev"] } +digest = { version = "0.10.2", features = ["dev"] } hex-literal = "0.2" [features] diff --git a/sha2/src/core_api.rs b/sha2/src/core_api.rs index 8484cca5..cfec02a6 100644 --- a/sha2/src/core_api.rs +++ b/sha2/src/core_api.rs @@ -6,7 +6,7 @@ use digest::{ AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, OutputSizeUser, TruncSide, UpdateCore, VariableOutputCore, }, - generic_array::typenum::{Unsigned, U128, U32, U64}, + typenum::{Unsigned, U128, U32, U64}, HashMarker, InvalidOutputSize, Output, }; diff --git a/sha2/src/sha256.rs b/sha2/src/sha256.rs index 2f493713..a45331e1 100644 --- a/sha2/src/sha256.rs +++ b/sha2/src/sha256.rs @@ -1,4 +1,4 @@ -use digest::generic_array::{typenum::U64, GenericArray}; +use digest::{generic_array::GenericArray, typenum::U64}; cfg_if::cfg_if! { if #[cfg(feature = "force-soft")] { diff --git a/sha2/src/sha512.rs b/sha2/src/sha512.rs index 05d16963..e71fdfa4 100644 --- a/sha2/src/sha512.rs +++ b/sha2/src/sha512.rs @@ -1,4 +1,4 @@ -use digest::generic_array::{typenum::U128, GenericArray}; +use digest::{generic_array::GenericArray, typenum::U128}; cfg_if::cfg_if! { if #[cfg(feature = "force-soft")] { diff --git a/shabal/Cargo.toml b/shabal/Cargo.toml index e0f0edb9..2fd851ed 100644 --- a/shabal/Cargo.toml +++ b/shabal/Cargo.toml @@ -15,7 +15,7 @@ categories = ["cryptography", "no-std"] digest = "0.10" [dev-dependencies] -digest = { version = "0.10", features = ["dev"] } +digest = { version = "0.10.2", features = ["dev"] } hex-literal = "0.2" [features] diff --git a/shabal/src/lib.rs b/shabal/src/lib.rs index 0997d2a7..6c794361 100644 --- a/shabal/src/lib.rs +++ b/shabal/src/lib.rs @@ -42,7 +42,7 @@ html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg", html_root_url = "https://docs.rs/shabal/0.4.0" )] -#![deny(unsafe_code)] +#![forbid(unsafe_code)] #![warn(missing_docs, rust_2018_idioms)] #[cfg(feature = "std")] diff --git a/sm3/Cargo.toml b/sm3/Cargo.toml index 28fe163b..9337f766 100644 --- a/sm3/Cargo.toml +++ b/sm3/Cargo.toml @@ -12,10 +12,10 @@ keywords = ["crypto", "sm3", "hash", "digest"] categories = ["cryptography", "no-std"] [dependencies] -digest = "0.10" +digest = "0.10.2" [dev-dependencies] -digest = { version = "0.10", features = ["dev"] } +digest = { version = "0.10.2", features = ["dev"] } hex-literal = "0.2" [features] diff --git a/sm3/src/lib.rs b/sm3/src/lib.rs index 95062fa4..385f4c31 100644 --- a/sm3/src/lib.rs +++ b/sm3/src/lib.rs @@ -34,7 +34,7 @@ html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg", html_root_url = "https://docs.rs/sm3/0.4.0" )] -#![deny(unsafe_code)] +#![forbid(unsafe_code)] #![warn(missing_docs, rust_2018_idioms)] pub use digest::{self, Digest}; @@ -46,7 +46,7 @@ use digest::{ AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, CoreWrapper, FixedOutputCore, OutputSizeUser, Reset, UpdateCore, }, - generic_array::typenum::{Unsigned, U32, U64}, + typenum::{Unsigned, U32, U64}, HashMarker, Output, }; diff --git a/streebog/Cargo.toml b/streebog/Cargo.toml index ade2b10d..95f1758e 100644 --- a/streebog/Cargo.toml +++ b/streebog/Cargo.toml @@ -12,10 +12,10 @@ keywords = ["crypto", "streebog", "gost", "hash", "digest"] categories = ["cryptography", "no-std"] [dependencies] -digest = "0.10" +digest = "0.10.2" [dev-dependencies] -digest = { version = "0.10", features = ["dev"] } +digest = { version = "0.10.2", features = ["dev"] } hex-literal = "0.2" [features] diff --git a/streebog/src/core_api.rs b/streebog/src/core_api.rs index f16da4a6..a9bd09b0 100644 --- a/streebog/src/core_api.rs +++ b/streebog/src/core_api.rs @@ -93,8 +93,7 @@ impl StreebogVarCore { adc(&mut self.n[7], 0, &mut carry); } - fn compress(&mut self, block: &GenBlock, msg_len: u64) { - let block = unsafe { &*(block.as_ptr() as *const [u8; 64]) }; + fn compress(&mut self, block: &[u8; 64], msg_len: u64) { self.g(&to_bytes(&self.n), block); self.update_n(msg_len); self.update_sigma(block); @@ -115,7 +114,7 @@ impl UpdateCore for StreebogVarCore { #[inline] fn update_blocks(&mut self, blocks: &[GenBlock]) { for block in blocks { - self.compress(block, BLOCK_SIZE as u64); + self.compress(block.as_ref(), BLOCK_SIZE as u64); } } } @@ -143,7 +142,7 @@ impl VariableOutputCore for StreebogVarCore { let pos = buffer.get_pos(); let block = buffer.pad_with_zeros(); block[pos] = 1; - self.compress(block, pos as u64); + self.compress(block.as_ref(), pos as u64); self.g(&[0u8; 64], &to_bytes(&self.n)); self.g(&[0u8; 64], &to_bytes(&self.sigma)); out.copy_from_slice(&self.h); diff --git a/streebog/src/lib.rs b/streebog/src/lib.rs index 9b2fca40..dfcde8c3 100644 --- a/streebog/src/lib.rs +++ b/streebog/src/lib.rs @@ -39,6 +39,7 @@ html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg", html_root_url = "https://docs.rs/streebog/0.10.0" )] +#![forbid(unsafe_code)] #![warn(missing_docs, rust_2018_idioms)] #[cfg(feature = "std")] diff --git a/tiger/Cargo.toml b/tiger/Cargo.toml index 3e3c7171..4b1e48d7 100644 --- a/tiger/Cargo.toml +++ b/tiger/Cargo.toml @@ -12,10 +12,10 @@ keywords = ["crypto", "hash", "tiger", "digest"] categories = ["cryptography", "no-std"] [dependencies] -digest = "0.10" +digest = "0.10.2" [dev-dependencies] -digest = { version = "0.10", features = ["dev"] } +digest = { version = "0.10.2", features = ["dev"] } hex-literal = "0.2" [features] diff --git a/tiger/src/compress.rs b/tiger/src/compress.rs index 20f7c56f..9f7024d7 100644 --- a/tiger/src/compress.rs +++ b/tiger/src/compress.rs @@ -1,7 +1,6 @@ use super::tables::{T1, T2, T3, T4}; use super::State; use core::convert::TryInto; -use digest::generic_array::{typenum::U64, GenericArray}; #[inline(always)] fn round(a: &mut u64, b: &mut u64, c: &mut u64, x: &u64, mul: u8) { @@ -51,22 +50,20 @@ fn key_schedule(x: &mut [u64; 8]) { x[7] = x[7].wrapping_sub(x[6] ^ 0x0123_4567_89AB_CDEF); } -pub(crate) fn compress(state: &mut State, blocks: &[GenericArray]) { +pub(crate) fn compress(state: &mut State, raw_block: &[u8; 64]) { let mut block: [u64; 8] = Default::default(); - for raw_block in blocks { - for (o, chunk) in block.iter_mut().zip(raw_block.chunks_exact(8)) { - *o = u64::from_le_bytes(chunk.try_into().unwrap()); - } - let [mut a, mut b, mut c] = *state; + for (o, chunk) in block.iter_mut().zip(raw_block.chunks_exact(8)) { + *o = u64::from_le_bytes(chunk.try_into().unwrap()); + } + let [mut a, mut b, mut c] = *state; - pass(&mut a, &mut b, &mut c, &block, 5); - key_schedule(&mut block); - pass(&mut c, &mut a, &mut b, &block, 7); - key_schedule(&mut block); - pass(&mut b, &mut c, &mut a, &block, 9); + pass(&mut a, &mut b, &mut c, &block, 5); + key_schedule(&mut block); + pass(&mut c, &mut a, &mut b, &block, 7); + key_schedule(&mut block); + pass(&mut b, &mut c, &mut a, &block, 9); - state[0] ^= a; - state[1] = b.wrapping_sub(state[1]); - state[2] = c.wrapping_add(state[2]); - } + state[0] ^= a; + state[1] = b.wrapping_sub(state[1]); + state[2] = c.wrapping_add(state[2]); } diff --git a/tiger/src/lib.rs b/tiger/src/lib.rs index 8ef93ca5..1576f156 100644 --- a/tiger/src/lib.rs +++ b/tiger/src/lib.rs @@ -31,19 +31,19 @@ html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg", html_root_url = "https://docs.rs/tiger/0.2.0" )] -#![deny(unsafe_code)] +#![forbid(unsafe_code)] #![warn(missing_docs, rust_2018_idioms)] pub use digest::{self, Digest}; -use core::{fmt, slice::from_ref}; +use core::fmt; use digest::{ block_buffer::Eager, core_api::{ AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, CoreWrapper, FixedOutputCore, OutputSizeUser, Reset, UpdateCore, }, - generic_array::typenum::{Unsigned, U24, U64}, + typenum::{Unsigned, U24, U64}, HashMarker, Output, }; @@ -83,7 +83,9 @@ impl UpdateCore for TigerCore { #[inline] fn update_blocks(&mut self, blocks: &[Block]) { self.block_len += blocks.len() as u64; - compress(&mut self.state, blocks); + for block in blocks { + compress(&mut self.state, block.as_ref()); + } } } @@ -95,7 +97,7 @@ impl FixedOutputCore for TigerCore { let bit_len = 8 * (pos + bs * self.block_len); buffer.digest_pad(1, &bit_len.to_le_bytes(), |b| { - compress(&mut self.state, from_ref(b)) + compress(&mut self.state, b.as_ref()) }); for (chunk, v) in out.chunks_exact_mut(8).zip(self.state.iter()) { chunk.copy_from_slice(&v.to_le_bytes()); @@ -155,7 +157,9 @@ impl UpdateCore for Tiger2Core { #[inline] fn update_blocks(&mut self, blocks: &[Block]) { self.block_len += blocks.len() as u64; - compress(&mut self.state, blocks); + for block in blocks { + compress(&mut self.state, block.as_ref()); + } } } @@ -166,7 +170,7 @@ impl FixedOutputCore for Tiger2Core { let pos = buffer.get_pos() as u64; let bit_len = 8 * (pos + bs * self.block_len); - buffer.len64_padding_le(bit_len, |b| compress(&mut self.state, from_ref(b))); + buffer.len64_padding_le(bit_len, |b| compress(&mut self.state, b.as_ref())); for (chunk, v) in out.chunks_exact_mut(8).zip(self.state.iter()) { chunk.copy_from_slice(&v.to_le_bytes()); } diff --git a/whirlpool/Cargo.toml b/whirlpool/Cargo.toml index 777785c4..f6364436 100644 --- a/whirlpool/Cargo.toml +++ b/whirlpool/Cargo.toml @@ -12,11 +12,11 @@ keywords = ["crypto", "whirlpool", "hash", "digest"] categories = ["cryptography", "no-std"] [dependencies] -digest = "0.10" +digest = "0.10.2" whirlpool-asm = { version = "0.6", optional = true} [dev-dependencies] -digest = { version = "0.10", features = ["dev"] } +digest = { version = "0.10.2", features = ["dev"] } hex-literal = "0.2" [features] diff --git a/whirlpool/src/lib.rs b/whirlpool/src/lib.rs index 54197e8f..7ed1998e 100644 --- a/whirlpool/src/lib.rs +++ b/whirlpool/src/lib.rs @@ -53,14 +53,14 @@ use whirlpool_asm as compress; use compress::compress; -use core::{fmt, slice::from_ref}; +use core::fmt; use digest::{ block_buffer::Eager, core_api::{ AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, CoreWrapper, FixedOutputCore, OutputSizeUser, Reset, UpdateCore, }, - generic_array::typenum::{Unsigned, U64}, + typenum::{Unsigned, U64}, HashMarker, Output, }; @@ -107,7 +107,7 @@ impl FixedOutputCore for WhirlpoolCore { let mut state = self.state; buffer.digest_pad(0x80, &buf, |block| { - compress(&mut state, convert(from_ref(block))) + compress(&mut state, convert(core::slice::from_ref(block))); }); for (chunk, v) in out.chunks_exact_mut(8).zip(state.iter()) {