L3 Symmetric Ciphers
L3 Symmetric Ciphers
2
Categories of Modern cryptography
Introduction to Computer and Network Security CIS441
● Symmetric Crypto.
○ use a shred key (secret key) for encryption and decryption.
4
Categories of Modern cryptography
Introduction to Computer and Network Security CIS441
Encryption Decryption
Plaintext Ciphertext Plaintext
Algorithm Algorithm
6
Symmetric-Key Encryption: Definition
Introduction to Computer and Network Security CIS441
Encryption Decryption
Plaintext Ciphertext Plaintext
Algorithm Algorithm
7
Stream Ciphers
Introduction to Computer and Network Security CIS441
• A stream cipher is an algorithm that generates a long keystream from a (short) key
• Encrypts a digital data stream one bit or one byte at a time
• One time pad is example; but practical limitations
• Typical approach for stream cipher:
► Key (K ) used as input to bit-stream generator algorithm
► Algorithm generates cryptographic bit stream (ki ) used to encrypt plaintext
8
Stream Ciphers
Introduction to Computer and Network Security CIS441
● Stream ciphers were very popular because they are simple and fast
○ Used in WEP (RC4) and Cellular phones (A5/1, A5/2)
● They are still hard to use
○ Can’t ever reuse initial state (keys, initialization vector)
■ This was the major reason why WEP was broken
○ Stream ciphers can only encrypt a limited amount of data with the same seed
○ Ciphertext can be changed (just like OTP)
● Many popular stream ciphers are broken (RC4, A5/1, A5/2)
Stream Ciphers
Introduction to Computer and Network Security CIS441
• Substitution Attack:
– M = “Pay me $100.00”
– E(M) = M ⊕ K
– Suppose Eve knows M and E(M) but doesn't know K
– She can substitute M for M' by replacing E(M) with E’(M): Eve modifies this!
• E'(M) = E(M) ⊕ M ⊕ M' = M ⊕ K ⊕ M ⊕ M' = K ⊕ M'
• Eve can then replace E(M) with E'(M), which Bob will decrypt message as M' (“Pay me $900.00”)
10
Block Ciphers
Introduction to Computer and Network Security CIS441
• A block cipher is a function that replaces a fixed length input with a fixed
length output
– The input/output size is called the “block size”
11
• n-bit block cipher takes n bit plaintext and produces n bit ciphertext
• 2n possible different plaintext blocks
• Encryption must be reversible (decryption possible)
• Each plaintext block must produce unique ciphertext block
• Total transformations is 2n!
00 11 00 11
01 10 01 10
10 00 10 01
11 01 11 01
12
Ideal Block Cipher
Introduction to Computer and Network Security CIS441
problems
13
• Confusion
o Hide the relationship between the ciphertext and the key
o Make relationship between ciphertext and key as complex as possible
o Apply complex (non-linear) substitution algorithm
o Substitution a -> b (Caesar cipher)
• Diffusion
o Hide the relationship between the plaintext and ciphertext
o Distribute plaintext statistics over the ciphertext (statistical analysis hard)
o Transposition or Permutation • abcd-> dacb (DES)
o repeatedly apply permutation (transposition) to data, and then apply function 16
Confusion and Diffusion
Introduction to Computer and Network Security CIS441
17
18
Basic Form of Modern Block Ciphers
Introduction to Computer and Network Security CIS441
Sub-Key #1
Rounds of Sub-Key #2
Encryption
Sub-Key #3
i=1,2,…,n
…
Sub-Key #n
Postprocessing
Ciphertext block 19
Substitution
Introduction to Computer and Network Security CIS441
20
Block ciphers: Brute-force attacks?
Introduction to Computer and Network Security CIS441
21
22
Feistel Structure for Block Ciphers
Introduction to Computer and Network Security CIS441
24
Feistel Structure
Introduction to Computer and Network Security CIS441
25
● Let LEi and REi denote the output half-blocks at the end of the ith round of
processing. E’ denotes encryption.
● The relationship between output of the ith round and the output of the
previous round, (i − 1)th round, is given by
𝑹𝑬𝒊 = 𝑳𝑬𝒊 − 𝟏 ⊕ 𝑭 𝑹𝑬𝒊 − 𝟏, 𝑲𝒊
𝑳𝑬𝒊 = 𝑹𝑬𝒊 − 𝟏
● The decryption algorithm is exactly the same as the encryption algorithm with the
only difference that the round keys are used in the reverse order.
● let LDi and RDi denote the left half and the right half of the output of the ith round.
○ LD1 and RD1 : the output of the first decryption round
○ LD0 and RD0: the input of the first decryption round
● The relationship between the two
halves that are input to the first decryption
round and the last round in the encryption
algorithm is:
27
Feistel Structure
Introduction to Computer and Network Security CIS441
28
Feistel Example
Introduction to Computer and Network Security CIS441
● Blocks =32 bits (two 16-bit halves) , key size =24 bits.
● Encryption :After round 14 we have DE7F03A6
○ L14(E) = DE7F and R14(E) = 03A6
○ K15 =12DE52.
○ After round 15, we have
○ L15(E) = 03A6
○ R15(E) = F(03A6, 12DE52) ⊕DE7F
29
Feistel Example
Introduction to Computer and Network Security CIS441
● Decryption :
○ L1(D)=R15(E) , R1(D)=L15(E)
○ L2(D)=R14(E) , R2(D)=L14(E)
○ L1(D)=F(03A6, 12DE52) ⊕DE7F
○ R1(D)=03A6
○ L2(D)= 03A6=R14(E)
○ R2(D)= F(03A6, 12DE52)⊕F((03A6, 12DE52)⊕DE7F]= DE7F=L14(E)
30
Feistel Example
Introduction to Computer and Network Security CIS441
R2
L14 R14 L2
K15
L15 R15 L1 R1
31
32
DES structure
Introduction to Computer and Network Security CIS441
33
DES structure
Introduction to Computer and Network Security CIS441
34
Initial and Final Permutations P-Boxes
Introduction to Computer and Network Security CIS441
35
DES Function
Introduction to Computer and Network Security CIS441
Four sections:
● Expansion D-box:
○ RI−1 is a 32-bit input and KI is a 48-bit key,
we first need to expand RI−1 to 48 bits.
o RI−1 is divided into 8 4-bit sections.
o 4-bit is expanded to 6 bits.
36
DES Function
Introduction to Computer and Network Security CIS441
Four sections:
● XOR : on the expanded right section and the round key
● S-Boxes : mixing (confusion). DES uses 8 S-boxes, each with a 6-bit input
and a 4-bit output.
37
DES Function
Introduction to Computer and Network Security CIS441
38
DES Function
Introduction to Computer and Network Security CIS441
● S-boxes example: DES (S5), mapping 6-bit input into a 4-bit output:
○ an input "011011" has outer bits "01" and inner bits "1101"; the corresponding
output would be "1001”
Key Generation
Introduction to Computer and Network Security CIS441
● Compression D-box:
○ changes the 58 bits to 48 bits, which are used as akey for a round.
41
Simplified DES
Introduction to Computer and Network Security CIS441
42
Simplified DES
Introduction to Computer and Network Security CIS441
S-DES Operations
Introduction to Computer and Network Security CIS441
45
S-DES S-Boxes
Introduction to Computer and Network Security CIS441
46
S-DES Summary
Introduction to Computer and Network Security CIS441
►Educationalencryption algorithm
►S-DES expressed as functions:
►Security of S-DES:
►10-bit key, 1024 keys: brute force easy
47
50
The Avalanche Effect
Introduction to Computer and Network Security CIS441
● Aim: small change in key (or plaintext) produces large change in ciphertext
● Avalanche effect is present in DES (good for security)
● Following examples show the number of bits that change in output when two
different inputs are used, differing by 1 bit
○ Plaintext 1: 02468aceeca86420
○ Plaintext 2: 12468aceeca86420
○ Ciphertext difference: 32 bits
○ Key 1: 0f1571c947d9e859
○ Key 2: 1f1571c947d9e859
○ Ciphertext difference: 30
51
● Block cipher: operates on fixed length b-bit input to produce b-bit ciphertext
● What about encrypting plaintext longer than b bits?
● Break plaintext into b-bit blocks (padding if necessary) and apply cipher on
each block
● Security issues arise: different modes of operation have been developed
● Encryption with Block Ciphers: Modes of Operation
○ Electronic Code Book mode (ECB)
○ Cipher Block Chaining mode (CBC)
○ Output Feedback mode (OFB)
○ Cipher Feedback mode (CFB)
○ Counter mode (CTR)
○ Galois Counter Mode (GCM) 52
Block Cipher Modes of Operation
Introduction to Computer and Network Security CIS441
53
54
Electronic Code Book (ECB)
Introduction to Computer and Network Security CIS441
55
56
Electronic Code Book (ECB)
Introduction to Computer and Network Security CIS441
57
Original image
58
bitmaps in ECB mode
ECB Mode
Introduction to Computer and Network Security CIS441
59
ECB Mode
Introduction to Computer and Network Security CIS441
● Advantages
○ no block synchronization between sender and receiver is required
○ bit errors caused by noisy channels only affect the corresponding
○ block but not succeeding blocks
○ Block cipher operating can be parallelized
○ advantage for high-speed implementations
● Disadvantages
○ ECB encrypts highly deterministically
○ identical plaintexts result in identical ciphertexts
○ an attacker recognizes if the same message has been sent twice
○ plaintext blocks are encrypted independently of previous blocks
■ an attacker may reorder ciphertext blocks which results in valid plaintext
○ No integrity protection, susceptible to replay attacks 60
Substitution Attack on ECB – Integrity
Introduction to Computer and Network Security CIS441
61
62
How to overcome the problem of ECB?
Introduction to Computer and Network Security CIS441
63
64
Cipher Block Chaining mode (CBC)
Introduction to Computer and Network Security CIS441
● Input to encryption algorithm is XOR of next block plaintext and preceding block ciphertext
● Initialization Vector (IV) must be known by sender/receiver, but secret from attacker
● There are two main ideas behind the CBC mode:
○ The encryption of all blocks are “chained together”
○ ciphertext yi depends not only on block xi but on all previous plaintext blocks as well
○ The encryption is randomized by using an initialization vector (IV)
65
Encryption Decryption
C0 = E(IV P0, K), P0 = IV D(C0, K),
C1 = E(C0 P1, K), P1 = C0 D(C1, K),
C2 = E(C1 P2, K),… P2 = C1 D(C2, K),…
● Encryption: Ci = E(Ci-1 Pi ,K) , i ≥ 1
● Decryption: Pi=Ci-1 D(Ci ,K) , i ≥ 1
67
68
CBC Mode: Padding & Security
Introduction to Computer and Network Security CIS441
● What if you want to encrypt a message that isn’t a multiple of the block size?
○ 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
69
70
Output Feedback mode (OFB)
Introduction to Computer and Network Security CIS441
I0=
I1 I2 I1 I2
O0 O1 O2 O0 O1
71
• OFB is also nondeterministic,hence, encrypting the same plaintext twice results in different ciphertexts.
• One advantage: the block cipher computations are independent of the plaintext.
• Hence, one can precompute one or several blocks si of key stream material.
72
Cipher Feedback mode (CFB)
Introduction to Computer and Network Security CIS441
Encryption Decryption
C0 = E(IV ,K) P0 P0 = E(IV ,K) C0
C1 = E(C0,K ) P1 P1 = E(C0,K ) C1
C2 = E(C1,K ) P2… P2 = E(C1,K ) C2,…
73
74
Counter (CTR) Mode
Introduction to Computer and Network Security CIS441
The counter
increments per block
to ensure each block
cipher output is
different.
C1 Cm
● Encryption
● E(P, K):
○ Split M in plaintext blocks P1...Pm (each of block size n)
○ Choose random nonce
○ Compute and output (Nonce, C1, …, Cm) 75
C1 C2 Cm
76
Counter (CTR) Mode
Introduction to Computer and Network Security CIS441
● CTR is popular for random access Use block cipher like a stream cipher
Encryption Decryption
C0 = P0 E(IV, K), P0 = C0 E(IV, K),
C1 = P1 E(IV+1, K), P1 = C1 E(IV+1, K),
C2 = P2 E(IV+2, K),… P2 = C2 E(IV+2, K),…
● Encryption: Ci = Pi E(IV+i ,K) , i ≥ 1
● Decryption: Pi=Ci E(IV+i ,K) , i ≥ 1
○ The input to the block cipher is a counter which assumes a different value every time the block cipher
computes a new key stream block
○ the CTR mode can be parallelized since the 2nd encryption can begin before the 1st one has finished
○ Desirable for high-speed implementations, e.g., in network routers
77
78
IVs and Nonces
Introduction to Computer and Network Security CIS441
Error propagation
Introduction to Computer and Network Security CIS441
82
Lack of Integrity and Authenticity
Introduction to Computer and Network Security CIS441
83
P a y M a l $ 1 0 0
M 0x50 0x61 0x79 0x20 0x4d 0x61 0x6c 0x20 0x24 0x31 0x30 0x30
EK(i) 0x8a 0xe3 0x5e 0xcf 0x3b 0x40 0x46 0x57 0xb8 0x69 0xd2 0x96
C 0xda 0x82 0x27 0xef 0x76 0x21 0x2a 0x77 0x9c 0x58 0xe2 0xa6
84
Lack of Integrity and Authenticity
Introduction to Computer and Network Security CIS441
P a y M a l $ 1 0 0
M 0x50 0x61 0x79 0x20 0x4d 0x61 0x6c 0x20 0x24 0x31 0x30 0x30
EK(i) 0x8a 0xe3 0x5e 0xcf 0x3b 0x40 0x46 0x57 0xb8 0x69 0xd2 0x96
C 0xda 0x82 0x27 0xef 0x76 0x21 0x2a 0x77 0x9c 0x58 0xe2 0xa6
85
Padi = Mi ⊕ Ci Padi = 0x58 ⊕ 0x31 Solve for the ith byte of the pad
= 0x69
C'i = M'i ⊕ Padi C'i = 0x39 ⊕ 0x69 Compute the changed ith byte
= 0x50
C 0xda 0x82 0x27 0xef 0x76 0x21 0x2a 0x77 0x9c 0x58 0xe2 0xa6
C’ 0xda 0x82 0x27 0xef 0x76 0x21 0x2a 0x77 0x9c 0x50 0xe2 0xa6
86
Lack of Integrity and Authenticity
Introduction to Computer and Network Security CIS441
C' 0xda 0x82 0x27 0xef 0x76 0x21 0x2a 0x77 0x9c 0x50 0xe2 0xa6
EK(i) 0x8a 0xe3 0x5e 0xcf 0x3b 0x40 0x46 0x57 0xb8 0x69 0xd2 0x96
P' 0x50 0x61 0x79 0x20 0x4d 0x61 0x6c 0x20 0x24 0x39 0x30 0x30
87
P a y M a l $ 9 0 0
Cryptography Roadmap
Introduction to Computer and Network Security CIS441
Symmetric-key Asymmetric-key
Cryptographic Hashes
Message Authentication Codes (MACs)
• Doesn’t do
– Data integrity Middle-Person attack
– Source authentication
93
Hash Function
Introduction to Computer and Network Security CIS441
● Since a message hash depends on all the bits in the input message
○ any alteration of the input message during transmission would cause its message digest
to not match with its original message hash.
● Used to check for forgeries, unauthorized alterations, etc.
● The only difference between the two messages shown above is the extra space between the words
“brown” and “fox” in the second message
94
Hash Function: Properties
Introduction to Computer and Network Security CIS441
96
Hash Function Security Requirements
Introduction to Computer and Network Security CIS441
97
● Birthday attack: Finding a collision on an n-bit output requires only 2n/2 tries
on average
○ This is why a group of 23 people are >50% likely to have at least one birthday in common
○ Compare to second preimage resistance, which requires 2n - 1 tries on average because you
are constrained to a specified hash output
○ Brute-force collision search is only 2n/2, not 2n
○ For SHA-1, this means 280 vs. 2159
● Intuitively, the hash function behaves like a random mapping, so H(x) maps x
to a random value
○ An attacker should not be able to predict how a change in input affects the output
98
Hash Function: Examples
Introduction to Computer and Network Security CIS441
● MD5
○ Output: 128 bits
○ Security: Completely broken
● SHA-1
○ Output: 160 bits
○ Security: Completely broken in 2017
○ Was known to be weak before 2017, but still used sometimes
● SHA-2
○ Output: 256, 384, or 512 bits (sometimes labeled SHA-256, SHA-384, SHA-512)
○ Not currently broken, but some variants are vulnerable to a length extension attack
○ Current standard
● SHA-3 (Keccak)
○ Output: 256, 384, or 512 bits
○ Current standard
99
Message Authentication
Introduction to Computer and Network Security CIS441
101
103
104
Message hashing in a communication network.
Introduction to Computer and Network Security CIS441
105
106
Message hashing in a communication network.
Introduction to Computer and Network Security CIS441
107
108
Cryptography Roadmap
Introduction to Computer and Network Security CIS441
Symmetric-key Asymmetric-key
110
MACs: Usage
Introduction to Computer and Network Security CIS441
● Alice wants to send M to Bob, but doesn’t want Mallory to tamper with it
● Alice sends M and T = MAC(K, M) to Bob
● Bob receives M and T
● Bob computes MAC(K, M) and checks that it matches T
● If the MACs match, Bob is confident the message has not been tampered
with (integrity)
Alice Insecure Channel Bob
Key Key
T 111
MACs: Definition
Introduction to Computer and Network Security CIS441
● Two parts:
○ KeyGen() → K: Generate a key K
○ MAC(K, M) → T: Generate a tag T for the message M using key K
■ Inputs: A secret key and an arbitrary-length message
■ Output: A fixed-length tag on the message
● Properties
○ Correctness: Determinism **
○ Efficiency: Computing a MAC should be efficient
○ Security: secure
○ **A deterministic encryption scheme is a cryptosystem which always produces the same ciphertext for a given
plaintext and key, even over separate executions of the encryption algorithm
112
MAC Properties
Introduction to Computer and Network Security CIS441
113
114
HMAC :Keyed Hash Function as MAC
Introduction to Computer and Network Security CIS441
115
K K K
C1 C2 CN 28
● HMAC(K, M):
○ Compute K' as a version of K that is the length of the hash output
■ If K is too short, pad K with 0’s to make it n bits
■ If K is too long, hash it so it’s n bits
○ Output H((K' ⊕ opad) || H((K' ⊕ ipad) || M))
● Use K' to derive two different keys
○ opad (outer pad) is the hard-coded byte 0x5c repeated until it’s the same length as K'
○ ipad (inner pad) is the hard-coded byte 0x36 repeated until it’s the same length as K'
○ As long as opad and ipad are different, you’ll get two different keys
○ For paranoia, the designers chose two very different bit patterns, even though they
theoretically need only differ in one bit
122
HMAC Properties
Introduction to Computer and Network Security CIS441
● HMAC is a hash function, so it has the properties of the underlying hash too
○ Given HMAC(K, M) and K, an attacker can’t learn M
○ If the underlying hash is secure, HMAC doesn’t leak information about the message M
123
MACs from Block Ciphers: CBC-MAC
Introduction to Computer and Network Security CIS441
124
125
Confidentiality and Integrity
Introduction to Computer and Network Security CIS441
MAC-then-Encrypt or Encrypt-then-MAC?
Introduction to Computer and Network Security CIS441
● MAC-then-encrypt
○ First compute MAC(K2, M)
○ Then encrypt the message and the MAC together: Enc(k1, M || MAC(K2, M))
● Encrypt-then-MAC
○ First compute Enc(K1, M)
○ Then MAC the ciphertext: MAC(K2, Enc(K1, M))
● Which is better?
○ In theory, both are secure if applied properly
○ MAC-then-encrypt has a flaw: You don’t know if tampering has occurred until after decrypting
■ Attacker can supply arbitrary tampered input, and you always have to decrypt it
■ Passing attacker-chosen input through the decryption function can cause side-channel
leaks
● Always use encrypt-then-MAC because it’s more robust to mistakes
127
AE and AEAD Encryption
Introduction to Computer and Network Security CIS441
Encrypt-then-MAC (EtM)
MAC-then-Encrypt (MtE)
Encrypt-and-MAC (E&M)
129
Example Uses of MAC
Introduction to Computer and Network Security CIS441
130