One-time passwords
Generate and check HOTP and TOTP one-time passwords.
Select the HMAC hash, then call generate to compute a code, verify to
check one, or match to find the counter or time step it belongs to. Codes
are strings of decimal digits, so leading zeros are kept. verify returns
bool. match returns the matching counter or time step, or None if no
value matches; test it with is not None, because a match at counter 0 is
0. Generate secrets with oxcrypto.random.
from oxcrypto.otp.totp import TOTP
from oxcrypto.random import bytes as random_bytes
secret = random_bytes(20)
code = TOTP.SHA1.generate(secret)
assert TOTP.SHA1.verify(secret, code)Algorithms
Implementation and limitations
| Standards | RFC 4226 (HOTP) and RFC 6238 (TOTP), both Informational IETF RFCs |
| Crate | totp-rs 6.0.0, MIT, stable release line, no published audit |
| MSRV | totp-rs declares Rust 1.88, the oxcrypto MSRV |
| Experimental | No |
totp-rs implements only TOTP. RFC 6238 defines TOTP as HOTP over the
counter floor(time / period), so oxcrypto computes HOTP with totp-rs at a
1-second period and time = counter.
Secrets must be at least 16 bytes (128 bits) and codes 6 to 8 digits; other
values raise ValueError.
Important
Neither oxcrypto nor totp-rs records which counter or time step has been
accepted, so verify alone accepts the same code again. To reject a reused
code, as RFC 4226 section 7.2 and RFC 6238 section 5.2 require, call match,
store the returned value and accept only larger values next time.
totp-rs compares codes with constant_time_eq; when verify or match searches a
window or skew, it stops at the first match, so the time taken depends on the
position of the match. oxcrypto has not verified these timing properties
independently.
Zeroization
oxcrypto keeps no OTP object between calls. Each call copies the secret into a
totp-rs Secret and builds an HMAC from it. totp-rs is written to wipe the
Secret on drop, and the sha1 and sha2 crates are written to wipe the
inner and outer HMAC digest states and the block buffer on drop; oxcrypto
enables their zeroize features itself, because the totp-rs zeroize
feature does not. oxcrypto wipes the buffer that holds the code before it is
copied into a Python str. oxcrypto has not verified the upstream zeroization
independently.
Note
Zeroization does not cover:
- Secrets and codes passed in or returned as Python
bytesorstr. Python owns these objects, does not wipe them, and may copy them. - Short-lived stack copies during a call: the padded key block
(
key XOR ipad/opad) inhmac0.13.0 and the full HMAC output thattotp-rs6.0.0 truncates to the code.
Treat zeroization as defense in depth. It does not guarantee that secrets are gone from process memory.