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

Boundary checks improvement: detect when plaintextOffset + length is greater than length #12

Merged
merged 3 commits into from Sep 3, 2020

Conversation

polivar3
Copy link
Contributor

@polivar3 polivar3 commented Sep 2, 2020

This patch completes the previous commit by introducing one missing boundary check.

@@ -187,7 +187,7 @@ public int encryptWithAd(byte[] ad, byte[] plaintext, int plaintextOffset,
int space;
if (ciphertextOffset < 0 || ciphertextOffset > ciphertext.length)
throw new IllegalArgumentException();
if (length < 0 || plaintextOffset < 0 || plaintextOffset > plaintext.length || plaintextOffset + length > plaintext.length)
if (length < 0 || plaintextOffset < 0 || plaintextOffset > plaintext.length || plaintextOffset + length < plaintextOffset || plaintextOffset + length > plaintext.length)
Copy link
Owner

@rweather rweather Sep 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A simpler approach may be to use:
|| length > plaintext.length || (plaintext.length - plaintextOffset) < length
This avoids the wrap-around problem with "plaintextOffset + plaintext.length" going negative.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one, I've implemented your suggestion :)

@rweather rweather merged commit 594909c into rweather:master Sep 3, 2020
@polivar3 polivar3 deleted the fix branch September 3, 2020 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants