Part 1: Introduction to Block Ciphers

A block cipher encrypts data in fixed-size blocks. It typically works by dividing the plaintext into equal-sized portions called blocks, and encrypting each block separately.

Some terms involving block ciphers are:

  • Block size: The size (in bits) of each chunk of plaintext that the cipher operates on.
  • Key size: The size (in bits) of the secret key to be used for encryption and decryption.
  • Rounds: Block ciphers operate on rounds, where each round does a series of transformations on the data. More rounds = more security = slower performance.
  • Round key: The key derived from the main key that is used in the encryption process for each round.

Substitution-Permutation Networks (SPNs)

This is the most common structure for building block ciphers. It has two common operations.

  • Substitution: These replace small groups of bits into different groups of bits using S-boxes. They provide confusion to the cryptographic system.
  • Permutation: These rearrange bits within the block using P-boxes. It provides diffusion to the cryptographic system.

This is a simplified process of how SPNs work:

  1. The input block is XORed with a round key.
  2. The result is divided into smaller chunks.
  3. Each chunk is passed through an S-box.
  4. The bits are rearranged by a P-box.
  5. Steps 1-4 are repeated for multiple rounds.
  6. A final XOR might take place.

Feistel Networks

Another common structure for a block cipher. While the process is the similar (or identical), the round keys are applied in the reverse order.

  • F-function: It combines substitution and permutation in a single operation.

This is a simplified process of how Feistel networks work:

  1. The input block is divided into two halves (left and right).
  2. The F-function takes the one half and the round key as input and produces an output.
  3. The output is then XORed with the other half.
  4. The two halves are swapped.
  5. Steps 2-4 are repeated for multiple rounds.

Key Algorithms

AlgorithmBlock SizeKey SizeRoundsStructureSecurity
Data Encryption Standard (DES)645616FeistelBroken
3DES64112/16848Feistel (x3)Weakening/Legacy
Advanced Encryption Standard (AES)128128, 192, 25610, 12, 14SPNSecure

Part 2: Modes of Operation

Block ciphers operate on fixed-size blocks. Practically, real world messages are larger than a single block. Hence, the modes of operation allows us to define how we use a block cipher to encrypt multiple blocks.

There are 5 common modes of operation.

  • Electronic Codebook (ECB)
  • Cipher Block Chaining (CBC)
  • Counter Mode (CTR)
  • Cipher Feedback Mode (CFB)
  • Output Feedback Mode (OFB)

Electronic Codebook

The plaintext is divided into blocks, and each block is independently encrypted using the same key.

  • Using this method, identical plaintext blocks encrypt to identical ciphertext blocks, which reveals patterns in the plaintext, making it vulnerable to analysis.

Cipher Block Chaining Mode

Each plaintext block is XORed with the previous ciphertext block before it was encrypted.
An initialization vector (IV) is used to initialize the process. It is used to XOR with the first plaintext block.

  • While it prevents the problems with ECB, it can be computationally extensive as the encryption of a block depends on the previous block itself and so on.

Counter Mode

Instead of encrypting the plaintext directly, it encrypts a counter value (based on an IV), then XORs the result with the plaintext. The difference here is that the counter value used in the encryption is different each time by some constant increment from the previous counter used in the previous block.

Cipher Feedback Mode

Similar to CBC, the IV is encrypted then XORed to the first plaintext block to create the first ciphertext. For the next succeeding blocks, the previous ciphertext is encrypted then XORed to the plaintext block.

Output Feedback Mode

Similar to CFB, the plaintext block is XORed to the keystream block to form the ciphertext. The keystream block used is then created by encrypting the previous keystream block.

Part 3: Stream Ciphers

Unlike block ciphers that operate in fixed-sized chunks of data, stream ciplers encrypt data bit-by-bit (or sometimes byte-by-byte).

Core Principles

  • To encrypt plaintext, stream ciphers generate a keystream, which is a series of pseudorandom bits or bytes.
  • These keystreams can be generated via a pseudorandom number generator (PRNG).
  • Then the keystream is XORed to the plaintext to generate the ciphertext.
  • The security of a stream cipher relies on the quality of the keystream used.

Keystream Generation

  • Keystreams are generated using a pseudo-random number generator.
  • It takes the key (and sometimes a seed) then produces the seemingly random sequence of bits.
  • The PRNG is deterministic, which means that feeding it the same key and initial state will give the same keystream.
    • This principle is essential for decryption as the receiver must also be able to generate the same keystream.

Key Algorithms

  • RC4 (Rivest Cipher 4)
    • It uses a variable-length key to initialize a 256-bit internal state. Based on this state, it uses a complex shuffling process to generate the keystream bits.
    • It was widely used in protocols like WEP and older versions of SSL/TLS
    • It is now considered insecure due to its significant statistical biases in its keystream.
  • ChaCha20 and Salsa20
    • Modern stream ciphers designed by Daniel J. Bernstein.
    • Uses a quarter-round function that mixes bits through addition, XOR, and bitwise rotation.
    • Salsa20: The precursor to ChaCha20.
    • ChaCha20: Uses a 256-bit key, a 64/128-bit IV, and a 64-bit counter. It also performs 20 rounds of its quarter-round function.
  • One-Time Pad
    • The key is a truly random sequence of bits (that is not generated via a PRNG), then it encrypts by XORing with the plaintext.
    • Key Details: The key is a truly random sequence of bits that is as long as the plaintext.
    • Ensures Perfect Secrecy: If the key satisfies the details from above, used completely once, and kept completely secret, then the attacker can learn absolutely nothing about the ciphertext, even with unlimited computing power, providing perfect secrecy.
    • Has Practical Limitations: This is because of:
      • Key Distribution: The key must be securely shared between the sender and the receiver, which is a major logistical challenge, especially when the key is long.
      • Key Length: The key must be as long as the message, which is impractical when encrypting huge amounts of data.
      • Key Reuse: If any part of the key is reused, the security of the OTP is completely broken.
        • Using XOR on two ciphertexts encrypted with the same key, it can be used to obtain the XOR of the two plaintexts.