Sub topic 7
Key pairs, signatures and challenges
This chapter explains the cryptography underneath passkeys. It assumes you know what a password and two-factor authentication are, and that you can run a short Python script. You don't need prior cryptography knowledge. Later chapters cover WebAuthn itself, origin binding and passkey types. This chapter covers the building blocks they share.
Why the exchange needs something other than a shared secret
Passwords and TOTP codes both rely on a shared secret. Your exchange stores a copy of your password hash, or the TOTP seed, and you hold the same value. That design has two weaknesses:
- Whoever steals the server's copy can often use it. A leaked TOTP seed lets an attacker generate your codes for as long as the seed stays valid.
- Whatever you type or scan can be captured and replayed. A phishing page that collects your password and a fresh code can forward both to the real site.
Public-key cryptography removes the shared secret. The exchange stores something that lets it check your proof but not produce it.
Key pairs
A key pair is two mathematically linked numbers:
- The private key is generated on your device and never leaves it. It is the only thing that can create valid signatures.
- The public key is derived from the private key. You can give it to anyone. It can check signatures but cannot create them.
You can't realistically work backward from the public key to the private key. Passkeys typically use elliptic-curve keys, usually the P-256 curve (called ES256 in WebAuthn). A P-256 public key is 64 bytes plus encoding overhead, small enough to store per account without effort.
If an attacker breaches the exchange's database, they get public keys. A public key can't sign in as you. That is the main difference from a stored password hash or TOTP seed.
| Property | Shared secret (password, TOTP seed) | Key pair (passkey) |
|---|---|---|
| What the server stores | The secret, or a derivative that can verify and sometimes reproduce it | Only the public key |
| Server database breach | Attacker may be able to use or crack the values | Public keys alone cannot sign in |
| What travels at login | The secret, or a code derived from it | A signature over a one-time challenge |
| Value captured by a phishing page | Reusable within its validity window | A signature that is useless for any other challenge |
| Per-site uniqueness | Depends on your habits | Automatic: a new pair for each site |
Signatures
A signature is a value computed from a message and a private key. Anyone holding the matching public key can run a verification and get one of two answers: valid or invalid. Verification succeeds only if both of these are true:
- The signature was produced with the private key that matches this public key.
- The message is exactly the one that was signed. Changing one bit makes verification fail.
A signature proves possession of the private key without revealing it. The signature is also tied to one specific message. That second property matters for the next section.
Challenges
If the server asked you to sign a fixed message, an attacker who captured one signature could replay it forever. So the server asks you to sign a challenge: a fresh random value that it generates for each attempt.
A sound challenge has these properties:
- It is random and unpredictable. At least 16 bytes is the usual floor, and 32 bytes is common.
- It is single use. The server discards it once it has been used or has expired.
- It is bound to the attempt. The server remembers which challenge it issued to which session, so a signature over some other challenge is rejected.
- It is short-lived. A challenge that is valid for hours gives an attacker a large window.
This is called challenge-response authentication. The device never sends the private key. It sends only a signature over a value that will never be valid again.
A working example
The script below plays both roles. It uses the third-party cryptography package, which you can install with pip install cryptography. It registers a public key, issues a challenge, verifies a signature, and then shows three failures: a replayed signature, a signature from another key, and a tampered message.
import os
from cryptography.exceptions import InvalidSignature
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import ec
ALGO = ec.ECDSA(hashes.SHA256())
class Server:
def __init__(self):
self.public_keys = {} # user -> public key
self.pending = {} # user -> outstanding challenge
def register(self, user, public_key):
self.public_keys[user] = public_key
def new_challenge(self, user):
challenge = os.urandom(32)
self.pending[user] = challenge
return challenge
def verify(self, user, signature):
# pop() makes the challenge single use, whether or not the check passes
challenge = self.pending.pop(user, None)
if challenge is None:
return False
try:
self.public_keys[user].verify(signature, challenge, ALGO)
except InvalidSignature:
return False
return True
if __name__ == "__main__":
server = Server()
# Registration: the device makes a key pair and shares only the public half.
device_key = ec.generate_private_key(ec.SECP256R1())
server.register("alice", device_key.public_key())
# Normal login
challenge = server.new_challenge("alice")
signature = device_key.sign(challenge, ALGO)
print("valid login:", server.verify("alice", signature)) # True
# Replay: the same signature against the next challenge
server.new_challenge("alice")
print("replayed signature:", server.verify("alice", signature)) # False
# Attacker with a different key pair
attacker_key = ec.generate_private_key(ec.SECP256R1())
challenge = server.new_challenge("alice")
forged = attacker_key.sign(challenge, ALGO)
print("wrong key:", server.verify("alice", forged)) # False
# Tampered message: a signature over different bytes
server.new_challenge("alice")
other = device_key.sign(b"a different message", ALGO)
print("wrong message:", server.verify("alice", other)) # FalseRunning it prints True followed by three False lines. In each failure the server learns nothing it can reuse, and the legitimate private key is never exposed.
Where the guarantees stop
A signature proves that someone holding the private key signed this challenge. It does not prove who asked for it, or which website the request came from. Consider an attacker who runs a fake exchange page. The fake page can relay a real challenge from the real exchange to your device and forward your signature back. Plain challenge-response cannot detect this relay, because the signature is valid.
WebAuthn closes that gap by having the browser include the website's origin in the data that gets signed. It also gives each site its own key pair. The next chapters cover how this works. For now, remember that key pairs and challenges provide the cryptographic foundation, and origin binding provides phishing resistance on top of it.
A second limit is where the private key lives. If it sits in software on a compromised computer, malware may be able to use it. Secure hardware, such as a phone's secure enclave, a TPM or a security key, makes the key non-extractable. The device can sign but cannot reveal the key. Later chapters compare these storage options.
Common errors and misconceptions
- Treating the public key as a secret. It isn't one, and the server database holding it is not a high-value target in the way a password table is.
- Reusing challenges, or generating them with a predictable source such as a timestamp or a counter. Use the operating system's secure random generator, as
os.urandomdoes above. - Not discarding a challenge after use, or accepting one that is not tied to the current session.
- Assuming a valid signature means the login request came from the real site. As the previous section shows, it takes origin binding to guarantee that.
- Confusing signing with encryption. A signature does not hide the message. It only lets others check who signed it and that nothing changed.
Exercise
A server uses a fixed challenge, the string "login", for every user and every sign-in. Explain how an attacker who observes one successful sign-in could later sign in as that user without knowing the private key, and state which property of a good challenge prevents this.
Show answer
The attacker records the signature the device produced over "login". Because the challenge never changes, the same signature is valid on every later attempt, so the attacker replays it and the server's verification succeeds. A good challenge is random, unpredictable and single use. A fresh challenge per attempt means a recorded signature covers a value the server will never ask for again, so replaying it fails verification.
Exercise
An exchange suffers a database breach and attackers copy every user's stored passkey public key. Can they sign in to accounts with that data alone? Contrast this with a breach that exposes TOTP seeds.
Show answer
No. A public key can only verify signatures, and deriving the private key from it is not feasible, so the attackers cannot produce a valid signature over a new challenge. A TOTP seed is a shared secret, and anyone who holds it can compute valid codes, so a breach that exposes seeds lets attackers generate codes for the affected accounts.