diff --git a/Cargo.lock b/Cargo.lock index 4d90b0777..32e95649a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,16 +19,7 @@ checksum = "fc52553543ecb104069b0ff9e0fcc5c739ad16202935528a112d974e8f1a4ee8" [[package]] name = "block-buffer" version = "0.10.0" -source = "git+https://github.com/RustCrypto/utils?branch=pad_error#24b145e4b2935911e139d70769c4ff1573845b88" -dependencies = [ - "block-padding", - "generic-array", -] - -[[package]] -name = "block-padding" -version = "0.3.0" -source = "git+https://github.com/RustCrypto/utils?branch=pad_error#24b145e4b2935911e139d70769c4ff1573845b88" +source = "git+https://github.com/RustCrypto/utils?branch=block-buffer/v0.10#5cbb42cfcff2f75d47827cd7b77529c20c029f8e" dependencies = [ "generic-array", ] @@ -57,7 +48,7 @@ dependencies = [ [[package]] name = "crypto-common" version = "0.1.0" -source = "git+https://github.com/RustCrypto/traits/?branch=new_traits#60ee50e39ec2952e07e77c4b7813423007a73976" +source = "git+https://github.com/RustCrypto/traits/?branch=digest/v0.10#90f739265e38e74c51b5a23ed56bed4f70bcca35" dependencies = [ "generic-array", ] @@ -65,7 +56,7 @@ dependencies = [ [[package]] name = "digest" version = "0.10.0" -source = "git+https://github.com/RustCrypto/traits/?branch=new_traits#60ee50e39ec2952e07e77c4b7813423007a73976" +source = "git+https://github.com/RustCrypto/traits/?branch=digest/v0.10#90f739265e38e74c51b5a23ed56bed4f70bcca35" dependencies = [ "blobby", "block-buffer", diff --git a/Cargo.toml b/Cargo.toml index 74b914a1f..91c900778 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,5 +23,5 @@ members = [ opt-level = 2 [patch.crates-io] -digest = { git = "https://github.com/RustCrypto/traits/", branch = "new_traits" } -block-buffer = { git = "https://github.com/RustCrypto/utils", branch = "pad_error" } +digest = { git = "https://github.com/RustCrypto/traits/", branch = "digest/v0.10" } +block-buffer = { git = "https://github.com/RustCrypto/utils", branch = "block-buffer/v0.10" } diff --git a/fsb/Cargo.toml b/fsb/Cargo.toml index c0a8dafd4..678f23d84 100644 --- a/fsb/Cargo.toml +++ b/fsb/Cargo.toml @@ -11,7 +11,7 @@ keywords = ["crypto", "fsb", "hash", "digest"] categories = ["cryptography", "no-std"] [dependencies] -digest = { version = "0.10", features = ["block-padding"] } +digest = "0.10" whirlpool = { version = "0.10", path = "../whirlpool", default-features = false } [dev-dependencies] diff --git a/gost94/Cargo.toml b/gost94/Cargo.toml index 0439b1e62..7ea6fb2dd 100644 --- a/gost94/Cargo.toml +++ b/gost94/Cargo.toml @@ -12,7 +12,7 @@ keywords = ["crypto", "gost94", "gost", "hash", "digest"] categories = ["cryptography", "no-std"] [dependencies] -digest = { version = "0.10", features = ["block-padding"] } +digest = "0.10" [dev-dependencies] digest = { version = "0.10", features = ["dev"] } diff --git a/gost94/src/gost94_core.rs b/gost94/src/gost94_core.rs index 8ff49da83..569789a0b 100644 --- a/gost94/src/gost94_core.rs +++ b/gost94/src/gost94_core.rs @@ -1,7 +1,7 @@ #![allow(clippy::many_single_char_names)] use core::{convert::TryInto, fmt}; use digest::{ - block_buffer::{block_padding::ZeroPadding, Eager}, + block_buffer::Eager, consts::U32, core_api::{ AlgorithmName, Block as TBlock, BlockSizeUser, Buffer, BufferKindUser, FixedOutputCore, @@ -226,10 +226,7 @@ impl<P: Gost94Params> FixedOutputCore for Gost94Core<P> { fn finalize_fixed_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) { if buffer.get_pos() != 0 { self.update_n(buffer.get_pos()); - let block = buffer - .pad_with::<ZeroPadding>() - .expect("buffer pos is always smaller than block"); - self.compress(block); + self.compress(buffer.pad_with_zeros()); } let mut buf = Block::default(); diff --git a/md2/Cargo.toml b/md2/Cargo.toml index c02d7cb6b..9978137a9 100644 --- a/md2/Cargo.toml +++ b/md2/Cargo.toml @@ -12,7 +12,7 @@ keywords = ["crypto", "md2", "hash", "digest"] categories = ["cryptography", "no-std"] [dependencies] -digest = { version = "0.10", features = ["block-padding"] } +digest = "0.10" [dev-dependencies] digest = { version = "0.10", features = ["dev"] } diff --git a/md2/src/lib.rs b/md2/src/lib.rs index caf3e96d7..c363663fc 100644 --- a/md2/src/lib.rs +++ b/md2/src/lib.rs @@ -35,7 +35,7 @@ pub use digest::{self, Digest}; use core::fmt; use digest::{ - block_buffer::{block_padding::Pkcs7, Eager}, + block_buffer::Eager, consts::U16, core_api::{ AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, CoreWrapper, FixedOutputCore, @@ -105,9 +105,11 @@ impl UpdateCore for Md2Core { impl FixedOutputCore for Md2Core { #[inline] fn finalize_fixed_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) { - let block = buffer - .pad_with::<Pkcs7>() - .expect("buffer pos is always smaller than block"); + let pos = buffer.get_pos(); + let rem = buffer.remaining() as u8; + let block = buffer.pad_with_zeros(); + block[pos..].iter_mut().for_each(|b| *b = rem); + self.compress(block); let checksum = self.checksum; self.compress(&checksum); diff --git a/sha3/Cargo.toml b/sha3/Cargo.toml index 9d6ca4ee3..398304b20 100644 --- a/sha3/Cargo.toml +++ b/sha3/Cargo.toml @@ -12,7 +12,7 @@ keywords = ["crypto", "sha3", "keccak", "hash", "digest"] categories = ["cryptography", "no-std"] [dependencies] -digest = { version = "0.10", features = ["block-padding"] } +digest = "0.10" keccak = "0.1" [dev-dependencies] diff --git a/sha3/src/lib.rs b/sha3/src/lib.rs index 350e9971a..97236160d 100644 --- a/sha3/src/lib.rs +++ b/sha3/src/lib.rs @@ -78,19 +78,22 @@ use digest::{ HashMarker, Output, }; -mod paddings; #[macro_use] mod macros; mod state; use crate::state::Sha3State; +const KECCAK_PAD: u8 = 0x01; +const SHA3_PAD: u8 = 0x06; +const SHAKE_PAD: u8 = 0x1f; + sha3_impl!( Keccak224Core, Keccak224, U28, U144, - paddings::Keccak, + KECCAK_PAD, "Keccak-224", ); sha3_impl!( @@ -98,7 +101,7 @@ sha3_impl!( Keccak256, U32, U136, - paddings::Keccak, + KECCAK_PAD, "Keccak-256", ); sha3_impl!( @@ -106,7 +109,7 @@ sha3_impl!( Keccak384, U48, U104, - paddings::Keccak, + KECCAK_PAD, "Keccak-384", ); sha3_impl!( @@ -114,7 +117,7 @@ sha3_impl!( Keccak512, U64, U72, - paddings::Keccak, + KECCAK_PAD, "Keccak-512", ); @@ -123,7 +126,7 @@ sha3_impl!( Keccak256Full, U200, U136, - paddings::Keccak, + KECCAK_PAD, "SHA-3 CryptoNight variant", ); @@ -132,7 +135,7 @@ sha3_impl!( Sha3_224, U28, U144, - paddings::Sha3, + SHA3_PAD, "SHA-3-224", ); sha3_impl!( @@ -140,7 +143,7 @@ sha3_impl!( Sha3_256, U32, U136, - paddings::Sha3, + SHA3_PAD, "SHA-3-256", ); sha3_impl!( @@ -148,7 +151,7 @@ sha3_impl!( Sha3_384, U48, U104, - paddings::Sha3, + SHA3_PAD, "SHA-3-384", ); sha3_impl!( @@ -156,7 +159,7 @@ sha3_impl!( Sha3_512, U64, U72, - paddings::Sha3, + SHA3_PAD, "SHA-3-512", ); @@ -166,7 +169,7 @@ shake_impl!( Shake128ReaderCore, Shake128Reader, U168, - paddings::Shake, + SHAKE_PAD, "SHAKE128", ); shake_impl!( @@ -175,6 +178,6 @@ shake_impl!( Shake256ReaderCore, Shake256Reader, U136, - paddings::Shake, + SHAKE_PAD, "SHAKE256", ); diff --git a/sha3/src/macros.rs b/sha3/src/macros.rs index dcf93d37f..d1e2728cb 100644 --- a/sha3/src/macros.rs +++ b/sha3/src/macros.rs @@ -1,7 +1,7 @@ macro_rules! sha3_impl { ( $name:ident, $full_name:ident, $output_size:ident, - $rate:ident, $padding:ty, $alg_name:expr, + $rate:ident, $pad:expr, $alg_name:expr, ) => { #[doc = "Core "] #[doc = $alg_name] @@ -38,9 +38,12 @@ macro_rules! sha3_impl { impl FixedOutputCore for $name { #[inline] fn finalize_fixed_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) { - let block = buffer - .pad_with::<$padding>() - .expect("buffer pos is always smaller than block"); + let pos = buffer.get_pos(); + let block = buffer.pad_with_zeros(); + block[pos] = $pad; + let n = block.len(); + block[n - 1] |= 0x80; + self.state.absorb_block(block); let n = out.len(); @@ -87,7 +90,7 @@ macro_rules! sha3_impl { macro_rules! shake_impl { ( $name:ident, $full_name:ident, $reader:ident, $reader_full:ident, - $rate:ident, $padding:ty, $alg_name:expr, + $rate:ident, $pad:expr, $alg_name:expr, ) => { #[doc = "Core "] #[doc = $alg_name] @@ -122,9 +125,12 @@ macro_rules! shake_impl { #[inline] fn finalize_xof_core(&mut self, buffer: &mut Buffer<Self>) -> Self::ReaderCore { - let block = buffer - .pad_with::<$padding>() - .expect("buffer pos is always smaller than block"); + let pos = buffer.get_pos(); + let block = buffer.pad_with_zeros(); + block[pos] = $pad; + let n = block.len(); + block[n - 1] |= 0x80; + self.state.absorb_block(block); $reader { state: self.state.clone(), diff --git a/sha3/src/paddings.rs b/sha3/src/paddings.rs deleted file mode 100644 index be5769b0a..000000000 --- a/sha3/src/paddings.rs +++ /dev/null @@ -1,26 +0,0 @@ -use digest::block_buffer::block_padding::{generic_array::ArrayLength, Block, PadError, Padding}; - -macro_rules! impl_padding { - ($name:ident, $pad:expr) => { - #[derive(Copy, Clone, Default)] - pub struct $name; - - impl<B: ArrayLength<u8>> Padding<B> for $name { - #[inline] - fn pad(block: &mut Block<B>, pos: usize) -> Result<(), PadError> { - if pos >= B::USIZE { - return Err(PadError); - } - block[pos] = $pad; - block[pos + 1..].iter_mut().for_each(|b| *b = 0); - let n = block.len(); - block[n - 1] |= 0x80; - Ok(()) - } - } - }; -} - -impl_padding!(Keccak, 0x01); -impl_padding!(Sha3, 0x06); -impl_padding!(Shake, 0x1f); diff --git a/shabal/Cargo.toml b/shabal/Cargo.toml index 43594e0cf..144cc0a91 100644 --- a/shabal/Cargo.toml +++ b/shabal/Cargo.toml @@ -12,7 +12,7 @@ keywords = ["crypto", "shabal", "hash", "digest"] categories = ["cryptography", "no-std"] [dependencies] -digest = { version = "0.10", features = ["block-padding"] } +digest = "0.10" [dev-dependencies] digest = { version = "0.10", features = ["dev"] } diff --git a/shabal/src/lib.rs b/shabal/src/lib.rs index 7cfa173ab..d923393bb 100644 --- a/shabal/src/lib.rs +++ b/shabal/src/lib.rs @@ -54,7 +54,7 @@ pub use digest::{self, Digest}; use core::fmt; use digest::{ - block_buffer::{block_padding::Iso7816, Eager}, + block_buffer::Eager, consts::{U24, U28, U32, U48, U64}, core_api::{ AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, CoreWrapper, FixedOutputCore, @@ -101,9 +101,9 @@ macro_rules! impl_core { impl FixedOutputCore for $name { #[inline] fn finalize_fixed_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) { - let block = buffer - .pad_with::<Iso7816>() - .expect("buffer pos is always smaller than block"); + let pos = buffer.get_pos(); + let block = buffer.pad_with_zeros(); + block[pos] = 0x80; compress_final(&mut self.state, &block); let n = 16 - <$out_size>::USIZE / 4; let b = &self.state.get_b()[n..]; diff --git a/tiger/Cargo.toml b/tiger/Cargo.toml index fb459a70a..0f5959648 100644 --- a/tiger/Cargo.toml +++ b/tiger/Cargo.toml @@ -12,7 +12,7 @@ keywords = ["crypto", "hash", "tiger", "digest"] categories = ["cryptography", "no-std"] [dependencies] -digest = { version = "0.10", features = ["block-padding"] } +digest = "0.10" [dev-dependencies] digest = { version = "0.10", features = ["dev"] }