Internet-Draft Update to RFC 6238 (TOTP) December 2026
Rigopoulos Expires 9 June 2027 [Page]
Workgroup:
Network Working Group
Internet-Draft:
draft-rigopoulos-totp-implementation-guidance-00
Updates:
6238 (if approved)
Published:
Intended Status:
Informational
Expires:
Author:
E. A. Rigopoulos

Update to the Time-Based One-Time Password Algorithm (TOTP): One-Time Use of an OTP

Abstract

Section 5.2 of RFC 6238 requires that the verifier not accept the second attempt of an OTP after a successful validation, and leaves the method to the implementer. This document updates and extends RFC 6238 with a stored last accepted time step and a single atomic check-and-store step, so that each OTP is accepted at most once. It adds one recommendation on when an OTP is produced. Nothing else changes. Reference code and test results are included.

Status of This Memo

This Internet-Draft is submitted in full conformance with the provisions of BCP 78 and BCP 79.

Internet-Drafts are working documents of the Internet Engineering Task Force (IETF). Note that other groups may also distribute working documents as Internet-Drafts. The list of current Internet-Drafts is at https://datatracker.ietf.org/drafts/current/.

Internet-Drafts are draft documents valid for a maximum of six months and may be updated, replaced, or obsoleted by other documents at any time. It is inappropriate to use Internet-Drafts as reference material or to cite them other than as "work in progress."

This Internet-Draft will expire on 9 June 2027.

▲

Table of Contents

1. Introduction

Section 5.2 of [RFC6238] states that the verifier MUST NOT accept the second attempt of an OTP after the successful validation of the first, and leaves the method to the implementer. This document updates and extends [RFC6238] with that method. The transmission delay window, the time step size, and the OTP values do not change. An OTP inside the window is accepted at most once.

For each point, the following lists what [RFC6238] says, the piece it does not specify, and the answer given here.

RFC 6238 says       Not specified          This document
------------------  ---------------------  ----------------------
R1: know Unix time  How exact it must be   Accurate source (Sec. 3)
No second attempt   What is kept           Last accepted time step
                                           (Sec. 5.2)
Same                Simultaneous attempts  One atomic step (Sec. 5.2)
Same                Several instances      One durable store
                                           (Sec. 5.2)
Resynchronization   Use with the rule      Advance the time step
                                           (Sec. 6)
The OTP value       When it is produced    Only on request (Sec. 4)

2. Notation and Terminology

The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in BCP 14 [RFC2119] [RFC8174] when, and only when, they appear in all capitals, as shown here.

The terms of Section 2 of [RFC6238] apply. This document names the symbols of [RFC6238] once, here, and then uses words: the shared secret (K), the time step size (X), the Unix time to start counting time steps (T0), and the number of time steps (T) since that start time.

Last accepted time step:
The time step of the most recent OTP that the verifier has accepted for a prover. The verifier keeps one for each prover.

3. Algorithm Requirements

This section updates Section 3 of [RFC6238].

Requirement R1 says that the prover and the verifier MUST know or be able to derive the current Unix time. Unix time counts whole seconds since midnight UTC of January 1, 1970. It is the same everywhere, has no time zones, and gives an exact number of time steps. The verifier MUST obtain the current Unix time from an accurate time source, for example a network time source.

4. TOTP Algorithm

This section updates Section 4 of [RFC6238].

The algorithm, which uses HOTP [RFC4226], and the OTP values are unchanged. The prover and the verifier stay in step offline: each works out the same OTP from the shared secret and the current time step, with no contact between them. Section 6 of [RFC6238] covers clock drift.

An application that displays an OTP SHOULD produce it only when the user asks for it, and SHOULD make it no longer available once it has been used or has expired.

5. Security Considerations

5.1. General

No change to Section 5.1 of [RFC6238].

5.2. Validation and Time-Step Size

This document updates Section 5.2 of [RFC6238] as follows.

=== OLD ===
Note that a prover may send the same OTP inside a given time-step
window multiple times to a verifier.  The verifier MUST NOT accept
the second attempt of the OTP after the successful validation has
been issued for the first OTP, which ensures one-time only use of an
OTP.

=== NEW ===
Note that a prover may send the same OTP inside a given time-step
window multiple times to a verifier.  The verifier MUST NOT accept
the second attempt of the OTP after the successful validation has
been issued for the first OTP, which ensures one-time only use of an
OTP.

To meet this requirement, the verifier MUST keep, for each prover,
the time step of the last OTP it accepted (the "last accepted time
step"), and MUST handle each received OTP as follows:

1.  Accept the OTP only if its time step is inside the transmission
    delay window and later than the last accepted time step.  If no
    time step has been accepted yet, any time step inside the window
    qualifies.

2.  Check and store in one atomic step: a single operation of the
    store that reports whether the value changed (for example a
    compare-and-swap).  If another attempt has already stored that
    time step or a later one, reject the OTP.

3.  Reject the OTP if the store cannot be reached.

4.  Keep the stored value across restarts and failover, and use the
    same store in every verifier instance for that prover.

The transmission delay window and the recommended time-step size of
30 seconds are unchanged.
=== END ===

Rationale: [RFC6238] allows a window for transmission delay. With the last accepted time step, an OTP inside that window is accepted at most once, including when two verifier instances receive it at the same moment. A database update such as "UPDATE totp_state SET last_time_step = :time_step WHERE prover_id = :prover_id AND last_time_step < :time_step" is a single operation of the kind that rule 2 describes.

Compatibility: a prover built to [RFC6238] works unchanged with a verifier built to this document. Cost: one stored number for each prover, and one store operation for each accepted OTP.

An OTP that is produced but not sent leaves the stored time step unchanged (Appendix A, case 8). Only the verifier's acceptance confirms authentication, and beyond that single acceptance nothing about the OTP stays valid.

6. Resynchronization

This document updates Section 6 of [RFC6238] as follows.

=== ADD AT THE END OF SECTION 6 ===
Accepting an OTP found by resynchronization also advances the last
accepted time step.  The drift found by resynchronization does not
allow an OTP whose time step is not later than the last accepted time
step.
=== END ===

7. Implementation Status

[RFC Editor: please remove this section and the reference to RFC 7942 before publication.]

This section reports the status of known implementations of this update at the time of posting of this Internet-Draft, and is based on a proposal described in [RFC7942]. Listing an implementation here does not imply endorsement by the IETF, and other implementations may exist.

Implementation:
A Java reference verifier, the class TOTPVerifier in Appendix B, with a sample program and two test programs kept outside this document. Reference code (prototype), under the Revised BSD License.
Coverage:
The check and store of Section 5.2 in one verifier process, with the time step taken from Unix time. Not covered: a store shared by several verifier instances, restart and failover, an unreachable store, and resynchronization.
Last updated:
6 December 2026.

Test results, using SHA-1 and the secret and time steps of Appendix B of [RFC6238]:

8. IANA Considerations

This document has no IANA actions.

9. Normative References

[RFC2119]
Bradner, S., "Key words for use in RFCs to Indicate Requirement Levels", BCP 14, RFC 2119, DOI 10.17487/RFC2119, , <https://www.rfc-editor.org/info/rfc2119>.
[RFC4226]
M'Raihi, D., Bellare, M., Hoornaert, F., Naccache, D., and O. Ranen, "HOTP: An HMAC-Based One-Time Password Algorithm", RFC 4226, DOI 10.17487/RFC4226, , <https://www.rfc-editor.org/info/rfc4226>.
[RFC6238]
M'Raihi, D., Machani, S., Pei, M., and J. Rydell, "TOTP: Time-Based One-Time Password Algorithm", RFC 6238, DOI 10.17487/RFC6238, , <https://www.rfc-editor.org/info/rfc6238>.
[RFC8174]
Leiba, B., "Ambiguity of Uppercase vs Lowercase in RFC 2119 Key Words", BCP 14, RFC 8174, DOI 10.17487/RFC8174, , <https://www.rfc-editor.org/info/rfc8174>.

10. Informative References

[RFC7942]
Sheffer, Y. and A. Farrel, "Improving Awareness of Running Code: The Implementation Status Section", BCP 205, RFC 7942, DOI 10.17487/RFC7942, , <https://www.rfc-editor.org/info/rfc7942>.

Appendix A. Test Vectors

The values are from Appendix B of [RFC6238]: shared secret "12345678901234567890", SHA-1, 8 digits, time step size 30 seconds, start time 0. Unix time 1111111109 is time step 37037036 with OTP 07081804, and Unix time 1111111111 is time step 37037037 with OTP 14050471. The verifier clock reads 1111111111, and the window is the current time step and the one before it. Cases 1 to 3 use one prover, cases 4 to 6 a second, case 7 a third, and case 8 a fourth. "none" means no time step has been accepted yet. A conforming verifier gives these results.

Case  Before    OTP (time step)         Decision            After
1     none      14050471 (37037037)     ACCEPT              37037037
2     37037037  14050471 (37037037)     REJECT              37037037
3     37037037  07081804 (37037036)     REJECT              37037037
4     none      07081804 (37037036)     ACCEPT              37037036
5     37037036  14050471 (37037037)     ACCEPT              37037037
6     37037037  07081804 (37037036)     REJECT              37037037
7     none      14050471 x32 at once    1 ACCEPT, 31 REJECT 37037037
8     none      (none sent)             NO CHANGE           none
Time step size: 30 seconds
Verifier clock: Unix time 1111111111 = time step 37037037
Window: time steps 37037036 to 37037037

Appendix B. Reference Verifier (Java)

This class implements Section 5.2 and produced the results in Appendix A.

<CODE BEGINS> file "TOTPVerifier.java"

/**
 * Copyright (c) 2026 IETF Trust and the persons identified as
 * authors of the code. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, is permitted pursuant to, and subject
 * to the license terms contained in, the Revised BSD License
 * set forth in Section 4.e of the IETF Trust's Legal Provisions
 * Relating to IETF Documents
 * (https://trustee.ietf.org/license-info).
 */

import java.nio.ByteBuffer;
import java.security.GeneralSecurityException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

/**
 * TOTP verifier with the rule of Section 5.2 of this update.  The
 * store holds the last accepted time step for each prover (-1 if
 * none).
 */
public class TOTPVerifier {

    private final int digits;
    private final String crypto;     // HmacSHA1, HmacSHA256, ...
    final ConcurrentHashMap<String, AtomicLong> store =
        new ConcurrentHashMap<String, AtomicLong>();

    public TOTPVerifier(int digits, String crypto) {
        this.digits = digits;
        this.crypto = crypto;
    }

    /** The TOTP value for a shared secret and a time step. */
    public static String otp(byte[] sharedSecret, long timeStep,
            int digits, String crypto) {
        try {
            Mac hmac = Mac.getInstance(crypto);
            hmac.init(new SecretKeySpec(sharedSecret, "RAW"));
            byte[] hash = hmac.doFinal(
                ByteBuffer.allocate(8).putLong(timeStep).array());
            long binary = ByteBuffer.wrap(
                hash, hash[hash.length - 1] & 0xf, 4).getInt()
                    & 0x7fffffffL;
            return String.format("%0" + digits + "d",
                binary % (long) Math.pow(10, digits));
        } catch (GeneralSecurityException e) {
            throw new IllegalStateException(e);
        }
    }

    /**
     * Returns true (accept) or false (reject).  firstTimeStep to
     * lastTimeStep is the transmission delay window.
     */
    public boolean validate(String prover, byte[] sharedSecret,
            String otp, long firstTimeStep, long lastTimeStep) {
        AtomicLong last = store.computeIfAbsent(prover,
            x -> new AtomicLong(-1L));
        for (long step = Math.max(firstTimeStep, last.get() + 1);
                step <= lastTimeStep; step++) {
            if (otp(sharedSecret, step, digits, crypto)
                    .equals(otp)) {
                // Check and store in one atomic step.
                long prev = last.get();
                while (prev < step) {
                    if (last.compareAndSet(prev, step)) {
                        return true;
                    }
                    prev = last.get();
                }
                return false;
            }
        }
        return false;
    }
}

<CODE ENDS>

Acknowledgements

The author thanks the authors of RFC 6238 and RFC 4226, who did not write, check, or endorse this document.

Author's Address

E. A. Rigopoulos