Under Construction
Security••10 min•73 views••

Google Authenticator: why does it still produce the correct code without a network?

Minh Khoa

Minh Khoa

Author

image.png
The phone is in airplane mode, with no Internet, yet Google Authenticator still shows a new six-digit code. You enter it on the login page on the computer, and the system still accepts it.

Where does that code come from, when the phone receives no messages or notifications?

Google Authenticator can generate codes directly on the device. Google also confirms that the app works without Internet or mobile service. The login page on the computer still needs a connection to the website; the phone generating the code does not. Google — Receive verification codes through Authenticator

To understand why the two sides match, let’s go back to the first time you scanned the QR code.

1. The QR scan gave the phone a secret

When you enable app-based authentication, the service provides a secret key — secret key for that setup. The key is kept on the authentication system side and put into your app.

The QR code helps transfer this information without typing a long string. Inside it is usually data in the otpauth://format, containing the secret key together with account information and code-generation settings. Google Authenticator — Key URI Format

After setup, both sides already have the same ingredients:

  • The phone keeps the key to generate codes.
  • The authentication system keeps the corresponding key to verify the codes.

You can imagine two people receiving the same recipe and one secret ingredient. When it is time to compute the result, each person does it independently on their own side.

So the initial QR scan is what makes the later code generations possible independently.

2. The clock determines the code for the current moment

For time-based changing codes, the mechanism commonly used is TOTP — Time-based One-Time Password.

TOTP divides time into intervals, commonly 30 seconds. With this configuration, the index of the current interval is calculated as:

T = floor(UnixTime / 30)

Where UnixTime is the number of seconds since the Unix time epoch; floor is the floor function. Both sides use the same key, the same algorithm, and the same time interval, so they compute the same code. RFC 6238 — TOTP

The six-digit code generation process can be understood as follows:

  1. Determine the current time interval.
  2. Pass that value through HMAC, a cryptographic computation that uses the secret key.
  3. Reduce the result according to the algorithm’s rules to obtain the code, for example six digits.

HMAC is the part that means even someone who knows the current time still cannot easily infer the code without the key. This is not a matter of adding the key to the time and taking the last few digits. RFC 4226 — HOTP algorithm

The phone already has the key and the clock still runs without a network, so it has enough data to perform the calculation.

The countdown does not start when you open the app either. If you open it near the end of a time interval, the code may have only a few seconds left before it switches to the next interval. RFC 6238

3. The server knows the correct code by recalculating it

Suppose you are logging in to a website and the website requests an Authenticator code.

You open the phone, read the code, and enter it into the browser. The website sends that code to the authentication system. On the server side, it takes the key associated with the account, recalculates the code to compare against, and checks the result you submitted. NIST — OTP verifiers

PhoneAuthentication system
Has the account’s secret keyHas the corresponding key
Reads the device timeReads the server time
Calculates the code to displayCalculates the code to compare

Here, Google Authenticator acts as the code-generating app. Verification belongs to the service you are logging in to, not by default to Google just because the app has Google in its name.

The coordination was already prepared at setup time: both sides have the same key and the same method of calculation.

4. Why are you sometimes told the code is wrong even when you enter the right six digits?

One reason is the phone clock is out of sync. If the two sides are calculating based on different time intervals, the results may not match.

The server may accept a few neighboring intervals to compensate for latency and clock drift, but this limit depends on the system. So you should not understand this as meaning every code is accepted for exactly 30 seconds, or that there is always an extra grace interval. RFC 6238 — Validation and clock drift

When the code does not work, check the correct account, enter it before the code expires, and check the device’s date and time. Google Authenticator currently uses the operating system time. Google — Fix incorrect codes

As for Google Authenticator account sync, that is a separate matter. It helps transfer Authenticator data to other devices; the code currently displayed can still be generated on the device while offline. The app also allows use without signing in to a Google Account. Google — Sync Authenticator

5. The thing that must be kept secret is the code-generating key

The six digits on the screen are only a temporary result. The secret key is the real ingredient that can be used to generate the next codes.

Because the setup QR contains this key, anyone with a copy of the QR can put the same key into another app and generate the corresponding codes. The setup QR screenshot needs to be protected like authentication information, and should not appear in full in tutorials or publicly shared screenshots. Google — Setup QR structure

Six digits allow only one million combinations. Security therefore also depends on the server side: limiting the number of failed attempts, limiting the accepted time window, and refusing to reuse a code that has already been successfully verified while it is still valid. An app changing codes by the hour does not by itself guarantee “single-use” behavior. NIST — OTP verification requirements

TOTP also does not automatically protect against phishing: a fake site can take the code you just entered and immediately forward it to the real service. The ability to generate codes offline solves the phone’s dependence on connectivity; it does not eliminate all forms of code theft. NIST — Phishing resistance

The next time you turn on airplane mode and the code still changes, remember two things already inside the phone: the secret key and the clock. They are enough for the phone to calculate the result that the authentication system is also expecting.