Skip to content

Commit e6e19c4

Browse files
authored
cshake: fix zero padding when data already aligns to block boundary (#834)
When the buffer already aligns with the block boundary, the existing implementation adds an additional block of zeros to the input. This occurs when the total length of the function name and customization string is a multiple of the block size. This isn't compliant with the "bytepad" algorithm in section 2.3.3 of [SP 800-185]. If the buffer already aligns with the block boundary, skip the padding. [SP 800-185]: https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-185.pdf
1 parent 70eae76 commit e6e19c4

4 files changed

Lines changed: 7 additions & 2 deletions

File tree

cshake/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use digest::{
1616
CollisionResistance, CustomizedInit, ExtendableOutput, HashMarker, Update, XofReader,
1717
array::Array,
1818
block_api::{AlgorithmName, BlockSizeUser},
19-
block_buffer::{BlockSizes, EagerBuffer, ReadBuffer},
19+
block_buffer::{BlockSizes, EagerBuffer, LazyBuffer, ReadBuffer},
2020
consts::{U16, U32, U136, U168},
2121
};
2222
use keccak::{Keccak, State1600};
@@ -79,7 +79,7 @@ impl<Rate: BlockSizes> CShake<Rate> {
7979
}
8080

8181
keccak.with_f1600(|f1600| {
82-
let mut buffer: EagerBuffer<Rate> = Default::default();
82+
let mut buffer: LazyBuffer<Rate> = Default::default();
8383
let state = &mut state;
8484
let mut b = [0u8; 9];
8585

cshake/tests/cshake.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,8 @@ macro_rules! new_cshake_test {
7171

7272
new_cshake_test!(cshake128, cshake::CShake128);
7373
new_cshake_test!(cshake256, cshake::CShake256);
74+
75+
// When bytepad output aligns exactly to the block boundary,
76+
// no extra zero block should be appended (SP 800-185 2.3.3).
77+
new_cshake_test!(cshake128_bytepad_block_aligned, cshake::CShake128);
78+
new_cshake_test!(cshake256_bytepad_block_aligned, cshake::CShake256);
199 Bytes
Binary file not shown.
200 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)