Sub topic 7
Key pairs, signatures and challenges
Assume you understand passwords, two-factor authentication, and basic exchange account settings. This chapter builds the cryptographic model used by passkeys and security keys. The model explains why a server can verify your login without storing a secret that can be copied from its database.
The working model
Public-key cryptography uses two mathematically related keys:
- A private key is a secret value kept by your authenticator. It signs data.
- A public key is shared with the exchange. It verifies signatures.
- A signature is proof that the holder of the private key approved specific data.
- A challenge is fresh, unpredictable data created for one login attempt.
The exchange stores your public key during registration. It does not receive the private key. During sign-in, the exchange sends a challenge. Your authenticator signs that challenge, and the exchange verifies the signature.
| Item | Kept by | Purpose |
|---|---|---|
| Private key | Authenticator | Creates signatures |
| Public key | Exchange | Verifies signatures |
| Challenge | Exchange and authenticator during login | Prevents replay |
| Signature | Sent to exchange | Proves possession of the private key |
A signature does not encrypt the challenge. Anyone may be able to read the challenge and signature. The security property is that creating a valid signature requires the private key.
Registration and sign-in
Registration creates a new key pair and sends only the public key to the exchange. The exchange associates that key with your account.
During sign-in, the exchange must generate a new challenge with a secure random-number generator. The authenticator signs the challenge, usually together with additional context such as the website origin and account identifier. The server checks:
- The signature matches the stored public key.
- The signed challenge is the one it just issued.
- The request came from the expected website origin.
- The challenge has not already been accepted.
# Install first: python -m pip install cryptography
from cryptography.exceptions import InvalidSignature
from cryptography.hazmat.primitives.asymmetric.ed25519 import (
Ed25519PrivateKey,
)
private_key = Ed25519PrivateKey.generate()
public_key = private_key.public_key()
challenge = b"fresh-random-challenge-for-this-login"
signature = private_key.sign(challenge)
try:
public_key.verify(signature, challenge)
print("Valid signature: the authenticator proved possession")
except InvalidSignature:
print("Invalid signature")
try:
public_key.verify(signature, b"changed-challenge")
except InvalidSignature:
print("Changed data is rejected")The example uses Ed25519, a modern signature algorithm. Production WebAuthn implementations also verify structured fields defined by the WebAuthn protocol. Do not replace those checks with a home-grown protocol.
Why challenges stop replay
Suppose an attacker records a valid signature from yesterday. That signature covers yesterday’s challenge. When today’s challenge differs, the recorded signature fails verification. A challenge must be unpredictable and single-use. A fixed value, timestamp used without proper validation, or reusable challenge can allow replay attacks.
The private key also needs protection. Malware that controls an already-unlocked device may ask the authenticator to sign a live challenge. Key-pair cryptography reduces password theft and phishing risk, but it does not replace device security or account recovery planning.
Exercise
What does the exchange need to store to verify a future login, and why is a previously recorded signature normally useless?
Show answer
It stores the public key. A recorded signature covers an old, single-use challenge, so it will not verify against a new challenge.