diff --git a/src/main/java/com/southernstorm/noise/protocol/AESGCMFallbackCipherState.java b/src/main/java/com/southernstorm/noise/protocol/AESGCMFallbackCipherState.java index f627be3..b0ddd0e 100644 --- a/src/main/java/com/southernstorm/noise/protocol/AESGCMFallbackCipherState.java +++ b/src/main/java/com/southernstorm/noise/protocol/AESGCMFallbackCipherState.java @@ -187,7 +187,7 @@ class AESGCMFallbackCipherState implements CipherState { int space; if (ciphertextOffset < 0 || ciphertextOffset > ciphertext.length) throw new IllegalArgumentException(); - if (length < 0 || plaintextOffset < 0 || plaintextOffset > plaintext.length) + if (length < 0 || plaintextOffset < 0 || plaintextOffset > plaintext.length || plaintextOffset + length > plaintext.length) throw new IllegalArgumentException(); space = ciphertext.length - ciphertextOffset; if (!haskey) { @@ -221,7 +221,7 @@ class AESGCMFallbackCipherState implements CipherState { space = ciphertext.length - ciphertextOffset; if (length > space) throw new ShortBufferException(); - if (length < 0 || plaintextOffset < 0 || plaintextOffset > plaintext.length) + if (length < 0 || plaintextOffset < 0 || plaintextOffset > plaintext.length || plaintextOffset + length > plaintext.length) throw new IllegalArgumentException(); space = plaintext.length - plaintextOffset; if (!haskey) { diff --git a/src/main/java/com/southernstorm/noise/protocol/AESGCMOnCtrCipherState.java b/src/main/java/com/southernstorm/noise/protocol/AESGCMOnCtrCipherState.java index df40945..cb7a4de 100644 --- a/src/main/java/com/southernstorm/noise/protocol/AESGCMOnCtrCipherState.java +++ b/src/main/java/com/southernstorm/noise/protocol/AESGCMOnCtrCipherState.java @@ -220,7 +220,7 @@ class AESGCMOnCtrCipherState implements CipherState { int space; if (ciphertextOffset < 0 || ciphertextOffset > ciphertext.length) throw new IllegalArgumentException(); - if (length < 0 || plaintextOffset < 0 || plaintextOffset > plaintext.length) + if (length < 0 || plaintextOffset < 0 || plaintextOffset > plaintext.length || plaintextOffset + length > plaintext.length) throw new IllegalArgumentException(); space = ciphertext.length - ciphertextOffset; if (keySpec == null) { @@ -269,7 +269,7 @@ class AESGCMOnCtrCipherState implements CipherState { space = ciphertext.length - ciphertextOffset; if (length > space) throw new ShortBufferException(); - if (length < 0 || plaintextOffset < 0 || plaintextOffset > plaintext.length) + if (length < 0 || plaintextOffset < 0 || plaintextOffset > plaintext.length || plaintextOffset + length > plaintext.length) throw new IllegalArgumentException(); space = plaintext.length - plaintextOffset; if (keySpec == null) { diff --git a/src/main/java/com/southernstorm/noise/protocol/ChaChaPolyCipherState.java b/src/main/java/com/southernstorm/noise/protocol/ChaChaPolyCipherState.java index 40ce2bc..9af4d52 100644 --- a/src/main/java/com/southernstorm/noise/protocol/ChaChaPolyCipherState.java +++ b/src/main/java/com/southernstorm/noise/protocol/ChaChaPolyCipherState.java @@ -216,7 +216,7 @@ class ChaChaPolyCipherState implements CipherState { int space; if (ciphertextOffset < 0 || ciphertextOffset > ciphertext.length) throw new IllegalArgumentException(); - if (length < 0 || plaintextOffset < 0 || plaintextOffset > plaintext.length) + if (length < 0 || plaintextOffset < 0 || plaintextOffset > plaintext.length || plaintextOffset + length > plaintext.length) throw new IllegalArgumentException(); space = ciphertext.length - ciphertextOffset; if (!haskey) { @@ -248,7 +248,7 @@ class ChaChaPolyCipherState implements CipherState { space = ciphertext.length - ciphertextOffset; if (length > space) throw new ShortBufferException(); - if (length < 0 || plaintextOffset < 0 || plaintextOffset > plaintext.length) + if (length < 0 || plaintextOffset < 0 || plaintextOffset > plaintext.length || plaintextOffset + length > plaintext.length) throw new IllegalArgumentException(); space = plaintext.length - plaintextOffset; if (!haskey) {