Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 40 ยท Authentication and Keys

Authentication, Multi-Factor and One-Time Passwords

Learn the authentication factors, how two-factor authentication works, how HOTP and TOTP one-time passwords are computed with HMAC, and what non-repudiation means.

Authentication answers the question "who are you?" It is different from authorization ("what may you do?") and from identification (claiming an identity, such as typing a username). Cryptography appears everywhere in this process: passwords are stored as hashes, sessions are protected by MACs, and one-time passwords are computed from secret keys. This lesson covers the factors you can prove identity with, shows how the one-time codes in authenticator apps are built, and explains the related idea of non-repudiation.

The three factors

Authentication evidence falls into three classes:

Factor Meaning Examples
Something you know A secret in your head Password, PIN, security answer
Something you have A physical or digital object Phone with an authenticator app, hardware key, smart card
Something you are A body characteristic Fingerprint, face, iris, voice

Single-factor authentication (SFA) uses one class, almost always a password. Two-factor authentication (2FA) combines two different classes; a password plus a security question is still one factor, because both are things you know. Multi-factor authentication (MFA) means two or more. The point is that a stolen password alone no longer lets an attacker in, because they also need your phone or your finger.

Biometrics

Biometric systems compare a fresh measurement to a stored template. Fingerprints, face geometry, iris patterns, voice and typing rhythm are common. Their strengths are convenience and the fact that you cannot forget them. Their limits are real:

  • Matching is statistical, so there are false accepts (a stranger passes) and false rejects (you fail); lowering one raises the other.
  • You cannot change a fingerprint after a leak, unlike a password. Store only templates, protected on the device, never raw images.
  • Sensors can be fooled by copies, so serious systems use liveness checks.

Because of this, biometrics are best used to unlock a local key (as in passkeys), not as a secret shared with a server.

One-time passwords

A one-time password (OTP) is valid for a single login, so an attacker who sees it cannot reuse it. The two standard algorithms are both built on HMAC and a secret shared between the server and your device when you scan the QR code:

  • HOTP (RFC 4226) is event-based. Both sides keep a counter. The code is HMAC-SHA1(secret, counter) reduced to 6 to 8 digits. The server accepts codes in a small window ahead of its own counter to stay in sync.
  • TOTP (RFC 6238) is time-based. The counter is the number of 30-second steps since 1970, so codes expire on their own. This is what Google Authenticator-style apps use.

The reduction to digits is called dynamic truncation: take the low 4 bits of the last HMAC byte as an offset, read 4 bytes from there, clear the top bit and take the result modulo 10^digits.

Python HOTP and TOTP from scratch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first values match the published RFC test vectors, which is how implementers check their code. Notice what the code protects against and what it does not. A leaked old code is useless, but a phishing site can ask you for the current code and relay it to the real site within the 30-second window. Hardware keys and passkeys using public-key challenges (FIDO2/WebAuthn) are phishing-resistant because the response is bound to the real site's origin.

Other one-time-password forms are SMS codes (convenient, weak against SIM swapping and interception), emailed links and printed backup codes. Treat backup codes like passwords: store them hashed and invalidate each after use.

Non-repudiation

Non-repudiation means a party cannot credibly deny having performed an action, such as sending a message or approving a payment. Of the four goals of cryptography (confidentiality, integrity, authentication, non-repudiation), it is the one that symmetric tools cannot provide. If Alice and Bob share an HMAC key, either can produce a valid tag, so Alice can claim "Bob forged it". A digital signature made with a private key only Alice holds can be verified by anyone and produced by no one else, so she cannot plausibly deny it.

In practice non-repudiation needs more than a signature:

  • a trusted binding between the key and the person, which comes from certificates and PKI,
  • a trustworthy timestamp, so a signature cannot be backdated after the key is revoked,
  • audit logs that cannot be quietly edited (see the hash chain project),
  • protection of the private key, since "my key was stolen" is the standard defense.

Common uses are electronic contracts, e-commerce receipts, signed software releases and tax filings.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which pair counts as true two-factor authentication?

    A password is something you know; the app code proves you have the device. The other pairs are both knowledge factors.
  2. 2. What makes a TOTP code expire on its own?

    TOTP computes HOTP with the counter set to the number of time steps since 1970, so a new step gives a new code.
  3. 3. Why can an HMAC tag not provide non-repudiation?

    Only a signature made with a private key that one party alone holds can be attributed to that party.
  4. 4. What is a weakness of ordinary TOTP codes?

    A user can be tricked into typing the code into a fake site that forwards it immediately. Hardware keys and passkeys bind the response to the real origin.

Summary

  • Factors are something you know, have or are; real 2FA combines two different classes.
  • Biometrics are convenient but cannot be changed after a leak and are statistical, so use them to unlock local keys.
  • HOTP uses a counter, TOTP the current time step; both are HMAC plus dynamic truncation.
  • One-time codes do not stop real-time phishing; passkeys and hardware keys do.
  • Non-repudiation needs digital signatures, trusted key binding, timestamps and tamper-evident logs.

Next: Public-key ideas.