Block cipher modes, and sound ciphers used badly

8 min 6 sections 4 practice questions 7 sources citedISC2 objective 3.7

A block cipher such as AES encrypts one small block of data. A mode of operation is the rule for using it on a whole file or message, and the choice of mode can decide whether the result is safe. It is like a first-class lock that can be fitted to a door well or badly. No background beyond symmetric encryption is assumed. Most real failures of encryption are failures of use, so a security professional must be able to recognise them.

By the end of this unit you will be able to:

  • Explain what a mode of operation is and why a block cipher needs one
  • Tell apart ECB, CBC, CFB, OFB, CTR, GCM and CCM by how they work and what they need
  • Explain what an initialisation vector and a nonce are, and what happens when one is reused
  • Choose a mode that gives integrity as well as confidentiality
  • Explain an implementation attack and give the defences against it

The payroll file that gave itself away

An illustrative example

A payroll bureau encrypts its monthly salary file with AES and a 256-bit key before sending it to the bank. Each employee's record fills exactly one block. The cipher is the strongest in general use, and nobody at the bureau has lost the key.

A clerk at the bank, who cannot decrypt the file, notices that the same block of ciphertext turns up again and again. Staff on the same salary have identical encrypted records. He also finds that he can cut a director's encrypted record out and paste it over his own, and the file still decrypts without complaint.

The cipher was never broken. What went wrong?

A block cipher needs a mode

AES turns one block of 128 bits, which is 16 characters of text, into one block of . Almost every real message is longer than that.

A mode of operation is the rule for applying a block to a message of many blocks. The cipher and the key stay the same. The mode decides what goes into the cipher for each block and how the blocks depend on one another.

Two messages with the same cipher and the same key, in two different modes, can differ completely in how safe they are.

ECB: the mode that leaks patterns

Electronic Codebook (ECB) is the simplest mode. Each block is encrypted on its own, with the same key and nothing else.

Its fault follows directly: two blocks that are the same give two ciphertext blocks that are the same. The content stays hidden, but the pattern of repeats does not. An image encrypted in ECB still shows its outline, and a file of records shows which records match.

An attacker can also move, repeat or swap whole blocks, and the result still cleanly. That is what the bank clerk found.

ECB repeats show through; CBC chaining hides them

ECB repeats show through; CBC chaining hides themTwo frames side by side, each starting from blocks A B A C. In ECB each block is encrypted alone and the ciphertext reads X Y X Z. In CBC an initialisation vector and chaining give P Q R S, all different.Electronic Codebook (ECB)Plaintextblocks A B A CEncrypt alonesame key each timeCiphertextblocks X Y X ZCipher Block Chaining (CBC)Plaintextblocks A B A CInitialisationvectorunpredictableMix, then encryptwith previous blockCiphertextblocks P Q R SECB repeats show through; CBC chaining hides themTwo frames side by side, each starting from blocks A B A C. In ECB each block is encrypted alone and the ciphertext reads X Y X Z. In CBC an initialisation vector and chaining give P Q R S, all different.Electronic Codebook (ECB)Plaintextblocks A B A CEncrypt alonesame key each timeCiphertextblocks X Y X ZCipher Block Chaining (CBC)Plaintextblocks A B A CInitialisationvectorunpredictableMix, then encryptwith previous blockCiphertextblocks P Q R S
Read this figure as text

A mode of operation decides how a block cipher handles a message longer than one block. The same four plaintext blocks, A B A C, with the first and third equal, go through two modes under the same key.

  1. ECB, Electronic Codebook: each block is encrypted on its own, with the same key and nothing else.
  2. So the first and third ciphertext blocks come out equal, X Y X Z. The content stays hidden, but the pattern of repeats does not: an image encrypted in ECB still shows its outline.
  3. CBC, Cipher Block Chaining: each plaintext block is mixed with the previous ciphertext block, then encrypted.
  4. The first block has no previous block, so it is mixed with an initialisation vector, an unpredictable starting value that is not secret and travels with the ciphertext.
  5. All four CBC ciphertext blocks differ, P Q R S, because each depends on every block before it. The repair for ECB is a different mode, not a longer key.

ECB is tolerable only for encrypting one block of random data. For messages and files it is the wrong choice, and keys are protected with a dedicated key-wrap method, which encrypts a key and detects any change to it, not ECB.

Common misconception

“A longer key would have protected the payroll file.”

The key was never the weakness. ECB with a 256-bit key leaks repeats exactly as ECB with a 128-bit key does. The repair is a different mode.

Chaining, feedback and counters

The other modes break the link between equal plaintext and equal ciphertext. Cipher Block Chaining (CBC) combines each plaintext block with the previous ciphertext block before encrypting it, so every block depends on all the blocks before it.

Cipher Feedback (CFB), Output Feedback (OFB) and Counter (CTR) use the block cipher to generate a keystream and combine it with the plaintext. They make a block cipher behave as a .

Seven modes of operation compared
ModeHow it worksStarting valueOne damaged ciphertext bit
ECBEach block encrypted on its ownNoneRuins that block only
CBCEach block mixed with the previous ciphertext block, then encrypted, unpredictableRuins that block and flips the same bit in the next
CFBPrevious ciphertext is encrypted to make keystreamIV, unpredictableFlips that bit and ruins about one following block
OFBThe cipher's own output is fed back to make keystreamIV, never reusedFlips that bit only
CTRA counter is encrypted to make keystreamCounter, never reusedFlips that bit only
GCMCounter mode with an authentication tagIV, never reusedDetected: the message is rejected
CCMCounter mode with a CBC-based authentication codeNonce, never reusedDetected: the message is rejected

Speed differs too. ECB and CTR can encrypt many blocks at once, in parallel, and CTR can decrypt any single block without the rest. CBC must encrypt in order, because each block waits for the one before.

The initialisation vector and the nonce

An initialisation vector (IV) is a starting value fed into the mode along with the first block. It makes the same message, under the same key, encrypt differently each time. It is not secret, and it normally travels with the ciphertext.

A nonce is a value that is used only once. The modes that generate a keystream need their IV or counter to be a nonce: it must never repeat under the same key.

  • CBC and CFB need an IV that an attacker cannot predict in advance.
  • OFB, CTR and GCM need a starting value that is never used twice with one key.
  • If a keystream mode reuses its starting value, it produces the same keystream twice. Combining the two ciphertexts then cancels the keystream and exposes the plaintexts, as with a one-time pad used twice.

A third value with a similar job, the salt, is added to a password before it is hashed. All three exist to make equal inputs give unequal outputs.

Common misconception

“The IV must be kept as secret as the key.”

The IV is public. What matters is that it is unpredictable, or never repeated, according to the mode. Only the key is secret.

Authenticated modes: GCM and CCM

The five older modes give only. An attacker who cannot read a message can still change it. In CTR mode, flipping one bit of ciphertext flips the same bit of plaintext, so a payment of 1,000 pounds could be altered without the key.

Authenticated encryption encrypts the message and also produces a short check value, called a tag, that depends on the key and on every bit of the ciphertext. The recipient recalculates the tag before using the plaintext. A message that has been altered fails the check.

Galois/Counter Mode (GCM) and Counter with CBC-MAC (CCM) are the two standard authenticated modes. Both are built on counter mode, and both give confidentiality and together. Current versions of the protocol that protects web traffic allow only authenticated modes.

Implementation attacks: the cipher is sound, its use is not

An implementation attack exploits the way a cryptosystem has been built, configured or used. It does not find a weakness in the mathematics of the algorithm. The payroll bureau's AES was sound, and its file was open to attack.

  • The wrong mode: ECB used for files or messages.
  • A repeated IV, nonce or counter under one key.
  • Keys made from numbers that are not truly random, such as the time of day.
  • A key written into the program's code or left in a configuration file.
  • No check, so altered ciphertext is accepted.
  • Error messages that tell an attacker whether a guess decrypted correctly.
  • A home-made cipher, or a home-made version of a standard one.

A second family of implementation attacks works on the physical device: measuring its timing or power use, or disturbing it to cause errors.

The defences are matters of engineering practice. Use well-tested libraries and standard modes. Prefer cryptographic modules that an independent laboratory has validated against a standard such as FIPS 140-3, the United States standard for such modules. Review and test the code, and keep keys out of it.

If the question says the algorithm is strong and the system still failed, look at the mode, the IV, the random numbers and where the key was kept.

A question, worked through

An application security lead at a bank reviews a mobile app before release. The app encrypts each stored transaction record with AES and a 256-bit key in counter mode. To keep the code simple, the developers start the counter from the same fixed value for every record. The key is created on the phone the first time the app runs and is used for the life of the app. A tester has reported that the cipher itself is sound.

Which is the BEST description of the weakness that most needs fixing?

  1. An implementation flaw: the counter repeats under one key, so every record is encrypted with the same keystream
  2. An algorithm flaw: counter mode turns AES into a stream cipher, and a stream cipher is weaker than a block cipher
  3. A key management flaw: one key is used for the life of the app, so all records depend on a single secret
  4. A mode flaw: counter mode shows equal plaintext blocks as equal ciphertext blocks, in the same way that ECB does
Decide on your answer, then open the reasoning
  1. The question asks which weakness most needs fixing. The scenario says the cipher is sound, so the fault is in how it is used.
  2. Rule out b. Counter mode is a standard, approved way to use AES. Acting as a stream cipher is not a weakness in itself.
  3. Rule out d. Counter mode used properly hides repeats, because every block gets a different counter value. The description fits ECB.
  4. Between a and c: a long-lived key is worth improving, but it does not expose anything by itself.
  5. A counter that restarts at the same value under one key repeats the keystream. Two records can then be combined to cancel it. That is an implementation flaw, option a.

Answer: A. An implementation flaw: the counter repeats under one key, so every record is encrypted with the same keystream

Choosing between good answers

A scenario says identical records or blocks produce identical ciphertext
the mode is ECB. The fix is a mode with an IV, or an authenticated mode. A longer key is not the fix.
A question asks for a mode that protects integrity as well as confidentiality
choose GCM or CCM. CBC and CTR hide the data and do not detect changes.
The algorithm is described as strong and the system was still defeated
it is an implementation attack. Choose the answer that repairs the way the cipher is built or used.
An option says the IV must be kept secret
reject it. An IV is public: it must be unpredictable or never repeated, depending on the mode.

Check your understanding

4 questions on this unit, easiest first. Confirm an answer to see the reasoning for it, then the right answer, the other options and the rule to reuse. Your answers are kept in this browser and feed readiness and review.

These questions are drafts that no named reviewer has approved yet.

Question 1 of 4

A developer is choosing how to apply AES to messages of many blocks. Match each mode of operation to how it behaves.

Choose the match for each entry. Every match has to be right.

What to remember

  • A mode of operation is the rule for using a block cipher on more than one block.
  • ECB encrypts each block alone, so equal plaintext blocks give equal ciphertext blocks. It leaks patterns and lets blocks be swapped.
  • CBC chains blocks together. CFB, OFB and CTR turn the block cipher into a stream cipher.
  • An IV is public and makes equal messages encrypt differently. In keystream modes it must never repeat under one key.
  • GCM and CCM are authenticated modes: confidentiality and authenticity together.
  • An implementation attack defeats a sound algorithm through the way it was built or used. The defence is tested libraries, standard modes and validated modules.

Say it from memory

  1. What is an implementation attack, and what are three examples and the defences?

    An attack on the way a cryptosystem is built, configured or used, which leaves the algorithm's mathematics unbroken. Examples: ECB used for files, a repeated IV or nonce, predictable random numbers, a key left in code, no integrity check. Defences: tested libraries, standard modes, modules validated to FIPS 140-3, code review and testing.

    Read this part again

  2. What is a mode of operation, and why does it matter when the cipher and key are sound?

    A mode of operation is the rule for applying a block cipher to a message of many blocks. The cipher and the key stay the same; the mode decides what goes into the cipher for each block and how the blocks depend on one another. Two messages under the same cipher and key, in different modes, can differ completely in how safe they are.

    Read this part again

  3. What is the fault of Electronic Codebook mode, and does a longer key fix it?

    Each block is encrypted on its own, so two equal plaintext blocks give two equal ciphertext blocks. The pattern of repeats shows, and whole blocks can be moved, repeated or swapped and still decrypt cleanly. A longer key does not fix it; the repair is a different mode.

    Read this part again

  4. How does CBC differ from CFB, OFB and CTR, and which modes can encrypt blocks in parallel?

    CBC combines each plaintext block with the previous ciphertext block before encrypting it, so every block depends on those before it. CFB, OFB and CTR use the block cipher to generate a keystream combined with the plaintext, making it behave as a stream cipher. ECB and CTR can encrypt many blocks in parallel; CBC must encrypt in order.

    Read this part again

  5. Is the initialisation vector secret, and what must it be in CBC and in CTR?

    The IV is not secret and normally travels with the ciphertext. CBC and CFB need an IV an attacker cannot predict in advance. OFB, CTR and GCM need a starting value never used twice with one key, because a reused value repeats the keystream and exposes the plaintexts.

    Read this part again

  6. What does authenticated encryption add, and which two modes provide it?

    It encrypts the message and also produces a tag that depends on the key and every bit of the ciphertext; the recipient recalculates it, and an altered message fails the check. Galois/Counter Mode (GCM) and Counter with CBC-MAC (CCM) are the two standard authenticated modes, both built on counter mode.

    Read this part again

Sources