Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cipher.len check #10

Merged
merged 1 commit into from
Mar 15, 2024
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
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