Skip to content

[FEATURE REQUEST] Implement ChaCha20 stream cipher #7753

Closed

Description

@the404packet

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:

  1. State Initialization: Initialize the 4 × 4 state matrix using the predefined constants, secret key, counter, and nonce.
  2. Quarter Round: Implement the core ARX operations (addition, rotation, and XOR) with the specified rotation constants.
  3. Round Operations: Implement the 20-round transformation consisting of column rounds and diagonal rounds.
  4. Block Generation: Generate 64-byte keystream blocks by applying the ChaCha20 transformation and adding the original state.
  5. Encryption: XOR plaintext bytes with the generated keystream.
  6. 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

Additional Information

No response

Activity

  1. the404packet commented on Nov 12, 2026

    @the404packet
    Author

    @DenizAltunkapan is this algorithm within the scope of this repository?
    If yes, pls assign me this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions