Back
+20 XP
Concept

Digital Signatures & Non-Repudiation (Ed25519)

HMAC proves integrity, but both sides share the secretβ€”meaning the receiver could forge messages. Ed25519 digital signatures provide asymmetric non-repudiation: only the private key owner can sign, but anyone with the public key can verify authenticity βœοΈπŸ“œ.

python
from cryptography.hazmat.primitives.asymmetric import ed25519
from cryptography.exceptions import InvalidSignature

# Generate Ed25519 asymmetric signing keypair:
private_key = ed25519.Ed25519PrivateKey.generate()
public_key = private_key.public_key()

payload = b"AUTHORIZE_DEPLOYMENT_COMMIT=8f3c19"
signature = private_key.sign(payload)

# Public verification:
try:
    public_key.verify(signature, payload)
    print(f"Signature verified (length={len(signature)}B) πŸš€")
except InvalidSignature:
    print("Signature invalid! πŸ›‘")

# 68. Digital Signatures & Non-Repudiation (Ed25519) In multi-tenant financial infrastructure or decentralized systems, you cannot give every customer the secret HMAC key used to generate balance approvals. If you give them the key to verify their balance, they can use that exact same key to forge their own balance approvals πŸ›‘. ```python # Symmetric (HMAC): # Signer: Holds Secret Key (K) # Verifier: MUST ALSO Hold Secret Key (K) -> Can forge signatures! # Asymmetric (Ed25519): # Signer: Holds Private Key (k) -> Signs payload # Verifiers: Hold Public Key (K = k * B) -> Can ONLY verify, NEVER forge! ``` ### The Ed25519 Advantage 1. **Asymmetric Security**: The private key (32 bytes) is locked securely in a Hardware Security Module (HSM) or server environment variable. The public key (32 bytes) is published openly on GitHub, DNS TXT records, or client devices. 2. **Deterministic Signatures**: Unlike legacy ECDSA (where a faulty RNG nonce leaks the entire private key, as happened in the Sony PS3 hack), Ed25519 is strictly deterministic and immune to poor entropy during signing. 3. **High Throughput**: Ed25519 verifies tens of thousands of signatures per second per CPU core, making it the premier choice for Git commit signing, SSH, and modern zero-trust microservice communication βœοΈπŸ“œ. --- ### Interactive Pop-Quiz 🧠 #### What critical security guarantee do asymmetric digital signatures (like Ed25519) provide that symmetric HMAC cannot? - [ ] Non-repudiation: verifiers only possess the public key, so they can verify signatures but can never forge them ✍️ - [ ] They compress the signed payload to half its original byte size - [ ] They eliminate the requirement for TCP/IP network layers - [ ] They automatically rotate private keys every 24 hours --- ### Practical Challenge 🎯 Generate an `ed25519.Ed25519PrivateKey`. Sign `b'RELEASE_V1'`. Verify the signature using `public_key.verify()`. Then attempt to verify `signature` against `b'RELEASE_V2'`, catching `InvalidSignature`. Print `'NON_REPUDIATION_OK'`. <details> <summary>Solution</summary> ```python from cryptography.hazmat.primitives.asymmetric import ed25519 from cryptography.exceptions import InvalidSignature priv = ed25519.Ed25519PrivateKey.generate() pub = priv.public_key() msg = b"RELEASE_V1" sig = priv.sign(msg) pub.verify(sig, msg) tampered_caught = False try: pub.verify(sig, b"RELEASE_V2") except InvalidSignature: tampered_caught = True assert tampered_caught is True print("NON_REPUDIATION_OK") ``` </details>

Checkpoint

What critical security guarantee do asymmetric digital signatures (like Ed25519) provide that symmetric HMAC cannot? 🧠

Practice

Generate an `ed25519.Ed25519PrivateKey`. Sign `b'RELEASE_V1'`. Verify the signature using `public_key.verify()`. Then attempt to verify `signature` against `b'RELEASE_V2'`, catching `InvalidSignature`. Print `'NON_REPUDIATION_OK'`. 🎯

Terminal
01
Tap code blocks below to assemble
Available Blocks