CSS411 Cryptography

Block Cipher Modes

Slide deck 9: the five NIST modes ECB, CBC, CFB, OFB and CTR. 14 min read.

19 min read

Why block ciphers need modesSearch on YouTube

A block cipher encrypts a fixed-size block. DES encrypts 64-bit blocks. Real messages have an arbitrary length. A link needs a rule that turns the fixed-size cipher into a system for arbitrary data. That rule is a mode of operation.

NIST SP 800-38A defines 5 modes. They cover a wide variety of applications. Each mode can be used with any block cipher (DES, AES, and others).

ModeFull nameTreats data asStart value
ECBElectronic Codebookwhole blocks, all independentnone
CBCCipher Block Chainingwhole blocks, chainedIV
CFBCipher FeedBacka stream of bitsIV
OFBOutput FeedBacka stream of bitsIV
CTRCountera stream of bitscounter value

Learn all five in order: ECB, CBC, CFB, OFB, CTR. The exam asks for formulas, IV properties and error behaviour. It often asks for the whole table in one question.

Electronic Codebook (ECB)Search on YouTube

The message is broken into independent blocks. Each block is encrypted on its own. Each block is a value that is substituted, like a codebook entry. Hence the name.

Ci=EK[Pi]Pi=DK[Ci]C_i = E_K[P_i] \qquad P_i = D_K[C_i]

ECB encryption          P1 P2 P3 P4
                         |  |  |  |
                      E  K  K  K  K      (same key for every block)
                         |  |  |  |
                        C1 C2 C3 C4      (independent)
UseMotive
Secure transmission of single valuesone block, no linkage needed
Any value in a codebook lookupone block in, one block out

Advantages and limitations in the slides:

ECB advantagesECB limitations
Easy and simpleMessage repetitions may show in ciphertext when aligned with block boundaries
Can operate in parallelGraphics or messages that change little become a codebook analysis problem
No start value neededWeakness comes from block independence
Main use: a few blocks of dataNone

Two equal blocks encrypt to two equal blocks. A picture of a flat sky leaks a repeat pattern from ECB. The weakness is structural, not a key problem.

Cipher Block Chaining (CBC)Search on YouTube

The message is broken into blocks. The blocks are linked together in the encryption operation. Each previous cipher block is chained with the current plaintext block. Hence the name. An Initialization Vector (IV) starts the process.

Ci=EK[Pi⊕Ci−1]C0=IVC_i = E_K[P_i \oplus C_{i-1}] \qquad C_0 = IV

P1​Encryptkey KC1​P2​Encryptkey KC2​P3​Encryptkey KC3​IVCi​ = EK​(Pi​ ⊕ Ci-1​), C0​ = IV
CBC encryption: each plaintext block XORs with the previous ciphertext block before encryption. The IV starts the chain.
CBC encryption
IV --⊕ P1--> E(K) --> C1
C1 --⊕ P2--> E(K) --> C2
C2 --⊕ P3--> E(K) --> C3   ...
CBC decryption
C1 --> D(K) --⊕ IV--> P1
C2 --> D(K) --⊕ C1--> P2
C3 --> D(K) --⊕ C2--> P3   ...

The IV prevents a repeated plaintext block from always yielding the same ciphertext block. Uses: bulk data encryption and authentication.

How CBC behavesSearch on YouTube

A ciphertext block depends on all blocks before it. A change to one block affects all following ciphertext blocks. The slides call this effect the avalanche effect.

PropertyConsequence
Block CiC_i depends on P1,…,PiP_1,\ldots,P_iOne plaintext change ripples through all later ciphertext
Decryption needs Ci−1C_{i-1}Cannot decrypt a middle block in isolation
Errors in one cipher blockCorrupt that block and the next plaintext block, then resynchronise

IV requirementsSearch on YouTube

The sender and the receiver must both know the IV. If the sender sends the IV in clear, an attacker can change bits of the first block. The attacker changes the corresponding bits of the IV. So the slides give four safe options:

  • Use a fixed value (as in EFTPOS).
  • Derive the IV in a way that is hard to manipulate.
  • Send the IV encrypted in ECB mode before the rest of the message.
  • Otherwise, check the message integrity.

A clear IV flips bits in P1P_1. Bit jj of the IV maps to bit jj of the first plaintext block. Every later block is untouched. So the receiver must authenticate the IV, or the first block lies.

Cipher FeedBack (CFB)Search on YouTube

The message is treated as a stream of bits. The output of the block cipher is added to the message. The result is fed back for the next stage. Hence the name.

Ci=Pi⊕EK[Ci−1]C0=IVC_i = P_i \oplus E_K[C_{i-1}] \qquad C_0 = IV

CFB encryption
E(IV,K) --XOR--> P1 --> C1      (C1 fed back)
E(C1,K) --XOR--> P2 --> C2      (C2 fed back)
CFB decryption
E(IV,K) --XOR--> C1 --> P1      (C1 fed back)
E(C1,K) --XOR--> C2 --> P2      (C2 fed back)

The standard allows any number of bits to be fed back: 1, 8, 64 or 128. The notation is CFB-1, CFB-8, CFB-64, CFB-128. It is most efficient to use all bits in the block (64 or 128).

VariantBits fed backBlock cipher runsBest suited for
CFB-11once per bitpure bit streams
CFB-88once per bytebyte-oriented links
CFB-6464once per 64-bit segment64-bit block ciphers at full width
CFB-128128once per 128-bit block128-bit block ciphers at full width

Each variant still waits for a block encryption after every ss bits, where ss is the width in its name. Small ss means many stalls per block. Full width (s=64s = 64 or 128128) is the most efficient choice.

The s-bit Cipher FeedBack (CFB-s)Search on YouTube

The small variants feed the trailing ss bits of the last ciphertext block back into the shift register. The register then shifts by ss bits each round. The scheme stays usable when data arrives in bits or bytes. The following table keeps the slide wording.

CFB-s aspectSlide detail
Variant namesCFB-1, CFB-8, CFB-64, CFB-128
Most efficientfeed all block bits (64 or 128) at once
Most commonstream mode in practice
Appropriate whendata arrives in bits or bytes
Limitationmust stall while doing a block encryption after every ss bits
Errorspropagate for several blocks after the error

Exam wording: "CFB is a stream mode that must still run block encryption every ss bits." Name both halves. Stream outside, block cipher inside, stall in between.

Output FeedBack (OFB)Search on YouTube

The message is again a stream of bits. The cipher output is added to the message. The output (and not the ciphertext) is then fed back. Hence the name.

Oi=EK[Oi−1]O0=IVCi=Pi⊕OiO_i = E_K[O_{i-1}] \qquad O_0 = IV \qquad C_i = P_i \oplus O_i

OFB encryption
E(IV,K) --> O1 --XOR--> P1 --> C1      (O1 fed back, not C1)
E(O1,K)  --> O2 --XOR--> P2 --> C2
OFB decryption
E(IV,K) --> O1 --XOR--> C1 --> P1      (keystream reused)
PropertyMeaning
Feedback is independent of the messageOiO_i never depends on any PjP_j or CjC_j
Can be computed in advanceKeystream grows without new plaintext
Needs a unique IV per useReuse lets an attacker recover the outputs
Bit errors do not propagateA flipped bit corrupts its own bit position

If the IV repeats with the same key, the keystream repeats. Two messages then share a mask. The attacker computes the XOR of the two masks and recovers the outputs. This is the classic one-time pad (OTP) reuse failure.

Counter (CTR)Search on YouTube

A "new" mode, though proposed early on. Similar to OFB. The difference: CTR encrypts a counter value rather than any feedback value.

Oi=EK[CNTi]Ci=Pi⊕OiO_i = E_K[\mathrm{CNT}_i] \qquad C_i = P_i \oplus O_i

CounterEncryptkey KP1​C1​Counter + 1Encryptkey KP2​C2​Counter + 2Encryptkey KP3​C3​Never reuse a counter value with the same key.
CTR encryption: each block encrypts a fresh counter value, then XORs the output with the plaintext block. Blocks are independent.
CTR encryption
CNT = 1,2,3,...  (or a base + block index)
E(CNT1,K) --XOR--> P1 --> C1
E(CNT2,K) --XOR--> P2 --> C2      (blocks are independent)
CTR decryption
E(CNTi,K) --XOR--> Ci --> Pi      (counter recomputed locally)
CTR ruleDetail
Fresh label per blockNeed a different key and counter value for every plaintext block
Never reuseSame warning as the OTP issue in OFB
UsesHigh-speed network encryption

Advantages and limitations in the slides:

CTR advantagesCTR caveat
Efficiency: parallel encryptions in hardware or softwareNone
Can preprocess in advance of needNone
Good for bursty high-speed linksNone
Random access to encrypted data blocksNone
Provable security, as good as the other modesMust ensure never to reuse key or counter values, otherwise the scheme breaks (compare OFB)

The five modes side by sideSearch on YouTube

ECBCBCCFBOFBCTR
Data typeblockblockstreamstreamstream
EncryptionCi=EK[Pi]C_i = E_K[P_i]Ci=EK[Pi⊕Ci−1]C_i = E_K[P_i \oplus C_{i-1}]Ci=Pi⊕EK[Ci−1]C_i = P_i \oplus E_K[C_{i-1}]Ci=Pi⊕EK[Oi−1]C_i = P_i \oplus E_K[O_{i-1}]Ci=Pi⊕EK[CNTi]C_i = P_i \oplus E_K[\mathrm{CNT}_i]
DecryptionPi=DK[Ci]P_i = D_K[C_i]Pi=DK[Ci]⊕Ci−1P_i = D_K[C_i] \oplus C_{i-1}Pi=Ci⊕EK[Ci−1]P_i = C_i \oplus E_K[C_{i-1}]Pi=Ci⊕OiP_i = C_i \oplus O_iPi=Ci⊕EK[CNTi]P_i = C_i \oplus E_K[\mathrm{CNT}_i]
Start valuenoneIV (C0C_0)IV (C0C_0)IV (O0O_0)counter sequence
Parallel encryptyesnononoyes
Precompute carenononoyesyes
Error spreadone blocktwo blocksseveral blocksflipped bitsflipped bits
Main usesingle valuesbulk data, authenticationstream data, authenticationstream, precomputablehigh-speed networks

Order hook: Every Code Breaker Crashes One Counter. ECB, CBC, CFB, OFB, CTR, in NIST SP 800-38A order. Reuse hook: One Never Pads Twice. OFB and CTR share the "never reuse the start value or counter" rule. Error hook: ECB one, CBC two, CFB several, OFB CTR clean. ECB corrupts one block. CBC corrupts two blocks. CFB spreads errors over several blocks. OFB and CTR damage only the flipped bits.

Mode misuse checklistSearch on YouTube

The decks of the five modes share four failure rules. Read each before an exam answer:

MisuseModeEffect named in the slides
Repeating plaintext with ECBECBRepetitions show in the ciphertext
Clear IV with no integrity checkCBCAttacker flips bits of the first block through the IV
Repeated IVOFBAttacker recovers the outputs, the OTP failure
Repeated key and counter pairCTRThe scheme breaks, compare OFB

Exam trap: OFB and CTR look nearly identical. Tell them apart by what feeds the generator. OFB feeds back its own output Oi−1O_{i-1}. CTR feeds in a public counter CNTi\mathrm{CNT}_i. OFB keystream needs no counter; CTR needs no feedback loop.

Chain rule summary. Every stream mode masks the plaintext with a generated value: Ci=Pi⊕XiC_i = P_i \oplus X_i. ECB and CBC encrypt the plaintext directly: Ci=EK[ Pi⊕(chain) ]C_i = E_K[\,P_i \oplus (\text{chain})\,]. CFB generates from the ciphertext Xi=EK[Ci−1]X_i = E_K[C_{i-1}]. OFB generates from its own output Xi=EK[Oi−1]X_i = E_K[O_{i-1}]. CTR generates from a counter Xi=EK[CNTi]X_i = E_K[\mathrm{CNT}_i].

Quick self-testSearch on YouTube

Why do block cipher modes of operation exist?

Block ciphers encrypt fixed-size blocks (DES uses 64-bit blocks). Messages have arbitrary length. A mode links the per-block cipher into a system for arbitrary data. NIST SP 800-38A defines 5 modes.

Write the ECB encryption and decryption formulas and the main weakness.

Encryption: Ci=EK[Pi]C_i = E_K[P_i]. Decryption: Pi=DK[Ci]P_i = D_K[C_i]. Weakness: equal plaintext blocks give equal ciphertext blocks, so repeats in aligned data (graphics, slowly changing messages) leak a pattern. It is a codebook analysis problem.

What does a clear-text IV attack in CBC mode?

Only the first plaintext block. Changing bit jj of the IV changes bit jj of P1P_1, since P1=DK[C1]⊕IVP_1 = D_K[C_1] \oplus IV. Later blocks stay untouched. The slides list four defences: fixed IV (EFTPOS), a hard-to-manipulate derivation, encrypted IV in ECB mode, or message integrity checking.

How many bits can CFB feed back, and which size is most efficient?

Any number of bits: 1, 8, 64 or 128. The names are CFB-1, CFB-8, CFB-64, CFB-128. Feeding all block bits (64 or 128) is the most efficient. Small variants must stall for a block encryption after every ss bits.

Give the OFB formulas and state why the keystream is precomputable.

Oi=EK[Oi−1]O_i = E_K[O_{i-1}] with O0=IVO_0 = IV, and Ci=Pi⊕OiC_i = P_i \oplus O_i. The value OiO_i depends only on earlier OO values, the key and the IV. No plaintext or ciphertext enters the feedback loop. So the whole keystream can be generated before the data arrives.

Why is reusing an IV in OFB fatal?

The keystream repeats with the same key and IV. The attacker XORs the two ciphertexts, and the equal masks cancel. The attacker gets the XOR of the two plaintexts and can recover the outputs. This is the OTP reuse failure.

What does CTR encrypt instead of a feedback value?

A counter value: Oi=EK[CNTi]O_i = E_K[\mathrm{CNT}_i] and Ci=Pi⊕OiC_i = P_i \oplus O_i. Each block needs a different key and counter pair. The mode offers parallel work, preprocessing, random access and provable security. Never reuse the key or counter values.

Compare error propagation across ECB, CBC, CFB, OFB and CTR.

ECB: one corrupted ciphertext block damages one plaintext block. CBC: a corrupted block damages its own plaintext block and the next one. CFB: errors propagate for several blocks after the corruption. OFB: bit errors do not propagate, only the flipped bits are wrong. CTR behaves like OFB.

Which modes allow parallel encryption and preprocessing?

Parallel encryption: ECB and CTR only. CBC, CFB and OFB encryption are sequential, because each step needs the previous value. Preprocessing ahead of data arrival: OFB and CTR, because their generated values depend on no message bits. CFB and CBC wait for earlier cipher blocks.

Match each mode to a typical use named in the slides.

ECB: secure transmission of single values. CBC: bulk data encryption and authentication. CFB: stream data encryption and authentication. OFB: stream encryption with precomputed keystream. CTR: high-speed network encryption on bursty links.

  • Modes cover arbitrary data over a fixed-block cipher. NIST SP 800-38A defines the five modes in order: ECB, CBC, CFB, OFB, CTR.
  • ECB is Ci=EK[Pi]C_i = E_K[P_i]: parallel, simple, and leaks repeats. CBC chains Ci=EK[Pi⊕Ci−1]C_i = E_K[P_i \oplus C_{i-1}]: avalanche effect, IV rules, bulk data use.
  • CFB is Ci=Pi⊕EK[Ci−1]C_i = P_i \oplus E_K[C_{i-1}]: stream in, block cipher inside, stall every ss bits, errors hit several blocks.
  • OFB is Ci=Pi⊕EK[Oi−1]C_i = P_i \oplus E_K[O_{i-1}] with O0=IVO_0 = IV: keystream independent of the message, so it is precomputable and error-free per bit, but a reused IV is fatal.
  • CTR is Ci=Pi⊕EK[CNTi]C_i = P_i \oplus E_K[\mathrm{CNT}_i]: parallel, preprocessing friendly, random access, provable security; a reused key and counter pair breaks the scheme.