Skip to content

Commit

Permalink
Merge pull request #10 from dusk-network/mocello/9_len_check
Browse files Browse the repository at this point in the history
Add cipher.len check
  • Loading branch information
moCello authored Mar 15, 2024
2 parents be01e9f + 3496600 commit 6fd326a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/dusk_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
uses: dusk-network/.github/.github/workflows/code-analysis.yml@main
with:
clippy_default: false
clippy_args: -- -D warnings
clippy_args: --features=encryption -- -D warnings

dusk_analyzer:
name: Dusk Analyzer
Expand All @@ -21,3 +21,5 @@ jobs:
test_nightly:
name: Run tests
uses: dusk-network/.github/.github/workflows/run-tests.yml@main
with:
test_flags: --features=encryption
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Add authenticated encryption and decryption [#6]
- Add check for `cipher.len == message.len + 1` in `encrypt` and `decrypt` [#9]

### Changed

Expand All @@ -28,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add documentation

<!-- ISSUES -->
[#9]: https://github.com/dusk-network/safe/issues/9
[#6]: https://github.com/dusk-network/safe/issues/6
[#4]: https://github.com/dusk-network/safe/issues/4
[#3]: https://github.com/dusk-network/safe/issues/3
Expand Down
16 changes: 13 additions & 3 deletions src/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ where
cipher[i] = sponge.safe.add(&cipher[i], &message[i]);
}

// cipher should yield exactly message_len + 1 elements
if cipher.len() != message_len + 1 {
return Err(Error::EncryptionFailed);
}

// finish the sponge, erase cipher upon error
match sponge.finish() {
Ok(mut output) => {
Expand All @@ -93,7 +98,7 @@ where
}
Err(e) => {
cipher.zeroize();
Err(e.into())
Err(e)
}
}
}
Expand All @@ -118,7 +123,7 @@ where
safe,
domain_sep.into(),
message_len,
&shared_secret,
shared_secret,
&nonce,
)?;

Expand All @@ -143,6 +148,11 @@ where
return Err(Error::DecryptionFailed);
};

// cipher should yield exactly message_len + 1 elements
if cipher.len() != message_len + 1 {
return Err(Error::DecryptionFailed);
}

// finish sponge, erase message upon error
match sponge.finish() {
Ok(mut output) => {
Expand All @@ -151,7 +161,7 @@ where
}
Err(e) => {
message.zeroize();
Err(e.into())
Err(e)
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub enum Error {
/// The input doesn't yield enough input elements.
TooFewInputElements,

/// Failed to encrypt the message into the cipher with the provided secret
/// and nonce.
EncryptionFailed,

/// Failed to decrypt the message from the cipher with the provided secret
/// and nonce.
DecryptionFailed,
Expand Down

0 comments on commit 6fd326a

Please # to comment.