Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Read the `Sponge::absorb` input through a single `as_ref` call
- Wipe old output allocations before growth, including when continuing cloned sponges [#37]
- Return an error and invalidate the sponge when output allocation fails [#37]
- Wipe encryption/decryption temporary vectors on error and unwinding [#37]
Expand Down
5 changes: 3 additions & 2 deletions src/sponge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ where
return Err(Error::IOPatternViolation);
}
// Check that input yields enough elements
if input.as_ref().len() < len {
let input = input.as_ref();
if input.len() < len {
self.zeroize();
return Err(Error::TooFewInputElements);
}
Expand All @@ -224,7 +225,7 @@ where

// Absorb `len` elements into the state, calling [`permute`] when the
// absorb-position reached the rate.
for element in input.as_ref().iter().take(len) {
for element in input.iter().take(len) {
if self.pos_absorb == Self::RATE {
self.safe.permute(&mut self.state);

Expand Down
19 changes: 19 additions & 0 deletions tests/sponge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use core::cell::Cell;

use dusk_bls12_381::BlsScalar;
use dusk_safe::{Call, Error, Safe, Sponge};
use zeroize::Zeroize;
Expand Down Expand Up @@ -41,6 +43,23 @@ impl Rotate {
}
}

#[test]
fn absorb_uses_the_validated_slice() {
struct Input<'a>(Cell<&'a [BlsScalar]>);
impl AsRef<[BlsScalar]> for Input<'_> {
fn as_ref(&self) -> &[BlsScalar] {
self.0.replace(&[])
}
}
let input = [BlsScalar::from(7), BlsScalar::from(9)];
let mut sponge =
Sponge::start(Rotate::new(), [Call::Absorb(2), Call::Squeeze(1)], 0)
.unwrap();
sponge.absorb(2, Input(Cell::new(&input))).unwrap();
sponge.squeeze(1).unwrap();
assert_eq!(sponge.finish().unwrap(), [input[1]]);
}

#[test]
fn failures_are_terminal() {
for case in 0..9 {
Expand Down