Skip to content

Commit

Permalink
Streamlined ChachaPolyCipher.update() method
Browse files Browse the repository at this point in the history
  • Loading branch information
exceptionfactory committed Oct 16, 2023
1 parent 243f64c commit 32b4e12
Showing 1 changed file with 7 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,15 @@ public void update(byte[] input, int inputOffset, int inputLen) {
if (!MessageDigest.isEqual(actualPolyTag, expectedPolyTag)) {
throw new SSHRuntimeException("MAC Error");
}
}

try {
cipher.update(input, inputOffset, inputLen, input, inputOffset);
} catch (GeneralSecurityException e) {
throw new SSHRuntimeException("ChaCha20 decryption failed", e);
}
} else {
try {
cipher.update(input, inputOffset, inputLen, input, inputOffset);
} catch (GeneralSecurityException e) {
throw new SSHRuntimeException("ChaCha20 encryption failed", e);
}
try {
cipher.update(input, inputOffset, inputLen, input, inputOffset);
} catch (GeneralSecurityException e) {
throw new SSHRuntimeException("ChaCha20 cipher processing failed", e);
}

if (mode == Mode.Encrypt) {
byte[] macInput = Arrays.copyOf(input, macInputLength);
byte[] polyTag = mac.doFinal(macInput);
System.arraycopy(polyTag, 0, input, macInputLength, POLY_TAG_LENGTH);
Expand Down

0 comments on commit 32b4e12

Please sign in to comment.