Reinventing TOTP: A Journey from First Principles

456789 123456 789012 TIME • SECURITY • AUTHENTICATION

Table of Contents

  1. The Problem We're Trying to Solve
  2. The One-Time Password (OTP)
  3. Exploring Solutions: Generating Random Passwords Independently
  4. Time as the Variable (TOTP)
  5. Handling Clock Skew and Time Steps
  6. The Complete Algorithm
  7. Why This Design Works
  8. Downsides and Implementation Considerations
  9. Security Considerations
  10. The Evolution
  11. Conclusion

The Problem We're Trying to Solve

Imagine you're building a system where users need to authenticate themselves. The traditional approach is simple: username and password. But passwords have several fundamental problems:

Once an attacker has your password, they can impersonate you indefinitely—until you discover the breach and change it (if you even know it was compromised).

The core problem: How do we create a credential that's valid only once, or only for a short period of time?

This is the foundation of multi-factor authentication (MFA). Even if someone steals your password, they can't use it without also having access to your second factor—something that changes frequently and can't be easily reused.

So you start to think about coming up with a solution. You soon realize the system must have the following properties at the minimum:

  1. Should be random: Each password must appear random and unpredictable to an observer. Even if someone sees one password, they shouldn't be able to predict the next one.
  1. Cannot be guessed or brute forced: The password space must be large enough that an attacker can't feasibly try all possibilities within the valid time window. Additionally, the generation algorithm must be cryptographically secure.
  1. Should be one-time use only: Once a password is used, it must become invalid. This prevents replay attacks where an attacker intercepts and reuses a password.
  1. Inexpensive and ideally must work offline: The solution shouldn't require expensive infrastructure (like SMS gateways) or constant network connectivity. Users should be able to authenticate even when they're offline or in areas with poor connectivity.

The One-Time Password (OTP)

Let's start with the simplest solution: one-time passwords.

The idea is straightforward:

  1. Generate a random password
  2. Send it to the user (via SMS, email, etc.)
  3. The user enters it to authenticate
  4. Once used, it's invalid forever

While this works, you soon realize this has several problems: