Code
The cryptography of computers: hashes, AES and HMAC, RSA’s weak keys, Diffie–Hellman, certificates, one-time passwords, Shor and Grover, ML-KEM, BB84, Shamir’s shares, time locks, passwords.
One-Time Passwords
Works out a one-time password from a secret and a counter (HOTP, RFC 4226) or the time (TOTP, RFC 6238): the counter is hashed with the secret by HMAC, four bytes of the result are taken at an offset its last byte names, and their last digits are the code. Each step is drawn. The secret is never saved, shared or sent.
Terms
History, how it works, how it was checked, limits
History
A one-time password proves that its holder has a secret without sending it. HOTP (RFC 4226, 2005), from the Initiative for Open Authentication, hashes a counter that both sides keep; TOTP (RFC 6238, 2011) puts the time in the counter’s place, counted in steps of 30 seconds, so that nothing has to be kept in step but the clocks. It is what an authenticator app shows as six digits, set up from a key URI in a QR code.
How it works
The secret is typed in Base32, as apps show it, in hexadecimal or as text; a key URI (otpauth://) pasted there sets the other fields from its parameters. For TOTP the counter is the whole time steps since 1970 at the Unix time given, or now. The counter’s eight bytes are hashed with the secret by HMAC with SHA-1, SHA-256 or SHA-512. The low four bits of the result’s last byte name an offset; the four bytes from there, the top bit cleared, are a number, and its last six to eight digits are the code. The figure draws the result’s bytes with the offset’s digit shaded and the four bytes taken in gold; the table gives the steps either side.
How it was checked
The tests work out all ten test values of RFC 4226’s Appendix D with their HMAC results and truncated numbers, and all eighteen of RFC 6238’s Appendix B for the three hashes at six moments from 1970 to 2603. The RFC’s table names one secret, but its reference code uses secrets of 20, 32 and 64 bytes for the three hashes: the tests follow the code, with which the printed values agree. The worked example of RFC 4226 §5.4 gives 872921. The Key Uri Format’s own examples are read.
The tests hold 10 checks of this tool; 3 rest on outside material:
- A standard’s own vectors RFC 4226, Appendix D: for counts 0 to 9 the HMAC-SHA-1, the truncated value in hexadecimal and decimal, and the HOTP code, all ten as printed
- A standard’s own vectors RFC 4226, §5.4: the last byte 5a gives the offset 10, the four bytes there 50ef7f19, and the code 872921
- A standard’s own vectors RFC 6238, Appendix B: at its six moments from 59 seconds to the year 2603 the value of T and the eight-digit TOTP by SHA-1, SHA-256 and SHA-512, all eighteen as printed (the secrets of 20, 32 and 64 bytes its reference code uses)
Limits
It works a code out; it checks none. A verifier’s window of steps and its handling of a drifting clock (RFC 6238 §5.2 and §6) are a server’s policy and are not modelled. The clock is this device’s. A code is as good as the secret is kept: one typed here is never saved, put in a share link or sent, but a secret that guards an account belongs in its app, not in a web page. Codes of other alphabets, such as Steam’s, are not made, no standard having been read for them.
- D. M’Raihi and others, HOTP: An HMAC-Based One-Time Password Algorithm, RFC 4226 (2005): §5.3 and the test values of Appendix D
- D. M’Raihi and others, TOTP: Time-Based One-Time Password Algorithm, RFC 6238 (2011): §4 and the test vectors of Appendix B
- Google Authenticator, Key Uri Format (the project’s wiki)