Pulsars
0 %
Log inSign up

The second factor and one-time codes

HOTP and TOTP: the 6-digit codes

The most widespread second factor is the six-digit code that changes every thirty seconds, displayed by an authenticator app. Behind this simplicity lie two standards: HOTP and TOTP.

HOTP: a code tied to a counter

HOTP (HMAC-based One-Time Password, RFC 4226) produces a one-time code from two ingredients shared between the server and the user's device:

  • a shared secret, drawn just once at registration;
  • a counter, incremented with each generated code.

The heart of the computation is an HMAC function (a message authentication code based on a hash function). The result, long and unreadable, is truncated to give six digits a human can type.

secret + counter ---> HMAC ---> truncation ---> 123456

The counter guarantees that a code is valid only once. But it raises a synchronization problem: if the device and the server no longer count the same way (a generated but unused code), they drift apart.

TOTP: time instead of the counter

TOTP (Time-based One-Time Password, RFC 6238) solves this problem by replacing the counter with the current time. Concretely, the clock is divided into thirty-second windows, and you count the number of windows elapsed since a reference date.

secret + (time / 30 s) ---> HMAC ---> truncation ---> 6 digits

This is the only fundamental difference from HOTP: the variable input is no longer an event counter but a counter of time windows. The server and the app, sharing the same secret and reading the same clock, compute the same code without ever communicating.

The initial sharing of the secret

Everything rests on the secret shared at the outset. At registration, the service displays a QR code encoding this secret; the app scans it and stores it. From then on, both sides can generate the same code at any moment.

+-----------+                         +----------------------+
| Server    |  --- QR (secret) --->   | App (phone)          |
| keeps the |                         | keeps the same       |
| secret S  |                         | secret S             |
+-----------+                         +----------------------+
     |   every 30 s, each one computes |
     |   TOTP(S, time) on its own side |
     v                                 v
   123456          ==                123456

The weakness: phishing

TOTP protects against password leaks: stealing the database is no longer enough. But it remains vulnerable to real-time phishing. A fake site imitates the login page, asks for the password and the six-digit code, then immediately replays them on the real site. The code is valid for thirty seconds: that is enough for the attacker.

The deeper cause: the code is just a value that the user retypes, with no link to the site receiving it. Nothing prevents entering it in the wrong place.

In summary

  • HOTP derives a code from a secret and a counter via HMAC, then truncates to six digits.
  • TOTP replaces the counter with time (30 s windows); it is the code used by authenticator apps.
  • The secret is shared at the outset via QR code; it remains vulnerable to phishing.