CSS411 Cryptography

RSA

Slide deck 10: public-key principles, requirements, the RSA algorithm with worked numbers, and the attacks on RSA.

28 min read

Public-key principlesSearch on YouTube

Private-key cryptographySearch on YouTube

Traditional private / secret / single key cryptography uses one key. The sender and the receiver share that key. If the key is disclosed, the communications are compromised. The scheme is also symmetric, because the parties are equal.

Because both parties hold the same key, the scheme does not protect the sender. The receiver can forge a message and claim that the sender sent it.

Why public keySearch on YouTube

Public-key cryptography uses two keys: a public key and a private key. It is asymmetric, because the parties are not equal. It uses number theory concepts. It complements private-key cryptography. It does not replace it.

Public-key cryptography was developed to address two key issues:

  • Key distribution: secure communication without the need to trust a KDC with your key.
  • Digital signatures: a method to verify that a message comes intact from the claimed sender.

The public invention is by Whitfield Diffie and Martin Hellman at Stanford University in 1976. The classified community knew the idea earlier.

Two-key cryptographySearch on YouTube

Public-key / two-key / asymmetric cryptography uses two keys:

  • A public key. Anybody can know it. It can encrypt messages and verify signatures.
  • A related private key. Only the recipient knows it. It decrypts messages and signs (creates) signatures.

It is infeasible to find the private key from the public key. The scheme is asymmetric because the parties that encrypt messages or verify signatures cannot decrypt messages or create signatures.

Think of a mailbox with a slot. Anyone can put a letter in the slot (encrypt with the public key). Only the owner has the key that opens the box (decrypt with the private key).

Encryption with a public keySearch on YouTube

The slide figure "(a) Encryption with public key" shows these steps:

  1. Bob has a public key ring with the public keys of Joy, Mike, Ted and Alice.
  2. Bob encrypts the plaintext XX with Alice's public key PUaPU_a (for example, with RSA).
  3. The transmitted ciphertext is Y=E[PUa,X]Y = E[PU_a, X].
  4. Alice decrypts with her private key PRaPR_a: X=D[PRa,Y]X = D[PR_a, Y].

Symmetric vs public keySearch on YouTube

Conventional encryptionPublic-key encryption
Needed to workNeeded to work
1. The same algorithm with the same key is used for encryption and decryption.1. One algorithm is used for encryption and decryption with a pair of keys, one for encryption and one for decryption.
2. The sender and receiver must share the algorithm and the key.2. The sender and receiver must each have one of the matched pair of keys (not the same one).
Needed for securityNeeded for security
1. The key must be kept secret.1. One of the two keys must be kept secret.
2. It must be impossible or at least impractical to decipher a message if no other information is available.2. It must be impossible or at least impractical to decipher a message if no other information is available.
3. Knowledge of the algorithm plus samples of ciphertext must be insufficient to determine the key.3. Knowledge of the algorithm plus one of the keys plus samples of ciphertext must be insufficient to determine the other key.

Public-key cryptosystemsSearch on YouTube

The "Public-Key Cryptosystems" figure gives secrecy and authentication together. Source A has the key pair (PUa,PRa)(PU_a, PR_a). Destination B has the key pair (PUb,PRb)(PU_b, PR_b).

Source A                                         Destination B
X --E(PRa)--> Y --E(PUb)--> Z ----> Z --D(PRb)--> Y --D(PUa)--> X
  • A encrypts XX with its own private key PRaPR_a. This step gives authentication (a signature).
  • A encrypts the result YY with B's public key PUbPU_b. This step gives secrecy.
  • B decrypts ZZ with PRbPR_b to get YY. Then B decrypts YY with PUaPU_a to get XX.

Z=E(PUb, E(PRa,X))X=D(PUa, D(PRb,Z))Z = E\big(PU_b,\, E(PR_a, X)\big) \qquad X = D\big(PU_a,\, D(PR_b, Z)\big)

Uses and requirementsSearch on YouTube

ApplicationsSearch on YouTube

The uses fall into 3 categories:

  • Encryption/decryption: provides secrecy.
  • Digital signatures: provide authentication.
  • Key exchange: of session keys.

Some algorithms are suitable for all uses. Other algorithms are specific to one use.

AlgorithmEncryption/DecryptionDigital SignatureKey Exchange
RSAYesYesYes
Elliptic CurveYesYesYes
Diffie-HellmanNoNoYes
DSSNoYesNo

RSA and ECC do all three. DH deals keys. DSS signs. Diffie-Hellman does only key exchange. DSS does only digital signatures.

RequirementsSearch on YouTube

Public-key algorithms rely on two keys with these properties:

  • It is computationally infeasible to find the decryption key from only the algorithm and the encryption key.
  • It is computationally easy to encrypt or decrypt messages when the related key is known.
  • For some algorithms, either of the two related keys can encrypt, and the other key decrypts.

These requirements are formidable. Only a few algorithms satisfy them.

Trapdoor one-way functionsSearch on YouTube

A public-key scheme needs a trapdoor one-way function. A one-way function is easy in the forward direction and infeasible in the inverse direction:

Y=f(X) easyX=f−1(Y) infeasibleY = f(X) \ \text{easy} \qquad X = f^{-1}(Y) \ \text{infeasible}

A trap-door one-way function has these properties:

  • Y=fk(X)Y = f_k(X) is easy, if kk and XX are known.
  • X=fk−1(Y)X = f_k^{-1}(Y) is easy, if kk and YY are known.
  • X=fk−1(Y)X = f_k^{-1}(Y) is infeasible, if YY is known but kk is not known.

A practical public-key scheme depends on a suitable trap-door one-way function. In RSA, the trapdoor is the factorization of nn.

Security of public-key schemesSearch on YouTube

A brute-force exhaustive search attack is always possible in theory, as with private-key schemes. But the keys are too large (more than 512 bits). Security relies on a large difference in difficulty between the easy problem (encrypt/decrypt) and the hard problem (cryptanalysis).

The hard problem is known, but it is made too hard to break in practice. This requires very large numbers. Thus public-key schemes are slow compared to private-key schemes.

The RSA algorithmSearch on YouTube

OverviewSearch on YouTube

Rivest, Shamir and Adleman of MIT made RSA in 1977. It is the best known and most widely used public-key scheme. It uses large integers (for example, 1024 bits). Its security comes from the cost of factoring large numbers.

The slide gives the cost of factorization as:

O ⁣(elog⁡n log⁡log⁡n) operations (hard)O\!\left(e^{\log n \,\log\log n}\right) \ \text{operations (hard)}

Encryption and decryptionSearch on YouTube

To encrypt a message MM, the sender gets the recipient's public key PU={e,n}PU = \{e, n\} and computes:

C=Me mod nwhere 0≤M<nC = M^{e} \bmod n \qquad\text{where } 0 \le M \lt n

To decrypt the ciphertext CC, the owner uses the private key PR={d,n}PR = \{d, n\} and computes:

M=Cd mod nM = C^{d} \bmod n

The message MM must be smaller than the modulus nn. Break a long message into blocks if necessary.

Key setupSearch on YouTube

Each user makes a public/private key pair with these steps:

  1. Select two large primes at random: pp, qq.
  2. Compute the system modulus n=p⋅qn = p \cdot q. Also compute ϕ(n)=(p−1)(q−1)\phi(n) = (p-1)(q-1).
  3. Select at random the encryption key ee, where 1<e<ϕ(n)1 \lt e \lt \phi(n) and gcd⁡(e,ϕ(n))=1\gcd(e, \phi(n)) = 1.
  4. Solve e⋅d=1 mod ϕ(n)e \cdot d = 1 \bmod \phi(n), with 0≤d≤n0 \le d \le n, to find the decryption key dd.
  5. Publish the public encryption key: PU={e,n}PU = \{e, n\}.
  6. Keep the private decryption key secret: PR={d,n}PR = \{d, n\}.
Key generation by AlicePick p, qtwo primesn = pqφ(n)(p−1)(q−1)Pick egcd(e, φ(n)) = 1Find dd = e-1​ mod φ(n)Public PU = {e, n}Private PR = {d, n}Bob encryptsC = Me​ mod n, M < nciphertext CAlice decryptsM = Cd​ mod n
RSA: Alice makes the key pair and publishes {e, n}. Bob encrypts with it, and Alice decrypts with her private key {d, n}.

Figure 9.5 (The RSA Algorithm) gives the same steps in a table:

Key generation by AliceCondition
Select p,qp, qpp and qq both prime, p≠qp \ne q
Calculate n=p×qn = p \times q
Calculate ϕ(n)=(p−1)(q−1)\phi(n) = (p-1)(q-1)
Select integer eegcd⁡(ϕ(n),e)=1\gcd(\phi(n), e) = 1; 1<e<ϕ(n)1 \lt e \lt \phi(n)
Calculate ddd≡e−1(modϕ(n))d \equiv e^{-1} \pmod{\phi(n)}
Public keyPU={e,n}PU = \{e, n\}
Private keyPR={d,n}PR = \{d, n\}
StepPlaintextCiphertext
Encryption by Bob with Alice's public keyM<nM \lt nC=Me mod nC = M^{e} \bmod n
Decryption by AliceM=Cd mod nM = C^{d} \bmod nCC

The figure heading says "Decryption by Alice with Alice's Public Key". This is a typo in the figure. Alice decrypts with her private key PR={d,n}PR = \{d, n\}.

Key setup order: Primes, N, Phi, E, D, Publish, Protect. Pick p,qp, q; multiply for nn; compute ϕ(n)\phi(n); choose ee coprime to ϕ(n)\phi(n); compute d=e−1d = e^{-1}; publish {e,n}\{e, n\}; protect {d,n}\{d, n\}.

Why RSA worksSearch on YouTube

RSA works because of Euler's theorem:

aϕ(n) mod n=1where gcd⁡(a,n)=1a^{\phi(n)} \bmod n = 1 \qquad\text{where } \gcd(a, n) = 1

In RSA, n=p⋅qn = p \cdot q and ϕ(n)=(p−1)(q−1)\phi(n) = (p-1)(q-1). The values ee and dd are inverses mod ϕ(n)\phi(n). Thus e⋅d=1+k⋅ϕ(n)e \cdot d = 1 + k \cdot \phi(n) for some integer kk. Therefore:

Cd=Me⋅d=M1+k⋅ϕ(n)=M1⋅(Mϕ(n))k=M1⋅(1)k=M1=M mod nC^{d} = M^{e \cdot d} = M^{1 + k \cdot \phi(n)} = M^{1} \cdot \left(M^{\phi(n)}\right)^{k} = M^{1} \cdot (1)^k = M^{1} = M \bmod n

Med mod n=MM^{ed} \bmod n = M, because ed=1+kϕ(n)ed = 1 + k\phi(n) and Mϕ(n) mod n=1M^{\phi(n)} \bmod n = 1.

RSA worked exampleSearch on YouTube

Key setup exampleSearch on YouTube

The slide example has seven steps:

StepComputeResult
1Select primesp=17p = 17, q=11q = 11
2n=pq=17×11n = pq = 17 \times 11n=187n = 187
3ϕ(n)=(p−1)(q−1)=16×10\phi(n) = (p-1)(q-1) = 16 \times 10ϕ(n)=160\phi(n) = 160
4Select ee with gcd⁡(e,160)=1\gcd(e, 160) = 1choose e=7e = 7
5Find dd: de=1 mod 160de = 1 \bmod 160 and d<160d \lt 160d=23d = 23, because 23×7=161=10×160+123 \times 7 = 161 = 10 \times 160 + 1
6Publish the public keyPU={7,187}PU = \{7, 187\}
7Keep the private key secretPR={23,187}PR = \{23, 187\}

The gcd⁡\gcd check: 160=25×5160 = 2^{5} \times 5. The prime 7 is not a factor of 160, thus gcd⁡(7,160)=1\gcd(7, 160) = 1.

Encryption exampleSearch on YouTube

The message is M=88M = 88 (88<18788 \lt 187). Encryption with e=7e = 7:

C=887 mod 187=11C = 88^{7} \bmod 187 = 11

Trace of 88788^{7}, with 7=2×3+17 = 2 \times 3 + 1:

PowerValue computedRemainder mod 187
88188^{1}8888
88288^{2}7744=41×187+777744 = 41 \times 187 + 7777
88388^{3}77×88=6776=36×187+4477 \times 88 = 6776 = 36 \times 187 + 4444
88688^{6}442=1936=10×187+6644^{2} = 1936 = 10 \times 187 + 6666
88788^{7}66×88=5808=31×187+1166 \times 88 = 5808 = 31 \times 187 + 1111=C11 = C

Decryption exampleSearch on YouTube

Decryption with d=23d = 23:

M=1123 mod 187=88M = 11^{23} \bmod 187 = 88

Square-and-multiply trace for 112311^{23}, with 23=16+4+2+123 = 16 + 4 + 2 + 1:

PowerValue computedRemainder mod 187
11111^{1}1111
11211^{2}121121
11411^{4}1212=14641=78×187+55121^{2} = 14641 = 78 \times 187 + 5555
11811^{8}552=3025=16×187+3355^{2} = 3025 = 16 \times 187 + 3333
111611^{16}332=1089=5×187+15433^{2} = 1089 = 5 \times 187 + 154154

Multiply the remainders of 111611^{16}, 11411^{4}, 11211^{2} and 11111^{1}. Reduce mod 187 after each multiplication.

Partial productComputationRemainder mod 187
1116×11411^{16} \times 11^{4}154×55=8470=45×187+55154 \times 55 = 8470 = 45 \times 187 + 5555
× 112\times\ 11^{2}55×121=6655=35×187+11055 \times 121 = 6655 = 35 \times 187 + 110110
× 111\times\ 11^{1}110×11=1210=6×187+88110 \times 11 = 1210 = 6 \times 187 + 8888=M88 = M

Round trip: encrypt 88→1188 \to 11 with {7,187}\{7, 187\}. Decrypt 11→8811 \to 88 with {23,187}\{23, 187\}. The recovered value is equal to the message.

Key generation in practiceSearch on YouTube

The users of RSA must:

  • Find two primes at random: pp, qq.
  • Select either ee or dd, and compute the other.

An attacker must not be able to find the primes pp, qq easily from the modulus n=p⋅qn = p \cdot q. Thus the primes must be sufficiently large. Typically, you guess a candidate and use a probabilistic primality test. The exponents ee and dd are inverses, so use the inverse algorithm (extended Euclid) to compute one from the other.

Attacks on RSASearch on YouTube

Attack typesSearch on YouTube

The possible approaches to attack RSA are:

AttackSlide description
Brute force key searchInfeasible, given the size of the numbers
Mathematical attacksBased on the difficulty of computing ϕ(n)\phi(n), by factoring the modulus nn
Timing attacksOn the running of decryption
Chosen ciphertext attacksUse the properties of RSA

Factoring problemSearch on YouTube

The mathematical approach takes 3 forms:

  • Factor n=p⋅qn = p \cdot q, then compute ϕ(n)\phi(n) and then dd.
  • Find ϕ(n)\phi(n) directly and compute dd.
  • Find dd directly.

Timing attacksSearch on YouTube

Paul Kocher developed timing attacks in the mid-1990s. The attack uses timing variations in operations. For example, a multiplication by a small number takes a different time than a multiplication by a large number. IF statements also change which instructions execute.

The attacker infers the operand size from the time taken. In RSA, the attacker uses the time taken in exponentiation. The countermeasures are:

  • Use a constant exponentiation time.
  • Add random delays.
  • Blind the values used in calculations.

Chosen ciphertext attacksSearch on YouTube

RSA is vulnerable to a Chosen Ciphertext Attack (CCA). The attacker chooses ciphertexts and gets the decrypted plaintext back. The attacker chooses the ciphertexts to use the properties of RSA. The results give information that helps cryptanalysis.

The countermeasures are a random pad of the plaintext, or Optimal Asymmetric Encryption Padding (OAEP).

The slide prints "OASP". The standard name is OAEP (Optimal Asymmetric Encryption Padding).

Quick self-testSearch on YouTube

Which two issues did public-key cryptography address?

Key distribution: secure communication without the need to trust a KDC with your key. Digital signatures: proof that a message comes intact from the claimed sender. Diffie and Hellman published the idea at Stanford in 1976.

Classify RSA, Elliptic Curve, Diffie-Hellman and DSS by the 3 public-key uses.

RSA: all three (encryption/decryption, digital signature, key exchange). Elliptic Curve: all three. Diffie-Hellman: key exchange only. DSS: digital signature only.

Give the 3 public-key requirements.

1: It is computationally infeasible to find the decryption key from the algorithm and the encryption key. 2: It is computationally easy to encrypt or decrypt when the related key is known. 3 (for some algorithms): either key of the pair can encrypt, and the other key decrypts.

Define a trap-door one-way function.

Y=fk(X)Y = f_k(X) is easy if kk and XX are known. X=fk−1(Y)X = f_k^{-1}(Y) is easy if kk and YY are known. X=fk−1(Y)X = f_k^{-1}(Y) is infeasible if YY is known but kk is not known. In RSA the trapdoor is the factorization of nn.

In the "Public-Key Cryptosystems" figure, which keys does A use, and in which order?

A encrypts XX with PRaPR_a (authentication), then with PUbPU_b (secrecy): Z=E(PUb,E(PRa,X))Z = E(PU_b, E(PR_a, X)). B decrypts with PRbPR_b and then PUaPU_a.

Why are public-key schemes slow compared with private-key schemes?

Security needs a large gap between easy encryption/decryption and hard cryptanalysis. This requires very large numbers (keys larger than 512 bits). Calculations on such large numbers are slow.

Do the RSA key setup with p=17p=17, q=11q=11, e=7e=7.

n=17×11=187n = 17 \times 11 = 187. ϕ(n)=16×10=160\phi(n) = 16 \times 10 = 160. gcd⁡(7,160)=1\gcd(7, 160) = 1. d=23d = 23, because 23×7=161=10×160+123 \times 7 = 161 = 10 \times 160 + 1. PU={7,187}PU = \{7, 187\}, PR={23,187}PR = \{23, 187\}.

Show why CdC^d gives back MM.

e⋅d=1+kϕ(n)e \cdot d = 1 + k\phi(n). So Cd=Med=M1+kϕ(n)=M⋅(Mϕ(n))kC^d = M^{ed} = M^{1 + k\phi(n)} = M \cdot (M^{\phi(n)})^k. Euler's theorem gives Mϕ(n) mod n=1M^{\phi(n)} \bmod n = 1 when gcd⁡(M,n)=1\gcd(M, n) = 1. Thus Cd=M⋅1k=M mod nC^d = M \cdot 1^k = M \bmod n.

Encrypt M=88M = 88 with PU={7,187}PU = \{7, 187\} and decrypt it.

C=887 mod 187=11C = 88^{7} \bmod 187 = 11. M=1123 mod 187=88M = 11^{23} \bmod 187 = 88.

Name the four attack types on RSA and the three timing-attack countermeasures.

Attacks: brute force key search, mathematical attacks (factoring nn), timing attacks, chosen ciphertext attacks. Timing countermeasures: constant exponentiation time, random delays, blinding of the values. Paul Kocher developed the timing attack in the mid-1990s.

  • Private-key cryptography uses one shared key and does not protect the sender from forgery by the receiver. Public-key cryptography uses a public key (encrypt, verify) and a private key (decrypt, sign).
  • Public-key schemes need a trap-door one-way function and very large numbers, so they are slow.
  • RSA: choose primes p,qp, q; n=pqn = pq; ϕ(n)=(p−1)(q−1)\phi(n) = (p-1)(q-1); gcd⁡(e,ϕ(n))=1\gcd(e, \phi(n)) = 1; d≡e−1(modϕ(n))d \equiv e^{-1} \pmod{\phi(n)}. Encrypt C=Me mod nC = M^{e} \bmod n. Decrypt M=Cd mod nM = C^{d} \bmod n.
  • Example: p=17p = 17, q=11q = 11, e=7e = 7, d=23d = 23, M=88M = 88 gives C=11C = 11. Euler's theorem is the reason that RSA works.
  • Attacks: brute force, mathematical (factoring), timing (Kocher), chosen ciphertext (counter with random padding or OAEP).