YubiKey OTP Verification Library
Offline verification primitives for YubiKey OTPs.
This crate provides:
- modhex parsing and formatting
- OTP parsing and AES-128 decryption
- replay-detection validation helpers
- a small file-backed example store behind an optional feature
Feature flags
Default features:
decryptionmodhex
Optional features:
simple_store— enables a small example file-backed store implementationdecryptionimplies the optionalaesdependencysimple_storedepends on bothdecryptionandmodhex
Modhex basics
YubiKey OTPs are encoded with modhex, a keyboard-layout-friendly alphabet.
The mapping is:
| Hex | 0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
a |
b |
c |
d |
e |
f |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Modhex | c |
b |
d |
e |
f |
g |
h |
i |
j |
k |
l |
n |
r |
t |
u |
v |
Examples
Encode and decode modhex
use ModHex;
let raw = ;
let encoded = from.to_string;
assert_eq!;
let decoded = try_from.unwrap;
assert_eq!;
assert!;
assert!;
Parse and decrypt a YubiKey OTP
use Otp;
let otp = from_modhex.unwrap;
let decrypted = otp.decrypt.unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Validate a decrypted OTP against a previous one
The crate exposes validation helpers for replay detection and monotonic counter checks.
use ;
let otp = from_modhex.unwrap;
let decrypted = otp.decrypt.unwrap;
let previous = DecryptedOtp ;
assert!;
Notes on validation behavior
otp_offline::otp validates decrypted OTPs using the embedded counters and identifiers:
- public IDs must match
- private IDs must match
- usage counters must not decrease
- if the usage counter is unchanged, the session counter must increase
- usage counter
0x7fffis treated as exhausted and rejected
CRC validation happens during decryption of the private 16-byte OTP payload.
License
MIT