1 of 67

One-Time Pad & Block Ciphers

CS 161 Spring 2024 - Lecture 6

Computer Science 161

2 of 67

Project 1 Parties

Project 1 is due this Friday, February 9, 11:59pm.

  • Project 1 Party this Wednesday, February 7th 5-6:30pm in Cory 540AB.
  • Project 1 Party this Thursday, February 8th 5-7pm in Cory 540AB.

My office hours: Monday 2-3pm, Soda 729

2

Computer Science 161

3 of 67

Recall the IND-CPA security definition

  1. Eve may choose plaintexts to send to Alice and receives their ciphertexts
  2. Eve issues a pair of plaintexts M0 and M1 to Alice
  3. Alice randomly chooses either M0 or M1 to encrypt and sends the encryption back
    • Alice does not tell Eve which one was encrypted!
  4. Eve may again choose plaintexts to send to Alice and receives their ciphertexts
  5. Eventually, Eve outputs a guess as to whether Alice encrypted M0 or M1

An encryption scheme is IND-CPA secure if for all polynomial time attackers Eve:

  • Eve can win with probability ≤ 1/2 + Ɛ, where Ɛ is negligible.

3

M

Enc(K, M)

(repeat)

Alice (challenger)

Eve (adversary)

M0 and M1

Enc(K, Mb)

M

Enc(K, M)

Guess b = 0 or b = 1

(repeat)

pick b

Keygen()

Computer Science 161

4 of 67

One-Time Pads

4

Textbook Chapter 6.2 & 6.3

Computer Science 161

5 of 67

Cryptography Roadmap

  • Hash functions
  • Pseudorandom number generators
  • Public key exchange (e.g. Diffie-Hellman)

5

Symmetric-key

Asymmetric-key

Confidentiality

  • One-time pads
  • Block ciphers with chaining modes (e.g. AES-CBC)
  • Stream ciphers
  • RSA encryption
  • ElGamal encryption

Integrity,�Authentication

  • MACs (e.g. HMAC)
  • Digital signatures (e.g. RSA signatures)
  • Key management (certificates)
  • Password management

Computer Science 161

6 of 67

Review: XOR

6

0 ⊕ 0 = 0

0 ⊕ 1 = 1

1 ⊕ 0 = 1

1 ⊕ 1 = 0

The XOR operator takes two bits and outputs one bit:

Useful properties of XOR:

x ⊕ 0 = x

x ⊕ x = 0

x ⊕ y = y ⊕ x

(x ⊕ y) ⊕ z = x ⊕ (y ⊕ z)

(x ⊕ y) ⊕ x = y

Computer Science 161

7 of 67

Review: XOR Algebra

  • Algebra works on XOR too

7

y ⊕ 1 = 0

Goal: Solve for y

y ⊕ 1 ⊕ 1 = 0 ⊕ 1

XOR both sides by 1

y = 1

Simplify with identities

Computer Science 161

8 of 67

One-Time Pads: Key Generation

8

Alice

0

1

1

0

0

1

0

1

0

1

1

1

K

The key K is a randomly-chosen bitstring.

Recall: We are in the symmetric-key setting, so we’ll assume Alice and Bob both know this key.

Computer Science 161

9 of 67

One-Time Pads: Encryption

9

Alice

0

1

1

0

0

1

0

1

0

1

1

1

K

1

0

0

1

1

0

0

1

0

1

0

0

M

The plaintext M is the bitstring that Alice wants to encrypt.

Idea: Use XOR to scramble up M with the bits of K.

Computer Science 161

10 of 67

One-Time Pads: Encryption

10

Alice

0

1

1

0

0

1

0

1

0

1

1

1

K

1

0

0

1

1

0

0

1

0

1

0

0

M

1

1

1

1

1

1

0

0

0

0

1

1

C

Encryption algorithm: XOR each bit of K with the matching bit in M.

The ciphertext C is the encrypted bitstring that Alice sends to Bob over the insecure channel.

Computer Science 161

11 of 67

One-Time Pads: Decryption

11

Bob

0

1

1

0

0

1

0

1

0

1

1

1

K

1

1

1

1

1

1

0

0

0

0

1

1

C

Bob receives the ciphertext C. Bob knows the key K. How does Bob recover M?

Computer Science 161

12 of 67

One-Time Pads: Decryption

12

Bob

0

1

1

0

0

1

0

1

0

1

1

1

K

1

1

1

1

1

1

0

0

0

0

1

1

C

1

0

0

1

1

0

0

1

0

1

0

0

M

Decryption algorithm: XOR each bit of K with the matching bit in C.

Computer Science 161

13 of 67

One-Time Pad

  • KeyGen()
    • Randomly generate an n-bit key, where n is the length of your message
      • Recall: For today, we assume that Alice and Bob can securely share this key
      • For one-time pads, we generate a new key for every message
  • Enc(K, M) = KM
    • Bitwise XOR M and K to produce C
      • In other words: XOR the ith bit of the plaintext with the ith bit of the key.
      • Ci = KiMi
    • Alice and Bob use a different key for each encryption (this is the “one-time” in one-time pad).
  • Dec(K, C) = KC
    • Bitwise XOR C and K to produce M
      • Mi = KiCi

13

Computer Science 161

14 of 67

One-Time Pad: Correctness

  • Correctness: If we encrypt and then decrypt, we should get the original message back

14

Enc(K, M)

=

KM

Definition of encryption

Dec(K, Enc(K, M))

=

Dec(K, KM)

Decrypting the ciphertext

=

K ⊕ (KM)

Definition of decryption

=

M

XOR property

Computer Science 161

15 of 67

One-Time Pad: Security

  • Recall our definition of confidentiality: The ciphertext should not give the attacker any additional information about the plaintext
  • Recall our preliminary experiment (before IND-CPA) to test confidentiality from earlier:
    • If the probability that Eve correctly guesses

which message was sent is 1/2, then the

encryption scheme is confidential

15

Alice (challenger)

Eve (adversary)

M0 and M1

Enc(K, Mb)

Guess b = 0 or b = 1

pick b

15

KeyGen():K

Computer Science 161

16 of 67

One-Time Pad: Security

16

Possibility 0: Alice sends Enc(K, M0)

Possibility 1: Alice sends Enc(K, M1)

The ciphertext is C = KM0

Therefore, K = CM0

The ciphertext is C = KM1

Therefore, K = CM1

K was chosen randomly, so both possibilities are equally possible

Eve has learned no new information, so the scheme is perfectly secure

By “perfectly” we mean that any attacker has chance of winning the security game exactly ½ (not ½+epsilon)

Computer Science 161

17 of 67

Two-Time Pads?

17

M0

K

OTP Enc

KM0

Alice

Insecure Channel

M1

K

OTP Enc

KM1

What if we use the same key K to encrypt two different messages?

Eve sees two ciphertexts over the insecure channel.

Computer Science 161

18 of 67

Two-Time Pads?

18

M0

K

OTP Enc

KM0

Alice

Insecure Channel

M1

K

OTP Enc

KM1

(KM0) ⊕ (KM1)�= M0M1

Eve

If Eve XORs the two ciphertexts, she learns M0M1!

Computer Science 161

19 of 67

Two-Time Pads?

  • What if we use the same key twice?
    • Alice encrypts M0 and M1 with the same key
    • Eve observes KM0 and KM1
    • Eve computes (KM0) ⊕ (KM1) = M0 M1
      • Recall the XOR property: the K's cancel out
  • Eve has learned M0 M1. This is partial information about the messages!
    • In other words, Eve knows which bits in M0 match bits in M1
    • If Eve knows M0, she can deduce M1 (and vice-versa)
    • Eve can also guess M0 and check that M1 matches her guess for M0
  • Result: One-time pads are not secure if the key is reused
    • Alice and Bob must use a different key for every message!

19

Computer Science 161

20 of 67

OTP does not offer IND-CPA

  • What is an attack strategy?
  • Many possible, e.g.
    • In the trial phase and the challenge phase, ask for “00” and “11”, it is deterministic.

20

M

Enc(K, M)

(repeat)

Alice (challenger)

Eve (adversary)

M0 and M1

Enc(K, Mb)

M

Enc(K, M)

Guess b = 0 or b = 1

(repeat)

pick b

Keygen()

Computer Science 161

21 of 67

Impracticality of One-Time Pads

  • Problem #1: Key generation
    • For security to hold, keys must be randomly generated for every message, and never reused
    • Randomness is expensive, as we’ll see later
  • Problem #2: Key distribution
    • To communicate an n-bit message, we need to securely communicate an n-bit key first
    • But if we have a way to securely communicate an n-bit key, we could have communicated the message directly!
  • Communicate keys in advance
    • You have a secure channel now, but you won’t have it later
    • Use the secure channel now to communicate keys in advance
    • Use one-time pad later to communicate over the insecure channel
    • And people can compute this by hand without computers!

21

Computer Science 161

22 of 67

Is one-time pad IND-CPA secure?

  • Eve may choose plaintexts to send to Alice and receives their ciphertexts
  • Eve issues a pair of plaintexts M0 and M1 to Alice
  • Alice randomly chooses either M0 or M1 to encrypt and sends the encryption back
    1. Alice does not tell Eve which one was encrypted!
  • Eve may again choose plaintexts to send to Alice and receives their ciphertexts
  • Eventually, Eve outputs a guess as to whether Alice encrypted M0 or M1

An encryption scheme is IND-CPA secure if for all polynomial time attackers Eve: Eve can win with probability <= 1/2 + Ɛ, where Ɛ is negligible.

22

M

Enc(K, M)

(repeat)

Alice (challenger)

Eve (adversary)

M0 and M1

Enc(K, Mb)

M

Enc(K, M)

Guess b = 0 or b = 1

(repeat)

pick b

A: No, because it requires that the scheme is able to reuse a key K without helping the attacker distinguish. In repeat phase, attacker can ask for M0

Computer Science 161

23 of 67

One-Time Pads in Practice: Spies

  • At home base, the spy obtains a large amount of key material (e.g. a book of random bits)
  • In the field, the spy listens for secret messages from their home country
    • There are shortwave and terrestrial radio “number stations”
    • At a regular time, a voice gets on the air and reads a series of numbers
    • If you don’t know the key, this looks like a meaningless sequence of random numbers
    • If you know the key, you can decrypt the spy message!
  • What if you don’t want to send anything to any spies?
    • Read out a list of random numbers anyway
    • Because one-time pad leaks no information, an eavesdropper can’t distinguish between an encrypted message and random numbers!

23

Computer Science 161

24 of 67

Two-Time Pads in Practice: VENONA

  • Soviet spies used one-time pads for communication from their spies in the US
  • During WWII, the Soviets started reusing key material
    • Uncertain whether it was just the cost of generating pads or what…
  • VENONA was a US cryptanalysis project designed to break these messages
    • Included confirming/identifying the spies targeting the US Manhattan project
    • Project continued until 1980!
  • Not declassified until 1995!
    • So secret even President Truman wasn’t informed about it
    • The Soviets found out about it in 1949 through their spy Ken Philby, but their one-time pad reuse was fixed after 1948 anyway
  • Takeaway: Otherwise-secure cryptographic systems can fail very badly if used improperly!

24

Computer Science 161

25 of 67

Summary for one-time pads

  • Symmetric encryption scheme: Alice and Bob share a secret key.
  • Encryption and decryption: Bitwise XOR with the key.
  • No information leakage if the key is never reused.
  • Information leaks if the key is reused.
  • Impractical for real-world usage, unless you’re a spy.

25

Computer Science 161

26 of 67

Block Ciphers

26

Textbook Chapter 6.4 & 6.5

Computer Science 161

27 of 67

Cryptography Roadmap

  • Hash functions
  • Pseudorandom number generators
  • Public key exchange (e.g. Diffie-Hellman)

Symmetric-key

Asymmetric-key

Confidentiality

  • One-time pads
  • Block ciphers with chaining modes (e.g. AES-CBC)
  • Stream ciphers
  • RSA encryption
  • ElGamal encryption

Integrity,�Authentication

  • MACs (e.g. HMAC)
  • Digital signatures (e.g. RSA signatures)
  • Key management (certificates)
  • Password management

Computer Science 161

28 of 67

Block Ciphers: Definition

  • Block cipher: A cryptographic scheme consisting of encode/decode algorithms for a fixed-sized block of bits:
  • EK(M) → C: Encode
    • Inputs: k-bit key K and an n-bit plaintext M
    • Output: An n-bit ciphertext C
    • Sometimes written as: {0, 1}k × {0, 1}n → {0, 1}n
  • DK(C) → M: Decode
    • Inputs: a k-bit key, and an n-bit ciphertext C
    • Output: An n-bit plaintext
    • Sometimes written as: {0, 1}k × {0, 1}n → {0, 1}n
    • The inverse of the encryption function
  • Properties
    • Correctness: EK is a permutation, DK is its inverse
    • Efficiency: Encode/decode should be fast
    • Security: E behaves like a random permutation

28

E

K

k bits

n bits

plaintext

n bits

ciphertext

D

K

k bits

n bits

ciphertext

n bits

plaintext

Computer Science 161

29 of 67

Block Ciphers: Correctness

  • EK(M) must be a permutation (bijective function) on n-bit strings
    • Each input must correspond to exactly one unique output
  • Intuition
    • Suppose EK(M) is not bijective
    • Then two inputs might correspond to the same output: E(K, x1) = E(K, x2) = y
    • Given ciphertext y, you can’t uniquely decode. D(K, y) = x1? D(K, y) = x2?

29

00

01

10

11

00

01

10

11

00

01

10

11

00

01

10

11

Not bijective: Two inputs encode to the same output

Bijective: Each input maps to exactly one unique output

Computer Science 161

30 of 67

Block Ciphers: Security

  • A secure block cipher behaves like a randomly chosen permutation permutation from the set of all permutations on n-bit strings
    • A random permutation: Each n-bit input is mapped to one randomly-chosen n-bit output
  • Defined by a distinguishing game
    • Eve gets two boxes: One is a randomly chosen permutation, and one is EK with a randomly chosen key K
    • Eve should not be able to tell which is which with probability > ½+negl

30

00

01

10

11

00

01

10

11

00

01

10

11

00

01

10

11

One of these is EK with a randomly chosen K, and the other one is a randomly chosen permutation. Eve can’t distinguish them.

??

Computer Science 161

31 of 67

Block ciphers: Brute-force attacks?

  • How hard is it to run a brute-force attack on a 128-bit key?
    • We have to try 2128 possibilities. How big is 2128?
  • Handy approximation: 210 ≈ 103
    • 2128 = 210*12.8 ≈ (103)12.8 ≈ (103)13 = 1039
  • Suppose we have massive hardware that can try 109 (1 billion) keys in 1 nanosecond (a billionth of a second). That’s 1018 keys per second
    • We’ll need 1039 / 1018 = 1021 seconds. How long is that?
    • One year ≈ 3×107 seconds
    • 1021 seconds / 3×107 ≈ 3×1013 years ≈ 30 trillion years
  • Takeaway: Brute-forcing a 128-bit key takes astronomically long.�Don’t even try.

31

Computer Science 161

32 of 67

Block ciphers: Brute-force attacks?

  • How hard is it to run a brute-force attack on a 256-bit key in the same time?
    • We need 1052 of the brute-force devices from before
    • If each brute-force device from before is 1 cubic millimeter, this would take 1043 cubic meters of space
    • That’s the volume of 7×1015 suns!
    • For reference, the Milky Way galaxy has just 1011 stars
  • Takeaway: Brute-force attacks on modern block ciphers are not possible, assuming the key is random and secret
    • 128-bit key? Definitely not happening.
    • 256-bit key? Lol no.

32

Computer Science 161

33 of 67

Block Ciphers: Efficiency

  • Encryption and decryption should be computable in microseconds
    • Formally: KeyGen(), Enc(), and Dec(), should not take exponential time
  • Block cipher algorithms typically use operations like XOR, bit-shifting, and small table lookups
    • Very fast on modern processors
  • Modern CPUs provide dedicated hardware support for block ciphers

33

Computer Science 161

34 of 67

DES (Data Encryption Standard)

  • Designed in late 1970s
  • Block size 64 bits (n = 64)
  • Key size 56 bits (k = 56)
  • NSA influenced two facets of its design
    • Altered some subtle internal workings in a mysterious way
    • Reduced key size from 64 bits to 56 bits
    • Made brute force attacks feasible for an attacker with massive computational resources (by 1970s standards)
  • The algorithm remains essentially unbroken 40 years later
    • The NSA’s tweaking hardened it against an attack publicly revealed a decade later
  • However, modern computer speeds make it completely unsafe due to small key size
    • ~6.4 × 1016, say 1010 tries per second on my single desktop computer's Nvidia graphics card: Takes ~6.4 × 106 seconds or ~70 days

34

Computer Science 161

35 of 67

AES (Advanced Encryption Standard)

  • 1997–2000: NIST (National Institute of Standards and Technology) in the US held a competition to pick a new block cipher standard
    • One of the finalists, Twofish, was designed by Berkeley professor and occasional CS 161 instructor David Wagner!
  • Out of the 5 finalists:
    • Rijndael, Twofish, and Serpent had really good performance
    • RC6 had okay performance
    • Mars had bad performance
  • On any given computing platform, Rijndael was never the fastest
  • But on every computing platform, Rijndael was always the second-fastest
    • Twofish and Serpent each had at least one compute platform they were bad at
  • Rijndael was selected as the new block cipher standard

35

Computer Science 161

36 of 67

Are Block Ciphers IND-CPA Secure?

  • Consider the following adversary:
    • Eve sends two different messages M0 and M1
    • Eve receives either EK(M0) or EK(M1)
    • Eve requests the encryption of M0 again
    • Strategy: If the encryption of M0 matches what she received, guess b = 0. Else, guess b = 1.
  • Eve can win the IND-CPA game with probability 1!
    • Block ciphers are not IND-CPA secure because they are deterministic

36

M

Enc(K, M)

(repeat)

Alice (challenger)

Eve (adversary)

M0 and M1

Enc(K, Mb)

M

Enc(K, M)

Guess b = 0 or b = 1

(repeat)

pick b

Computer Science 161

37 of 67

AES (Advanced Encryption Standard)

  • Key size 128, 192, or 256 bits (k = 128, 192, or 256)
    • Use key size 256 these days
  • Block size 128 bits (n = 128)
    • Note: The block size is still always 128 bits, regardless of key size
  • You don’t need to know how AES works, but you do need to know its parameters
    • here’s a comic

37

Computer Science 161

38 of 67

AES Algorithm

  • Different key sizes use different numbers of rounds
    • 10 rounds for 128-bit keys
    • 12 rounds for 192-bit keys
    • 14 rounds for 256-bit keys
  • Each round uses its own “round key” derived from the cipher key
  • Each round:
    • SubBytes()
    • ShiftRows()
    • MixColumns() (if not last round)
    • AddRoundKey()

38

Computer Science 161

39 of 67

AES Algorithm: SubBytes()

  • Replace each byte in the block with another byte using an 8-bit substitution box

39

Computer Science 161

40 of 67

AES Algorithm: ShiftRows()

  • Cyclically shifts the bytes in each row by a certain offset
  • The number of places each byte is shifted differs for each row

40

Computer Science 161

41 of 67

AES Algorithm: MixColumns()

  • Treats the 16-byte block as a 4 × 4 matrix and multiply it by by another matrix

41

Computer Science 161

42 of 67

AES Algorithm: AddRoundKey()

  • XOR the 16-byte block with the 16-byte round key

42

Computer Science 161

43 of 67

AES (Advanced Encryption Standard)

  • There is no formal proof that AES is secure (indistinguishable from a random permutation)
  • However, in 20 years, nobody has been able to break it, so it is assumed to be secure
    • The NSA uses AES-256 for secrets they want to keep secure for the 40 years (even in the face of unknown breakthroughs in research)
  • Takeaway: AES is the modern standard block cipher algorithm
    • The standard key size (128 bits) is large enough to prevent brute-force attacks

43

Computer Science 161

44 of 67

Issues with Block Ciphers

  • Block ciphers are not IND-CPA secure, because they’re deterministic
    • A scheme is deterministic if the same input always produces the same output
    • No deterministic scheme can be IND-CPA secure because the adversary can always tell if the same message was encrypted twice
  • Block ciphers can only encrypt messages of a fixed size
    • For example, AES can only encrypt-decrypt 128-bit messages
    • What if we want to encrypt something longer than 128 bits?
  • To address these problems, we’ll add modes of operation that use block ciphers as a building block!

44

Computer Science 161

45 of 67

Summary: Block Ciphers

  • Encryption: input a k-bit key and n-bit plaintext, receive n-bit ciphertext
  • Decryption: input a k-bit key and n-bit ciphertext, receive n-bit plaintext
  • Correctness: when the key is fixed, EK(M) should be bijective
  • Security
    • Without the key, EK(m) is computationally indistinguishable from a random permutation
    • Brute-force attacks take astronomically long and are not possible
  • Efficiency: algorithms use XORs and bit-shifting (very fast)
  • Implementation: AES is the modern standard
  • Issues
    • Not IND-CPA secure because they’re deterministic
    • Can only encrypt n-bit messages

45

Computer Science 161

46 of 67

Block Cipher Modes of Operation

46

Textbook Chapter 6.6–6.9

Computer Science 161

47 of 67

Scratchpad: Let’s design it together

47

Here’s an AES block. Remember that it can only encrypt 128-bit messages.

How can we use AES to encrypt a longer message (say, 256 bits?)

Computer Science 161

48 of 67

Scratchpad: Let’s design it together

48

Idea: Let’s use AES twice!

First 128 bits of message

Second 128 bits of message

Computer Science 161

49 of 67

Scratchpad: Let’s design it together

49

Note that we are using the same key twice. We want to avoid a situation like one-time pads where we need very long keys.

Computer Science 161

50 of 67

ECB Mode

  • We’ve just designed electronic code book (ECB) mode
    • Enc(K, M) = C1 || C2 || … || Cm
    • Assume m is the number of blocks of plaintext in M, each of size n
  • AES-ECB is not IND-CPA secure. Why?
    • Because ECB is deterministic

50

Computer Science 161

51 of 67

ECB Mode: Penguin

Original image

51

Computer Science 161

52 of 67

ECB Mode: Penguin

Encrypted with ECB

52

Computer Science 161

53 of 67

Scratchpad: Let’s design it together

53

Here’s ECB mode. It’s not IND-CPA secure because it’s deterministic.

Let’s fix that by adding some randomness.

Computer Science 161

54 of 67

Scratchpad: Let’s design it together

54

The Initialization Vector (IV) is different for every encryption. Now the first ciphertext block will be different for every encryption!

Okay, but the other blocks are still deterministic...

Computer Science 161

55 of 67

Scratchpad: Let’s design it together

55

Idea: The first ciphertext block was computed with some randomness. Let’s use it to add randomness to the second block.

Computer Science 161

56 of 67

Scratchpad: Let’s design it together

56

Now the second ciphertext block has some randomness in it. Let’s use it to add randomness to the third block.

Computer Science 161

57 of 67

CBC Mode

  • We’ve just designed cipher block chaining (CBC) mode
  • Ci = EK(MiCi-1); C0 = IV
  • Enc(K, M):
    • Split M in m plaintext blocks P1 … Pm each of size n
    • Choose a random IV
    • Compute and output (IV, C1, …, Cm) as the overall ciphertext
  • How do we decrypt?

57

P1

P2

Pm

Computer Science 161

58 of 67

CBC Mode: Decryption

  • How do we decrypt CBC mode?
    • Parse ciphertext as (IV, C1, …, Cm)
    • Decrypt each ciphertext and then XOR with IV or previous ciphertext

58

Computer Science 161

59 of 67

CBC Mode: Decryption

59

Ci

=

EK(Mi Ci-1)

Definition of encryption

DK(Ci)

=

DK(EK(MiCi-1))

Decrypting both sides

DK(Ci)

=

MiCi-1

Decryption and encryption cancel

DK(Ci) ⊕ Ci-1

=

MiCi-1 Ci-1

XOR both sides with Ci-1

DK(Ci) ⊕ Ci-1

=

Mi

XOR property

Computer Science 161

60 of 67

CBC Mode: Efficiency & Parallelism

  • Can encryption be parallelized?
    • No, we have to wait for block i to finish before encrypting block i+1
  • Can decryption be parallelized?
    • Yes, decryption only requires ciphertext as input

60

Computer Science 161

61 of 67

CBC Mode: Padding

  • What if you want to encrypt a message that isn’t a multiple of the block size?
    • AES-CBC is only defined if the plaintext length is a multiple of the block size
  • Solution: Pad the message until it’s a multiple of the block size
    • Padding: Adding dummy bytes at the end of the message until it’s the proper length

61

Computer Science 161

62 of 67

CBC Mode: Padding

  • What padding scheme should we use?
    • Padding with 0’s?
      • Doesn’t work: What if our message already ends with 0’s?
    • Padding with 1’s?
      • Same problem
  • We need a scheme that can be unpadded without ambiguity
    • One scheme that works: Append a 1, then pad with 0’s
      • If plaintext is multiple of n, you still need to pad with an entire block
    • Another scheme: Pad with the number of padding bytes
      • So if you need 1 byte, pad with 01; if you need 3 bytes, pad with 03 03 03
      • If you need 0 padding bytes, pad an entire dummy block
      • This is called PKCS #7

62

Computer Science 161

63 of 67

CBC Mode: Security

  • AES-CBC is IND-CPA secure. With what assumption?
    • The IV must be randomly generated and never reused
  • What happens if you reuse the IV?
    • The scheme becomes deterministic: No more IND-CPA security

63

Computer Science 161

64 of 67

CBC Mode: IV Reuse

  • Consider two three-block messages: P1P2P3 and P1P2P4
    • The first two blocks are the same for both messages, but the last block is different
    • What if we encrypt them with the same IV?
  • When the IV is reused, CBC mode reveals when two messages start with the same plaintext blocks, up to the first different plaintext block

64

Computer Science 161

65 of 67

CBC Mode is IND-CPA (when used correctly)

  • Enc(K, M):
    • Split M in m plaintext blocks P1 Pm each of size n
    • Choose random IV, compute and output (IV, C1, …, Cm) as the overall ciphertext
  • Why IND-CPA?
    • If there exists an attacker that wins in the IND-CPA game, then there exists an attacker that breaks the block cipher security. Proof is out of scope.

65

P1

P2

Pm

Computer Science 161

66 of 67

CBC Mode: Penguin

Original image

66

Computer Science 161

67 of 67

CBC Mode: Penguin

Encrypted with CBC, with random IVs

67

Computer Science 161