Symmetric encryption, modes and randomness

Unit 3 of 5 in Cryptography basics.

You will be able to explain why the mode of encryption and the source of randomness matter and why you should use a maintained library.

Lesson

A cipher is not the whole story

AES takes a fixed block of 128 bits and a key and produces a 128-bit block. Real messages are longer than that, so a second decision is needed: how to apply the cipher to many blocks. That decision is called the mode of operation.

NIST SP 800-38A defines five confidentiality modes for a symmetric block cipher: ECB, CBC, CFB, OFB and CTR. They are not interchangeable. The choice changes what an attacker can learn.

Why ECB is the one to avoid

In ECB, the cipher is applied directly and independently to each block of the plaintext. Because blocks are treated independently, the same plaintext block always gives the same ciphertext block, so patterns in the data show through. The lab for this unit shows this with an image whose shape is still visible after encryption. OWASP says plainly that ECB should not be used outside of very specific circumstances.

Use authenticated encryption

Encryption alone hides data but does not prove it was not changed. OWASP advises that where available, authenticated modes should always be used, because they guarantee integrity and authenticity as well as confidentiality. The most commonly used authenticated modes are GCM and CCM. Modes can also require a fresh initialization vector or nonce, and OWASP says these should be handled automatically by a maintained library, which is the point: do not assemble these pieces yourself.

Randomness: keys must be unguessable

A strong cipher with a guessable key is no protection. OWASP warns that ordinary random number generators must not be used for anything security critical, because it is often possible for attackers to guess or predict their output. Cryptographically secure generators are designed to produce much higher quality randomness and are safe for security-sensitive uses. Always take keys, tokens and nonces from the secure source your platform provides.

Key points

Sources

Practice in the labs

See a bad mode and a predictable generator fail, so you never reach for either.

Knowledge check

  1. Why does ECB mode leak patterns? Answer: Each block is encrypted independently, so equal plaintext blocks give equal ciphertext blocks. SP 800-38A describes ECB as applying the cipher directly and independently to each block, so repeated blocks are visible in the output.
  2. Which should you prefer for encrypting stored data? Answer: An authenticated mode such as GCM through a maintained library. OWASP advises authenticated modes (GCM, CCM) where available because they also guarantee integrity and authenticity, and says to rely on a maintained library.
  3. Where should a key or token come from? Answer: A cryptographically secure random generator. OWASP warns ordinary random number generators can be predicted by attackers and must not be used for security-critical values.

Back to the syllabus or see all classes.

Related