Back
+20 XP
Concept

Ephemeral Diffie-Hellman Key Exchange (ECDH)

Symmetric encryption requires a shared secret. Elliptic-Curve Diffie-Hellman (ECDH over X25519) allows two parties to exchange public keys across an insecure network and compute identical shared secrets without transmitting the secret itself 🤝🔐.

python
from cryptography.hazmat.primitives.asymmetric import x25519

# Alice generates keypair:
alice_private = x25519.X25519PrivateKey.generate()
alice_public = alice_private.public_key()

# Bob generates keypair:
bob_private = x25519.X25519PrivateKey.generate()
bob_public = bob_private.public_key()

# Alice computes shared secret using Bob's public key:
alice_secret = alice_private.exchange(bob_public)

# Bob computes shared secret using Alice's public key:
bob_secret = bob_private.exchange(alice_public)

print(f"Secrets match? {alice_secret == bob_secret} 🚀")

# 67. Ephemeral Diffie-Hellman Key Exchange (ECDH) If you hardcode a shared symmetric AES key into your mobile app or microservice configuration, the moment one developer accidentally pushes that secret to GitHub, your entire encryption system is dead 🛑. ```python # The Key Agreement Magic: # Alice: Private (a), Public (A = a * G) # Bob: Private (b), Public (B = b * G) # Alice calculates: S = a * B = a * (b * G) # Bob calculates: S = b * A = b * (a * G) # S is identical on both sides! Eavesdroppers only see A and B! ``` ### Perfect Forward Secrecy (PFS) 1. **Ephemeral Keys (ECDHE)**: Parties generate brand-new, throwaway keypairs for each session. 2. **Forward Secrecy Guarantee**: Even if an attacker records encrypted network packets for 10 years and eventually compromises the server's long-term master private key, they CANNOT decrypt past sessions because the ephemeral private keys were deleted from RAM immediately after handshake. 3. **X25519 Modern Standard**: Fast, immune to timing side-channels, and avoids the complex parameter-tuning flaws of legacy NIST elliptic curves 🤝🔐. --- ### Interactive Pop-Quiz 🧠 #### What mathematical property makes X25519 Elliptic-Curve Diffie-Hellman secure against an eavesdropper recording network traffic? - [ ] The Discrete Logarithm Problem on elliptic curves: deriving the private key from a public point is computationally intractable 🔐 - [ ] Because public keys automatically erase themselves after 5 milliseconds - [ ] Because packets sent via ECDH bypass public internet routing nodes - [ ] Because elliptic curves require quantum computers to transmit bits --- ### Practical Challenge 🎯 Generate two X25519 private keys for Client and Server. Perform key exchange so both compute their shared secret. Assert both secrets are 32 bytes and strictly equal. Print `'ECDH_KEY_EXCHANGE_OK'`. <details> <summary>Solution</summary> ```python from cryptography.hazmat.primitives.asymmetric import x25519 client_priv = x25519.X25519PrivateKey.generate() server_priv = x25519.X25519PrivateKey.generate() client_secret = client_priv.exchange(server_priv.public_key()) server_secret = server_priv.exchange(client_priv.public_key()) assert len(client_secret) == 32 assert client_secret == server_secret print("ECDH_KEY_EXCHANGE_OK") ``` </details>

Checkpoint

What mathematical property makes X25519 Elliptic-Curve Diffie-Hellman secure against an eavesdropper recording network traffic? 🧠

Practice

Generate two X25519 private keys for Client and Server. Perform key exchange so both compute their shared secret. Assert both secrets are 32 bytes and strictly equal. Print `'ECDH_KEY_EXCHANGE_OK'`. 🎯

Terminal
01
Tap code blocks below to assemble
Available Blocks