Information SecurityUnit 515 min read

Public Key Cryptography: RSA, ECC, Diffie-Hellman, Digital Signatures

Unit 5 of Information Security explores public key cryptography, covering RSA and ECC algorithms, key exchange protocols (Diffie-Hellman), digital signatures, and their applications in secure communication, authentication, and e-commerce.

TAKEAWAYS:

  • Public key cryptography uses asymmetric keys (public/private pairs) to enable secure communication without pre-shared secrets.
  • RSA and Elliptic Curve Cryptography (ECC) are widely used algorithms for encryption, digital signatures, and key exchange.
  • Diffie-Hellman (DH) and its variants (ECDH) enable secure key exchange over insecure channels.
  • Digital signatures provide non-repudiation and authentication using private keys and public key verification.
  • Public key infrastructure (PKI) manages certificates to bind public keys to identities.
  • Real-world applications include HTTPS (TLS/SSL), eSewa transactions, and Ncell digital signatures.

Core Concepts: Asymmetric Keys and Mathematical Foundations

Public key cryptography relies on mathematical problems that are easy to compute in one direction but hard to reverse. Unlike symmetric cryptography (Unit 4), it uses two keys:

  • Public key: Shared openly (e.g., posted on a website or embedded in software).
  • Private key: Kept secret by the owner.

Why Asymmetric Keys?

  • No need to share secrets beforehand (unlike symmetric keys).
  • Supports digital signatures (proof of identity and integrity).
  • Scalability: One public key can encrypt messages for many recipients.

Mathematical Hard Problems

Public key cryptography is based on these one-way functions:

  1. Integer Factorization (used in RSA):
    • Easy to multiply two large primes, but hard to factor their product.
    • Example: , where and are large primes.
  2. Discrete Logarithm Problem (DLP) (used in Diffie-Hellman and ECC):
    • Given , finding is computationally hard.
  3. Elliptic Curve Discrete Logarithm Problem (ECDLP) (used in ECC):
    • Harder than DLP for the same security level, enabling smaller key sizes.

RSA: The Workhorse of Public Key Cryptography

RSA (Rivest-Shamir-Adleman) is the most widely used public key algorithm. It works as follows:

Key Generation

  1. Choose two large primes and (e.g., 1024+ bits).
  2. Compute (modulus).
  3. Compute Euler’s totient function .
  4. Choose a public exponent (typically 65537) such that and .
  5. Compute the private exponent as the modular inverse of :
  6. The public key is , and the private key is .

Encryption and Decryption

  • Encryption: (where is the plaintext message).
  • Decryption: .

Worked Example: RSA Encryption

Scenario: Ncell wants to securely send a message to a user. The user’s public key is . The message (represented as an integer).

  1. Encrypt :
  2. Decrypt using the private key : (In practice, this is computed using efficient algorithms like the square-and-multiply method.)

Real-World Tie-In: Ncell uses RSA to encrypt OTP (One-Time Password) tokens sent to users for authentication. The OTP is encrypted with Ncell’s public key, ensuring only the authorized server (with the private key) can decrypt it.


stateDiagram-v2
    [*] --> RSA_KeyGen: Start
    RSA_KeyGen --> ChoosePrimes: Pick p, q
    ChoosePrimes --> ComputeN: n = p × q
    ComputeN --> ComputePhi: φ(n) = (p-1)(q-1)
    ComputePhi --> ChooseE: Pick e (gcd(e, φ(n)) = 1)
    ChooseE --> ComputeD: d = e⁻¹ mod φ(n)
    ComputeD --> PublicKey: (e, n)
    ComputeD --> PrivateKey: (d, n)
    PublicKey --> [*]
    PrivateKey --> [*]

Figure 1: RSA Key Generation Process


Elliptic Curve Cryptography (ECC): Smaller Keys, Stronger Security

ECC provides equivalent security to RSA but with smaller key sizes, making it efficient for mobile and IoT devices.

Why ECC?

  • 160-bit ECC ≈ 1024-bit RSA in security.
  • Faster computations and lower power consumption (critical for Pathao’s ride-hailing app or eSewa’s mobile payments).
  • Used in Bitcoin, Signal, and TLS 1.3.

Mathematical Foundation

ECC relies on elliptic curves over finite fields. An elliptic curve is defined by: where .

Key Generation in ECC

  1. Choose a curve over a finite field .
  2. Select a base point on with large prime order .
  3. Choose a private key (a random integer ).
  4. Compute the public key (scalar multiplication).

Encryption (ElGamal-like)

  1. Encrypt a message with public key :
    • Choose a random .
    • Compute .
    • Compute , where .
    • Ciphertext: .

Digital Signatures (ECDSA)

ECC Digital Signature Algorithm (ECDSA) works similarly to RSA signatures but uses elliptic curve math.


classDiagram
    class EllipticCurve {
        +y² = x³ + ax + b (mod p)
        +Point: (x, y)
    }
    class PrivateKey {
        -d: integer
    }
    class PublicKey {
        -Q = d × G
    }
    EllipticCurve "1" --> "1" PrivateKey : Defines field
    PrivateKey --> PublicKey : Generates
    PublicKey --> EllipticCurve : Lies on curve

Figure 2: ECC Key Structure


Key Exchange: Diffie-Hellman and ECDH

Diffie-Hellman (DH) enables two parties to establish a shared secret over an insecure channel (e.g., the internet). It is the foundation of TLS/SSL (used in HTTPS) and VPNs.

How DH Works

  1. Agree on a prime and generator (public parameters).
  2. Alice picks a private key , computes , and sends to Bob.
  3. Bob picks a private key , computes , and sends to Alice.
  4. Both compute the shared secret:
    • Alice: .
    • Bob: .

Worked Example: DH Key Exchange

Scenario: Daraz wants to securely exchange a session key with a customer over an untrusted network.

  1. Public parameters: , .
  2. Alice (Daraz) picks , computes .
  3. Bob (Customer) picks , computes .
  4. Shared secret:
    • Alice: .
    • Bob: .

Real-World Tie-In: When you log into eSewa via HTTPS, your browser and eSewa’s server perform an ECDH (Elliptic Curve Diffie-Hellman) handshake to establish a symmetric session key for encrypting your transaction data. This prevents eavesdroppers from reading your payment details.


sequenceDiagram
    participant Alice
    participant Bob
    participant Eavesdropper
    Alice->>Bob: p, g (public)
    Alice->>Bob: A = g^a mod p
    Bob->>Alice: B = g^b mod p
    Alice->>Alice: s = B^a mod p
    Bob->>Bob: s = A^b mod p
    Note over Alice,Bob: Shared secret s = g^(ab) mod p
    Eavesdropper--)Alice: Sees A, B, p, g
    Eavesdropper--)Bob: Cannot compute s (DLP hard)

Figure 3: Diffie-Hellman Key Exchange


Digital Signatures: Non-Repudiation and Authentication

Digital signatures use public key cryptography to verify the authenticity and integrity of a message. They are used in:

  • eSewa receipts (proving you made a payment).
  • Ncell SIM registration (proving your identity).
  • NEPSE stock trades (preventing fraud).

How Digital Signatures Work (RSA Example)

  1. Signing:
    • Hash the message: .
    • Encrypt the hash with the private key: .
    • Signature: .
  2. Verification:
    • Decrypt the signature with the public key: .
    • Hash the received message: .
    • If , the signature is valid.

Worked Example: Signing a NEPSE Trade Order

Scenario: You sign a stock trade order for NEPSE using your private key.

  1. Message "Buy 100 shares of NEPSE at 1000 NPR".
  2. Hash : .
  3. Sign with private key : .
  4. Send to NEPSE.
  5. NEPSE verifies using your public key :
    • Compute .
    • Check if .

Real-World Tie-In: When you submit a Khalti payment request, the merchant’s server verifies your digital signature to ensure the request hasn’t been tampered with and that it truly came from you.


sequenceDiagram
    participant Sender
    participant Receiver
    Sender->>Sender: M = "Trade Order"
    Sender->>Sender: h = Hash(M)
    Sender->>Sender: σ = h^d mod n (sign)
    Sender->>Receiver: (M, σ)
    Receiver->>Receiver: h' = σ^e mod n
    Receiver->>Receiver: h'' = Hash(M)
    alt h' == h'' then
        Receiver->>Receiver: Valid signature!
    else
        Receiver->>Receiver: Invalid or tampered!
    end

Figure 4: Digital Signature Process


Public Key Infrastructure (PKI): Managing Certificates

PKI is the system that binds public keys to identities using digital certificates. It includes:

  • Certificate Authorities (CAs): Trusted entities that issue certificates (e.g., GlobalSign, DigiCert).
  • Certificate Revocation Lists (CRL): Lists of invalidated certificates.
  • Online Certificate Status Protocol (OCSP): Real-time certificate validation.

How PKI Works

  1. Certificate Request: A user generates a key pair and sends a Certificate Signing Request (CSR) to a CA.
  2. Certificate Issuance: The CA verifies the user’s identity and signs the certificate with its private key.
  3. Certificate Distribution: The CA issues the certificate to the user.
  4. Validation: Recipients verify the certificate using the CA’s public key.

Example: HTTPS Certificate for Daraz

When you visit Daraz.com, your browser:

  1. Checks if Daraz’s certificate is valid (not expired, not revoked).
  2. Verifies the certificate’s signature using the CA’s public key (e.g., Let’s Encrypt).
  3. Establishes a secure TLS connection using the public key.

erDiagram
    CERTIFICATE ||--o{ USER : "Issued to"
    CERTIFICATE ||--|| CA : "Issued by"
    CA }|--|| TRUST_STORE : "Public key in"
    USER ||--|| PUBLIC_KEY : "Owns"
    USER ||--|| PRIVATE_KEY : "Owns"
    CERTIFICATE {
        string serialNumber
        string subject
        string issuer
        date validityPeriod
        string publicKey
        string signature
    }

Figure 5: PKI Entity-Relationship Diagram


In the Real World

Public key cryptography is everywhere in Nepal’s digital economy. Here’s how it’s used:

  1. eSewa and Khalti Payments:

    • What it uses: RSA for encrypting payment details and ECDSA for digital signatures.
    • How it works: When you pay via eSewa, your device generates an ephemeral ECDH key pair to establish a secure session with eSewa’s server. Your payment request is signed with your private key, and eSewa verifies it using your public key (stored in their PKI).
  2. Ncell Digital Identity:

    • What it uses: X.509 certificates (PKI) for authenticating SIM registrations.
    • How it works: When you register a SIM, Ncell’s server issues you a digital certificate binding your identity to a public key. This certificate is used to sign all subsequent transactions (e.g., mobile money transfers).
  3. NEPSE Secure Trading:

    • What it uses: RSA and ECC for encrypting trade orders and digital signatures.
    • How it works: Before you can trade stocks, you must register with NEPSE using a digital signature. Your trade orders are signed with your private key, and NEPSE verifies them using your public key to prevent fraud.
  4. Pathao Driver-Verification:

    • What it uses: TLS (which relies on DH/ECDH for key exchange and RSA/ECC for signatures).
    • How it works: When a Pathao driver’s app connects to the server, they perform an ECDH handshake to establish a secure channel. The app’s identity is verified using a certificate issued by a CA.
  5. Bank Internet Banking (e.g., NMB, Global IME):

    • What it uses: PKI for user authentication and TLS for secure communication.
    • How it works: When you log into your bank’s website, your browser presents a client certificate (stored on your token or smartphone) to prove your identity. The bank’s server verifies this certificate before allowing access.


Advantages and Disadvantages of Public Key Cryptography

Advantages Disadvantages
No need to share secrets beforehand. Computationally slower than symmetric crypto.
Supports digital signatures. Key management is complex (PKI required).
Scalable for large systems (e.g., internet). Larger key sizes (though ECC mitigates this).
Provides non-repudiation. Vulnerable to quantum attacks (Shor’s algorithm).

Common Attacks on Public Key Cryptography

Understanding attacks helps in securing implementations:

  1. Man-in-the-Middle (MITM) Attacks:

    • Eavesdropper intercepts key exchange (e.g., DH) and impersonates both parties.
    • Mitigation: Use authenticated DH (e.g., Signal Protocol) or certificates.
  2. Brute Force Attacks:

    • Trying all possible private keys (e.g., guessing in RSA).
    • Mitigation: Use sufficiently large keys (e.g., 2048-bit RSA or 256-bit ECC).
  3. Side-Channel Attacks:

    • Exploiting implementation flaws (e.g., timing attacks on RSA decryption).
    • Mitigation: Use constant-time algorithms and blinding techniques.
  4. Quantum Attacks:

    • Shor’s algorithm can break RSA and ECDH in polynomial time on a quantum computer.
    • Mitigation: Research post-quantum cryptography (e.g., lattice-based schemes).

Exam Tip

This unit is heavily tested in TU exams, especially on:

  1. RSA/ECC Math: Be able to derive keys, encrypt/decrypt small messages, and explain the math behind them.

    • Example Question: "Given , compute the private key ."
    • Solution: Compute , , then find such that . Use the Extended Euclidean Algorithm.
  2. Diffie-Hellman Workings: Trace the key exchange step-by-step and compute shared secrets.

    • Example Question: "Alice and Bob use . Alice picks , Bob picks . What is their shared secret?"
    • Solution: , . Shared secret .
  3. Digital Signatures: Explain how hashing + private key signing works, and how verification uses the public key.

    • Example Question: "How does ECDSA prevent repudiation?"
    • Answer: Only the private key holder can create a valid signature. The public key can verify it, proving the signer’s identity.
  4. PKI and Certificates: Know the roles of CAs, CSRs, and how certificates are validated.

    • Example Question: "What is the purpose of a Certificate Revocation List (CRL)?"
    • Answer: To list certificates that should no longer be trusted (e.g., compromised or expired keys).
  5. Real-World Applications: Relate concepts to eSewa, Ncell, NEPSE, or Pathao.

    • Example Question: "How does Khalti use public key cryptography to secure payments?"
    • Answer: Khalti uses TLS (with ECDH for key exchange and ECDSA for signatures) to encrypt transactions and authenticate users.

Final Checklist for Full Marks

  • Explain the mathematical hardness behind RSA and ECC.
  • Derive RSA keys from primes and compute encryption/decryption.
  • Trace a Diffie-Hellman key exchange and compute shared secrets.
  • Describe digital signatures and their role in non-repudiation.
  • Draw a PKI diagram showing CAs, users, and certificates.
  • Link concepts to Nepali examples (eSewa, Ncell, NEPSE, Pathao).
  • Discuss attacks (MITM, brute force, quantum) and mitigations.

Based on the TU BIM syllabus for Information Security (IT244), unit 5.

Discussion

Loading…