Elliptic Curve Cryptography
ECDSA, ECDH, Ed25519, and why ECC beats RSA for size
Skills taught: crypto.ecc
Elliptic Curve Cryptography
ECC provides the same security as RSA with dramatically smaller keys. A 256-bit ECC key provides equivalent security to a 3072-bit RSA key. Smaller keys mean faster operations, less storage, and reduced bandwidth — critical for TLS certificates and mobile devices.
- 256-bit ECC ≈ 3072-bit RSA ≈ 128-bit symmetric security
- 384-bit ECC ≈ 7680-bit RSA ≈ 192-bit symmetric security
- 521-bit ECC ≈ 15360-bit RSA ≈ 256-bit symmetric security
- ECDSA
- Elliptic Curve Digital Signature Algorithm. Uses a private key to sign, public key to verify. Requires a unique random nonce per signature (nonce reuse breaks the system).
- ECDH
- Elliptic Curve Diffie-Hellman. A key agreement protocol. Two parties exchange public keys and independently compute the same shared secret without transmitting it.
- Ed25519
- A modern signing scheme based on Edwards Curve 25519. Deterministic (no random nonce per signature), constant-time, and significantly faster than ECDSA P-256.
ECDSA Signatures
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives import hashes, serialization
import os
# --- Generate ECDSA key pair (P-256 curve) ---
private_key = ec.generate_private_key(ec.SECP256R1())
public_key = private_key.public_key()
# --- Sign ---
document = b"Git commit hash: abc123def456"
signature = private_key.sign(document, ec.ECDSA(hashes.SHA256()))
print("Signature length:", len(signature), "bytes") # ~70 bytes (DER encoded)
# --- Verify ---
try:
public_key.verify(signature, document, ec.ECDSA(hashes.SHA256()))
print("ECDSA signature VALID")
except Exception:
print("ECDSA signature INVALID")
# --- Export public key to PEM ---
pem = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
print(pem.decode())
Warning: ECDSA requires a unique random nonce (k) for every signature. If you sign two different messages with the same k and the same private key, an attacker can recover your private key. This is how the PlayStation 3 was hacked in 2010. Prefer Ed25519 which is deterministic.
Ed25519: The Modern Choice
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
from cryptography.hazmat.primitives import serialization
# --- Generate Ed25519 key pair ---
private_key = Ed25519PrivateKey.generate()
public_key = private_key.public_key()
# --- Sign (no hash parameter needed; Ed25519 handles it internally) ---
document = b"This is the message to sign"
signature = private_key.sign(document)
print("Ed25519 signature length:", len(signature), "bytes") # always 64 bytes
# --- Verify ---
try:
public_key.verify(signature, document)
print("Ed25519 signature VALID")
except Exception:
print("Ed25519 signature INVALID")
# --- Export for SSH / API ---
ssh_pub = public_key.public_bytes(
encoding=serialization.Encoding.OpenSSH,
format=serialization.PublicFormat.OpenSSH
)
print(ssh_pub.decode()) # ssh-ed25519 AAAA...
ECDH Key Agreement
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
from cryptography.hazmat.primitives import hashes
import os
# --- Alice and Bob each generate their own key pairs ---
alice_private = ec.generate_private_key(ec.SECP256R1())
bob_private = ec.generate_private_key(ec.SECP256R1())
alice_public = alice_private.public_key()
bob_public = bob_private.public_key()
# --- Alice computes shared secret using Bob's public key ---
alice_shared = alice_private.exchange(ec.ECDH(), bob_public)
# --- Bob computes shared secret using Alice's public key ---
bob_shared = bob_private.exchange(ec.ECDH(), alice_public)
# They get the same 32 bytes WITHOUT transmitting the secret!
assert alice_shared == bob_shared
print("Shared secret established:", alice_shared.hex()[:16], "...")
# --- Derive AES key from shared secret using HKDF ---
aes_key = HKDF(
algorithm=hashes.SHA256(),
length=32,
salt=None,
info=b"handshake session key"
).derive(alice_shared)
print("AES session key ready:", len(aes_key), "bytes")
ECDSA P-256 vs Ed25519
- ECDSA P-256: NIST curve, requires random nonce per signature
- Ed25519: Modern Edwards curve, deterministic signing
Tip: For new applications: use Ed25519 for signing and X25519 (Curve25519 ECDH) for key agreement. Both are constant-time, fast, and use the safest known curve parameters.
Frequently Asked Questions
What is Curve25519?
An elliptic curve designed by Daniel Bernstein for high security and performance. Used in TLS 1.3, SSH, WireGuard, and Signal Protocol. The signing variant is Ed25519; the key agreement variant is X25519.
Is ECC quantum-safe?
No. Shor's algorithm running on a sufficiently large quantum computer can solve the elliptic curve discrete logarithm, breaking ECC just as it breaks RSA. NIST's post-quantum standards (ML-KEM, ML-DSA) are the replacements.
What is the difference between ECDH and ECDHE?
ECDHE uses ephemeral keys — fresh key pairs generated for each connection. This provides forward secrecy: even if the server's long-term private key is later compromised, past sessions cannot be decrypted.