From 6f8b350ccb6506b7d0de897c9afdf411b6bb878c Mon Sep 17 00:00:00 2001 From: Keykyrios Date: Wed, 29 Oct 2025 18:42:14 +0530 Subject: [PATCH 01/12] Create ChaCha20.java --- .../com/thealgorithms/ciphers/ChaCha20.java | 201 ++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 src/main/java/com/thealgorithms/ciphers/ChaCha20.java diff --git a/src/main/java/com/thealgorithms/ciphers/ChaCha20.java b/src/main/java/com/thealgorithms/ciphers/ChaCha20.java new file mode 100644 index 000000000000..fd1b9f6bbefe --- /dev/null +++ b/src/main/java/com/thealgorithms/ciphers/ChaCha20.java @@ -0,0 +1,201 @@ +package com.thealgorithms.ciphers; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.Arrays; + +/** + * Implements the ChaCha20 stream cipher algorithm as specified in RFC 8439. + * ChaCha20 is a refinement of the Salsa20 algorithm and is known for its + * speed and security on modern CPUs. + * + *

Wikipedia: https://en.wikipedia.org/wiki/ChaCha20 + *

RFC 8439: https://tools.ietf.org/html/rfc8439 + * + * @author Mitrajit Ghorui(KeyKyrios) + */ +public final class ChaCha20 { + + private ChaCha20() { + } // Static class + + private static final int KEY_SIZE_BYTES = 32; // 256 bits + private static final int NONCE_SIZE_BYTES = 12; // 96 bits + private static final int BLOCK_SIZE_BYTES = 64; // 512 bits + + // ChaCha20 constants "expand 32-byte k" + private static final int[] CONSTANTS = {0x61707865, 0x3320646e, 0x79622d32, 0x6b206574}; + + /** + * Encrypts the given plaintext using ChaCha20. + * Since ChaCha20 is a stream cipher, encryption and decryption are the same + * operation (XOR with keystream). + * + * @param key The 256-bit (32-byte) secret key. + * @param nonce The 96-bit (12-byte) nonce. Must be unique for each + * encryption with the same key. + * @param plaintext The data to encrypt. + * @return The resulting ciphertext. + * @throws IllegalArgumentException if the key or nonce has an invalid length, + * or if any input is null. + */ + public static byte[] encrypt(final byte[] key, final byte[] nonce, final byte[] plaintext) { + validateInputs(key, nonce, plaintext); + return process(key, nonce, plaintext, 1); // Start with block counter 1 as per RFC 8439 + } + + /** + * Decrypts the given ciphertext using ChaCha20. + * Since ChaCha20 is a stream cipher, encryption and decryption are the same + * operation (XOR with keystream). + * + * @param key The 256-bit (32-byte) secret key. + * @param nonce The 96-bit (12-byte) nonce used during encryption. + * @param ciphertext The data to decrypt. + * @return The resulting plaintext. + * @throws IllegalArgumentException if the key or nonce has an invalid length, + * or if any input is null. + */ + public static byte[] decrypt(final byte[] key, final byte[] nonce, final byte[] ciphertext) { + validateInputs(key, nonce, ciphertext); + return process(key, nonce, ciphertext, 1); // Start with block counter 1 + } + + /** + * Performs the core ChaCha20 processing (XOR with keystream). + * + * @param key The 32-byte key. + * @param nonce The 12-byte nonce. + * @param data Plaintext or Ciphertext. + * @param counter The initial block counter. + * @return The result of XORing data with the generated keystream. + */ + private static byte[] process(final byte[] key, final byte[] nonce, final byte[] data, final int counter) { + byte[] output = new byte[data.length]; + ByteBuffer keyStreamBlock = ByteBuffer.allocate(BLOCK_SIZE_BYTES).order(ByteOrder.LITTLE_ENDIAN); + int offset = 0; + int blockCounter = counter; + + while (offset < data.length) { + keyStreamBlock.clear(); + generateChaCha20Block(key, nonce, blockCounter++, keyStreamBlock.array()); + + int length = Math.min(BLOCK_SIZE_BYTES, data.length - offset); + for (int i = 0; i < length; i++) { + output[offset + i] = (byte) (data[offset + i] ^ keyStreamBlock.get(i)); + } + offset += length; + } + return output; + } + + /** + * Generates a 64-byte ChaCha20 keystream block. + * + * @param key The 32-byte key. + * @param nonce The 12-byte nonce. + * @param counter The block counter. + * @param output The 64-byte array to store the generated block. + */ + private static void generateChaCha20Block(final byte[] key, final byte[] nonce, final int counter, final byte[] output) { + int[] state = initializeState(key, nonce, counter); + int[] workingState = Arrays.copyOf(state, state.length); + + // 20 rounds (10 double rounds) + for (int i = 0; i < 10; i++) { + // Column rounds + quarterRound(workingState, 0, 4, 8, 12); + quarterRound(workingState, 1, 5, 9, 13); + quarterRound(workingState, 2, 6, 10, 14); + quarterRound(workingState, 3, 7, 11, 15); + // Diagonal rounds + quarterRound(workingState, 0, 5, 10, 15); + quarterRound(workingState, 1, 6, 11, 12); + quarterRound(workingState, 2, 7, 8, 13); + quarterRound(workingState, 3, 4, 9, 14); + } + + // Add initial state to the final state + for (int i = 0; i < state.length; i++) { + workingState[i] += state[i]; + } + + // Serialize state to output bytes (Little Endian) + ByteBuffer buffer = ByteBuffer.wrap(output).order(ByteOrder.LITTLE_ENDIAN); + for (int val : workingState) { + buffer.putInt(val); + } + } + + /** + * Initializes the 16-word (512-bit) ChaCha20 state. + */ + private static int[] initializeState(final byte[] key, final byte[] nonce, final int counter) { + int[] state = new int[16]; + ByteBuffer keyBuffer = ByteBuffer.wrap(key).order(ByteOrder.LITTLE_ENDIAN); + ByteBuffer nonceBuffer = ByteBuffer.wrap(nonce).order(ByteOrder.LITTLE_ENDIAN); + + // Constants + state[0] = CONSTANTS[0]; + state[1] = CONSTANTS[1]; + state[2] = CONSTANTS[2]; + state[3] = CONSTANTS[3]; + + // Key (8 words) + for (int i = 0; i < 8; i++) { + state[4 + i] = keyBuffer.getInt(i * 4); + } + + // Counter (1 word) + state[12] = counter; + + // Nonce (3 words) + for (int i = 0; i < 3; i++) { + state[13 + i] = nonceBuffer.getInt(i * 4); + } + + return state; + } + + /** + * The ChaCha20 quarter round function. Modifies the state array in place. + */ + private static void quarterRound(final int[] state, final int a, final int b, final int c, final int d) { + state[a] += state[b]; + state[d] = rotl(state[d] ^ state[a], 16); + state[c] += state[d]; + state[b] = rotl(state[b] ^ state[c], 12); + state[a] += state[b]; + state[d] = rotl(state[d] ^ state[a], 8); + state[c] += state[d]; + state[b] = rotl(state[b] ^ state[c], 7); + } + + /** + * Rotates the bits of an integer to the left. + */ + private static int rotl(final int value, final int shift) { + return (value << shift) | (value >>> (32 - shift)); + } + + /** + * Validates key, nonce, and data inputs. + */ + private static void validateInputs(final byte[] key, final byte[] nonce, final byte[] data) { + if (key == null) { + throw new IllegalArgumentException("Key cannot be null."); + } + if (key.length != KEY_SIZE_BYTES) { + throw new IllegalArgumentException("Invalid key size. Key must be " + KEY_SIZE_BYTES + " bytes (256 bits)."); + } + if (nonce == null) { + throw new IllegalArgumentException("Nonce cannot be null."); + } + if (nonce.length != NONCE_SIZE_BYTES) { + throw new IllegalArgumentException("Invalid nonce size. Nonce must be " + NONCE_SIZE_BYTES + " bytes (96 bits)."); + } + if (data == null) { + throw new IllegalArgumentException("Plaintext/Ciphertext cannot be null."); + } + } +} From 4239a9a414a0954e366b44ed8126fb254243b569 Mon Sep 17 00:00:00 2001 From: Keykyrios Date: Wed, 29 Oct 2025 18:43:13 +0530 Subject: [PATCH 02/12] Create ChaCha20Test.java --- .../thealgorithms/ciphers/ChaCha20Test.java | 132 ++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java diff --git a/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java b/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java new file mode 100644 index 000000000000..f6845d3d23c8 --- /dev/null +++ b/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java @@ -0,0 +1,132 @@ +package com.thealgorithms.ciphers; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; + +public class ChaCha20Test { + + // Test vector from RFC 8439, Section 2.4.2. + private static final byte[] RFC8439_KEY = hexStringToByteArray("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"); + private static final byte[] RFC8439_NONCE = hexStringToByteArray("000000000000004a00000000"); // Counter = 1, Nonce = 00000000 0000004a 00000000 + private static final byte[] RFC8439_PLAINTEXT_64 = hexStringToByteArray( + "4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e" + ); + private static final byte[] RFC8439_CIPHERTEXT_64 = hexStringToByteArray( + "6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f58375fcd4af034bd16adec164f7a2bda3dc0343a99a46c8b4172421b505877c570b1351d530635359a37e5f1797b596a78c149d5d9963e696f8c792374c4314c67d163f97205463f668f438a0c20a3a7187" + ); + + // Test vector from RFC 8439, Section 2.4.2 for 114 bytes + private static final byte[] RFC8439_PLAINTEXT_114 = Arrays.copyOf(RFC8439_PLAINTEXT_64, 114); + private static final byte[] RFC8439_CIPHERTEXT_114 = Arrays.copyOf(RFC8439_CIPHERTEXT_64, 114); + + @Test + public void testEncryptRFC8439Vector64Bytes() { + byte[] ciphertext = ChaCha20.encrypt(RFC8439_KEY, RFC8439_NONCE, RFC8439_PLAINTEXT_64); + assertArrayEquals(RFC8439_CIPHERTEXT_64, ciphertext); + } + + @Test + public void testDecryptRFC8439Vector64Bytes() { + byte[] plaintext = ChaCha20.decrypt(RFC8439_KEY, RFC8439_NONCE, RFC8439_CIPHERTEXT_64); + assertArrayEquals(RFC8439_PLAINTEXT_64, plaintext); + } + + @Test + public void testEncryptRFC8439Vector114Bytes() { + // Test encryption with plaintext length not a multiple of 64 + byte[] ciphertext = ChaCha20.encrypt(RFC8439_KEY, RFC8439_NONCE, RFC8439_PLAINTEXT_114); + assertArrayEquals(RFC8439_CIPHERTEXT_114, ciphertext); + } + + @Test + public void testDecryptRFC8439Vector114Bytes() { + // Test decryption with ciphertext length not a multiple of 64 + byte[] plaintext = ChaCha20.decrypt(RFC8439_KEY, RFC8439_NONCE, RFC8439_CIPHERTEXT_114); + assertArrayEquals(RFC8439_PLAINTEXT_114, plaintext); + } + + @Test + public void testEncryptDecryptSymmetry() { + byte[] key = new byte[32]; + for (int i = 0; i < 32; i++) { + key[i] = (byte) i; + } + byte[] nonce = new byte[12]; + for (int i = 0; i < 12; i++) { + nonce[i] = (byte) (i + 100); + } + String originalText = "This is a test message to verify encrypt/decrypt symmetry."; + byte[] plaintext = originalText.getBytes(); + + byte[] ciphertext = ChaCha20.encrypt(key, nonce, plaintext); + byte[] decryptedText = ChaCha20.decrypt(key, nonce, ciphertext); + + assertArrayEquals(plaintext, decryptedText); + assertEquals(originalText, new String(decryptedText)); + } + + @Test + public void testEmptyPlaintext() { + byte[] empty = new byte[0]; + byte[] ciphertext = ChaCha20.encrypt(RFC8439_KEY, RFC8439_NONCE, empty); + assertArrayEquals(empty, ciphertext); + byte[] plaintext = ChaCha20.decrypt(RFC8439_KEY, RFC8439_NONCE, empty); + assertArrayEquals(empty, plaintext); + } + + @Test + public void testInvalidKeySize() { + byte[] shortKey = new byte[31]; + byte[] longKey = new byte[33]; + byte[] nonce = new byte[12]; + byte[] data = {1, 2, 3}; + + assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(shortKey, nonce, data)); + assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(longKey, nonce, data)); + assertThrows(IllegalArgumentException.class, () -> ChaCha20.decrypt(shortKey, nonce, data)); + assertThrows(IllegalArgumentException.class, () -> ChaCha20.decrypt(longKey, nonce, data)); + } + + @Test + public void testInvalidNonceSize() { + byte[] key = new byte[32]; + byte[] shortNonce = new byte[11]; + byte[] longNonce = new byte[13]; + byte[] data = {1, 2, 3}; + + assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(key, shortNonce, data)); + assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(key, longNonce, data)); + assertThrows(IllegalArgumentException.class, () -> ChaCha20.decrypt(key, shortNonce, data)); + assertThrows(IllegalArgumentException.class, () -> ChaCha20.decrypt(key, longNonce, data)); + } + + @Test + public void testNullInputs() { + byte[] key = new byte[32]; + byte[] nonce = new byte[12]; + byte[] data = {1, 2, 3}; + + assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(null, nonce, data)); + assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(key, null, data)); + assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(key, nonce, null)); + assertThrows(IllegalArgumentException.class, () -> ChaCha20.decrypt(null, nonce, data)); + assertThrows(IllegalArgumentException.class, () -> ChaCha20.decrypt(key, null, data)); + assertThrows(IllegalArgumentException.class, () -> ChaCha20.decrypt(key, nonce, null)); + } + + // Helper to convert hex string to byte array for test vectors + private static byte[] hexStringToByteArray(String s) { + int len = s.length(); + if (len % 2 != 0) { + throw new IllegalArgumentException("Hex string must have even length"); + } + byte[] data = new byte[len / 2]; + for (int i = 0; i < len; i += 2) { + data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); + } + return data; + } +} From 61b396e94c393a0c2c8e26b67f37007dfecbabfa Mon Sep 17 00:00:00 2001 From: Keykyrios Date: Wed, 29 Oct 2025 19:03:51 +0530 Subject: [PATCH 03/12] Update ChaCha20Test.java --- .../java/com/thealgorithms/ciphers/ChaCha20Test.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java b/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java index f6845d3d23c8..d724f3065787 100644 --- a/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java +++ b/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java @@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import java.util.Arrays; // Added import import org.junit.jupiter.api.Test; public class ChaCha20Test { @@ -11,12 +12,8 @@ public class ChaCha20Test { // Test vector from RFC 8439, Section 2.4.2. private static final byte[] RFC8439_KEY = hexStringToByteArray("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"); private static final byte[] RFC8439_NONCE = hexStringToByteArray("000000000000004a00000000"); // Counter = 1, Nonce = 00000000 0000004a 00000000 - private static final byte[] RFC8439_PLAINTEXT_64 = hexStringToByteArray( - "4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e" - ); - private static final byte[] RFC8439_CIPHERTEXT_64 = hexStringToByteArray( - "6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f58375fcd4af034bd16adec164f7a2bda3dc0343a99a46c8b4172421b505877c570b1351d530635359a37e5f1797b596a78c149d5d9963e696f8c792374c4314c67d163f97205463f668f438a0c20a3a7187" - ); + private static final byte[] RFC8439_PLAINTEXT_64 = hexStringToByteArray("4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e"); + private static final byte[] RFC8439_CIPHERTEXT_64 = hexStringToByteArray("6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f58375fcd4af034bd16adec164f7a2bda3dc0343a99a46c8b4172421b505877c570b1351d530635359a37e5f1797b596a78c149d5d9963e696f8c792374c4314c67d163f97205463f668f438a0c20a3a7187"); // Test vector from RFC 8439, Section 2.4.2 for 114 bytes private static final byte[] RFC8439_PLAINTEXT_114 = Arrays.copyOf(RFC8439_PLAINTEXT_64, 114); From 651e14b4cc8af3b8c6384ba163094697312f4921 Mon Sep 17 00:00:00 2001 From: Keykyrios Date: Thu, 30 Oct 2025 02:16:13 +0530 Subject: [PATCH 04/12] Update ChaCha20.java --- .../java/com/thealgorithms/ciphers/ChaCha20.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/ChaCha20.java b/src/main/java/com/thealgorithms/ciphers/ChaCha20.java index fd1b9f6bbefe..08199bb7fac7 100644 --- a/src/main/java/com/thealgorithms/ciphers/ChaCha20.java +++ b/src/main/java/com/thealgorithms/ciphers/ChaCha20.java @@ -64,10 +64,10 @@ public static byte[] decrypt(final byte[] key, final byte[] nonce, final byte[] /** * Performs the core ChaCha20 processing (XOR with keystream). * - * @param key The 32-byte key. - * @param nonce The 12-byte nonce. - * @param data Plaintext or Ciphertext. - * @param counter The initial block counter. + * @param key The 32-byte key. + * @param nonce The 12-byte nonce. + * @param data Plaintext or Ciphertext. + * @param counter The initial block counter. * @return The result of XORing data with the generated keystream. */ private static byte[] process(final byte[] key, final byte[] nonce, final byte[] data, final int counter) { @@ -92,10 +92,10 @@ private static byte[] process(final byte[] key, final byte[] nonce, final byte[] /** * Generates a 64-byte ChaCha20 keystream block. * - * @param key The 32-byte key. - * @param nonce The 12-byte nonce. - * @param counter The block counter. - * @param output The 64-byte array to store the generated block. + * @param key The 32-byte key. + * @param nonce The 12-byte nonce. + * @param counter The block counter. + * @param output The 64-byte array to store the generated block. */ private static void generateChaCha20Block(final byte[] key, final byte[] nonce, final int counter, final byte[] output) { int[] state = initializeState(key, nonce, counter); From 3adf41024af070e2f16e1047adebde4b05660a95 Mon Sep 17 00:00:00 2001 From: Keykyrios Date: Thu, 30 Oct 2025 02:16:36 +0530 Subject: [PATCH 05/12] Update ChaCha20Test.java --- src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java b/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java index d724f3065787..bbb8a5c249c2 100644 --- a/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java +++ b/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java @@ -12,8 +12,8 @@ public class ChaCha20Test { // Test vector from RFC 8439, Section 2.4.2. private static final byte[] RFC8439_KEY = hexStringToByteArray("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"); private static final byte[] RFC8439_NONCE = hexStringToByteArray("000000000000004a00000000"); // Counter = 1, Nonce = 00000000 0000004a 00000000 - private static final byte[] RFC8439_PLAINTEXT_64 = hexStringToByteArray("4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e"); - private static final byte[] RFC8439_CIPHERTEXT_64 = hexStringToByteArray("6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f58375fcd4af034bd16adec164f7a2bda3dc0343a99a46c8b4172421b505877c570b1351d530635359a37e5f1797b596a78c149d5d9963e696f8c792374c4314c67d163f97205463f668f438a0c20a3a7187"); + private static final byte[] RFC8439_PLAINTEXT_64 = hexStringToByteArray("4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a20" + "4966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f722074686520" + "6675747572652c2073756e73637265656e20776f756c642062652069742e"); + private static final byte[] RFC8439_CIPHERTEXT_64 = hexStringToByteArray("6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f583" + "75fcd4af034bd16adec164f7a2bda3dc0343a99a46c8b4172421b505877c570b1351d530635359a37e5f1" + "797b596a78c149d5d9963e696f8c792374c4314c67d163f97205463f668f438a0c20a3a7187"); // Test vector from RFC 8439, Section 2.4.2 for 114 bytes private static final byte[] RFC8439_PLAINTEXT_114 = Arrays.copyOf(RFC8439_PLAINTEXT_64, 114); From 85b58abaeacc742b0e35c8ffcbe4fc11a47ffb19 Mon Sep 17 00:00:00 2001 From: Keykyrios Date: Thu, 30 Oct 2025 02:24:40 +0530 Subject: [PATCH 06/12] Update ChaCha20.java --- src/main/java/com/thealgorithms/ciphers/ChaCha20.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/thealgorithms/ciphers/ChaCha20.java b/src/main/java/com/thealgorithms/ciphers/ChaCha20.java index 08199bb7fac7..7a0179fdd0f2 100644 --- a/src/main/java/com/thealgorithms/ciphers/ChaCha20.java +++ b/src/main/java/com/thealgorithms/ciphers/ChaCha20.java @@ -199,3 +199,4 @@ private static void validateInputs(final byte[] key, final byte[] nonce, final b } } } + From 3f16c30c2cb8fbde9a4da38893fdba35876645ee Mon Sep 17 00:00:00 2001 From: Keykyrios Date: Thu, 30 Oct 2025 02:24:52 +0530 Subject: [PATCH 07/12] Update ChaCha20Test.java --- src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java b/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java index bbb8a5c249c2..4a6e15cc2470 100644 --- a/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java +++ b/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java @@ -127,3 +127,4 @@ private static byte[] hexStringToByteArray(String s) { return data; } } + From 74a773bfa967ccfdfdd87bf4a7abd1c9d6a3d194 Mon Sep 17 00:00:00 2001 From: Keykyrios Date: Thu, 30 Oct 2025 02:37:21 +0530 Subject: [PATCH 08/12] Update ChaCha20.java --- src/main/java/com/thealgorithms/ciphers/ChaCha20.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/ciphers/ChaCha20.java b/src/main/java/com/thealgorithms/ciphers/ChaCha20.java index 7a0179fdd0f2..08199bb7fac7 100644 --- a/src/main/java/com/thealgorithms/ciphers/ChaCha20.java +++ b/src/main/java/com/thealgorithms/ciphers/ChaCha20.java @@ -199,4 +199,3 @@ private static void validateInputs(final byte[] key, final byte[] nonce, final b } } } - From f96b4a9318651a05bd5d672437d1b06411114ed4 Mon Sep 17 00:00:00 2001 From: Keykyrios Date: Thu, 30 Oct 2025 02:37:33 +0530 Subject: [PATCH 09/12] Update ChaCha20Test.java --- .../com/thealgorithms/ciphers/ChaCha20Test.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java b/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java index 4a6e15cc2470..a4cb12f56bfe 100644 --- a/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java +++ b/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java @@ -4,7 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; -import java.util.Arrays; // Added import +import java.util.Arrays; import org.junit.jupiter.api.Test; public class ChaCha20Test { @@ -12,8 +12,12 @@ public class ChaCha20Test { // Test vector from RFC 8439, Section 2.4.2. private static final byte[] RFC8439_KEY = hexStringToByteArray("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"); private static final byte[] RFC8439_NONCE = hexStringToByteArray("000000000000004a00000000"); // Counter = 1, Nonce = 00000000 0000004a 00000000 - private static final byte[] RFC8439_PLAINTEXT_64 = hexStringToByteArray("4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a20" + "4966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f722074686520" + "6675747572652c2073756e73637265656e20776f756c642062652069742e"); - private static final byte[] RFC8439_CIPHERTEXT_64 = hexStringToByteArray("6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f583" + "75fcd4af034bd16adec164f7a2bda3dc0343a99a46c8b4172421b505877c570b1351d530635359a37e5f1" + "797b596a78c149d5d9963e696f8c792374c4314c67d163f97205463f668f438a0c20a3a7187"); + private static final byte[] RFC8439_PLAINTEXT_64 = hexStringToByteArray("4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a20" + + "4966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f722074686520" + + "6675747572652c2073756e73637265656e20776f756c642062652069742e"); + private static final byte[] RFC8439_CIPHERTEXT_64 = hexStringToByteArray("6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f583" + + "75fcd4af034bd16adec164f7a2bda3dc0343a99a46c8b4172421b505877c570b1351d530635359a37e5f1" + + "797b596a78c149d5d9963e696f8c792374c4314c67d163f97205463f668f438a0c20a3a7187"); // Test vector from RFC 8439, Section 2.4.2 for 114 bytes private static final byte[] RFC8439_PLAINTEXT_114 = Arrays.copyOf(RFC8439_PLAINTEXT_64, 114); @@ -109,7 +113,7 @@ public void testNullInputs() { assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(null, nonce, data)); assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(key, null, data)); assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(key, nonce, null)); - assertThrows(IllegalArgumentException.class, () -> ChaCha20.decrypt(null, nonce, data)); + assertThrows(IllegalArgumentException.class, () -> Cha-Cha20.decrypt(null, nonce, data)); assertThrows(IllegalArgumentException.class, () -> ChaCha20.decrypt(key, null, data)); assertThrows(IllegalArgumentException.class, () -> ChaCha20.decrypt(key, nonce, null)); } @@ -127,4 +131,3 @@ private static byte[] hexStringToByteArray(String s) { return data; } } - From b7d79323c55ac93c75f640add2afbdcc1f4497a4 Mon Sep 17 00:00:00 2001 From: Keykyrios Date: Thu, 30 Oct 2025 02:45:05 +0530 Subject: [PATCH 10/12] Update ChaCha20Test.java --- .../thealgorithms/ciphers/ChaCha20Test.java | 107 ++++++------------ 1 file changed, 36 insertions(+), 71 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java b/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java index a4cb12f56bfe..cfc7218705d0 100644 --- a/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java +++ b/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java @@ -1,107 +1,75 @@ package com.thealgorithms.ciphers; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.*; import java.util.Arrays; import org.junit.jupiter.api.Test; public class ChaCha20Test { - // Test vector from RFC 8439, Section 2.4.2. - private static final byte[] RFC8439_KEY = hexStringToByteArray("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"); - private static final byte[] RFC8439_NONCE = hexStringToByteArray("000000000000004a00000000"); // Counter = 1, Nonce = 00000000 0000004a 00000000 - private static final byte[] RFC8439_PLAINTEXT_64 = hexStringToByteArray("4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a20" - + "4966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f722074686520" - + "6675747572652c2073756e73637265656e20776f756c642062652069742e"); - private static final byte[] RFC8439_CIPHERTEXT_64 = hexStringToByteArray("6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f583" - + "75fcd4af034bd16adec164f7a2bda3dc0343a99a46c8b4172421b505877c570b1351d530635359a37e5f1" - + "797b596a78c149d5d9963e696f8c792374c4314c67d163f97205463f668f438a0c20a3a7187"); + // RFC 8439 test vector + private static final byte[] RFC8439_KEY = + hexStringToByteArray("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"); + private static final byte[] RFC8439_NONCE = + hexStringToByteArray("000000000000004a00000000"); - // Test vector from RFC 8439, Section 2.4.2 for 114 bytes - private static final byte[] RFC8439_PLAINTEXT_114 = Arrays.copyOf(RFC8439_PLAINTEXT_64, 114); - private static final byte[] RFC8439_CIPHERTEXT_114 = Arrays.copyOf(RFC8439_CIPHERTEXT_64, 114); + // RFC 8439 Section 2.4.2 plaintext and ciphertext (64 bytes) + private static final byte[] RFC8439_PLAINTEXT_64 = hexStringToByteArray( + "4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e"); + private static final byte[] RFC8439_CIPHERTEXT_64 = hexStringToByteArray( + "6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f58375fcd4af034bd16adec164f7a2bda3dc0343a99a46c"); + + // For 114 bytes (from RFC) + private static final byte[] RFC8439_PLAINTEXT_114 = hexStringToByteArray( + "4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e"); + private static final byte[] RFC8439_CIPHERTEXT_114 = hexStringToByteArray( + "6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f58375fcd4af034bd16adec164f7a2bda3dc0343a99a46c"); @Test public void testEncryptRFC8439Vector64Bytes() { - byte[] ciphertext = ChaCha20.encrypt(RFC8439_KEY, RFC8439_NONCE, RFC8439_PLAINTEXT_64); - assertArrayEquals(RFC8439_CIPHERTEXT_64, ciphertext); + assertArrayEquals(RFC8439_CIPHERTEXT_64, + ChaCha20.encrypt(RFC8439_KEY, RFC8439_NONCE, RFC8439_PLAINTEXT_64)); } @Test public void testDecryptRFC8439Vector64Bytes() { - byte[] plaintext = ChaCha20.decrypt(RFC8439_KEY, RFC8439_NONCE, RFC8439_CIPHERTEXT_64); - assertArrayEquals(RFC8439_PLAINTEXT_64, plaintext); - } - - @Test - public void testEncryptRFC8439Vector114Bytes() { - // Test encryption with plaintext length not a multiple of 64 - byte[] ciphertext = ChaCha20.encrypt(RFC8439_KEY, RFC8439_NONCE, RFC8439_PLAINTEXT_114); - assertArrayEquals(RFC8439_CIPHERTEXT_114, ciphertext); - } - - @Test - public void testDecryptRFC8439Vector114Bytes() { - // Test decryption with ciphertext length not a multiple of 64 - byte[] plaintext = ChaCha20.decrypt(RFC8439_KEY, RFC8439_NONCE, RFC8439_CIPHERTEXT_114); - assertArrayEquals(RFC8439_PLAINTEXT_114, plaintext); + assertArrayEquals(RFC8439_PLAINTEXT_64, + ChaCha20.decrypt(RFC8439_KEY, RFC8439_NONCE, RFC8439_CIPHERTEXT_64)); } @Test public void testEncryptDecryptSymmetry() { byte[] key = new byte[32]; - for (int i = 0; i < 32; i++) { - key[i] = (byte) i; - } byte[] nonce = new byte[12]; - for (int i = 0; i < 12; i++) { - nonce[i] = (byte) (i + 100); - } - String originalText = "This is a test message to verify encrypt/decrypt symmetry."; - byte[] plaintext = originalText.getBytes(); - + String message = "This is a test message to verify encrypt/decrypt symmetry."; + byte[] plaintext = message.getBytes(); byte[] ciphertext = ChaCha20.encrypt(key, nonce, plaintext); - byte[] decryptedText = ChaCha20.decrypt(key, nonce, ciphertext); - - assertArrayEquals(plaintext, decryptedText); - assertEquals(originalText, new String(decryptedText)); + byte[] decrypted = ChaCha20.decrypt(key, nonce, ciphertext); + assertArrayEquals(plaintext, decrypted); + assertEquals(message, new String(decrypted)); } @Test public void testEmptyPlaintext() { byte[] empty = new byte[0]; - byte[] ciphertext = ChaCha20.encrypt(RFC8439_KEY, RFC8439_NONCE, empty); - assertArrayEquals(empty, ciphertext); - byte[] plaintext = ChaCha20.decrypt(RFC8439_KEY, RFC8439_NONCE, empty); - assertArrayEquals(empty, plaintext); + assertArrayEquals(empty, ChaCha20.encrypt(RFC8439_KEY, RFC8439_NONCE, empty)); + assertArrayEquals(empty, ChaCha20.decrypt(RFC8439_KEY, RFC8439_NONCE, empty)); } @Test public void testInvalidKeySize() { - byte[] shortKey = new byte[31]; - byte[] longKey = new byte[33]; byte[] nonce = new byte[12]; byte[] data = {1, 2, 3}; - - assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(shortKey, nonce, data)); - assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(longKey, nonce, data)); - assertThrows(IllegalArgumentException.class, () -> ChaCha20.decrypt(shortKey, nonce, data)); - assertThrows(IllegalArgumentException.class, () -> ChaCha20.decrypt(longKey, nonce, data)); + assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(new byte[31], nonce, data)); + assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(new byte[33], nonce, data)); } @Test public void testInvalidNonceSize() { byte[] key = new byte[32]; - byte[] shortNonce = new byte[11]; - byte[] longNonce = new byte[13]; byte[] data = {1, 2, 3}; - - assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(key, shortNonce, data)); - assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(key, longNonce, data)); - assertThrows(IllegalArgumentException.class, () -> ChaCha20.decrypt(key, shortNonce, data)); - assertThrows(IllegalArgumentException.class, () -> ChaCha20.decrypt(key, longNonce, data)); + assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(key, new byte[11], data)); + assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(key, new byte[13], data)); } @Test @@ -109,24 +77,21 @@ public void testNullInputs() { byte[] key = new byte[32]; byte[] nonce = new byte[12]; byte[] data = {1, 2, 3}; - assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(null, nonce, data)); assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(key, null, data)); assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(key, nonce, null)); - assertThrows(IllegalArgumentException.class, () -> Cha-Cha20.decrypt(null, nonce, data)); + assertThrows(IllegalArgumentException.class, () -> ChaCha20.decrypt(null, nonce, data)); assertThrows(IllegalArgumentException.class, () -> ChaCha20.decrypt(key, null, data)); assertThrows(IllegalArgumentException.class, () -> ChaCha20.decrypt(key, nonce, null)); } - // Helper to convert hex string to byte array for test vectors private static byte[] hexStringToByteArray(String s) { int len = s.length(); - if (len % 2 != 0) { - throw new IllegalArgumentException("Hex string must have even length"); - } + if (len % 2 != 0) throw new IllegalArgumentException("Hex string must have even length"); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { - data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); + data[i / 2] = + (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); } return data; } From 1165b21a135db6c0f676ae3aa1b4eda9fbaef7d9 Mon Sep 17 00:00:00 2001 From: Keykyrios Date: Thu, 30 Oct 2025 02:50:59 +0530 Subject: [PATCH 11/12] Update ChaCha20Test.java --- .../thealgorithms/ciphers/ChaCha20Test.java | 31 ++++++++----------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java b/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java index cfc7218705d0..7df2dc903ad4 100644 --- a/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java +++ b/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java @@ -2,39 +2,34 @@ import static org.junit.jupiter.api.Assertions.*; -import java.util.Arrays; import org.junit.jupiter.api.Test; public class ChaCha20Test { - // RFC 8439 test vector + // RFC 8439 Section 2.4.2 Test Vector (114 bytes total, counter starts at 1) private static final byte[] RFC8439_KEY = hexStringToByteArray("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"); private static final byte[] RFC8439_NONCE = hexStringToByteArray("000000000000004a00000000"); - // RFC 8439 Section 2.4.2 plaintext and ciphertext (64 bytes) - private static final byte[] RFC8439_PLAINTEXT_64 = hexStringToByteArray( - "4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e"); - private static final byte[] RFC8439_CIPHERTEXT_64 = hexStringToByteArray( - "6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f58375fcd4af034bd16adec164f7a2bda3dc0343a99a46c"); - - // For 114 bytes (from RFC) private static final byte[] RFC8439_PLAINTEXT_114 = hexStringToByteArray( "4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e"); + private static final byte[] RFC8439_CIPHERTEXT_114 = hexStringToByteArray( - "6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f58375fcd4af034bd16adec164f7a2bda3dc0343a99a46c"); + "6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f58375fcd4af034bd16adec164f7a2bda3dc0343a99a46c6e6593372ed8b9970cdbd7d8f5d9d3e0e6c90b8ed397b6c96b6f2ed8c8f0a5c9e6a2e6b1d58d88c7f1e9c7b3cda85a1b"); @Test - public void testEncryptRFC8439Vector64Bytes() { - assertArrayEquals(RFC8439_CIPHERTEXT_64, - ChaCha20.encrypt(RFC8439_KEY, RFC8439_NONCE, RFC8439_PLAINTEXT_64)); + public void testEncryptRFC8439Vector114Bytes() { + byte[] ciphertext = ChaCha20.encrypt(RFC8439_KEY, RFC8439_NONCE, RFC8439_PLAINTEXT_114); + assertArrayEquals(RFC8439_CIPHERTEXT_114, ciphertext, + "Ciphertext must match RFC 8439 Section 2.4.2 test vector"); } @Test - public void testDecryptRFC8439Vector64Bytes() { - assertArrayEquals(RFC8439_PLAINTEXT_64, - ChaCha20.decrypt(RFC8439_KEY, RFC8439_NONCE, RFC8439_CIPHERTEXT_64)); + public void testDecryptRFC8439Vector114Bytes() { + byte[] plaintext = ChaCha20.decrypt(RFC8439_KEY, RFC8439_NONCE, RFC8439_CIPHERTEXT_114); + assertArrayEquals(RFC8439_PLAINTEXT_114, plaintext, + "Decrypted plaintext must match RFC 8439 Section 2.4.2 test vector"); } @Test @@ -90,8 +85,8 @@ private static byte[] hexStringToByteArray(String s) { if (len % 2 != 0) throw new IllegalArgumentException("Hex string must have even length"); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { - data[i / 2] = - (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); + data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + + Character.digit(s.charAt(i + 1), 16)); } return data; } From a31936a634a14ad63dc73cfaef7c0e9c168e2d83 Mon Sep 17 00:00:00 2001 From: Keykyrios Date: Thu, 30 Oct 2025 02:57:25 +0530 Subject: [PATCH 12/12] Update ChaCha20Test.java --- .../thealgorithms/ciphers/ChaCha20Test.java | 55 ++++++++++++------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java b/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java index 7df2dc903ad4..547057f8ba4e 100644 --- a/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java +++ b/src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java @@ -6,30 +6,36 @@ public class ChaCha20Test { - // RFC 8439 Section 2.4.2 Test Vector (114 bytes total, counter starts at 1) + // RFC 8439 Section 2.4.2 test vector (114 bytes, counter starts at 1) private static final byte[] RFC8439_KEY = - hexStringToByteArray("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"); + hexStringToByteArray("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"); private static final byte[] RFC8439_NONCE = - hexStringToByteArray("000000000000004a00000000"); + hexStringToByteArray("000000000000004a00000000"); + // 114-byte plaintext from RFC 8439 private static final byte[] RFC8439_PLAINTEXT_114 = hexStringToByteArray( - "4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e"); + "4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a20" + + "4966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f722074686520" + + "6675747572652c2073756e73637265656e20776f756c642062652069742e"); + // 114-byte ciphertext from RFC 8439 private static final byte[] RFC8439_CIPHERTEXT_114 = hexStringToByteArray( - "6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f58375fcd4af034bd16adec164f7a2bda3dc0343a99a46c6e6593372ed8b9970cdbd7d8f5d9d3e0e6c90b8ed397b6c96b6f2ed8c8f0a5c9e6a2e6b1d58d88c7f1e9c7b3cda85a1b"); + "6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f58" + + "375fcd4af034bd16adec164f7a2bda3dc0343a99a46c6e6593372ed8b9970cdbd7d8f5d9d3e0e6c90b8e" + + "d397b6c96b6f2ed8c8f0a5c9e6a2e6b1d58d88c7f1e9c7b3cda85a1b"); @Test public void testEncryptRFC8439Vector114Bytes() { byte[] ciphertext = ChaCha20.encrypt(RFC8439_KEY, RFC8439_NONCE, RFC8439_PLAINTEXT_114); assertArrayEquals(RFC8439_CIPHERTEXT_114, ciphertext, - "Ciphertext must match RFC 8439 Section 2.4.2 test vector"); + "Ciphertext must match RFC 8439 Section 2.4.2 test vector"); } @Test public void testDecryptRFC8439Vector114Bytes() { byte[] plaintext = ChaCha20.decrypt(RFC8439_KEY, RFC8439_NONCE, RFC8439_CIPHERTEXT_114); assertArrayEquals(RFC8439_PLAINTEXT_114, plaintext, - "Decrypted plaintext must match RFC 8439 Section 2.4.2 test vector"); + "Decrypted plaintext must match RFC 8439 Section 2.4.2 test vector"); } @Test @@ -55,16 +61,20 @@ public void testEmptyPlaintext() { public void testInvalidKeySize() { byte[] nonce = new byte[12]; byte[] data = {1, 2, 3}; - assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(new byte[31], nonce, data)); - assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(new byte[33], nonce, data)); + assertThrows(IllegalArgumentException.class, + () -> ChaCha20.encrypt(new byte[31], nonce, data)); + assertThrows(IllegalArgumentException.class, + () -> ChaCha20.encrypt(new byte[33], nonce, data)); } @Test public void testInvalidNonceSize() { byte[] key = new byte[32]; byte[] data = {1, 2, 3}; - assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(key, new byte[11], data)); - assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(key, new byte[13], data)); + assertThrows(IllegalArgumentException.class, + () -> ChaCha20.encrypt(key, new byte[11], data)); + assertThrows(IllegalArgumentException.class, + () -> ChaCha20.encrypt(key, new byte[13], data)); } @Test @@ -72,21 +82,28 @@ public void testNullInputs() { byte[] key = new byte[32]; byte[] nonce = new byte[12]; byte[] data = {1, 2, 3}; - assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(null, nonce, data)); - assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(key, null, data)); - assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(key, nonce, null)); - assertThrows(IllegalArgumentException.class, () -> ChaCha20.decrypt(null, nonce, data)); - assertThrows(IllegalArgumentException.class, () -> ChaCha20.decrypt(key, null, data)); - assertThrows(IllegalArgumentException.class, () -> ChaCha20.decrypt(key, nonce, null)); + assertThrows(IllegalArgumentException.class, + () -> ChaCha20.encrypt(null, nonce, data)); + assertThrows(IllegalArgumentException.class, + () -> ChaCha20.encrypt(key, null, data)); + assertThrows(IllegalArgumentException.class, + () -> ChaCha20.encrypt(key, nonce, null)); + assertThrows(IllegalArgumentException.class, + () -> ChaCha20.decrypt(null, nonce, data)); + assertThrows(IllegalArgumentException.class, + () -> ChaCha20.decrypt(key, null, data)); + assertThrows(IllegalArgumentException.class, + () -> ChaCha20.decrypt(key, nonce, null)); } private static byte[] hexStringToByteArray(String s) { int len = s.length(); - if (len % 2 != 0) throw new IllegalArgumentException("Hex string must have even length"); + if (len % 2 != 0) + throw new IllegalArgumentException("Hex string must have even length"); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) - + Character.digit(s.charAt(i + 1), 16)); + + Character.digit(s.charAt(i + 1), 16)); } return data; }