Skip to content
CoreSource checked

ChaCha20

Encrypts data with a fast stream cipher built from add-rotate-xor rounds.

Quick Facts

Compact metadata for implementation and discovery.

Category
Cryptography
Difficulty
Advanced
Family
Stream Cipher
Tier
Core
Status
Verified
Review
Source Checked
Sources
1
Formulas
1
Relationships
3
Code example
Not ready
Visualization
Ready

Overview

ChaCha20 generates a keystream from a key, nonce, and counter, then combines it with plaintext bytes.

Core Concepts

security primitivekeystream generationshared secret keysecurity assumptionstream cipherkey materialmessage transformationmisuse resistance

Quick facts

Complexity

Runtime and memory behavior at a glance.

Best

O(n)

Average

O(n)

Worst

O(n)

Space

O(1)

When to Use

  • Use ChaCha20 when you need to generate a pseudorandom keystream.
  • Use it for tLS encryption with inputs like secret key.
  • Use it only through reviewed libraries when the security goal matches the primitive.
  • Use it when the Stream Cipher tradeoff is acceptable for the problem size.

When Not to Use

  • Avoid it when key distribution and nonce management are not solved.
  • Avoid custom implementations for production security or when key, nonce, and padding rules are unclear.
  • Avoid it when the required output is not ciphertext or plaintext.

What It Does

  • Generates a pseudorandom keystream
  • Combines the keystream with data using XOR

Use Cases

  • TLS encryption
  • Mobile encryption
  • High-speed software cryptography

Inputs

  • Secret key
  • Nonce
  • Counter
  • Plaintext or ciphertext

Outputs

  • Ciphertext or plaintext

Key Formulas (1)

Quarter round pattern

a = a + b; d = rotateleft(d xor a, 16)
a = a + b; d = rotate_left(d xor a, 16)

Shows one add-rotate-xor step from the repeated ChaCha20 quarter round.

Pseudocode

  1. 1.Initialize state from key, nonce, and counter
  2. 2.Apply ChaCha rounds to produce a block
  3. 3.Serialize the block as keystream bytes
  4. 4.XOR keystream with input bytes
  5. 5.Increment the counter for the next block

Canonical Example

Input
message = 'pay 10' with configured keys, nonces, or public parameters
Output
ciphertext, tag, digest, signature, proof, or verification result
Explanation
The primitive applies its defined arithmetic, hashing, or block operation without inventing extra protocol steps.

Implementation Notes

  • Keep the input contract explicit: Secret key; Nonce; Counter; Plaintext or ciphertext.
  • Validate parameters, key sizes, nonces, salts, and error handling against the library's recommended API.
  • Use reviewed libraries and protocol specifications for production security-sensitive code.

Common Pitfalls

  • ChaCha20 needs a unique nonce for each key.
  • Confusing encryption, hashing, signatures, and authentication codes as interchangeable tools.
  • Handle empty, invalid, and boundary inputs before trusting ciphertext or plaintext.
  • Treating educational pseudocode as production crypto can hide nonce, parameter, and side-channel requirements.

Interactive Mechanism Walkthrough

Graph Frontier & Edge Relaxation

Show keystream blocks combining with plaintext bytes.

ChaCha20 mechanism walkthrough step 1 of 5suvwxt
Step 1 of 5: Initialize state from key, nonce, and counter
stream-cipher

Notes

ChaCha20 operates in O(n) time and O(1) space within the Stream Cipher family. Key governing relation: a = a + b; d = rotate_left(d xor a, 16). Often compared with AES and HMAC.

Sources (1)

What each reference supports for this entry.

RFC 8439

Documentation

IETF/CFRG reference for ChaCha20 and Poly1305 algorithms.

Open source

Explore Next

Good next jumps from the relationship graph, ranked for useful discovery.

Alternative toAES

ChaCha20 is a stream cipher alternative to AES-based encryption modes.

Related Algorithms

Tags

CryptographyStream CipherSymmetric KeySecurity