Files
blog/_posts/lecture-notes/internet-security/2023-10-04-rsa-elgamal.md
Sungchan Yi 23aeb29ad8 feat: breaking change (unstable) (#198)
* [PUBLISHER] upload files #175

* PUSH NOTE : 3. Symmetric Key Encryption.md

* PUSH NOTE : 03. Symmetric Key Cryptography (2).md

* DELETE FILE : _posts/lecture-notes/modern-cryptography/2023-09-18-symmetric-key-cryptography-2.md

* DELETE FILE : _posts/lecture-notes/modern-cryptography/2023-09-19-symmetric-key-encryption.md

* [PUBLISHER] upload files #177

* PUSH NOTE : 3. Symmetric Key Encryption.md

* PUSH NOTE : 03. Symmetric Key Cryptography (2).md

* DELETE FILE : _posts/lecture-notes/modern-cryptography/2023-09-18-symmetric-key-cryptography-2.md

* DELETE FILE : _posts/lecture-notes/modern-cryptography/2023-09-19-symmetric-key-encryptio.md

* [PUBLISHER] upload files #178

* PUSH NOTE : 3. Symmetric Key Encryption.md

* PUSH NOTE : 03. Symmetric Key Cryptography (2).md

* DELETE FILE : _posts/lecture-notes/modern-cryptography/2023-09-18-symmetric-key-cryptography-2.md

* [PUBLISHER] upload files #179

* PUSH NOTE : 3. Symmetric Key Encryption.md

* PUSH NOTE : 03. Symmetric Key Cryptography (2).md

* DELETE FILE : _posts/lecture-notes/modern-cryptography/2023-09-18-symmetric-key-cryptography-2.md

* [PUBLISHER] upload files #180

* PUSH NOTE : 3. Symmetric Key Encryption.md

* PUSH NOTE : 03. Symmetric Key Cryptography (2).md

* DELETE FILE : _posts/lecture-notes/modern-cryptography/2023-09-18-symmetric-key-cryptography-2.md

* [PUBLISHER] upload files #181

* PUSH NOTE : 3. Symmetric Key Encryption.md

* PUSH NOTE : 03. Symmetric Key Cryptography (2).md

* DELETE FILE : _posts/lecture-notes/modern-cryptography/2023-09-18-symmetric-key-cryptography-2.md

* [PUBLISHER] upload files #182

* PUSH NOTE : 3. Symmetric Key Encryption.md

* PUSH NOTE : 03. Symmetric Key Cryptography (2).md

* [PUBLISHER] upload files #183

* PUSH NOTE : 3. Symmetric Key Encryption.md

* PUSH NOTE : 03. Symmetric Key Cryptography (2).md

* DELETE FILE : _posts/lecture-notes/modern-cryptography/2023-09-18-symmetric-key-cryptography-2.md

* [PUBLISHER] upload files #184

* PUSH NOTE : 3. Symmetric Key Encryption.md

* PUSH NOTE : 03. Symmetric Key Cryptography (2).md

* DELETE FILE : _posts/lecture-notes/modern-cryptography/2023-09-18-symmetric-key-cryptography-2.md

* [PUBLISHER] upload files #185

* PUSH NOTE : 3. Symmetric Key Encryption.md

* PUSH NOTE : 03. Symmetric Key Cryptography (2).md

* DELETE FILE : _posts/lecture-notes/modern-cryptography/2023-09-18-symmetric-key-cryptography-2.md

* [PUBLISHER] upload files #186

* PUSH NOTE : 3. Symmetric Key Encryption.md

* PUSH NOTE : 03. Symmetric Key Cryptography (2).md

* [PUBLISHER] upload files #187

* PUSH NOTE : 3. Symmetric Key Encryption.md

* PUSH NOTE : 14. Secure Multiparty Computation.md

* DELETE FILE : _posts/Lecture Notes/Modern Cryptography/2023-09-19-symmetric-key-encryption.md

* DELETE FILE : _posts/lecture-notes/modern-cryptography/2023-09-18-symmetric-key-cryptography-2.md

* [PUBLISHER] upload files #188

* PUSH NOTE : 3. Symmetric Key Encryption.md

* PUSH NOTE : 14. Secure Multiparty Computation.md

* DELETE FILE : _posts/Lecture Notes/Modern Cryptography/2023-09-19-symmetric-key-encryption.md

* chore: remove files

* [PUBLISHER] upload files #197

* PUSH NOTE : 수학 공부에 대한 고찰.md

* PUSH NOTE : 09. Lp Functions.md

* PUSH ATTACHMENT : mt-09.png

* PUSH NOTE : 08. Comparison with the Riemann Integral.md

* PUSH ATTACHMENT : mt-08.png

* PUSH NOTE : 04. Measurable Functions.md

* PUSH ATTACHMENT : mt-04.png

* PUSH NOTE : 06. Convergence Theorems.md

* PUSH ATTACHMENT : mt-06.png

* PUSH NOTE : 07. Dominated Convergence Theorem.md

* PUSH ATTACHMENT : mt-07.png

* PUSH NOTE : 05. Lebesgue Integration.md

* PUSH ATTACHMENT : mt-05.png

* PUSH NOTE : 03. Measure Spaces.md

* PUSH ATTACHMENT : mt-03.png

* PUSH NOTE : 02. Construction of Measure.md

* PUSH ATTACHMENT : mt-02.png

* PUSH NOTE : 01. Algebra of Sets and Set Functions.md

* PUSH ATTACHMENT : mt-01.png

* PUSH NOTE : Rules of Inference with Coq.md

* PUSH NOTE : 블로그 이주 이야기.md

* PUSH NOTE : Secure IAM on AWS with Multi-Account Strategy.md

* PUSH ATTACHMENT : separation-by-product.png

* PUSH NOTE : You and Your Research, Richard Hamming.md

* PUSH NOTE : 10. Digital Signatures.md

* PUSH ATTACHMENT : mc-10-dsig-security.png

* PUSH ATTACHMENT : mc-10-schnorr-identification.png

* PUSH NOTE : 9. Public Key Encryption.md

* PUSH ATTACHMENT : mc-09-ss-pke.png

* PUSH NOTE : 8. Number Theory.md

* PUSH NOTE : 7. Key Exchange.md

* PUSH ATTACHMENT : mc-07-dhke.png

* PUSH ATTACHMENT : mc-07-dhke-mitm.png

* PUSH ATTACHMENT : mc-07-merkle-puzzles.png

* PUSH NOTE : 6. Hash Functions.md

* PUSH ATTACHMENT : mc-06-merkle-damgard.png

* PUSH ATTACHMENT : mc-06-davies-meyer.png

* PUSH ATTACHMENT : mc-06-hmac.png

* PUSH NOTE : 5. CCA-Security and Authenticated Encryption.md

* PUSH ATTACHMENT : mc-05-ci.png

* PUSH ATTACHMENT : mc-05-etm-mte.png

* PUSH NOTE : 1. OTP, Stream Ciphers and PRGs.md

* PUSH ATTACHMENT : mc-01-prg-game.png

* PUSH ATTACHMENT : mc-01-ss.png

* PUSH NOTE : 4. Message Authentication Codes.md

* PUSH ATTACHMENT : mc-04-mac.png

* PUSH ATTACHMENT : mc-04-mac-security.png

* PUSH ATTACHMENT : mc-04-cbc-mac.png

* PUSH ATTACHMENT : mc-04-ecbc-mac.png

* PUSH NOTE : 3. Symmetric Key Encryption.md

* PUSH ATTACHMENT : is-03-ecb-encryption.png

* PUSH ATTACHMENT : is-03-cbc-encryption.png

* PUSH ATTACHMENT : is-03-ctr-encryption.png

* PUSH NOTE : 2. PRFs, PRPs and Block Ciphers.md

* PUSH ATTACHMENT : mc-02-block-cipher.png

* PUSH ATTACHMENT : mc-02-feistel-network.png

* PUSH ATTACHMENT : mc-02-des-round.png

* PUSH ATTACHMENT : mc-02-DES.png

* PUSH ATTACHMENT : mc-02-aes-128.png

* PUSH ATTACHMENT : mc-02-2des-mitm.png

* PUSH NOTE : 18. Bootstrapping & CKKS.md

* PUSH NOTE : 17. BGV Scheme.md

* PUSH NOTE : 16. The GMW Protocol.md

* PUSH ATTACHMENT : mc-16-beaver-triple.png

* PUSH NOTE : 15. Garbled Circuits.md

* PUSH NOTE : 14. Secure Multiparty Computation.md

* PUSH NOTE : 13. Sigma Protocols.md

* PUSH ATTACHMENT : mc-13-sigma-protocol.png

* PUSH ATTACHMENT : mc-13-okamoto.png

* PUSH ATTACHMENT : mc-13-chaum-pedersen.png

* PUSH ATTACHMENT : mc-13-gq-protocol.png

* PUSH NOTE : 12. Zero-Knowledge Proofs (Introduction).md

* PUSH ATTACHMENT : mc-12-id-protocol.png

* PUSH NOTE : 11. Advanced Topics.md

* PUSH NOTE : 0. Introduction.md

* PUSH NOTE : 02. Symmetric Key Cryptography (1).md

* PUSH NOTE : 09. Transport Layer Security.md

* PUSH ATTACHMENT : is-09-tls-handshake.png

* PUSH NOTE : 08. Public Key Infrastructure.md

* PUSH ATTACHMENT : is-08-certificate-validation.png

* PUSH NOTE : 07. Public Key Cryptography.md

* PUSH NOTE : 06. RSA and ElGamal Encryption.md

* PUSH NOTE : 05. Modular Arithmetic (2).md

* PUSH NOTE : 03. Symmetric Key Cryptography (2).md

* PUSH ATTACHMENT : is-03-feistel-function.png

* PUSH ATTACHMENT : is-03-cfb-encryption.png

* PUSH ATTACHMENT : is-03-ofb-encryption.png

* PUSH NOTE : 04. Modular Arithmetic (1).md

* PUSH NOTE : 01. Security Introduction.md

* PUSH ATTACHMENT : is-01-cryptosystem.png

* PUSH NOTE : Search Time in Hash Tables.md

* PUSH NOTE : 랜덤 PS일지 (1).md

* chore: rearrange articles

* feat: fix paths

* feat: fix all broken links

* feat: title font to palatino
2024-11-13 14:28:45 +09:00

8.3 KiB

share, toc, math, categories, path, tags, title, date, github_title
share toc math categories path tags title date github_title
true true true
Lecture Notes
Internet Security
_posts/lecture-notes/internet-security
lecture-note
security
cryptography
number-theory
06. RSA and ElGamal Encryption 2023-10-04 2023-10-04-rsa-elgamal

Exponential Inverses

Suppose we are given integers a and N. For any integer x that is relatively prime to N, we choose b so that


 
\tag{$*$}
ab \equiv 1 \pmod{\phi(N)}.

Then we have


x^{ab} \equiv x^{1 + k\phi(N)} \equiv x \pmod N

by Euler's generalization.

Definition. The integer b satisfying (\ast) is called the exponential inverse of a modulo $N$.

Using exponential inverses will be a key idea in the RSA cryptosystem.

RSA Cryptosystem

This is an explanation of textbook RSA encryption scheme.

Key Generation

  • We pick two large primes p, q and set N = pq.
  • Select (e, d) so that ed \equiv 1 \pmod{\phi(N)}.
  • Set (N, e) as the public key and make it public.
  • Set d as the private key and keep it secret.

RSA Encryption and Decryption

Suppose we want to encrypt a message m \in \mathbb{Z} _ N.

  • Encryption
    • Using the public key (N, e), compute the ciphertext c = m^e \bmod N.
  • Decryption
    • Recover the original message by computing c^d \bmod N.

Correctness of RSA?

Since ed \equiv 1 \pmod{\phi(N)}, we have


c^d \equiv m^{ed} \equiv m \pmod N

by the properties of exponential inverses.

Wait, but the properties requires that \gcd(m, N) = 1. So it seems like we can't use some values of m. Furthermore, it should be computationally infeasible to recover d using e and N.

Regarding the Choice of N

If N is prime, it is very easy to find d. Since the relation ed \equiv 1 \pmod {(N-1)} holds, we directly see that d can be computed efficiently using the extended Euclidean algorithm.

The next simplest case would be setting N = pq for two large primes p and q. We expose N to the public but hide primes p and q. Now suppose the attacker wants to compute d using (N, e). The attacker knows that ed \equiv 1 \pmod {\phi(N)}, and \phi(N) = (p-1)(q-1). So to calculate d, the attacker must know \phi(N), which requires the factorization of $N$.

If the factorization N = pq is known, finding d is easy. But factoring large prime numbers (especially a product of two primes of similar size) is known to be very difficult.1 No one has formally proven this, but we believe and assume that it is hard.2

Chinese Remainder Theorem in RSA

Assume that the message m is not divisible by both p and q. By Fermat's little theorem, we have m^{p-1} \equiv 1 \pmod p and m^{q-1} \equiv 1 \pmod q.

Therefore, for decryption in RSA, the following holds. Note that N = pq.


c^d \equiv m^{ed} \equiv m^{1 + k\phi(N)} \equiv m \cdot (m^{p-1})^{k(q-1)} \equiv m \cdot 1^{k(q-1)} \equiv m \pmod p.

A similar result holds for modulus q. This does not exactly recover the message yet, since m could have been chosen to be larger than p. The above equation is true, but during actual computation, one may get a result that is less than p. This may not be equal to the original message.3

Since N = pq, we use the Chinese remainder theorem. Instead of computing c^d \pmod N, we can compute


c^d \equiv m \pmod p, \qquad c^d \equiv m \pmod q

independently and solve the system of equations to recover the message.

Can I Encrypt p with RSA?

Now we return to the problem where \gcd(m, N) \neq 1. The probability of \gcd(m, N) \neq 1 is actually \frac{1}{p} + \frac{1}{q} - \frac{1}{pq}, so if we take large primes p, q \approx 2^{1000} as in RSA2048, the probability of this occurring is roughly 2^{-999}, which is negligible. But for completeness, we also prove for this case.

e, d are still chosen to satisfy ed \equiv 1 \pmod {\phi(N)}. Suppose we want to decrypt c \equiv m^e \pmod N.

We will also use the Chinese remainder theorem here.

Since \gcd(m, N) \neq 1 and N = pq, we have p \mid m. So if we compute in \mathbb{Z} _ p, we will get 0,


c^d \equiv m^{ed} \equiv 0^{ed} \equiv 0 \pmod p.

We also do the computation in \mathbb{Z} _ q and get


c^d \equiv m^{ed} \equiv m^{1 + k\phi(N)} \equiv m\cdot (m^{q-1})^{k(p-1)} \equiv m \cdot 1^{k(p-1)} \equiv m \pmod q.

Here, we used the fact that m^{q-1} \equiv 1 \pmod q. This holds because if p \mid m, m is a multiple of p that is less than N, so m = pm' for some m' such that 1 \leq m' < q. Then \gcd(m, q) = \gcd(pm', q) = 1 since q does not divide p and m' is less than q.

Therefore, from c^d \equiv 0 \pmod p and c^d \equiv (m \bmod q) \pmod q, we can recover a unique solution c^d \equiv m \pmod N.

Now we must argue that the recovered solution is actually equal to the original m. But what we did above was showing that m^{ed} and m in \mathbb{Z} _ N are mapped to the same element (0, m \bmod q) in \mathbb{Z} _ p \times \mathbb{Z} _ q. Since the Chinese remainder theorem tells us that this mapping is an isomorphism, m^{ed} and m must have been the same elements of \mathbb{Z} _ N in the first place.

Notice that we did not require m to be relatively prime to N. Thus the RSA encryption scheme is correct for any m \in \mathbb{Z} _ N.

Correctness of RSA with Fermat's Little Theorem

Actually, the above argument can be proven only with Fermat's little theorem. In the above proof, the Chinese remainder theorem was used to transform the operation, but for N = pq, the situation is simple enough that this theorem is not necessarily required.

Let M = m^{ed} - m. We have shown above only using Fermat's little theorem that p \mid M and q \mid M, for any choice of m \in \mathbb{Z} _ N. Then since N = pq = \mathrm{lcm}(p, q), we have N \mid M, so m^{ed} \equiv m \pmod N. Hence the RSA scheme is correct.

So we don't actually need Euler's generalization for proving the correctness of RSA...?! In fact, the proof given in the original paper of RSA used Fermat's little theorem.

Discrete Logarithms

This is an inverse problem of exponentiation. The inverse of exponentials is logarithms, so we consider the discrete logarithm of a number modulo $p$.

Given y \equiv g^x \pmod p for some prime p, we want to find x = \log _ g y. We set g to be a generator of the group \mathbb{Z} _ p or \mathbb{Z} _ p^\ast, since if g is the generator, a solution always exists.

Read more in discrete logarithm problem (Modern Cryptography).

ElGamal Encryption

This is an encryption scheme built upon the hardness of the DLP.

  1. Let p be a large prime.
  2. Select a generator g \in \mathbb{Z} _ p^\ast.
  3. Choose a private key x \in \mathbb{Z} _ p^\ast.
  4. Compute the public key y = g^x \pmod p. - p, g, y will be publicly known. - x is kept secret.

ElGamal Encryption and Decryption

Suppose we encrypt a message m \in \mathbb{Z} _ p^\ast.

  1. The sender chooses a random k \in \mathbb{Z} _ p^\ast, called ephemeral key.
  2. Compute c _ 1 = g^k \pmod p and c _ 2 = my^k \pmod p.
  3. c _ 1, c _ 2 are sent to the receiver.
  4. The receiver calculates c _ 1^x \equiv g^{xk} \equiv y^k \pmod p, and find the inverse y^{-k} \in \mathbb{Z} _ p^\ast.
  5. Then c _ 2y^{-k} \equiv m \pmod p, recovering the message.

The attacker will see g^k. By the hardness of DLP, the attacker is unable to recover k even if he knows g.

Ephemeral Key Should Be Distinct

If the same k is used twice, the encryption is not secure. Suppose we encrypt two different messages m _ 1, m _ 2 \in \mathbb{Z} _ p^\ast. The attacker will see (g^k, m _ 1y^k) and (g^k, m _ 2 y^k). Then since we are in a multiplicative group \mathbb{Z} _ p^\ast, inverses exist. So


m _ 1y^k \cdot (m _ 2 y^k)^{-1} \equiv m _ 1m _ 2^{-1} \equiv 1 \pmod p

which implies that m _ 1 \equiv m _ 2 \pmod p, leaking some information.


  1. If one of the primes is small, factoring is easy. Therefore we require that p, q both be large primes. ↩︎

  2. There is a quantum polynomial time (BQP) algorithm for integer factorization. See Shor's algorithm. ↩︎

  3. This part of the explanation is not necessary if we use abstract algebra! ↩︎