Repository navigation
Description
What would you like to Propose?
I would like to propose adding ChaCha20, a modern symmetric-key stream cipher, to the ciphers package.
ChaCha20 is a stream cipher that generates a pseudorandom keystream using a 256-bit key, a 96-bit nonce, and a 32-bit block counter. It uses ARX (Addition, Rotation, and XOR) operations to generate 512-bit keystream blocks, which are XORed with plaintext to perform encryption and decryption.
Currently, the repository contains several classical and modern cryptographic algorithms, but does not appear to include a standalone ChaCha20 implementation.
Issue details
Algorithm Name
ChaCha20 Stream Cipher
Problem Statement
Implement the ChaCha20 stream cipher in Java following the specification defined in [RFC 8439](https://www.rfc-editor.org/rfc/rfc8439).
The implementation should generate a 512-bit keystream block from a 256-bit secret key, a 96-bit nonce, and a 32-bit block counter. The generated keystream should then be XORed with the input data to support encryption and decryption.
Proposed Implementation
The implementation should include the following components:
- State Initialization: Initialize the 4 × 4 state matrix using the predefined constants, secret key, counter, and nonce.
- Quarter Round: Implement the core ARX operations (addition, rotation, and XOR) with the specified rotation constants.
- Round Operations: Implement the 20-round transformation consisting of column rounds and diagonal rounds.
- Block Generation: Generate 64-byte keystream blocks by applying the ChaCha20 transformation and adding the original state.
- Encryption: XOR plaintext bytes with the generated keystream.
- Decryption: Reuse the same keystream generation process to recover the original plaintext.
Expected Behavior
- Accept a 256-bit key and a 96-bit nonce.
- Support arbitrary-length byte-array input.
- Correctly process inputs spanning multiple 64-byte blocks.
- Validate key and nonce lengths.
- Handle empty input.
- Ensure correct counter progression between blocks.
- Prevent counter overflow and invalid counter usage.
Testing Requirements
Add JUnit tests covering:
- RFC 8439 known test vectors.
- Quarter-round correctness.
- Block-function correctness.
- Encryption and decryption round trips.
- Empty input.
- Inputs smaller than, equal to, and larger than 64 bytes.
- Invalid key and nonce lengths.
- Multiple-block encryption and decryption.
- Counter boundary conditions.
References
- [RFC 8439 – ChaCha20 and Poly1305 for IETF Protocols](https://www.rfc-editor.org/rfc/rfc8439)
- [ChaCha20 Original Specification](https://cr.yp.to/chacha.html)
Additional Information
No response
Activity
the404packet commentedon Nov 12, 2026
@DenizAltunkapan is this algorithm within the scope of this repository?
If yes, pls assign me this issue.