10. Public Key Cryptography

Public key cryptography (PKC) has some features that symmetric key cryptography (SKC) does not and is applied for key management in protocols such as TLS and IPsec.

Discrete log-based ciphers are alternatives to PKC.

One-Way Functions

A function is one-way if f(x)=yf(x) = y is easily computed given xx, but f1(y)=xf^{-1}(y) = x is hard to computed given yy.

It is not known if one-way functions exist, but there are many functions that are believed to be one-way:

Trapdoor One-Way Functions

A function such that f1(y)f^{-1}(y) is easily computed given additional information, called a trapdoor.

Example: let f(x)=xemodnf(x) = x^e \bmod n for any xx co-prime to nn:

Asymmetric Cryptography

Another word to describe public key cryptography.

Public key cryptosystems, such as the Diffie-Hellman key exchange, are designed by using a trapdoor one-way function: the trapdoor is the decryption key.

This allows a public key to be stored in a public directory: anyone can obtain the public key and use it to form an encrypted message that only a person with the private key can decrypt.

Asymmetry: encryption and decryption keys are different. Encryption key is public and known to anybody; the decryption key is private and known only to its owner.

Finding the private key from the public key must be a hard computational problem.

Advantages of shared key/symmetric cryptography:

RSA

Designed in 1977 by Rivest, Shamir, and Adleman from MIT (patent expired 2000).

It is based on integer factorization problem.

Algorithm

Key generation:

Encryption:

Decryption:

Any message must be pre-processed:

Numerical Example

Key generation:

Encryption:

Decryption:

Numerical Example 2

Key generation:

Encryption:

Decryption:

Correctness

Decrypting an encrypted message: does (Me)dmodn=M(M^e)^d \bmod n = M?

As d=e1modϕ(n)d = e^{-1} \bmod \phi(n), edmodϕ(n)=1ed \bmod \phi(n) = 1: this can be written as being some integer kk such that ed=1+kϕ(n)ed = 1 + k\phi(n).

Hence, (Me)dmodn=Medmodn=M1+kϕ(n)modn(M^e)^d \bmod n = M^{ed} \bmod n = M^{1 + k\phi(n)} \bmod n.

Now we must show that M1+kϕ(n)modn=MM^{1 + k\phi(n)} \bmod n = M. There are two cases:

Case 1: Coprime to n

gcd(M,n)=1\mathrm{gcd}(M, n) = 1. In this case, we can apply Euler’s theorem, Mϕ(n)modn=1M^{\phi(n)} \bmod n = 1:

M1+kϕ(n)modn=M(Mϕ(n))kmodn=M1kmodn=M \begin{aligned} M^{1 + k\phi(n)} \bmod n &= M \cdot (M^{\phi(n)})^k \bmod n \\ &= M \cdot 1^k \bmod n \\ &= M \end{aligned}
Case 2: Multiple of p or q

Since pp and qq are prime and M<pqM < pq, if gcd(M,n)1\mathrm{gcd}(M, n) \neq 1, MM must be a multiple of either pp and qq and hence coprime to the other prime.

Supposing that gcd(M,p)=1\mathrm{gcd}(M, p) = 1, gcd(M,q)=q\mathrm{gcd}(M, q) = q and hence, there is some integer ll such that M=lqM = lq.

Applying Fermat’s theorem, Mϕ(p)modp=Mp1modp=1M^{\phi(p)} \bmod p = M^{p - 1} \bmod p = 1:

M1+kϕ(n)modp=M(Mϕ(n))kmodp=M(Mp1)k(q1)modp=M1k(q1)modp=Mmodp \begin{aligned} M^{1 + k\phi(n)} \bmod p &= M \cdot \left(M^{\phi(n)}\right)^k &\bmod p \\ &= M \cdot \left(M^{p - 1}\right)^{k(q - 1)} &\bmod p \\ &= M \cdot 1^{k(q - 1)} &\bmod p \\ &= M &\bmod p \end{aligned}

As M=lqM = lq M1+kϕ(n)modq=0M^{1 + k\phi(n)} \bmod q = 0. As n=pqn = pq and pp and qq are primes, we can use the Chinese Remainder Theorem:

Applications

Implementation

A few implementation details

Key Generation

Generating large primes pp and qq:

Choice of ee:

Encryption/decryption

Fast Exponentiation

A square-and-multiply modular exponentiation algorithm.

In binary, e=e020+e121++ek2ke = e_0 \cdot 2^0 + e_1 \cdot 2^1 + \dots + e_k \cdot 2^k, where eie_i are bits.

If MM is the plaintext, Me=Me0(M2)e1(M2k)ekM^e = M^{e_0} \cdot (M^2)^{e_1} \cdot \dots \cdot (M^{2^k})^{e_k} where (M2i)ei(M^{2^i})^{e_i} is zero when ei=0e_i = 0 and M2iM^{2^i} when ei=1e_i = 1.

Code:

// 2^66 % 100
M = 2   // base
n = 100 // modulus
e = [0, 1, 0, 0, 0, 0, 1] // exponent as bits, LSB first
z = 1; // Result - M^e \bmod n
for(let i = 0; i < e.length - 1; i++) {
  if(e[i] == 1) {
    z = (z * M) % n;
    // z = 1 initially so first multiplication is unnecessary
    // Hence e.filter(i => i == 1).length - 1 multiplications
  }
  
  M = (M * M) % n; // equals base^{2^{i + 1}}
  // When i = 0, M = base^1 - right for the next iteration
  // Does not need to be calculated for the i + 1 == e.length
  // as the value is used for the next iteration
}

Cost:

Faster Decryption Using CRT

Decrypting CC with respect to pp and qq separately.

Compute Mp=Cdmod(p1)(modp)M_p = C^{d \bmod (p - 1)} \pmod p and Mq=Cdmod(q1)(modq)M_q = C^{d \bmod (q - 1)} \pmod q

Solve MmodnM \bmod n using CRT. d=(dmod(p1))+k(p1)d = (d \bmod (p - 1)) + k(p - 1) for some kk. Hence:

Mmodp=Cdmodnmodp=Cdmodp=Cdmod(p1)Ck(p1)modp=Cdmod(p1)=Mp \begin{aligned} M \bmod p &= C^{d \bmod n} \bmod p = C^d \bmod p \\ &= C^{d \bmod (p - 1)} C^{k(p - 1)} \bmod p = C^{d \bmod (p - 1)} \\ &= M_p \end{aligned}

Hence, MMpmodpM \equiv M_p \bmod p and similarly, MMqmodqM \equiv M_q \bmod q.

Then we can output M=q(q1modp)Mp+p(p1modq)MqmodnM = q\cdot (q^{-1} \bmod p) \cdot M_p + p \cdot (p^{-1} \bmod q) \cdot M_q \bmod n.

Speedup

Exponents dmod(p1)d \bmod (p - 1) and dmod(q1)d \bmod (q - 1) are about half the length of dd and the complexity of exponentiation with square-and-multiply increases with the cube of input length.

Hence, computing each of MpM_p and MqM_q uses about an eighth of the computation, leading to four times less computation.

Hence, storing pp and qq instead of just nn allows for faster decryption.

Padding

Encryption directly on the message encoded as a number is bad as it is vulnerable to attacks:

Hence, a padding mechanism must be used to prepare the message for encryption, adding redundancy and randomness.

Håstad’s Attack

The same message MM is encrypted without padding to three different ciphertexts C1C_1, C2C_2, C3C_3 with the public exponent e=3e = 3 being used by all recipients.

C1=M3modn1C2=M3modn2C3=M3modn3 \begin{aligned} C_1 = M^3 \bmod n_1 \\ C_2 = M^3 \bmod n_2 \\ C_3 = M^3 \bmod n_3 \end{aligned}

Equations can be solved using the CRT to obtain M3M^3 in the ordinary (non-modular integers). The attacker can then simply take the cube root to find MM.

Padding Types

Security

Attacks

Mostly avoided through the use of standardized padding mechanisms.

Possible attacks:

Practical Problems with Key Generation

Diffie-Hellman Key Exchange

Two users sharing a secret using only public communication.

Public elements:

Alice and Bob randomly select values aa and bb respectively, where 1<a,b<p1 < a, b < p.

Over an insecure channel, Alice sends gag^a, Bob sends gbg^b.

Both compute the secret key Z=gabmodpZ = g^{ab} \bmod p: (gb)a(ga)b)(modp)(g^b)^a \equiv (g^a)^b) \pmod p.

Example

Let p=181p = 181, g=2g = 2, a=50a = 50, b=33b = 33.

Both compute Z=(gb)amodp=(ga)bmodp=3050mod181=11633mod181=49Z = (g^b)^a \bmod p = (g^a)^b \bmod p = 30^{50} \bmod{181} = 116^{33} \bmod{181} = 49

Properties

Security

Relies on the difficulty of the discrete logarithm problem.

If an attacker intercepts gamodpg^a \bmod p and take the discrete logarithm to find aa, they can compute (gb)a(g^b)^a in the same way as Bob.

There is no better known way for a passive adversary to find the shared secret.

Authenticated Diffie-Hellman

In the basic protocol, messages are not authenticated: a man-in-the-middle-attack is possible where the attacker acts as a proxy between the two parties, decrypting messages from one party and then re-encrypting messages to send to the other party with the other key.

Both parties must know each other’s public signature verification keys, AA and BB (identity + public key).

Static and Ephemeral Diffie-Hellman

The above protocol uses ephemeral keys: keys are used once. In the static protocol:

Elgamal Cryptosystem

Proposed by Elgamal in 1985, turning the Diffie-Hellman protocol into a cryptosystem for encryption and for signature.

Alice combines her ephemeral private key with Bob’s long-term public key

Algorithms

Key generation:

Encryption:

Decryption:

Correctness

Example

Key generation:

Encryption:

Decryption:

Security

Elliptic Curves

Algebraic structures formed from cubic equations.

Curves are defined over any field.

e.g. set of all (x,y)(x, y) pairs which satisfy y2=x3+ax+bmodpy^2 = x^3 + ax + b \bmod p, then creates a curve over the field Zp\mathbb{Z}_p.

A point on the curve is the identity element, and by defining a binary operation on the points (e.g. multiplication), we can form a group over elliptic curve’s points: the elliptic curve group.

Any elliptic curve can be used but most applications use standardized curves generated in a verifiably random way (e.g. NIST curve P-192 has curve of nn points over Zp\mathbb{Z}_p with generator (Gx,Gy)(G_x, G_y) and equation y2=x33x+bmodpy^2 = x^3 - 3x + b \bmod p with a defined random values for pp, nn, bb, GxG_x, GyG_y and seed ss.

Discrete log defined on elliptic curve groups: if elliptic curve operation operation denoted as multiplication, definition is the same as in Zp\mathbb{Z}_p^*.

Elliptic curve implementations require smaller keys compared to RSA:

Symmetric key RSA modulus EC element length
80 1024 160
128 3072 256
192 7680 384
256 15360 512

From Ars: