Files
blog/_posts/lecture-notes/internet-security/2023-10-04-rsa-elgamal.md
Sungchan Yi 307bb79179 [PUBLISHER] upload files #159
* 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 : 04. Modular Arithmetic (1).md

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

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

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

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

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

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

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

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

* PUSH NOTE : 01. Security Introduction.md

* PUSH ATTACHMENT : is-01-cryptosystem.png

* PUSH NOTE : 9. Public Key Encryption.md

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

* 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 : 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 : 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 : 16. The GMW Protocol.md

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

* PUSH NOTE : 13. Sigma Protocols.md

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

* PUSH ATTACHMENT : mc-10-schnorr-identification.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 : 10. Digital Signatures.md

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

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

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

* PUSH ATTACHMENT : mc-01-ss.png

* DELETE FILE : _posts/Lecture Notes/Internet Security/2023-09-10-security-intro.md

* DELETE FILE : _posts/Lecture Notes/Internet Security/2023-09-11-symmetric-key-cryptography-1.md

* DELETE FILE : _posts/Lecture Notes/Internet Security/2023-09-18-symmetric-key-cryptography-2.md

* DELETE FILE : _posts/Lecture Notes/Internet Security/2023-09-25-modular-arithmetic-1.md

* DELETE FILE : _posts/Lecture Notes/Internet Security/2023-10-04-modular-arithmetic-2.md

* DELETE FILE : _posts/Lecture Notes/Internet Security/2023-10-04-rsa-elgamal.md

* DELETE FILE : _posts/Lecture Notes/Internet Security/2023-10-09-public-key-cryptography.md

* DELETE FILE : _posts/Lecture Notes/Internet Security/2023-10-16-pki.md

* DELETE FILE : _posts/Lecture Notes/Internet Security/2023-10-18-tls.md

* DELETE FILE : _posts/lecture-notes/internet-security/2023-10-19-public-key-encryption.md

* DELETE FILE : assets/img/posts/Lecture Notes/Internet Security/is-01-cryptosystem.png

* DELETE FILE : assets/img/posts/Lecture Notes/Internet Security/is-03-cbc-encryption.png

* DELETE FILE : assets/img/posts/Lecture Notes/Internet Security/is-03-cfb-encryption.png

* DELETE FILE : assets/img/posts/Lecture Notes/Internet Security/is-03-ctr-encryption.png

* DELETE FILE : assets/img/posts/Lecture Notes/Internet Security/is-03-ecb-encryption.png

* DELETE FILE : assets/img/posts/Lecture Notes/Internet Security/is-03-feistel-function.png

* DELETE FILE : assets/img/posts/Lecture Notes/Internet Security/is-03-ofb-encryption.png

* DELETE FILE : assets/img/posts/Lecture Notes/Internet Security/is-08-certificate-validation.png

* DELETE FILE : assets/img/posts/Lecture Notes/Internet Security/is-09-tls-handshake.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-01-prg-game.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-01-ss.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-02-2des-mitm.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-02-DES.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-02-aes-128.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-02-block-cipher.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-02-des-round.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-02-feistel-network.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-04-cbc-mac.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-04-ecbc-mac.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-04-mac-security.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-04-mac.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-05-ci.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-05-etm-mte.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-06-davies-meyer.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-06-hmac.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-06-merkle-damgard.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-07-dhke-mitm.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-07-dhke.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-07-merkle-puzzles.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-09-ss-pke.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-10-dsig-security.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-10-schnorr-identification.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-12-id-protocol.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-13-chaum-pedersen.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-13-gq-protocol.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-13-okamoto.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-13-sigma-protocol.png

* DELETE FILE : assets/img/posts/Lecture Notes/Modern Cryptography/mc-16-beaver-triple.png
2024-11-12 20:54:38 +09:00

8.2 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^*, 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^*.
  3. Choose a private key x \in \mathbb{Z}_p^*.
  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^*.

  1. The sender chooses a random k \in \mathbb{Z}_p^*, 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^*.
  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^{ * }. 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^{ * }, 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! ↩︎