|
| 1 | +package com.thealgorithms.ciphers; |
| 2 | + |
| 3 | +import java.util.Objects; |
| 4 | + |
| 5 | +/** |
| 6 | + * Implements the ChaCha20 stream cipher described in RFC 8439. |
| 7 | + * |
| 8 | + * <p>This implementation is intended for educational purposes and should not |
| 9 | + * be used as a replacement for a vetted cryptographic library in production. |
| 10 | + * |
| 11 | + * @see <a href="https://www.rfc-editor.org/rfc/rfc8439.html">RFC 8439</a> |
| 12 | + */ |
| 13 | +public final class ChaCha20 { |
| 14 | + |
| 15 | + private static final int BLOCK_SIZE = 64; |
| 16 | + private static final long MAX_COUNTER = 0xFFFF_FFFFL; |
| 17 | + private static final int[] CONSTANTS = {0x61707865, 0x3320646e, 0x79622d32, 0x6b206574}; |
| 18 | + |
| 19 | + private ChaCha20() { |
| 20 | + // Utility class |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Encrypts bytes with ChaCha20. Decryption uses the same operation. |
| 25 | + * |
| 26 | + * @param input plaintext bytes; must not be {@code null} |
| 27 | + * @param key 32-byte key |
| 28 | + * @param nonce 12-byte nonce |
| 29 | + * @param initialCounter unsigned 32-bit initial block counter |
| 30 | + * @return encrypted bytes |
| 31 | + * @throws NullPointerException if an argument is {@code null} |
| 32 | + * @throws IllegalArgumentException if a key, nonce, or counter is invalid, |
| 33 | + * or the counter would overflow while processing the input |
| 34 | + */ |
| 35 | + public static byte[] encrypt(byte[] input, byte[] key, byte[] nonce, long initialCounter) { |
| 36 | + validateInputs(input, key, nonce, initialCounter); |
| 37 | + |
| 38 | + long blockCount = (input.length + (long) BLOCK_SIZE - 1) / BLOCK_SIZE; |
| 39 | + if (blockCount > 0 && initialCounter > MAX_COUNTER - (blockCount - 1)) { |
| 40 | + throw new IllegalArgumentException("Counter would overflow"); |
| 41 | + } |
| 42 | + |
| 43 | + byte[] output = new byte[input.length]; |
| 44 | + for (int offset = 0; offset < input.length; offset += BLOCK_SIZE) { |
| 45 | + long counter = initialCounter + offset / BLOCK_SIZE; |
| 46 | + byte[] keyStream = block(key, nonce, counter); |
| 47 | + int bytesToProcess = Math.min(BLOCK_SIZE, input.length - offset); |
| 48 | + |
| 49 | + for (int i = 0; i < bytesToProcess; i++) { |
| 50 | + output[offset + i] = (byte) (input[offset + i] ^ keyStream[i]); |
| 51 | + } |
| 52 | + } |
| 53 | + return output; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Decrypts bytes with ChaCha20. This is identical to encryption. |
| 58 | + * |
| 59 | + * @param input ciphertext bytes; must not be {@code null} |
| 60 | + * @param key 32-byte key |
| 61 | + * @param nonce 12-byte nonce |
| 62 | + * @param initialCounter unsigned 32-bit initial block counter |
| 63 | + * @return decrypted bytes |
| 64 | + */ |
| 65 | + public static byte[] decrypt(byte[] input, byte[] key, byte[] nonce, long initialCounter) { |
| 66 | + return encrypt(input, key, nonce, initialCounter); |
| 67 | + } |
| 68 | + |
| 69 | + private static void validateInputs(byte[] input, byte[] key, byte[] nonce, long counter) { |
| 70 | + Objects.requireNonNull(input, "input must not be null"); |
| 71 | + Objects.requireNonNull(key, "key must not be null"); |
| 72 | + Objects.requireNonNull(nonce, "nonce must not be null"); |
| 73 | + |
| 74 | + if (key.length != 32) { |
| 75 | + throw new IllegalArgumentException("Key must be 32 bytes"); |
| 76 | + } |
| 77 | + if (nonce.length != 12) { |
| 78 | + throw new IllegalArgumentException("Nonce must be 12 bytes"); |
| 79 | + } |
| 80 | + if (counter < 0 || counter > MAX_COUNTER) { |
| 81 | + throw new IllegalArgumentException("Counter must be an unsigned 32-bit value"); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private static byte[] block(byte[] key, byte[] nonce, long counter) { |
| 86 | + int[] state = new int[16]; |
| 87 | + System.arraycopy(CONSTANTS, 0, state, 0, CONSTANTS.length); |
| 88 | + |
| 89 | + for (int i = 0; i < 8; i++) { |
| 90 | + state[4 + i] = littleEndianInt(key, i * 4); |
| 91 | + } |
| 92 | + |
| 93 | + state[12] = (int) counter; |
| 94 | + state[13] = littleEndianInt(nonce, 0); |
| 95 | + state[14] = littleEndianInt(nonce, 4); |
| 96 | + state[15] = littleEndianInt(nonce, 8); |
| 97 | + |
| 98 | + int[] workingState = state.clone(); |
| 99 | + for (int i = 0; i < 10; i++) { |
| 100 | + quarterRound(workingState, 0, 4, 8, 12); |
| 101 | + quarterRound(workingState, 1, 5, 9, 13); |
| 102 | + quarterRound(workingState, 2, 6, 10, 14); |
| 103 | + quarterRound(workingState, 3, 7, 11, 15); |
| 104 | + |
| 105 | + quarterRound(workingState, 0, 5, 10, 15); |
| 106 | + quarterRound(workingState, 1, 6, 11, 12); |
| 107 | + quarterRound(workingState, 2, 7, 8, 13); |
| 108 | + quarterRound(workingState, 3, 4, 9, 14); |
| 109 | + } |
| 110 | + |
| 111 | + byte[] output = new byte[BLOCK_SIZE]; |
| 112 | + for (int i = 0; i < workingState.length; i++) { |
| 113 | + int word = workingState[i] + state[i]; |
| 114 | + output[i * 4] = (byte) word; |
| 115 | + output[i * 4 + 1] = (byte) (word >>> 8); |
| 116 | + output[i * 4 + 2] = (byte) (word >>> 16); |
| 117 | + output[i * 4 + 3] = (byte) (word >>> 24); |
| 118 | + } |
| 119 | + return output; |
| 120 | + } |
| 121 | + |
| 122 | + static void quarterRound(int[] state, int a, int b, int c, int d) { |
| 123 | + state[a] += state[b]; |
| 124 | + state[d] = Integer.rotateLeft(state[d] ^ state[a], 16); |
| 125 | + state[c] += state[d]; |
| 126 | + state[b] = Integer.rotateLeft(state[b] ^ state[c], 12); |
| 127 | + state[a] += state[b]; |
| 128 | + state[d] = Integer.rotateLeft(state[d] ^ state[a], 8); |
| 129 | + state[c] += state[d]; |
| 130 | + state[b] = Integer.rotateLeft(state[b] ^ state[c], 7); |
| 131 | + } |
| 132 | + |
| 133 | + private static int littleEndianInt(byte[] bytes, int offset) { |
| 134 | + return (bytes[offset] & 0xFF) | ((bytes[offset + 1] & 0xFF) << 8) | ((bytes[offset + 2] & 0xFF) << 16) | ((bytes[offset + 3] & 0xFF) << 24); |
| 135 | + } |
| 136 | +} |
0 commit comments