Digital Signatures
An HMAC proves a message came from someone who knows the shared key, but both sides know that key, so either could have written the message. A digital signature uses a key pair instead: only the private key can sign, and anyone with the public key can verify. That gives three guarantees:
- Integrity: the message wasn't changed.
- Authenticity: it was signed by the holder of the private key.
- Non-repudiation: the signer can't plausibly deny it, since nobody else has the private key.
Signatures protect software updates, code commits, TLS certificates, JWTs, e-mail (DKIM) and legal documents.
How signing works
Signing schemes first hash the message, then perform a private-key operation on the hash. Verification uses the public key to check that the signature matches the hash of the received message.
signer: message ──hash──▶ digest ──sign with PRIVATE key──▶ signature
verifier: message + signature + PUBLIC key ──verify──▶ valid / invalidThe modern default is Ed25519: small keys (32 bytes), small signatures (64 bytes), fast, and hard to misuse. ECDSA and RSA-PSS are also widely used.
Signing and verifying with Ed25519
Ed25519 signatures are deterministic: the same key and message always give the same signature. Using a fixed demo private key therefore gives repeatable output:
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
from cryptography.hazmat.primitives import serialization
# DEMO ONLY: a fixed key so the output is stable. Use Ed25519PrivateKey.generate().
private_key = Ed25519PrivateKey.from_private_bytes(bytes(range(32)))
public_key = private_key.public_key()
raw_pub = public_key.public_bytes(serialization.Encoding.Raw, serialization.PublicFormat.Raw)
print("public key:", raw_pub.hex())
message = b"release v2.1.0 sha256=9f2c..."
signature = private_key.sign(message)
print("signature:", len(signature), "bytes")
print(signature.hex()[:64] + "…")
print("same again:", private_key.sign(message) == signature)public key: 03a107bff3ce10be1d70dd18e74bc09967e4d6309ba50d5f1ddc8664125531b8
signature: 64 bytes
33b2c736b6b51b165d67aa73660273c8237c774037c8ef591ade37d42856bab0…
same again: True
Rejecting tampered messages
verify raises InvalidSignature when the message, the signature or the key doesn't match. Wrap it in a helper that returns a boolean, and treat any exception as "invalid":
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
from cryptography.exceptions import InvalidSignature
signer = Ed25519PrivateKey.from_private_bytes(bytes(range(32))) # DEMO ONLY
impostor = Ed25519PrivateKey.from_private_bytes(bytes(range(1, 33))) # DEMO ONLY
trusted_public = signer.public_key()
def is_valid(public_key, message, signature):
try:
public_key.verify(signature, message)
return True
except InvalidSignature:
return False
msg = b"pay 100 EUR to account 1234"
sig = signer.sign(msg)
print("genuine: ", is_valid(trusted_public, msg, sig))
print("message changed: ", is_valid(trusted_public, b"pay 900 EUR to account 1234", sig))
print("signed by impostor:", is_valid(trusted_public, msg, impostor.sign(msg)))genuine: True
message changed: False
signed by impostor: False
Verifying software updates
A classic use is an updater that only installs packages signed by the vendor. The vendor's public key is built into the updater, so even an attacker who controls the download server can't produce a package that verifies:
import hashlib
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
from cryptography.exceptions import InvalidSignature
vendor_key = Ed25519PrivateKey.from_private_bytes(b"\x07" * 32) # DEMO: kept offline by the vendor
EMBEDDED_PUBLIC_KEY = vendor_key.public_key() # shipped inside the app
def publish(package: bytes):
return package, vendor_key.sign(hashlib.sha256(package).digest())
def install(package: bytes, signature: bytes):
try:
EMBEDDED_PUBLIC_KEY.verify(signature, hashlib.sha256(package).digest())
except InvalidSignature:
return "REFUSED: signature check failed"
return f"installed {len(package)} bytes"
pkg, sig = publish(b"updater-2.1.0 binary contents")
print(install(pkg, sig))
print(install(pkg + b" plus malware", sig))installed 29 bytes
REFUSED: signature check failed
Compare this with the hash check in the Hashing lesson: a hash published on the same server can be replaced along with the file, but the attacker can't forge a signature without the vendor's private key.
Where signatures go wrong
- Not verifying at all: code that parses a signed token or file but skips the check. Always verify before using the content.
- Trusting the key that came with the message: if the attacker supplies both the message and the public key, they simply sign with their own key. The trusted key must come from somewhere the attacker can't control (built into the app, or a certificate chain, next lesson).
- Signing ambiguous data: sign a canonical encoding (exact bytes), not something that can be re-serialised differently.
- Leaked private keys: keep signing keys in hardware (HSM, security keys) or a KMS, and have a plan to revoke and rotate.
Go further: Ed25519 Signatures checks the RFC test vector, and RSA Done Right: OAEP and PSS explains why RSA needs randomised padding.
Test yourself
Which key is used to verify a digital signature?
- The signer's private key
- The signer's public key
- A shared secret key
- The verifier's private key
What does a signature provide that an HMAC doesn't?
- Integrity
- Non-repudiation, because only one party holds the signing key
- Encryption
- Compression
An attacker sends a file, a signature and a public key together, and the verifier uses that public key. What's wrong?
- Nothing
- The attacker can sign with their own key, so verification proves nothing
- Ed25519 doesn't support files
- The signature is too short