Lecture 2-Modern Ciphers
Lecture 2-Modern Ciphers
• Admin
• Recap
• Tutorials:
o Starts next week
o Tuesday: no tutorials after 2PM (either attend other tutorials sessions or use video)
https://pollev.com/nitya
1 Recap
CIA Triad
• Integrity: Assures that the data has not been altered or tampered with by
unauthorized users.
• Availability: Assures that systems work promptly, and service is not denied to
authorized users.
https://pollev.com/nitya
Attack Model
https://pollev.com/nitya
2 Classical Ciphers: Substitution and Permutation
Substitution Cipher
https://pollev.com/nitya
Substitution Cipher: Encryption
• Plaintext: h e l l o _ wo r l d
• Ciphertext: h n l l q p o q i l b
https://pollev.com/nitya
Substitution Cipher: Decryption
• Ciphertext: h n l l q p o q i l b
• Plaintext: h e l l o _ wo r l d
https://pollev.com/nitya
Permutation Cipher/Transposition Cipher
• The encryption first groups the plaintext into blocks of t characters, and then
apply a secret “permutation” to each block by shuffling the characters.
• The key is the secret “permutation”, which is an 1-1 onto function, e
from {1, 2, …, t} to {1, 2, …, t}
• The size t could be part of the key, that is, t is also kept secret.
• Permutation p as a sequence p = (p1, p2, p3,..., pt)
o shifts the character at position i to the position pi.
https://pollev.com/nitya
Permutation Cipher: Example
Plaintext:
Ciphertext:
https://pollev.com/nitya
3 Attacks On Substitution and Permutation Cipher
Exhaustive Search (aka Brute-Force Search)
• Attacker’s goal: is to find the key or to obtain some information of the
plaintext.
• Attack step: exhaustively search the keys, examine all possible keys one by
one.
o Eventually (although this might take very long time) the correct key can be found.
https://pollev.com/nitya
Terminology
• The key space size or size of key space is the total number of possible keys.
o For a 3-bit key, key space size = 8 (23)
• The key size or key length is the number of bits required to represent a key.
o For a 3-bit key, key length = 3
https://pollev.com/nitya
Exhaustive Search on Substitution Cipher
• Assume that the attacker knows a ciphertext C and the corresponding plaintext X.
• The attacker wants to find the key.
• Let S be the set of all possible substitution tables (i.e., the key space).
• Given X, C.
What is the key
o For each s in S space size here
− Compute X’ = DS(C); to find the key?
− If (X’== X) then break
o end-for Can the
attacker do
o Display ( “The key is ”, s ); better?
https://pollev.com/nitya
Known-Plaintext Attack on Substitution Cipher
https://pollev.com/nitya
Ciphertext-Only Attack on Substitution Cipher
• The attackers have access to ciphertext only (i.e., without the corresponding plaintext).
• Suppose an attacker knows that the plaintext is an English sentence.
• Let S be the set of all possible substitution tables. Given C.
o For each s in S
− Compute X = DS(C);
Can the
− if X contains words in the English dictionary, then break
attacker
o end-for do better?
o Display ( “The key is ”, s );
https://pollev.com/nitya
Ciphertext-Only Attack on Substitution Cipher
• Are there efficient ciphertext-only attacks on substitution cipher?
o Yes. Substitution cipher is vulnerable to frequency analysis attack.
• Hence, substitution cipher is not secure under ciphertext only attack, when
the plaintexts are English sentences.
https://pollev.com/nitya
https://pollev.com/nitya
Known-Plaintext Attack on Permutation Cipher
m= h e l l o wo r l d
c = e h l o l o wr d l
https://pollev.com/nitya
Attack
Plaintext: h e l l o w o r l d
P= (2, 1, 3 , 5, 4)
Ciphertext: e h l o l o w r d l
https://pollev.com/nitya
Takeaway
https://pollev.com/nitya
4 One-Time Pad
Recap: XOR Operation
• Commutative : A⊕B=B⊕A
• Associative : A⊕(B⊕C)=(A⊕B)⊕C
• Identity element : A ⊕ 0 = A
• Self-inverse : A ⊕ A = 0
https://pollev.com/nitya
One-Time Pad
• It is a pure substitution cipher.
Src: https://www.cryptomuseum.com/crypto/otp/index.htm
https://pollev.com/nitya
Example
• Correctness
Ciphertext 1 1 1 0 0 0 1
o For any x, k
o (x ⊕ k) ⊕ k = x ⊕ (k ⊕ k)=(x ⊕ 0) = x
Ciphertext Plaintext
https://pollev.com/nitya
Is this Secure?
https://pollev.com/nitya
5 Stream Cipher and IVs
OTP vs Stream Cipher
https://pollev.com/nitya
Stream Cipher
https://pollev.com/nitya
Stream Cipher With Same Keystream
• Consider that the same key is used to encrypt two different plaintexts
o X = x1 x2 x3 x4 x5 ; Y = y1 y2 y3 y4 y5
oU = X ⊕ K ; V = Y ⊕ K
https://pollev.com/nitya
Stream Cipher With Same Keystream
Reveals some
partial
information
X Y ⊕ about the PT
⊕ ⊕
X⊕Y
U V ⊕
https://pollev.com/nitya
Stream Cipher with Initialization Vector (IV)
• IV is random value used as additional input to generate the keystream
• Ensures that even if the same key is used for multiple encryptions, the generated
keystream will be different if unique IV is used.
• Pseudo-random generator (PRG or G) to generate keystream from a seed (i.e., IV).
• Encryption: use shared key k and initialization vector IV for the seed.
ciphertext = plaintext ⊕ PRG( k, IV)
https://pollev.com/nitya
Example: Stream Cipher with IV
Sender
0 0 1 0 1 1 0 Plaintext
⊕ ⊕ ⊕ ⊕ ⊕ ⊕ ⊕
K PRG 1 1 0 0 1 1 1 Key stream
Transmit CT, IV
IV
IV need not secret 1 1 1 0 0 0 1 Ciphertext
⊕ ⊕ ⊕ ⊕ ⊕ ⊕ ⊕
K PRG 1 1 0 0 1 1 1 Key stream
Receiver 0 0 1 0 1 1 0 Plaintext
https://pollev.com/nitya
Stream Cipher With IV
X Y ⊕
K1 K2
⊕ ⊕
X ⊕ Y ⊕ (K1⊕ K2)
U V ⊕
https://pollev.com/nitya
6 Modern Ciphers – DES and AES
Modern Cipher
• Designs of modern ciphers take into considerations of
o known-plaintext-attack, frequency analysis and other known attacks
• E.g.
o DES (Data Encryption Standard, 1977)
o RC4 (Rivest’s Cipher 4, 1987)
o A5/1 (used in GSM, 1987)
o AES (Advanced Encryption Standard, 2001)
• They are “supposed” to be secure so that any successful attack does not
perform noticeably better than exhaustive search.
https://pollev.com/nitya
Data Encryption Standard (DES)
• Symmetric-key block cipher developed by IBM in the early 1970s
• Security Concerns: Over time, DES's 56-bit key length has proven vulnerable to
brute-force attacks.
o There are 256 possible keys.
o While exhaustive search on 56 bits seemed infeasible in the 70s
o In 1998, a DES key was found in approximately 56 hours
https://pollev.com/nitya
Advanced Encryption Standard (AES)
• Currently, no known attacks on AES (there are some attacks on the mode-of-
operation)
• How many key bits is considered “secure”?
o NIST (link)
https://pollev.com/nitya
7 Block Cipher & Mode-of-Operations
Block Cipher
• DES and AES are also known as “Block Cipher”.
What?! Only 128 bits!
• A block cipher has fixed size input/output. How to encrypt the
Key ( 128, 192, 256 bits) movie!!!
• For large plaintext (say 10 MB), first divide into blocks, and then apply block cipher.
https://pollev.com/nitya
Mode-of-Operation: Electronic Code Book (ECB)
• Divides plaintext into blocks and then applies block cipher to each block, all
with the same key.
PT1 PT2 PT3
• CTj = EK(PTj)
• PTj = E-1K(CTj) K E K E K E
EK: encryption function using key K
E-1K: decryption function using key K
CT1 CT2 CT3
• Disadvantage?
o Same plaintext always corresponds to same ciphertext
https://pollev.com/nitya
Problem with ECB
https://pollev.com/nitya
Mode-of-Operation: Cipher Block Chaining (CBC)
https://pollev.com/nitya
Mode-of-Operation: Counter Mode (CTR)
• Xj = X 1 + j – 1
• CTj = EK(Xj) ⊕ PTj
K E K E K E
• PTj = EK(Xj) ⊕ CTj
• Advantages PT1 ⊕ PT2 ⊕ PT3 ⊕
o Semantic security
o Can be parallelized
CT1 CT2 CT3
• Similar to stream cipher
https://pollev.com/nitya
8 Meet in the Middle Attack ( on DES)
2-DES
• DES is not secure: improve it by multiple encryptions; c = DESk2(DESk1(m))
• Using exhaustive search, what is the amount of DES encryption/decryption
required?
o 256+56, meaning key-strength could be 112 bits ?
o Unfortunately, it is much less than that via “meet-in-the-middle” attack.
https://pollev.com/nitya
Meet-in-the-Middle Attack
Meet-in-the-middle
k1 = 001
m'2 k2 = 010
k1 = 010 c'2
m'3 k2 = 011
Plaintext m, k1 = 011 c'3 Ciphertext c
m'4 k2 = 100
k1 = 100 c'4
Encryption k2 = 101 Decryption
k1 = 101 m'5
c'5
k2 = 110
k1 = 110 m'6
c'6
k2 = 111
k1 = 111 m'7
c'7
• Previous Example
o Assumes 3-bit keys, 2 such keys.
o Using two keys, there are 23 + 23 = 2 x 23 = 24 = 16 possibilities.
o However, the attack only perform 8 encryptions and 8 decryptions.
https://pollev.com/nitya
Remedy: 3-DES with 2 keys
https://pollev.com/nitya
9 Padding Oracle Attack (on AES CBC)
Recall: Oracle in Security Analysis
• Encryption Oracle. On query a plaintext x, the oracle outputs the ciphertext Ek(x)
where the key k is a secret key.
• While encryption oracle make sense, it is not immediately clear why we need to
consider attacker with decryption oracle. Padding oracle attack illustrate the
need.
https://pollev.com/nitya
Padding Format
https://pollev.com/nitya
Padding - PKCS#7 (Padding Standard)
• Suppose the block size is 8 bytes, and the last block has 5 bytes (thus 3 extra
bytes required), padding is done as follow: Value of each
DD DD DD DD DD DD DD DD DD DD DD DD DD 03 03 03 padded byte is the
number of bytes
• In general, padding are: that are added
01
If the last block is full, i.e., it has 8 bytes,
02 02 an extra block is added where each byte is
03 03 03 08 (i.e., the block size).
08 08 08 08 08 08 08 08
https://pollev.com/nitya
Padding Oracle
• Attacker’s goal:
o The plaintext of (iv, c)
https://pollev.com/nitya
Padding Oracle Attack on AES CBC Mode
• AES block is 16 bytes, for ease of presentation, we consider 8-byte block here
• AES CBC mode is not secure against padding oracle attack.
https://pollev.com/nitya
PTj = E-1K(CTj) ⊕ CTj-1
PT1 = E-1K(CT1) ⊕ CT0
AES Decryption by the Oracle
8 bytes 1 block of IV 8 bytes 1 block of ciphertext
CT0 = IV = IV0 IV1 IV2 IV3 IV4 IV5 IV6 IV7 C0 C1 C2 C3 C4 C5 C6 C7 = CT1
Block Cipher
decryption
I0 I1 I2 I3 I4 I5 I6 I7 Intermediate state
1. Original, X4 = I4 ⊕ IV4 X0 X1 X2 X3 X4 03 03 03
2. Original, I4 = X4 ⊕ IV4 What is X4?
https://pollev.com/nitya
Attackers Steps
• End-for-loop
• Idea: Attacker uses the padding oracle by submitting modified CTs and
observing the system's responses (yes/no) to infer the correct padding bytes,
eventually to learning about the plaintext.
https://pollev.com/nitya
Force a padding of 0x04 so that padding
oracle return yes when last 4 bytes is 0x04
Why Last Three Bytes 0x07?
8 bytes 1 block of IV 8 bytes 1 block of ciphertext
IV0 IV1 IV2 IV3 IV4 IV5 IV6 IV7 C0 C1 C2 C3 C4 C5 C6 C7
⊕
Block Cipher
0 0 0 0 t ?07 ?
07 ?
07 decryption
IV '= IV'0 IV'1 IV'2 IV'3 IV'4 IV'5 IV'6 IV'7 I0 I1 I2 I3 I4 I5 I6 I7 Intermediate state
⊕
1. No change for I7 = X7 ⊕ IV7 = 0x03 ⊕ IV7
2. When IV' is used X'7 = 0x04 = I7 ⊕ IV'7 X0 X1 X2 X3 X4 03
04 03
04 03
04
3. Replace I7 from step 1,
1. 0x04 = 0x03 ⊕ IV7 ⊕ IV'7 What is X4?
2. IV'7 = IV7 ⊕ 0x03 ⊕ 0x04 = IV7 ⊕ 0x07
https://pollev.com/nitya
Brute force t until the padding oracle return
How about t? yes when last 4 bytes is 0x04
IV '= IV'0 IV'1 IV'2 IV'3 IV'4 IV'5 IV'6 IV'7 I0 I1 I2 I3 I4 I5 I6 I7 Intermediate state
https://pollev.com/nitya
How to Compute X4?
8 bytes 1 block of IV 8 bytes 1 block of ciphertext
IV0 IV1 IV2 IV3 IV4 IV5 IV6 IV7 C0 C1 C2 C3 C4 C5 C6 C7
⊕
Block Cipher
0 0 0 0 t ?07 ?
07 ?
07 decryption
IV '= IV'0 IV'1 IV'2 IV'3 IV'4 IV'5 IV'6 IV'7 I0 I1 I2 I3 I4 I5 I6 I7 Intermediate state
1. No change, I4 = IV4 ⊕ X4
2. X'4 = 0x04 = I4 ⊕ IV'4 X0 X1 X2 X3 04 03
04 03
04 03
04
3. X'4 = 0x04 = IV4 ⊕ X4 ⊕ t ⊕ IV4 Find t such that X'4 = 0x04,
4. X4 = 0x04 ⊕ IV4 ⊕ t ⊕ IV4 = t ⊕ 0x04 Until oracle return yes
https://pollev.com/nitya
Remarks
• We can easily extend the algorithm to find the full plaintext.
• There is still a gap. The algorithm needs to know the initial padding length.
• Fortunately, it is easy to determine the length (exercise).
• This attack is practical. There are protocols between a client and server which
performs this:
o If the client submits a ciphertext whose plaintext is not padded correctly, the server will reply
with an error message.
o Now, if an attacker obtaines a ciphertext, the attacker could interact with the server to get the
plaintext.
https://pollev.com/nitya
More on Padding Oracle Attack
• Prevention:
o One method is to deny access to such oracle.
− Might not be feasible in some applications.
o Changing the padding standard may mitigate the attack.
− However, there could be other smarter way to attack the new padding.
https://pollev.com/nitya
Summary
• Designs of various symmetric key encryption schemes
o One-time pad: “unbreakable” even if attacker has sufficient time to exhaustively search
o Stream Cipher: xor’ing with a “pseudo-random” string
o Block Cipher: Mode of operations (CBC, ECB, CTR)
https://pollev.com/nitya