15-853: algorithms in the real world15853-f19/slides/crypto1.pdf · computational secrecy •...

47
Announcements Homework 2 graded. Recitation tomorrow: Eigenvalues and SVD. HW solution discussion. No lecture next Tuesday (November 5 th ). Make-up lecture next Friday (November 8 th ). 15-853 Page 1

Upload: others

Post on 02-Mar-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

Announcements

• Homework 2 graded.• Recitation tomorrow:

• Eigenvalues and SVD.• HW solution discussion.

• No lecture next Tuesday (November 5th).• Make-up lecture next Friday (November 8th).

15-853 Page 1

Page 2: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 2

15-853:Algorithms in the Real World

Cryptography #1

Page 3: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 3

Cryptography Outline

Introduction: terminology, cryptanalysis, security Private-Key Algorithms: Rijndael, DESNumber Theory

Page 4: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 4

Cryptography Outline

Introduction:– terminology – cryptanalytic attacks– security

Private-Key Algorithms: Rijndael, DESNumber Theory

Page 5: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 5

Some Terminology

Cryptography – the general termCryptology – the theoryEncryption – encoding (but sometimes used as general term)Cryptanalysis – breaking codesCipher – a method or algorithm for encrypting or decrypting

Page 6: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 6

More Definitions

Private Key or Symmetric: Key1 = Key2

Public Key or Asymmetric: Key1 ≠ Key2Key1 or Key2 is public depending on the protocol

Encryption

Decryption

Key1

Key2

Ciphertext

Ek(m) = c

Dk(c) = m

Original Plaintext

Plaintext

Key Generator

Page 7: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

Private key encryption

15-853 Page 7

Alice Bob

Eve

Encrypt Decryptm c m

k k

We assume Eve knows everything about the encryption scheme (except the secret key)

Page 8: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 8

What does it mean to be secure?Attempt 1: it should be impossible for Eve to get the key.

Attempt 2: it should be impossible for Eve to recover m.

Attempt 3: impossible for Eve to recover any bit of m.

Attempt 4: regardless of any information that Eve has, c should not leak any additional information about m.

Page 9: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

One-time pad

• Key generation:• Input: length n (in unary)• Output: uniformly random k ∈ {0,1}n

• Encryption:• Input: m ∈ {0,1}n, k ∈ {0,1}n

• Output: c = m ⊕ k• Decryption:

• Input: c ∈ {0,1}n, k ∈ {0,1}n

• Output: m = c ⊕ k

15-853 Page 9

Page 10: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

One-time pad

One-time pad is perfectly secret:• Let M, C be r.v.s for the message and ciphertext.• For every message m and ciphertext c with Pr[C=c] > 0:

Pr[M = m | C = c] = Pr[M = m]

• Ciphertext contains no information about message!

15-853 Page 10

Page 11: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

One-time pad

One-time pad is perfectly secret.

Proof:Pr[C = c | M = m] = Pr[m ⊕ K = c] = Pr[K = m ⊕ c] = 2-n

Pr[C = c] = Σm Pr[C = c | M = m] Pr[M = m]= 2-n Σm Pr[M = m] = 2-n

Pr[M = m | C = c] = Pr[C = c | M = m] Pr[M = m] / Pr[C = c]= Pr[M = m]

15-853 Page 11

Can we reuse a one-time pad?No.

Page 12: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

The importance of randomness

• Previous proof only works if key is truly uniformly random.• An adversary can exploit biases in randomness.• There are techniques to extract uniformly random bits

from biased sources.• E.g: suppose we have a biased coin with probabilities p

and 1 – p for heads and tails.• How to obtain uniformly random bits from this coin?• Flip twice:

• If (heads, tails) then output 1.• If (tails, heads) then output 0.• If (heads, heads) or (tails, tails) then no output.

15-853 Page 12

Page 13: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

Computational secrecy

• Perfect secrecy requires the key to be at least as long as the message. This is impractical!

• We need to settle for a weaker definition.• Any efficient adversary succeeds in breaking the

scheme with at most negligible probability.• Efficient = runs in probabilistic polynomial time (PPT).• Negligible = goes to zero faster than any inverse poly:

– A positive function f is negligible if for every positive integer c, there exists Nc such that:

f(n) < n-c, for all n > Nc

– Denoted as f = negl(n).

15-853 Page 13

Page 14: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 14

Cryptanalytic Attacks

c = ciphertext messagesm = plaintext messages

Ciphertext Only: Attacker has multiple cs but does not know the corresponding ms

Known Plaintext: Attacker knows some number of (c,m) pairs.

Chosen Plaintext: Attacker gets to choose ms and generatecs before.

Chosen Ciphertext: Attacker gets to choose cs and generate ms before.

Page 15: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 15

Cryptography Outline

Introduction: terminology, cryptanalysis, security Private-Key Algorithms:

– Block ciphers and product ciphers– Rijndael, DES

Number Theory

Page 16: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 16

Private Key Algorithms

Encryption

Decryption

Key1

Key1

Ciphertext

Ek(M) = C

Dk(C) = M

Original Plaintext

Plaintext

What granularity of the message does Ek encrypt?

Page 17: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 17

Private Key Algorithms

Block Ciphers: blocks of bits at a time– DES (Data Encryption Standard)

Banks, linux passwords (almost), SSL, kerberos, …– Blowfish (SSL as option)– IDEA (used in PGP, SSL as option)– Rijdael (AES) – the new standard

Page 18: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 18

Private Key: Block Ciphers

A Block cipher C is a function with:• Input: a key k ∈ {0,1}|k|, block x ∈ {0,1}n (with|k| ≤ n)• Output: a block y ∈ {0,1}n

• Objective: should be hard to distinguish from a random permutation from {0,1}n to {0,1}n.

• We can chop long messages into blocks.• Suppose we encrypt each block as c = C(k, m).

• What’s the problem with this?• Equal messages have equal encryptions!

Page 19: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 19

Private Key: Block Ciphers

Intuition: generate a “fresh” one-time pad for each block.

Counter (CTR) mode:

ctr

ctr

ctr+1

m1

c1

C(k, ⋅)

ctr+2

m2

c2

C(k, ⋅)

ctr+3

m3

c3

C(k, ⋅)

Page 20: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

Block cipher implementations

15-853 Page 20

Page 21: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 21

Iterated Block Ciphers

Consists of n rounds

R = the “round” functionsi = state after round iki = the ith round key

R

R

R

s1

.

.

.

m

c

.

.

.

key

k1

k2

kn

s2

Page 22: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 22

Iterated Block Ciphers: DecryptionRun the rounds in reverse.Requires that R has an

inverse.R-1

R-1

R-1

s1

.

.

.

m

c

.

.

.

key

k2

kn

s2

k1

Page 23: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 23

Feistel Networks

• Run with round keys in reverse order to decrypt.

• Used by DES (the Data Encryption Standard)

Image: “Feistel cipher diagram” by Amirki CC BY-SA 3.0

Page 24: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 24

Substitution-Permutation network

Each round has two components:– Substitution (S-box)

one-to-one mapping of subblocks.

– Permutation (P-box)Mix the bits around.

Both operations are invertible.

Avalanche effect: changing one bit of m affects all of c.

Image: “Substitution-Permutation Network” by GaborPete CC BY-SA 3.0

Page 25: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 25

Rijndael

Selected by AES (Advanced Encryption Standard, part of NIST) as the new private-key encryption standard.

Based on an open “competition”.– Competition started Sept. 1997.– Narrowed to 5 Sept. 1999

• MARS by IBM, RC6 by RSA, Twofish by Counterplane, Serpent, and Rijndael

– Rijndael selected Oct. 2000.– Official Nov. 2001 (AES page on Rijndael)

Designed by Rijmen and Daemen (Dutch)

Page 26: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 26

Goals of RijndaelResistance against known attacks:

– Differential cryptanalysis– Linear cryptanalysis– Truncated differentials– Square attacks– Interpolation attacks– Weak and related keys

Speed + Memory efficiency across platforms– 32-bit processors– 8-bit processors (e.g smart cards)– Dedicated hardware

Design simplicity and clearly stated security goals

Page 27: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 27

High-level overview

An iterated block cipher with– 10–14 rounds, – 128-256 bit blocks, and – 128-256 bit keys

Mathematically reasonably sophisticated

Page 28: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 28

Blocks and Keys

The blocks and keys are organized as matrices of bytes. For the 128-bit case, it is a 4x4 matrix.

151173

141062

13951

12840

bbbbbbbbbbbbbbbb

151173

141062

13951

12840

kkkkkkkkkkkkkkkk

Data block Key

b0, b1, …, b15 is the order of the bytes in the stream.

Page 29: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 29

Galois Fields in RijndaelUses GF(28) over bytes.The irreducible polynomial is:

M(x) = x8 + x4 + x3 + x + 1 or 100011011 or 0x11B

Also uses degree 3 polynomials with coefficients from GF(28).These are kept as 4 bytes (used for the columns)The polynomial used as a modulus is:

M(x) = 00000001x4 + 00000001 or x4 + 1Not irreducible, but we only need to find inverses of

polynomials that are relatively prime to it.

Page 30: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 30

Each round

03

12

RotateRows

.

.

Bytesubstitution

Mixcolumns

+

Keyi

outin

The inverse runs the steps and rounds backwards.Each step must be reversible!

Page 31: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 31

Byte Substitution

Non linear: y = b-1 (done over GF(28))Linear: z = Ay + B (done over GF(2), i.e., binary)

=

=

01100011

10001111

11000111

11100011

11110001

BA

To invert the substitution:y = A-1(z - B) (the matrix A is nonsingular)b = y-1 (over GF(28))

Page 32: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 32

Mix Columns

For each column a in data block

a0a1a2a3

compute b(x) = (a3x3+a2x2+a1x+a0)(3x3+x2+x+2) mod x4+1

where coefficients are taken over GF(28).

New column b is b0b1b2b3

where b(x)=b3x3+b2x2+b1x+b0

Page 33: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 33

Implementation

=

2113321113211132

C

Using xj mod (x4 + 1) = x(j mod 4)

M(x) is not irreducible, but the rows of C and M(x) are coprime, so the transform can be inverted.

(a3x3+a2x2+a1x+a0)(3x3+x2+x+2) mod x4+1

= (2a0+3a1+a2+a3) +(a0+2a1+3a2+a3)x +(a0+a1+2a2+3a3)x2 +(3a0+a1+a2+2a3)x3

Therefore, b = C • a

Page 34: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 34

Generating the round keys

f

+ + + +

Words corresponding to columns of the key

f =b1b2b3b4

b2b3b4b1

+

rotate sub byte consti

Page 35: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 35

PerformancePerformance: (64-bit AMD Athlon 2.2Ghz, 2005, Open SSL):

Algorithm Bits/key Mbits/secDES-cbc 56 399Blowfish-cbc 128 703Rijndael-cbc 128 917

Hardware implementations go up to 32 Gbits/sec

Page 36: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 36

Cryptography Outline

Introduction: terminology, cryptanalysis, security Private-Key Algorithms: Rijndael, DESNumber Theory

– Groups

Page 37: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 37

Groups

A Group (G,*,I) is a set G with operator * such that:1. Closure. For all a,b ∈ G, a * b ∈ G2. Associativity. For all a,b,c ∈ G, a*(b*c) = (a*b)*c3. Identity. There exists I ∈ G, such that for all

a ∈ G, a*I=I*a=a4. Inverse. For every a ∈ G, there exist a unique element b

∈ G, such that a*b=b*a=IAn Abelian or Commutative Group is a Group with the

additional condition5. Commutativity. For all a,b ∈ G, a*b=b*a

Page 38: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 38

Examples of groups

– Integers, Reals or Rationals with Addition– The nonzero Reals or Rationals with Multiplication– Non-singular n x n real matrices with Matrix

Multiplication – Permutations over n elements with composition

[0→1, 1→2, 2→0] o [0→1, 1→0, 2→2] = [0→0, 1→2, 2→1]

We will only be concerned with finite groups, I.e., ones with a finite number of elements.

Page 39: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 39

Key properties of finite groups

Notation: aj ≡ a * a * a * … j times

Definition: the order of g ∈ G is the smallest positive integer m such that gm = I

Definition: a group G is cyclic if there is a g ∈ G such that order(g) = |G|

Definition: an element g ∈ G of order |G| is called a generator or primitive element of G.

Page 40: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 40

Groups based on modular arithmeticThe group of positive integers modulo a prime p

Zp* ≡ {1, 2, 3, …, p-1}

*p ≡ multiplication modulo pDenoted as: (Zp

*, *p)Required properties

1. Closure. Yes.2. Associativity. Yes.3. Identity. 1.4. Inverse. Yes.

Example: Z7*= {1,2,3,4,5,6}

1-1 = 1, 2-1 = 4, 3-1 = 5, 6-1 = 6

Page 41: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 41

Other properties|Zp

*| = (p-1)By Fermat’s little theorem: a(p-1) = 1 (mod p)Example of Z7

*

x x2 x3 x4 x5 x6

1 1 1 1 1 12 4 1 2 4 13 2 6 4 5 14 2 1 4 2 15 4 6 2 3 16 1 6 1 6 1

For all p the group is cyclic.

Generators

Page 42: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 42

What if n is not a prime?

The group of positive integers modulo a non-prime nZn ≡ {1, 2, 3, …, n-1}, n not prime*p ≡ multiplication modulo n

Required properties?1. Closure. ?2. Associativity. ?3. Identity. ?4. Inverse. ?

How do we fix this?

Page 43: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 43

Groups based on modular arithmeticThe multiplicative group modulo n

Zn* ≡ {m : 1 ≤ m < n, gcd(n,m) = 1}

* ≡ multiplication modulo nDenoted as (Zn

*, *n)Required properties:

• Closure. Yes.• Associativity. Yes.• Identity. 1.• Inverse. Yes.

Example: Z15* = {1,2,4,7,8,11,13,14}

1-1 = 1, 2-1 = 8, 4-1 = 4, 7-1 = 13, 11-1 = 11, 14-1 = 14

Page 44: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 44

The Euler Phi Function

If n is a product of two primes p and q, then

)/11()(|

* pnnnp

n −∏=Ζ=φ

)1)(1()/11)(/11()( −−=−−= qpqppqnφ

Fermat-Euler Theorem:*)( for )(mod 1 n

n ana Ζ∈=φ

Or for n = pq*)1)(1( for )(mod 1 pq

qp ana Ζ∈=−−

This will be very important in RSA!

Page 45: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 45

Generators

Example of Z10*: {1, 3, 7, 9}

x x2 x3 x4

1 1 1 13 9 7 17 9 3 19 1 9 1

For n = (2, 4, pe, 2pe), p an odd prime, Zn is cyclic

Generators

Page 46: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 46

Operations we will need

Multiplication: a*b (mod n)– Can be done in O(log2 n) bit operations, or better

Power: ak (mod n)– The power method O(log n) steps, O(log3 n) bit ops

fun pow(a,k) =if (k = 0) then 1else if (k mod 2 = 1)

then a * (pow(a,k/2))2

else (pow(a, k/2))2

Inverse: a-1 (mod n)– Euclids algorithm O(log n) steps, O(log3 n) bit ops

Page 47: 15-853: Algorithms in the Real World15853-f19/slides/crypto1.pdf · Computational secrecy • Perfect secrecy requires the key to be at least as long as the message. This is impractical!

15-853 Page 47

Discrete Logarithms

If g is a generator of Zn*, then for all y there is a unique x

(mod φ(n)) such that– y = gx mod n

This is called the discrete logarithm of y and we use the notation– x = logg(y)

In general finding the discrete logarithm is conjectured to be hard…as hard as factoring.