From 95fa4af798af088bc49b41972a18a27343edb537 Mon Sep 17 00:00:00 2001 From: Pietro Oliva Date: Wed, 2 Sep 2020 15:12:31 -0400 Subject: [PATCH] Simplify boundary checks --- .../noise/protocol/AESGCMFallbackCipherState.java | 4 ++-- .../southernstorm/noise/protocol/AESGCMOnCtrCipherState.java | 4 ++-- .../southernstorm/noise/protocol/ChaChaPolyCipherState.java | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/southernstorm/noise/protocol/AESGCMFallbackCipherState.java b/src/main/java/com/southernstorm/noise/protocol/AESGCMFallbackCipherState.java index dd7eaea..22ccb17 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 || plaintextOffset + length < plaintextOffset || plaintextOffset + length > plaintext.length) + if (length < 0 || plaintextOffset < 0 || plaintextOffset > plaintext.length || length > plaintext.length || (plaintext.length - plaintextOffset) < 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 || plaintextOffset + length < plaintextOffset || plaintextOffset + length > plaintext.length) + if (length < 0 || plaintextOffset < 0 || plaintextOffset > plaintext.length || length > plaintext.length || (plaintext.length - plaintextOffset) < 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 6b137cd..9b5c2d0 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 || plaintextOffset + length < plaintextOffset || plaintextOffset + length > plaintext.length) + if (length < 0 || plaintextOffset < 0 || plaintextOffset > plaintext.length || length > plaintext.length || (plaintext.length - plaintextOffset) < 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 || plaintextOffset + length < plaintextOffset || plaintextOffset + length > plaintext.length) + if (length < 0 || plaintextOffset < 0 || plaintextOffset > plaintext.length || length > plaintext.length || (plaintext.length - plaintextOffset) < 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 8ab014e..afa52b1 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 || plaintextOffset + length < plaintextOffset || plaintextOffset + length > plaintext.length) + if (length < 0 || plaintextOffset < 0 || plaintextOffset > plaintext.length || length > plaintext.length || (plaintext.length - plaintextOffset) < 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 || plaintextOffset + length < plaintextOffset || plaintextOffset + length > plaintext.length) + if (length < 0 || plaintextOffset < 0 || plaintextOffset > plaintext.length || length > plaintext.length || (plaintext.length - plaintextOffset) < length) throw new IllegalArgumentException(); space = plaintext.length - plaintextOffset; if (!haskey) {