fastauth_plugin_two_factor/
totp.rs1use fastauth_core::crypto::ct_eq;
4use hmac::{Hmac, KeyInit, Mac};
5use sha1::Sha1;
6
7const BASE32: &[u8; 32] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
8
9pub fn hotp(secret: &[u8], counter: u64, digits: u32) -> String {
11 let mut mac =
12 <Hmac<Sha1> as KeyInit>::new_from_slice(secret).expect("HMAC takes any key length");
13 mac.update(&counter.to_be_bytes());
14 let digest = mac.finalize().into_bytes();
15 let offset = usize::from(digest[digest.len() - 1] & 0x0f);
16 let truncated = u32::from_be_bytes([
17 digest[offset] & 0x7f,
18 digest[offset + 1],
19 digest[offset + 2],
20 digest[offset + 3],
21 ]);
22 format!(
23 "{:0width$}",
24 truncated % 10u32.pow(digits),
25 width = digits as usize
26 )
27}
28
29pub fn totp(secret: &[u8], unix_time: u64, period: u64, digits: u32) -> String {
31 hotp(secret, unix_time / period, digits)
32}
33
34pub(crate) fn matching_step(secret: &[u8], code: &str, now_step: u64, digits: u32) -> Option<u64> {
36 let mut found = None;
37 for step in now_step.saturating_sub(1)..=now_step + 1 {
38 if ct_eq(hotp(secret, step, digits).as_bytes(), code.as_bytes()) {
39 found = Some(step);
40 }
41 }
42 found
43}
44
45pub fn base32(bytes: &[u8]) -> String {
47 let mut out = String::with_capacity(bytes.len().div_ceil(5) * 8);
48 for chunk in bytes.chunks(5) {
49 let mut block = [0u8; 8];
50 block[3..3 + chunk.len()].copy_from_slice(chunk);
51 let bits = u64::from_be_bytes(block);
52 for i in 0..(chunk.len() * 8).div_ceil(5) {
53 out.push(char::from(BASE32[((bits >> (35 - i * 5)) & 31) as usize]));
54 }
55 }
56 out
57}
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62
63 #[test]
64 fn matches_rfc_6238_sha1_vectors() {
65 let secret = b"12345678901234567890";
66 for (time, code) in [
67 (59, "94287082"),
68 (1_111_111_109, "07081804"),
69 (1_111_111_111, "14050471"),
70 (1_234_567_890, "89005924"),
71 (2_000_000_000, "69279037"),
72 (20_000_000_000, "65353130"),
73 ] {
74 assert_eq!(totp(secret, time, 30, 8), code, "T = {time}");
75 }
76 }
77
78 #[test]
79 fn matches_rfc_4226_vectors() {
80 let codes = [
81 "755224", "287082", "359152", "969429", "338314", "254676", "287922", "162583",
82 "399871", "520489",
83 ];
84 for (counter, code) in codes.into_iter().enumerate() {
85 assert_eq!(hotp(b"12345678901234567890", counter as u64, 6), code);
86 }
87 }
88
89 #[test]
90 fn accepts_one_step_either_side() {
91 let secret = b"12345678901234567890";
92 let code = hotp(secret, 100, 6);
93 assert_eq!(matching_step(secret, &code, 99, 6), Some(100));
94 assert_eq!(matching_step(secret, &code, 100, 6), Some(100));
95 assert_eq!(matching_step(secret, &code, 101, 6), Some(100));
96 assert_eq!(matching_step(secret, &code, 102, 6), None);
97 assert_eq!(matching_step(secret, &code[..5], 100, 6), None);
98 }
99
100 #[test]
101 fn base32_matches_rfc_4648_vectors() {
102 for (input, encoded) in [
103 ("", ""),
104 ("f", "MY"),
105 ("fo", "MZXQ"),
106 ("foo", "MZXW6"),
107 ("foob", "MZXW6YQ"),
108 ("fooba", "MZXW6YTB"),
109 ("foobar", "MZXW6YTBOI"),
110 ] {
111 assert_eq!(base32(input.as_bytes()), encoded);
112 }
113 }
114}