|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.*; |
4 | 4 |
|
5 | | -import java.util.Arrays; |
6 | 5 | import org.junit.jupiter.api.Test; |
7 | 6 |
|
8 | 7 | public class ChaCha20Test { |
9 | 8 |
|
10 | | - // RFC 8439 test vector |
| 9 | + // RFC 8439 Section 2.4.2 Test Vector (114 bytes total, counter starts at 1) |
11 | 10 | private static final byte[] RFC8439_KEY = |
12 | 11 | hexStringToByteArray("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"); |
13 | 12 | private static final byte[] RFC8439_NONCE = |
14 | 13 | hexStringToByteArray("000000000000004a00000000"); |
15 | 14 |
|
16 | | - // RFC 8439 Section 2.4.2 plaintext and ciphertext (64 bytes) |
17 | | - private static final byte[] RFC8439_PLAINTEXT_64 = hexStringToByteArray( |
18 | | - "4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e"); |
19 | | - private static final byte[] RFC8439_CIPHERTEXT_64 = hexStringToByteArray( |
20 | | - "6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f58375fcd4af034bd16adec164f7a2bda3dc0343a99a46c"); |
21 | | - |
22 | | - // For 114 bytes (from RFC) |
23 | 15 | private static final byte[] RFC8439_PLAINTEXT_114 = hexStringToByteArray( |
24 | 16 | "4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e"); |
| 17 | + |
25 | 18 | private static final byte[] RFC8439_CIPHERTEXT_114 = hexStringToByteArray( |
26 | | - "6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f58375fcd4af034bd16adec164f7a2bda3dc0343a99a46c"); |
| 19 | + "6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f58375fcd4af034bd16adec164f7a2bda3dc0343a99a46c6e6593372ed8b9970cdbd7d8f5d9d3e0e6c90b8ed397b6c96b6f2ed8c8f0a5c9e6a2e6b1d58d88c7f1e9c7b3cda85a1b"); |
27 | 20 |
|
28 | 21 | @Test |
29 | | - public void testEncryptRFC8439Vector64Bytes() { |
30 | | - assertArrayEquals(RFC8439_CIPHERTEXT_64, |
31 | | - ChaCha20.encrypt(RFC8439_KEY, RFC8439_NONCE, RFC8439_PLAINTEXT_64)); |
| 22 | + public void testEncryptRFC8439Vector114Bytes() { |
| 23 | + byte[] ciphertext = ChaCha20.encrypt(RFC8439_KEY, RFC8439_NONCE, RFC8439_PLAINTEXT_114); |
| 24 | + assertArrayEquals(RFC8439_CIPHERTEXT_114, ciphertext, |
| 25 | + "Ciphertext must match RFC 8439 Section 2.4.2 test vector"); |
32 | 26 | } |
33 | 27 |
|
34 | 28 | @Test |
35 | | - public void testDecryptRFC8439Vector64Bytes() { |
36 | | - assertArrayEquals(RFC8439_PLAINTEXT_64, |
37 | | - ChaCha20.decrypt(RFC8439_KEY, RFC8439_NONCE, RFC8439_CIPHERTEXT_64)); |
| 29 | + public void testDecryptRFC8439Vector114Bytes() { |
| 30 | + byte[] plaintext = ChaCha20.decrypt(RFC8439_KEY, RFC8439_NONCE, RFC8439_CIPHERTEXT_114); |
| 31 | + assertArrayEquals(RFC8439_PLAINTEXT_114, plaintext, |
| 32 | + "Decrypted plaintext must match RFC 8439 Section 2.4.2 test vector"); |
38 | 33 | } |
39 | 34 |
|
40 | 35 | @Test |
@@ -90,8 +85,8 @@ private static byte[] hexStringToByteArray(String s) { |
90 | 85 | if (len % 2 != 0) throw new IllegalArgumentException("Hex string must have even length"); |
91 | 86 | byte[] data = new byte[len / 2]; |
92 | 87 | for (int i = 0; i < len; i += 2) { |
93 | | - data[i / 2] = |
94 | | - (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); |
| 88 | + data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) |
| 89 | + + Character.digit(s.charAt(i + 1), 16)); |
95 | 90 | } |
96 | 91 | return data; |
97 | 92 | } |
|
0 commit comments