Get a Pentest and security assessment of your IT network.

Cyber Security

TOTP Decoding: Is it Possible?

TL;DR

No, a standard Time-based One-time Password (TOTP) key cannot be reliably decoded from generated values. TOTPs are designed to be one-way functions – easy to generate given the secret and time, but extremely difficult to reverse engineer back to the secret key.

Understanding TOTP

TOTP is a common two-factor authentication (2FA) method. It works like this:

  • A secret key is shared between your authenticator app and the service you’re protecting.
  • The current time (usually in 30-second intervals) is used as input.
  • An algorithm (typically HMAC-SHA1 or HMAC-SHA256) combines the secret key and time to generate a unique code.

Because of the one-way nature of the hashing algorithms, knowing several TOTP values doesn’t let you figure out the original secret key.

Why Decoding is So Difficult

  1. Hashing Algorithms: The core of TOTP relies on cryptographic hash functions. These are designed to be incredibly difficult to reverse. Even a small change in the input (the time or secret key) results in a drastically different output code.
  2. Time Sensitivity: TOTPs change frequently (usually every 30 seconds). This means even if you could somehow partially decode a value, it would quickly become invalid.
  3. Large Key Space: Secret keys are typically long and random. The number of possible secret keys is enormous, making brute-force attacks impractical.

What Attacks *Are* Possible (and How to Prevent Them)

While decoding a TOTP key from generated values isn’t feasible, other attacks are possible:

  1. Phishing: Attackers might try to trick you into revealing your secret key through fake websites or emails. Always verify the legitimacy of any website asking for your 2FA code.
  2. Man-in-the-Middle Attacks: An attacker could intercept the communication between your authenticator app and the service, potentially stealing your TOTP values in real-time. Use HTTPS (SSL/TLS) to encrypt your connection.
  3. Malware: Malware on your device could steal your secret key directly from your authenticator app’s storage. Keep your devices secure with anti-virus software and regular updates.
  4. Brute Force (Limited): If the service doesn’t rate limit TOTP attempts, an attacker *could* try guessing codes rapidly. This is unlikely to succeed quickly due to the short time windows and large key space but can be effective against poorly implemented systems. Services should implement rate limiting on TOTP verification attempts.

Example of TOTP Generation (for understanding, not decoding)

This example uses Python with the pyotp library to generate a TOTP code. It demonstrates how the secret and time are used, but doesn’t show how to reverse it.

import pyotp

secret = "YOUR_SECRET_KEY"
totp = pyotp.TOTP(secret)
code = totp.now()
print(f"Current TOTP code: {code}")

In Summary

Don’t waste your time trying to decode a TOTP key from generated values. Focus on protecting your secret key and using secure practices to prevent other attacks. If you suspect your secret key has been compromised, revoke it immediately and generate a new one.

Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation