Secrets and key pairs
A credential is the value that lets a person or an application prove a claim about an account. Shared secrets and key pairs both do that job, but they differ in one way that matters most: whether the party checking the proof could also make one.
What a credential is
When Ava signs in to the photo service, her password proves the account is hers. The printer, the application she connected to her photos, has the same problem in its own form. When it asks the photo service's authorization serverIn OAuth, the service that handles a person's sign-in and approval and decides which tokens to issue to a client. It may belong to the same company as the API, but its job is separate. for an access tokenA token that a client presents to an API to use access it has been granted, usually limited in what it allows and how long it lasts. It authorizes API calls; it does not tell the client who signed in., it identifies itself with its client ID and proves the claim with a client secretA shared secret that a confidential client uses to prove itself to the authorization server, much like a password for an application. It belongs on a backend, never in code sent to browsers or phones.. And when it sends a finished photo book to the print lab, the business that prints the books, the print lab's APIAn application programming interface: a defined way for software to interact with a service. A web API offers endpoints where other programs send HTTP requests. needs to know that each order really comes from the printer.
In this series, a credential is the secret or key that a person or application uses to prove that an account or relationship belongs to it. Ava might use a password or a passkeyA credential that your phone, computer or password manager creates for one website and keeps for you, unlocked with a fingerprint, face or PIN. Nothing you could type is sent, and it works only for the site it was made for, which helps it resist phishing.. The printer might use a client secret or a private keyThe half of a key pair that its owner keeps to themselves and uses to create signatures or decrypt data. Anyone who obtains it can act as its owner..
The word is used in more than one way, so it helps to know the others. NIST's digital identity guidelines call the secret or device itself an authenticator, and reserve "credential" for the record that binds an authenticator to one account. A verifiable credentialA credential that an issuer signs and gives to a holder, who can later present it to any verifier. The verifier checks the issuer's signature without contacting the issuer each time. is something else again: a set of claims about a person, signed by whoever issued them. The ideas in this lesson apply under any of these names.
A credential is different from an identifier. Ava's email address says which account is being claimed; the password supports the claim. The printer's client ID says which client is calling; the client secret is what the authorizationDeciding whether a requested action is permitted, such as whether a signed-in account may delete a particular photo. Authentication says which account is asking; authorization decides what it can do. server checks. Identifiers can be known by many people. A credential is only useful as evidence if its holder can keep it from others.
Key pairs
Public-key cryptography splits that capability in two. A key pair is a private key and a public keyThe half of a key pair that can be shared freely. It checks signatures made with the matching private key and can encrypt data that only that private key can decrypt. generated together and mathematically related. The private key can create a signatureA value made with a private key that lets anyone holding the matching public key check that a message came from the key's holder and has not been changed. It proves origin and integrity; it does not hide the message.. The public key can check that signature but cannot create one, and the private key cannot practically be worked out from it.
The printer keeps its private key to itself and gives the print lab the public key. Now the print lab can check that an order was signed by the printer, but a thief who steals the print lab's copy of the public key gains nothing they could not already find. The same public key can be given to any number of verifiers without adding risk.
HMAC tag on each order
- The printer proves with
- The shared key
- The print lab checks with
- The same shared key
- A thief copies the print lab key
- Can make new tags
Forged orders look genuine
Signature on each order
- The printer proves with
- The printer's private key
- The print lab checks with
- The printer's public key
- A thief copies the print lab key
- Can only check signatures
Forged orders are refused
Whenever a verifier needs the secret itself to do its check, its copy can create proofs as well as check them.
In code, the difference is which key each side holds. In the illustration below, constant_time_equal compares the two tags in the same amount of time however many characters match, so the time a refusal takes doesn't tell an attacker how close a guessed tag came.
# Shared key: both sides can create and check the same tag
# Printer
tag = hmac_sha256(shared_key, order)
# Print lab
accepted = constant_time_equal(received_tag, hmac_sha256(shared_key, received_order))
# Key pair: only the private key can sign
# Printer
signature = sign(printer_private_key, order)
# Print lab
accepted = verify(printer_public_key, received_order, received_signature)Two families of key pair are in wide use. RSA keys are built from very large numbers, a few thousand bits long. Elliptic curve keys are built on a named mathematical curve, most often P-256, and reach the same strength with much shorter keys and faster signing. The algorithm names from Keys, encryption, and signatures say which family they use: RS256 signs with an RSA key, and ES256 signs with ECDSA, the elliptic curve signature algorithm, on a P-256 key. Both hash the data with SHA-256 first.
Key pairs can also protect confidentiality: data encrypted with a public key can be decrypted only with the matching private key. In practice, public-key operations are much slower than shared-key ones, so protocols such as TLS use key pairs to authenticate and agree on fresh shared keys, then use those shared keys to protect the rest of the connection.
If Ava signs in to the photo service with a passkey, that is a key pair too. The photo service stores only the public key, so a breach of its database reveals nothing that could sign in as Ava.
| Question | Shared secret | Key pair |
|---|---|---|
| Who can create a valid proof? | Everyone holding the secret, including the verifier | Only the private key holder |
| What does the verifier store? | The secret, or a hash when the holder sends the secret itself, as with a password | The public key, which is not secret |
| What if the verifier's copy leaks? | The attacker may be able to impersonate the holder | The attacker can verify signatures, nothing more |
| How many verifiers can share it? | Ideally one per secret | Any number |
Proving possession
A signature over a fixed message has a weakness: if an attacker records it, they can send the same signed message again. To show that the key holder is present now, a verifier can issue a challenge, a random value it has never used before. The holder signs the challenge, and the verifier checks the signature with the public key. Because each challenge is fresh, an old answer is useless.
That is the core of passkey sign-in, and of how a TLS server proves it holds the private key for its certificate: the Certificate Verify signature in the handshake from HTTPS and certificates in practice covers the handshake so far, including random values both sides chose for that connection. The key itself never leaves its holder.
Many credentials work differently. A bearer credential, such as an API key sent with each request or an ordinary access token, is accepted from whoever presents it. Proof-of-possession designs bind a credential to a key, as Protecting credentials and messages described, so the client must answer for that key each time and a copied token is not enough on its own.
Whose key is it?
A public key on its own is just a long number. Before trusting a signature, the print lab needs to know that this public key belongs to the printer, not to someone who sent the print lab their own key while claiming to be the printer. A valid signature checked against the wrong key proves only that the wrong party signed.
For a small number of partners, the answer can come from setup: the printer hands the print lab its public key through a channel the print lab already trusts, and the print lab records it against the printer's registration. At the scale of the web, where a browser meets millions of servers it has never seen, that does not work. There the binding comes from a certificate: a signed document, issued by an authority the browser already trusts, that ties a public key to a name.
Check your understanding
Try these questions before moving on. If an answer isn't right, use the feedback and try again.
Try it in the Lab
- Tell a public identifier from a secret or public keyReplace a client secret and keep its ID, make an HMAC tag the sender never made with the verifier's copy of a shared key, then sign orders and a fresh challenge with a key pair that a stolen public key cannot imitate.