Skip to content

Commit

Permalink
update block-buffer and digest
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Nov 19, 2021
1 parent 3bb918d commit 1b76492
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 79 deletions.
15 changes: 3 additions & 12 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
2 changes: 1 addition & 1 deletion fsb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion gost94/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
7 changes: 2 additions & 5 deletions gost94/src/gost94_core.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion md2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
10 changes: 6 additions & 4 deletions md2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion sha3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
27 changes: 15 additions & 12 deletions sha3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,43 +78,46 @@ 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!(
Keccak256Core,
Keccak256,
U32,
U136,
paddings::Keccak,
KECCAK_PAD,
"Keccak-256",
);
sha3_impl!(
Keccak384Core,
Keccak384,
U48,
U104,
paddings::Keccak,
KECCAK_PAD,
"Keccak-384",
);
sha3_impl!(
Keccak512Core,
Keccak512,
U64,
U72,
paddings::Keccak,
KECCAK_PAD,
"Keccak-512",
);

Expand All @@ -123,7 +126,7 @@ sha3_impl!(
Keccak256Full,
U200,
U136,
paddings::Keccak,
KECCAK_PAD,
"SHA-3 CryptoNight variant",
);

Expand All @@ -132,31 +135,31 @@ sha3_impl!(
Sha3_224,
U28,
U144,
paddings::Sha3,
SHA3_PAD,
"SHA-3-224",
);
sha3_impl!(
Sha3_256Core,
Sha3_256,
U32,
U136,
paddings::Sha3,
SHA3_PAD,
"SHA-3-256",
);
sha3_impl!(
Sha3_384Core,
Sha3_384,
U48,
U104,
paddings::Sha3,
SHA3_PAD,
"SHA-3-384",
);
sha3_impl!(
Sha3_512Core,
Sha3_512,
U64,
U72,
paddings::Sha3,
SHA3_PAD,
"SHA-3-512",
);

Expand All @@ -166,7 +169,7 @@ shake_impl!(
Shake128ReaderCore,
Shake128Reader,
U168,
paddings::Shake,
SHAKE_PAD,
"SHAKE128",
);
shake_impl!(
Expand All @@ -175,6 +178,6 @@ shake_impl!(
Shake256ReaderCore,
Shake256Reader,
U136,
paddings::Shake,
SHAKE_PAD,
"SHAKE256",
);
22 changes: 14 additions & 8 deletions sha3/src/macros.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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(),
Expand Down
26 changes: 0 additions & 26 deletions sha3/src/paddings.rs

This file was deleted.

2 changes: 1 addition & 1 deletion shabal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
8 changes: 4 additions & 4 deletions shabal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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..];
Expand Down
2 changes: 1 addition & 1 deletion tiger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down

0 comments on commit 1b76492

Please sign in to comment.