Repository navigation
fix(ciphers): eliminate static mutable Cipher and bind IV to ciphertext in AESEncryption #7767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
e501196
e96e34e
89cecdb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,8 +2,11 @@ | |
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.util.Arrays; | ||
| import javax.crypto.AEADBadTagException; | ||
| import javax.crypto.SecretKey; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
|
|
@@ -59,4 +62,64 @@ public void testBytesToHex() { | |
| String hex = AESEncryption.bytesToHex(bytes); | ||
| assertEquals("00010F10FF", hex, "Hex representation should match the expected value"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testMultipleEncryptionsAndDecryptions() throws Exception { | ||
| SecretKey key = AESEncryption.getSecretEncryptionKey(); | ||
| byte[] cipher1 = AESEncryption.encryptText("First Message", key); | ||
| byte[] cipher2 = AESEncryption.encryptText("Second Message", key); | ||
|
|
||
| assertEquals("First Message", AESEncryption.decryptText(cipher1, key)); | ||
| assertEquals("Second Message", AESEncryption.decryptText(cipher2, key)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testShortCiphertextThrowsException() throws Exception { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a test at the 27-byte rejection boundary and a successful 28-byte empty-plaintext round trip. Also add authentication tests that verify that decryption rejects modified ciphertext and a different key.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in |
||
| SecretKey key = AESEncryption.getSecretEncryptionKey(); | ||
| assertThrows(IllegalArgumentException.class, () -> AESEncryption.decryptText(null, key)); | ||
| assertThrows(IllegalArgumentException.class, () -> AESEncryption.decryptText(new byte[27], key)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEmptyPlaintextRoundTrip() throws Exception { | ||
| SecretKey key = AESEncryption.getSecretEncryptionKey(); | ||
| byte[] cipherText = AESEncryption.encryptText("", key); | ||
|
|
||
| assertEquals(28, cipherText.length, "Empty plaintext should produce 12-byte IV + 16-byte tag"); | ||
| assertEquals("", AESEncryption.decryptText(cipherText, key)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFreshRandomIvPerEncryption() throws Exception { | ||
| SecretKey key = AESEncryption.getSecretEncryptionKey(); | ||
| byte[] cipher1 = AESEncryption.encryptText("Same Message", key); | ||
| byte[] cipher2 = AESEncryption.encryptText("Same Message", key); | ||
|
|
||
| assertTrue(!Arrays.equals(cipher1, cipher2), "Each encryption should use a fresh random IV"); | ||
| assertEquals("Same Message", AESEncryption.decryptText(cipher1, key)); | ||
| assertEquals("Same Message", AESEncryption.decryptText(cipher2, key)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testModifiedCiphertextFailsAuthentication() throws Exception { | ||
| SecretKey key = AESEncryption.getSecretEncryptionKey(); | ||
| byte[] cipherText = AESEncryption.encryptText("Authenticated Message", key); | ||
|
|
||
| byte[] modifiedCiphertext = cipherText.clone(); | ||
| modifiedCiphertext[12] ^= 0x01; | ||
| assertThrows(AEADBadTagException.class, () -> AESEncryption.decryptText(modifiedCiphertext, key)); | ||
|
|
||
| byte[] modifiedTag = cipherText.clone(); | ||
| modifiedTag[modifiedTag.length - 1] ^= 0x01; | ||
| assertThrows(AEADBadTagException.class, () -> AESEncryption.decryptText(modifiedTag, key)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDifferentKeyFailsAuthentication() throws Exception { | ||
| SecretKey key1 = AESEncryption.getSecretEncryptionKey(); | ||
| SecretKey key2 = AESEncryption.getSecretEncryptionKey(); | ||
| byte[] cipherText = AESEncryption.encryptText("Secret Message", key1); | ||
|
|
||
| assertThrows(AEADBadTagException.class, () -> AESEncryption.decryptText(cipherText, key2)); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please document
IV || ciphertext || authentication tagin the Javadoc for bothencryptTextanddecryptText, including the 12-byte IV and 16-byte tag, so callers know what bytes to store and pass back.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in
e96e34e5. Updated the Javadoc for bothencryptTextanddecryptTextto explicitly document theIV || ciphertext || authentication taglayout, including the 12-byte IV, 16-byte (128-bit) tag, and 28-byte minimum payload length.