Skip to content

Commit 578880c

Browse files
committed
Document message length checks
1 parent 9fb60ad commit 578880c

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

src/main/java/com/eatthepath/noise/NoiseHandshake.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,8 @@ static int getPayloadLength(final HandshakePattern handshakePattern,
639639
*
640640
* @return a new byte array containing the resulting handshake message
641641
*
642+
* @throws IllegalArgumentException if the message produced for the given payload would be larger than the maximum
643+
* allowed Noise handshake message size
642644
* @throws IllegalStateException if this handshake is not currently expecting to send a handshake message to its peer
643645
*
644646
* @see #isExpectingWrite()
@@ -679,6 +681,8 @@ public byte[] writeMessage(@Nullable final byte[] payload) {
679681
*
680682
* @return the number of bytes written to {@code message}
681683
*
684+
* @throws IllegalArgumentException if the message produced for the given payload would be larger than the maximum
685+
* allowed Noise handshake message size
682686
* @throws IllegalStateException if this handshake is not currently expecting to send a handshake message to its peer
683687
* @throws ShortBufferException if {@code message} is not large enough (after its offset) to hold the handshake
684688
* message
@@ -771,6 +775,8 @@ public int writeMessage(@Nullable final byte[] payload,
771775
*
772776
* @return a new byte buffer containing the resulting handshake message
773777
*
778+
* @throws IllegalArgumentException if the message produced for the given payload would be larger than the maximum
779+
* allowed Noise handshake message size
774780
* @throws IllegalStateException if this handshake is not currently expecting to send a handshake message to its peer
775781
*
776782
* @see #isExpectingWrite()
@@ -811,6 +817,8 @@ public ByteBuffer writeMessage(@Nullable final ByteBuffer payload) {
811817
*
812818
* @return the number of bytes written to {@code message}
813819
*
820+
* @throws IllegalArgumentException if the message produced for the given payload would be larger than the maximum
821+
* allowed Noise handshake message size
814822
* @throws IllegalStateException if this handshake is not currently expecting to send a handshake message to its peer
815823
* @throws ShortBufferException if {@code message} does not have enough remaining capacity to hold the handshake
816824
* message
@@ -899,7 +907,8 @@ private void checkOutboundMessageSize(final int payloadLength) {
899907
*
900908
* @throws AEADBadTagException if the AEAD tag for any encrypted component of the given handshake message does not
901909
* match the calculated value
902-
* @throws IllegalArgumentException if the given message is too short to contain the expected handshake message
910+
* @throws IllegalArgumentException if the given message is too short to contain the expected handshake message or if
911+
* the given message is larger than the maximum allowed Noise handshake message size
903912
*/
904913
public byte[] readMessage(final byte[] message) throws AEADBadTagException {
905914
checkInboundMessageSize(message.length);
@@ -934,8 +943,9 @@ public byte[] readMessage(final byte[] message) throws AEADBadTagException {
934943
* match the calculated value
935944
* @throws ShortBufferException if {@code payload} is too short (after its offset) to hold the plaintext of the
936945
* payload included in the given handshake message
937-
* @throws IllegalArgumentException if the given message is too short to contain the expected handshake message
938-
*
946+
* @throws IllegalArgumentException if the given message is too short to contain the expected handshake message or if
947+
* the given message is larger than the maximum allowed Noise handshake message size
948+
*
939949
* @see #getPayloadLength(int)
940950
*/
941951
public int readMessage(final byte[] message,
@@ -1013,7 +1023,8 @@ public int readMessage(final byte[] message,
10131023
*
10141024
* @throws AEADBadTagException if the AEAD tag for any encrypted component of the given handshake message does not
10151025
* match the calculated value
1016-
* @throws IllegalArgumentException if the given message is too short to contain the expected handshake message
1026+
* @throws IllegalArgumentException if the given message is too short to contain the expected handshake message or if
1027+
* the given message is larger than the maximum allowed Noise handshake message size
10171028
*/
10181029
public ByteBuffer readMessage(final ByteBuffer message) throws AEADBadTagException {
10191030
checkInboundMessageSize(message.remaining());
@@ -1049,7 +1060,8 @@ public ByteBuffer readMessage(final ByteBuffer message) throws AEADBadTagExcepti
10491060
* match the calculated value
10501061
* @throws ShortBufferException if {@code payload} does not have enough remaining capacity to hold the plaintext of
10511062
* the payload included in the given handshake message
1052-
* @throws IllegalArgumentException if the given message is too short to contain the expected handshake message
1063+
* @throws IllegalArgumentException if the given message is too short to contain the expected handshake message or if
1064+
* the given message is larger than the maximum allowed Noise handshake message size
10531065
*
10541066
* @see #getPayloadLength(int)
10551067
*/

src/main/java/com/eatthepath/noise/NoiseTransportReader.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public interface NoiseTransportReader {
3838
* @return a {@code ByteBuffer} containing the resulting plaintext
3939
*
4040
* @throws AEADBadTagException if the AEAD tag in the given ciphertext does not match the calculated value
41-
* @throws IllegalArgumentException if the given ciphertext is too short to contain a valid AEAD tag
41+
* @throws IllegalArgumentException if the given ciphertext is too short to contain a valid AEAD tag or if it is
42+
* larger than the maximum allowed Noise transport message size
4243
*
4344
* @see #getPlaintextLength(int)
4445
*/
@@ -60,7 +61,8 @@ public interface NoiseTransportReader {
6061
* @return the number of bytes written into the {@code plaintext} buffer
6162
*
6263
* @throws AEADBadTagException if the AEAD tag in the given ciphertext does not match the calculated value
63-
* @throws IllegalArgumentException if the given ciphertext is too short to contain a valid AEAD tag
64+
* @throws IllegalArgumentException if the given ciphertext is too short to contain a valid AEAD tag or if it is
65+
* larger than the maximum allowed Noise transport message size
6466
* @throws ShortBufferException if the given plaintext buffer does not have enough remaining capacity to hold the
6567
* resulting plaintext
6668
*
@@ -76,7 +78,8 @@ public interface NoiseTransportReader {
7678
* @return a new byte array containing the resulting plaintext
7779
*
7880
* @throws AEADBadTagException if the AEAD tag in the given ciphertext does not match the calculated AEAD tag
79-
* @throws IllegalArgumentException if the given ciphertext is too small to contain a valid AEAD tag
81+
* @throws IllegalArgumentException if the given ciphertext is too short to contain a valid AEAD tag or if it is
82+
* larger than the maximum allowed Noise transport message size
8083
*/
8184
byte[] readMessage(final byte[] ciphertext) throws AEADBadTagException;
8285

@@ -96,7 +99,8 @@ public interface NoiseTransportReader {
9699
* @throws AEADBadTagException if the AEAD tag in the given ciphertext does not match the calculated value
97100
* @throws ShortBufferException if {@code plaintext} is not long enough (after its offset) to contain the resulting
98101
* plaintext
99-
* @throws IllegalArgumentException if the given ciphertext is too short to contain a valid AEAD tag
102+
* @throws IllegalArgumentException if the given ciphertext is too short to contain a valid AEAD tag or if it is
103+
* larger than the maximum allowed Noise transport message size
100104
*/
101105
int readMessage(final byte[] ciphertext,
102106
final int ciphertextOffset,

src/main/java/com/eatthepath/noise/NoiseTransportWriter.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public interface NoiseTransportWriter {
3333
*
3434
* @return a new byte buffer containing the resulting ciphertext and AEAD tag
3535
*
36+
* @throws IllegalArgumentException if the ciphertext for the given plaintext would be larger than the maximum allowed
37+
* Noise transport message size
38+
*
3639
* @see #getCiphertextLength(int)
3740
*/
3841
ByteBuffer writeMessage(final ByteBuffer plaintext);
@@ -54,6 +57,8 @@ public interface NoiseTransportWriter {
5457
*
5558
* @return the number of bytes written into the ciphertext buffer
5659
*
60+
* @throws IllegalArgumentException if the ciphertext for the given plaintext would be larger than the maximum allowed
61+
* Noise transport message size
5762
* @throws ShortBufferException if the given ciphertext buffer does not have enough remaining capacity to hold the
5863
* resulting ciphertext and AEAD tag
5964
*
@@ -67,6 +72,9 @@ public interface NoiseTransportWriter {
6772
* @param plaintext the plaintext to encrypt
6873
*
6974
* @return a new byte array containing the resulting ciphertext
75+
*
76+
* @throws IllegalArgumentException if the ciphertext for the given plaintext would be larger than the maximum allowed
77+
* Noise transport message size
7078
*/
7179
byte[] writeMessage(final byte[] plaintext);
7280

@@ -89,6 +97,8 @@ public interface NoiseTransportWriter {
8997
* ciphertext and AEAD tag
9098
* @throws IndexOutOfBoundsException if the given plaintext length exceeds the length of the plaintext array after its
9199
* offset
100+
* @throws IllegalArgumentException if the ciphertext for the given plaintext would be larger than the maximum allowed
101+
* Noise transport message size
92102
*
93103
* @see #getCiphertextLength(int)
94104
*/

0 commit comments

Comments
 (0)