Skip to content

Commit

Permalink
Added test for ChachaPolyCipher without AAD
Browse files Browse the repository at this point in the history
  • Loading branch information
exceptionfactory committed Oct 16, 2023
1 parent 7f852d9 commit 243f64c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void update(byte[] input, int inputOffset, int inputLen) {
}
} else {
try {
cipher.update(input, AAD_LENGTH, inputLen, input, AAD_LENGTH);
cipher.update(input, inputOffset, inputLen, input, inputOffset);
} catch (GeneralSecurityException e) {
throw new SSHRuntimeException("ChaCha20 encryption failed", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,28 @@ public void testEncryptDecrypt() {
}
}

@Test
public void testEncryptDecryptWithoutAAD() {
final Cipher encryptionCipher = FACTORY.create();
final byte[] key = new byte[encryptionCipher.getBlockSize()];
Arrays.fill(key, (byte) 1);
encryptionCipher.init(Cipher.Mode.Encrypt, key, new byte[0]);

final byte[] plaintextBytes = PLAINTEXT.getBytes(StandardCharsets.UTF_8);
final byte[] message = new byte[plaintextBytes.length + POLY_TAG_LENGTH];
System.arraycopy(plaintextBytes, 0, message, 0, plaintextBytes.length);

encryptionCipher.update(message, 0, plaintextBytes.length);

final Cipher decryptionCipher = FACTORY.create();
decryptionCipher.init(Cipher.Mode.Decrypt, key, new byte[0]);
decryptionCipher.update(message, 0, plaintextBytes.length);

final byte[] decrypted = Arrays.copyOfRange(message, 0, plaintextBytes.length);
final String decoded = new String(decrypted, StandardCharsets.UTF_8);
assertEquals(PLAINTEXT, decoded);
}

@Test
public void testCheckOnUpdateParameters() {
Cipher cipher = FACTORY.create();
Expand Down

0 comments on commit 243f64c

Please sign in to comment.