Tutorial 2.7: Block Cipher Modes of Operation

Table of Contents

Learning Objectives

After completing this tutorial, you should be able to:

Overview

A block cipher such as AES or DES encrypts fixed-size blocks of data (128 bits for AES, 64 bits for DES). However, real-world data comes in arbitrary lengths—a file may be 10 KB, a network packet 1500 bytes, or a message just 8 bytes. Moreover, encrypting identical plaintext blocks with the same key produces identical ciphertext blocks, which can leak information. Block cipher modes of operation address these issues by defining how to apply the block cipher repeatedly to encrypt data of any length while ensuring that identical plaintext blocks do not produce identical ciphertext.

Modes of operation provide different security properties and performance characteristics. Some modes provide only confidentiality (e.g., ECB, CBC, CTR), while others provide both confidentiality and authenticity (e.g., GCM, CCM). Some modes support parallel encryption (CTR, GCM), while others require sequential processing (CBC, CFB, OFB).

In this tutorial, we will examine each major mode in detail, with diagrams, equations, and worked examples. We'll analyze their security properties, identify vulnerabilities, and discuss their use in real-world systems. By the end, you'll be able to select the appropriate mode for any application.

Relationship to the Tutorial Series

In Tutorials 2.4–2.6, we studied symmetric encryption algorithms (DES, AES). This tutorial shows how those algorithms are actually used in practice. Tutorial 2.8 will cover random number generation and key management, which are closely related to modes like CTR that require nonces.

The Need for Modes of Operation

Limitations of the Basic Block Cipher

A block cipher alone (often called "raw" encryption) has several limitations:

  1. Fixed block size: Data lengths rarely match the block size exactly. Padding is needed, but padding alone doesn't solve the other issues.
  2. Deterministic encryption: The same plaintext block always encrypts to the same ciphertext block with the same key, revealing patterns.
  3. No integrity: The block cipher provides confidentiality but no protection against modification or forgery.

Goals of Modes of Operation

Electronic Codebook (ECB)

ECB is the simplest mode: each plaintext block is encrypted independently with the same key.

Ci = EK(Pi)

Pi = DK(Ci)

┌─────────────────────────────────────────────────────────────────┐ │ ECB MODE │ ├─────────────────────────────────────────────────────────────────┤ │ │ │ P₁ ──► [ E ] ──► C₁ P₂ ──► [ E ] ──► C₂ ... │ │ [ K ] [ K ] │ │ │ │ • Each block is independent │ │ • Parallelizable │ │ • Deterministic: same P → same C │ │ • Patterns in plaintext appear in ciphertext │ └─────────────────────────────────────────────────────────────────┘

Figure 1: Electronic Codebook (ECB) mode.

Advantages

Vulnerabilities

⚠️ ECB is insecure for most applications
The pattern leakage is a serious vulnerability. For example, encrypting an image with ECB reveals the outline of the image because the same colors (patterns) encrypt to the same values. ECB should never be used for anything other than encrypting single blocks (e.g., key wrapping).

Cipher Block Chaining (CBC)

CBC chains blocks together by XORing each plaintext block with the previous ciphertext block before encryption. An initialization vector (IV) is used for the first block.

Ci = EK(Pi ⊕ Ci−1), with C0 = IV

Pi = DK(Ci) ⊕ Ci−1

┌─────────────────────────────────────────────────────────────────┐ │ CBC MODE │ ├─────────────────────────────────────────────────────────────────┤ │ │ │ IV ──► ⊕ ──► [ E ] ──► C₁ C₁ ──► ⊕ ──► [ E ] ──► C₂ │ │ P₁ [ K ] P₂ [ K ] │ │ │ │ Decryption: │ │ IV ──► [ D ] ──► ⊕ ──► P₁ C₁ ──► [ D ] ──► ⊕ ──► P₂ │ │ [ K ] C₁ [ K ] C₂ │ │ │ │ • Each block depends on all previous blocks │ │ • IV must be random and unpredictable │ │ • Not parallelizable for encryption │ └─────────────────────────────────────────────────────────────────┘

Figure 2: Cipher Block Chaining (CBC) mode.

IV Requirements

Properties

Padding

CBC requires padding to make the plaintext length a multiple of the block size. PKCS#7 (and the earlier PKCS#5) define padding: pad with n bytes of value n, where n = block_size − (data_length mod block_size). For AES, the block size is 16 bytes, so n ranges from 1 to 16.

Vulnerabilities

Cipher Feedback (CFB)

CFB turns a block cipher into a stream cipher. It encrypts the previous ciphertext block and XORs the result with the plaintext to produce the ciphertext.

CFB can operate on r-bit units (where r ≤ block size). For the common case r = block size:

Ci = Pi ⊕ EK(Ci−1), with C0 = IV

Pi = Ci ⊕ EK(Ci−1)

┌─────────────────────────────────────────────────────────────────┐ │ CFB MODE │ ├─────────────────────────────────────────────────────────────────┤ │ │ │ IV ──► [ E ] ──► ⊕ ──► C₁ C₁ ──► [ E ] ──► ⊕ ──► C₂ │ │ [ K ] P₁ [ K ] P₂ │ │ │ │ • Stream cipher mode │ │ • Encryption and decryption use the block cipher in │ │ encryption mode only │ │ • Self-synchronizing (if r < block size) │ └─────────────────────────────────────────────────────────────────┘

Figure 3: Cipher Feedback (CFB) mode (r = block size).

Properties

CFB with r < Block Size

CFB can operate on smaller units (e.g., 1 bit, 8 bits). The feedback register holds the last ciphertext block, and only the top r bits are used. This allows encryption of streaming data with low latency but is less efficient for large data.

Output Feedback (OFB)

OFB also turns a block cipher into a stream cipher, but unlike CFB, it generates the keystream independently of the plaintext. The block cipher encrypts the previous output to produce the next keystream block.

O0 = IV

Oi = EK(Oi−1)

Ci = Pi ⊕ Oi

Pi = Ci ⊕ Oi

┌─────────────────────────────────────────────────────────────────┐ │ OFB MODE │ ├─────────────────────────────────────────────────────────────────┤ │ │ │ IV ──► [ E ] ──► O₁ ──► ⊕ ──► C₁ │ │ [ K ] P₁ │ │ │ │ │ ▼ │ │ O₁ ──► [ E ] ──► O₂ ──► ⊕ ──► C₂ │ │ [ K ] P₂ │ │ │ │ │ ▼ │ │ ... │ │ │ │ • Keystream independent of plaintext/ciphertext │ │ • No padding needed │ │ • Not parallelizable (each keystream block depends on previous)│ └─────────────────────────────────────────────────────────────────┘

Figure 4: Output Feedback (OFB) mode.

Properties

Limitations

Counter Mode (CTR)

CTR mode uses a counter that is incremented for each block. The counter is encrypted to produce a keystream block, which is then XORed with the plaintext.

CTRi = (nonce || counter)

Oi = EK(CTRi)

Ci = Pi ⊕ Oi

Pi = Ci ⊕ Oi

┌─────────────────────────────────────────────────────────────────┐ │ CTR MODE │ ├─────────────────────────────────────────────────────────────────┤ │ │ │ Nonce || 0 ──► [ E ] ──► O₁ ──► ⊕ ──► C₁ │ │ [ K ] P₁ │ │ │ │ Nonce || 1 ──► [ E ] ──► O₂ ──► ⊕ ──► C₂ │ │ [ K ] P₂ │ │ │ │ Nonce || 2 ──► [ E ] ──► O₃ ──► ⊕ ──► C₃ │ │ [ K ] P₃ │ │ │ │ ... │ │ │ │ • Fully parallelizable │ │ • No padding needed │ │ • Random access: any block can be decrypted independently │ └─────────────────────────────────────────────────────────────────┘

Figure 5: Counter (CTR) mode.

Properties

Counter Management

Advantages Over Other Modes

Galois Counter Mode (GCM)

GCM is an authenticated encryption mode that combines CTR mode for confidentiality with a Galois hash for authentication. It provides both confidentiality and integrity/authenticity in a single pass.

GCM uses:

┌─────────────────────────────────────────────────────────────────┐ │ GCM MODE │ ├─────────────────────────────────────────────────────────────────┤ │ │ │ Plaintext ──────────► CTR Encryption ──► Ciphertext │ │ │ (AES) │ │ │ │ │ ▼ │ │ AAD ──► Galois Hash (GHASH) ──► Authentication Tag (T) │ │ │ │ Ciphertext ──► Decrypt (CTR) ──► Plaintext │ │ │ │ │ ▼ │ │ AAD ──► GHASH ──► Verify Tag │ │ │ │ • Authenticated encryption: confidentiality + integrity │ │ • Parallelizable │ │ • Used in TLS 1.3, IPsec, SSH, etc. │ └─────────────────────────────────────────────────────────────────┘

Figure 6: Galois Counter Mode (GCM) – high-level structure.

GCM Components

Properties

Security Considerations

Authenticated Encryption Modes

Authenticated encryption (AE) modes combine confidentiality and authentication in a single operation, providing both secrecy and integrity/authenticity. This is essential for secure protocols because encryption alone does not protect against tampering.

Common AE Modes

ModeConfidentialityAuthenticationParallelizableCommon Use
GCMCTRGHASH (GF(2¹²⁸))YesTLS 1.3, IPsec, SSH
CCMCBC-MAC + CTRCBC-MACLimitedWi-Fi (802.11i), TLS
EAXCTRCMACYesGeneral purpose
OCBXOR + AESHashYesGeneral purpose (patented)

Why Authenticated Encryption Matters

Best Practice: For new systems, use an authenticated encryption mode (GCM, CCM, or EAX) instead of combining encryption and authentication separately. This reduces the risk of implementation errors (e.g., forgetting to authenticate, using the wrong key for MAC).

Comparison and Selection Guide

Mode Comparison Table

FeatureECBCBCCFBOFBCTRGCM
Confidentiality
Authentication
Pattern hiding
Parallel encryption
Parallel decryption
Random access
Padding required
Padding oracle risk
Error propagationBlock only2 blocksUntil shift1 bit1 bit1 bit

Selection Guidelines

ApplicationRecommended Mode(s)Rationale
General secure communicationGCM (AES-GCM)Authenticated encryption, parallelizable, standard in TLS 1.3
Disk encryptionXTS-AESDesigned for storage; provides confidentiality with tweak per sector
Low-latency streamingCTR or GCMNo padding, parallelizable, random access
Legacy systems (no AE)CBC + HMACCommon in older protocols; must authenticate separately
Key wrappingECB (single block) or AES-KWFor wrapping a single key block
Wi-FiCCM (CCMP)Standard in 802.11i, provides authenticated encryption

Implementation Considerations

IV/Nonce Management

Performance Characteristics

Security Considerations

Case Study: Mode Selection in Practice

TLS 1.3

TLS 1.3 (RFC 8446) mandates authenticated encryption. The only cipher suites are AEAD (Authenticated Encryption with Associated Data) suites:

TLS 1.3 does not support CBC, ECB, or any mode without authentication. This eliminates many vulnerabilities from earlier TLS versions (e.g., POODLE, Lucky Thirteen).

Disk Encryption (BitLocker, LUKS)

Disk encryption uses modes designed for storage:

Wi-Fi (WPA2)

WPA2 uses the CCMP (CCM Protocol) for encryption, which is based on AES-CCM (Counter with CBC-MAC). This provides authenticated encryption for Wi-Fi traffic.

SSH

SSH supports multiple modes, including AES-GCM (recommended), AES-CTR, and AES-CBC. The default is often AES-GCM in modern implementations.

Key Takeaways

Section Summaries

Quiz

  1. What is the main security vulnerability of ECB mode?
  2. AnswerECB mode produces identical ciphertext for identical plaintext blocks, revealing patterns in the plaintext. This makes it insecure for most applications (e.g., encrypting images, data with repeated patterns).
  3. What is the role of the IV in CBC mode?
  4. AnswerThe IV (initialization vector) randomizes the encryption so that identical plaintexts produce different ciphertexts. It is XORed with the first plaintext block before encryption. The IV must be random and unpredictable.
  5. How does CTR mode achieve parallelism?
  6. AnswerCTR mode encrypts a counter value for each block independently. Since the counter values are known in advance and the encryption of each counter block is independent, all blocks can be encrypted and decrypted in parallel.
  7. What is the difference between OFB and CFB modes?
  8. AnswerIn OFB, the keystream is generated independently of the plaintext and ciphertext (feedback from the block cipher output). In CFB, the feedback comes from the ciphertext (previous ciphertext block). OFB does not propagate errors; CFB does.
  9. What is authenticated encryption, and why is it important?
  10. AnswerAuthenticated encryption provides both confidentiality (encryption) and authenticity/integrity (message authentication) in a single operation. It is important because encryption alone does not protect against tampering or forgery.
  11. Which mode is used in TLS 1.3, and why?
  12. AnswerTLS 1.3 mandates authenticated encryption modes such as AES-GCM and ChaCha20-Poly1305. This eliminates vulnerabilities from earlier TLS versions that used CBC without proper authentication.
  13. What happens if the same IV/nonce is used twice with the same key in CTR mode?
  14. AnswerThe same keystream is generated for both encryptions. XORing the two ciphertexts reveals the XOR of the two plaintexts, which can allow an attacker to recover the plaintexts if one is known.
  15. What is the padding oracle attack, and which mode is vulnerable to it?
  16. AnswerA padding oracle attack exploits the fact that the system reveals whether the padding is valid after decryption. This allows an attacker to decrypt ciphertext block by block. CBC mode is vulnerable when combined with PKCS#7 padding and an oracle that distinguishes valid padding.
  17. What are the components of GCM mode?
  18. AnswerGCM combines CTR mode for confidentiality with GHASH (a universal hash function in GF(2¹²⁸)) for authentication. It also supports Additional Authenticated Data (AAD) that is authenticated but not encrypted.
  19. What mode is recommended for disk encryption, and why?
  20. AnswerXTS-AES is recommended for disk encryption because it provides random access (each sector is encrypted independently) and uses a tweak (sector number) to ensure that the same plaintext in different sectors encrypts differently.

Exercises

  1. CBC Encryption

    For CBC mode with AES (block size 16 bytes), suppose the IV is 0x000102030405060708090A0B0C0D0E0F and the plaintext is the single block 0x101112131415161718191A1B1C1D1E1F. The key K produces EK(IV ⊕ P) = 0xAABBCCDDEEFF00112233445566778899. What is the ciphertext?

  2. Sample Solution

    C = EK(P ⊕ IV)

    P ⊕ IV = 0x101112131415161718191A1B1C1D1E1F ⊕ 0x000102030405060708090A0B0C0D0E0F = 0x10101010101010101010101010101010

    C = EK(0x10101010101010101010101010101010) = 0xAABBCCDDEEFF00112233445566778899

  3. CTR Mode Calculation

    For CTR mode with AES (block size 16 bytes), the nonce is 0x00000000 and the counter starts at 0x00000001. What is the keystream block for counter value 0x00000001? (Assume EK(0x00000000000000000000000000000001) = 0x1234567890ABCDEF1234567890ABCDEF). Encrypt the plaintext block 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF.

  4. Sample Solution

    CTR value = nonce || counter = 0x00000000 00000000 00000000 00000001

    Keystream = EK(CTR) = 0x1234567890ABCDEF1234567890ABCDEF

    Ciphertext = P ⊕ Keystream = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ⊕ 0x1234567890ABCDEF1234567890ABCDEF = 0xEDCBA9876F543210EDCBA9876F543210

  5. ECB vs. CBC Pattern Leakage

    Consider a plaintext with 8 identical blocks. In ECB mode, what would the ciphertext look like? In CBC mode, what would the ciphertext look like? Explain why CBC hides patterns better.

  6. Sample Solution

    ECB: All 8 ciphertext blocks are identical because each plaintext block encrypts the same way: C1 = C2 = ... = C8.

    CBC: The first block is C1 = E(P1 ⊕ IV). The second block is C2 = E(P2 ⊕ C1), and so on. Even though P2 = P1, the input to the block cipher is different because of the chaining, so the ciphertext blocks are different. This hides patterns.

  7. GCM Authentication

    In GCM mode, what is the role of the Additional Authenticated Data (AAD)? Give an example of when AAD would be useful.

  8. Sample Solution

    AAD is data that is authenticated (protected against tampering) but not encrypted. It is included in the GHASH computation to ensure its integrity. Example: In a network packet, the IP header and TCP/UDP header may need to be authenticated but not encrypted (to allow routing and processing). AAD allows the receiver to verify that these fields have not been modified.

  9. Mode Selection Scenario

    You are designing a secure messaging application that needs to encrypt 1 KB messages. The messages are sent over a network with high latency and limited bandwidth. Which mode would you recommend, and why?

  10. Sample Solution

    Recommendation: AES-GCM.

    Rationale: GCM provides authenticated encryption, ensuring both confidentiality and integrity. It is parallelizable, which allows efficient use of multi-core processors. It does not require padding, saving bandwidth. The authentication tag ensures that messages are not tampered with. Since the messages are relatively small, the overhead of GCM is acceptable.

    Alternative: AES-CTR with an independent HMAC (if GCM is not available), but GCM is preferred for its single-pass efficiency.

Homework

  1. Research: Padding Oracle Attacks

    Research the padding oracle attack (e.g., POODLE, Lucky Thirteen). Write a 500-word report that explains:

  2. Sample Answer

    Complete answer would describe the padding oracle attack as an adaptive chosen-ciphertext attack where the attacker sends modified ciphertexts and observes whether the padding is valid. It exploits the fact that CBC mode with PKCS#7 padding reveals padding validity. The attack allows decryption of arbitrary ciphertext blocks. Mitigations include using authenticated encryption (GCM, CCM), using CTR mode (no padding), or using constant-time padding checks.

  3. Compare AE Modes

    Compare GCM, CCM, and EAX modes of operation. Create a table showing:

  4. Sample Answer

    A complete answer would include a table with the requested information. GCM uses CTR + GHASH, is parallelizable, requires unique IV, and is NIST-standardized. CCM uses CBC-MAC + CTR, has limited parallelizability, requires unique nonce, and is NIST-standardized. EAX uses CTR + CMAC, is parallelizable, requires unique nonce, and is not NIST-standardized but is widely supported.

  5. XTS-AES Analysis

    Research XTS-AES mode (used for disk encryption). Write a summary that addresses:

  6. Sample Answer

    Complete answer would explain that XTS-AES uses a tweak (sector number and block offset) to encrypt each sector independently. It has two keys: a tweak key for the encryption of the tweak and a data key for the encryption of the data. GCM is not suitable because disk encryption requires random access and the authentication tag would need to be stored separately. XTS provides confidentiality but no authentication (disk integrity is handled at a higher layer).

  7. CTR Nonce Management

    Design a nonce management scheme for CTR mode with a 128-bit key and a 96-bit nonce. Consider:

  8. Sample Answer

    A complete answer would propose using a 96-bit nonce that is a combination of a per-session random value and a monotonic counter. The counter would be 32 bits (allowing 2³² blocks per nonce). If the counter overflows, a new nonce must be generated. Alternatively, use a 64-bit nonce and 64-bit counter for larger messages. The key point is that the (nonce, counter) pair must be unique for each block.

  9. Mini-Project: Implement CTR and GCM

    Implement AES-CTR and AES-GCM encryption/decryption in your preferred language (using a library or custom implementation). Test with NIST test vectors. Compare the performance and code complexity of the two modes. Write a report on your findings.

  10. Sample Answer

    Complete answer would include source code, test results (passing NIST vectors), performance measurements, and a discussion of the additional complexity of GCM (GHASH) compared to CTR. Observations might include that CTR is simpler to implement but GCM provides authentication "for free" with little additional overhead.

Summary

This tutorial has provided a comprehensive examination of block cipher modes of operation. We began by explaining why modes are necessary: block ciphers operate on fixed-size blocks, but real-world data comes in arbitrary lengths, and deterministic encryption reveals patterns.

We examined the major modes in detail:

We discussed authenticated encryption and its importance for secure protocols, and we compared modes across dimensions such as parallelism, authentication, and IV requirements. We also considered implementation considerations, including IV/nonce management and performance.

The case studies illustrated mode selection in practice: TLS 1.3 uses GCM, disk encryption uses XTS-AES, and Wi-Fi uses CCM. These real-world examples show how the theoretical properties of modes translate into practical choices.

With this knowledge, you can now select the appropriate mode for any application and understand the security implications of your choice.

Connection to the Next Tutorial

In Tutorial 2.8: Random Numbers and Key Generation, we will explore the generation of cryptographic keys and random numbers—essential components for IVs, nonces, and key material. The security of many modes (especially CTR, GCM, and CBC) depends on the quality of randomness, making this an important follow-up topic.