Menu

RFC 6238 by Hand: One TOTP Code, 18 Vectors, and the 32-Byte Key Trap
📰

RFC 6238 by Hand: One TOTP Code, 18 Vectors, and the 32-Byte Key Trap

DEV Community: authentication·WGG·12 days ago
#DlsF60sB
#devtoauthentication#devto#dev#time#server
Reading 0:00
15s threshold

Most TOTP bugs are key-length, clock, and replay bugs, not crypto bugs. The algorithm is short enough to work through on paper, and doing it once makes the failure modes easier to see.

Here is the full path from a shared secret and Unix time 59 to 94287082 , using the RFC's test key, then the parts the RFC leaves to your server.

Three inputs: T0, X, and a counter

RFC 6238 defines the time step as T = floor((Unix time - T0) / X) . T0 defaults to 0, X to 30 seconds. At Unix time 59: T = floor((59 - 0) / 30) = 1 .

That counter goes into HMAC as an 8-byte big-endian integer: 00 00 00 00 00 00 00 01 . Keep the width at 8 bytes even when T is small: RFC 6238 §4.2 requires support for T beyond 32 bits past 2038, and a variable-width counter breaks there.

HOTP first, TOTP second

TOTP is HOTP (RFC 4226) with a different counter source. HOTP computes HMAC-SHA-1(key, counter) and truncates. TOTP takes the counter from the clock instead of incrementing it per code.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.