ArticleslgStudy

computer science

HMAC-based one-time password

HMAC-based one-time password is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand HMAC-based one-time password rather than just read about it. In short: HMAC-based one-time password (HOTP) is a one-time password (OTP) algorithm based on the hash-based message authentication code (HMAC) cryptographic algorithm and is a basis for the commonly used Time-based one-time password (TOTP) algorithm which extends upon it. The HOTP algorithm is a freely available open standard and often forms part of multi-factor authentication protocols such as the Open Authentication initia…

HMAC-based one-time password — main illustration
HMAC-based one-time password — illustration

Key takeaways

  • HMAC-based one-time password belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect HMAC-based one-time password to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of HMAC-based one-time password from memory before moving on to harder problems.

Reference excerpt

HMAC-based one-time password (HOTP) is a one-time password (OTP) algorithm based on the hash-based message authentication code (HMAC) cryptographic algorithm and is a basis for the commonly used Time-based one-time password (TOTP) algorithm which extends upon it. The HOTP algorithm is a freely available open standard and often forms part of multi-factor authentication protocols such as the Open Authentication initiative (OATH) challenge-response algorithm.. At setup time a client and server share a secret and other initialization settings. In future exchanges when a client attempts to access a server a challenge is sent from the server to the client and the client then uses the previously exchanged secret and computes a value (a one time password) to be used as the server's challenge response. HOTP was published as an informational IETF RFC 4226 in December 2005, documenting the algorithm along with a Java implementation. Since then, the algorithm has been adopted by many companies worldwide (see below).

Algorithm The HOTP algorithm provides a method of authentication by symmetric generation of human-readable passwords, or values, each used for only one authentication attempt. The one-time property follows directly from the single use of each counter value. Parties intending to use HOTP must establish some initial parameters, which are typically specified by the authenticator, and either accepted or not by the authenticated entity:

A cryptographic hash method H (default is SHA-1) A secret key K, which is an arbitrary byte string and must remain private A counter C, which is 8 bytes long and counts the number of iterations A HOTP value length d (6–10, default is 6, and 6–8 is recommended) Both parties compute the HOTP value derived from the secret key K and the counter C. Then the authenticator checks its locally generated value against the value supplied by the authenticated. The authenticator and the authenticated entity increment the counter C independently. Since the authenticated entity may increment the counter more than the authenticator, RFC 4226 recommends a resynchronization protocol. It proposes that the authenticator repeatedly try verification ahead of their counter through a window of size s. The authenticator's counter continues forward of the value at which verification succeeds, and requires no actions by the authenticated entity. To protect against brute-force attacks targeting the small size of HOTP values, the RFC also recommends implementing persistent throttling of HOTP verification. This can be achieved by either locking out verification after a small number of failed attempts, or by linearly increasing the delay after each failed attempt. 6-digit codes are commonly provided by proprietary hardware tokens from a number of vendors informing the default value of d. Truncation extracts 31 bits or log 10 ⁡ ( 2 31 ) ≈ 9.3 {\textstyle \log _{10}(2^{31})\approx 9.3} decimal digits, meaning that d can be at most 10, with the 10th digit adding less variation, taking values of 0, 1, and 2 (i.e., 0.3 digits). After verification, the authenticator can authenticate itself simply by generating the next HOTP value, returning it, and then the authenticated can generate their own HOTP value to verify it. Note that counters are guaranteed to be synchronised at this point in the process. The HOTP value is the human-readable design output, a d-digit decimal number (without omission of leading 0s):

HOTP value = HOTP(K, C) mod 10d. That is, the value is the d least significant base-10 digits of HOTP. HOTP is a truncation of the HMAC of the counter C (under the key K and hash function H):

HOTP(K, C) = truncate(HMACH(K, C)), where the counter C must be used big-endian. Truncation first takes the 4 least significant bits of the MAC and uses them as a byte offset i:

truncate(MAC) = extract31(MAC, MAC[(19 × 8 + 4):(19 × 8 + 7)]), where ":" is used to extract bits from a starting bit number up to and including an ending bit number, where these bit numbers are 0-origin. The use of "19" in the above formula relates to the size of the output from the hash function. With the default of SHA-1, the output is 20 bytes, and so the last byte is byte 19 (0-origin). That index i is used to select 31 bits from MAC, starting at bit i × 8 + 1:

extract31(MAC, i) = MAC[(i × 8 + 1):(i × 8 + 4 × 8 − 1)]. 31 bits are a single bit short of a 4-byte word. Thus the value can be placed inside such a word without using the sign bit (the most significant bit). This is done to definitely avoid doing modular arithmetic on negative numbers, as this has many differing definitions and implementations.

Implementation The following Python code implements the HMAC-SHA1 and HOTP algorithms.

otpauth:// URI scheme

Some implementations of HOTP and TOTP for smartphones allow users to scan QR codes to add HOTP and TOTP tokens to their authenticator apps. These QR codes contain Uniform Resource Identifiers (URIs) with the scheme otpauth://. HOTP otpauth:// URIs begin with otpauth://hotp/ and must contain a label, secret, and counter. The label is encoded as part of the path, while the secret and counter are encoded as query parameters. The URI may optionally contain other fields, such as the number of digits (which defaults to 6), the algorithm used (which defaults to SHA1), and the issuer name. The secret is encoded as RFC 4648 Base32, with padding omitted. For example, the URI otpauth://hotp/Wikipedian?secret=JBSWY3DPFQQHO33SNRSCC&counter=42 represents a HOTP token labeled "Wikipedian", with the secret Hello, world! encoded as ASCII, and the initial counter 42. When added to an authenticator, it should produce the code 439256.

Tokens Both hardware and software tokens are available from various vendors, for some of them see references below. Software tokens are available for (nearly) all major mobile/smartphone platforms (J2ME, Android, iPhone, BlackBerry, Maemo, macOS, and Windows Mobile).

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with HMAC-based one-time password

Start with the simplest possible case. Write down what HMAC-based one-time password claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to HMAC-based one-time password before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about HMAC-based one-time password ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of HMAC-based one-time password

In research
HMAC-based one-time password appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses HMAC-based one-time password in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
HMAC-based one-time password is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer access control protocols, Cryptographic algorithms, Internet protocols, so understanding it makes those chapters shorter.
In everyday life
Look for HMAC-based one-time password outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “HMAC-based one-time password” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study HMAC-based one-time password in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what HMAC-based one-time password means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain HMAC-based one-time password out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is HMAC-based one-time password in simple terms?

HMAC-based one-time password (HOTP) is a one-time password (OTP) algorithm based on the hash-based message authentication code (HMAC) cryptographic algorithm and is a basis for the commonly used Time-based one-time password (TOTP) algorithm which extends upon it. The HOTP algorithm is a freely avai…

Why does HMAC-based one-time password matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study HMAC-based one-time password?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on HMAC-based one-time password.

Tags

  • Computer access control protocols
  • Cryptographic algorithms
  • Internet protocols

Keep exploring