buenomini

Block vs stream ciphers

Fixed-size blocks or a keystream: the two ways ciphers process data, and why I picked ChaCha20 for my key-value store.

Adding encryption to my key-value store, I had to learn the difference between block and stream ciphers.

# Block ciphers

A block cipher processes data in fixed-size chunks and encrypts one block at a time. AES is the common one. Its block size is 128 bits (16 bytes), so it takes 16 bytes of plaintext, outputs 16 bytes of ciphertext, and repeats that for each block.

The implementation is more complex, but there's hardware support (AES-NI). Without hardware acceleration it's slower, and a poor implementation can be vulnerable to side-channel attacks.

And if your data isn't exactly 16 bytes (or a multiple of 16), you need padding.

# Stream ciphers

A stream cipher encrypts data bit by bit or byte by byte. It generates a keystream and XORs it with your plaintext. ChaCha20 is the example here:

  • No fixed block size. Encrypt 5 bytes, get 5 bytes of ciphertext, no padding
  • Simpler design (only additions, XORs and rotations)
  • Fast in software without special hardware
  • Better for power consumption

# What I'm using

ChaCha20, the stream cipher. Pure software implementation, simpler, faster without hardware acceleration. That's it. Both are secure when used correctly, ChaCha20 just made more sense for a learning project where I'm not using hardware crypto.