mfa_cli/
totp.rs

1use super::hotp;
2use byteorder::{BigEndian, WriteBytesExt};
3use std::time::SystemTime;
4
5const TIME_STEP: u64 = 30;
6const TOTP_DIGITS: u8 = 6;
7
8// TOTP を現在時刻から計算する
9pub fn totp(secret: &[u8]) -> Result<String, String> {
10    match current_time() {
11        Ok(current_time) => gen_totp(secret, current_time, TOTP_DIGITS),
12        Err(err) => Err(err),
13    }
14}
15
16// TOTP を任意の時刻で計算する
17fn gen_totp(secret: &[u8], time: u64, digits: u8) -> Result<String, String> {
18    let t = time / TIME_STEP;
19
20    let mut byte_t = Vec::new();
21    byte_t.write_u64::<BigEndian>(t).unwrap();
22
23    hotp::hotp(secret, &byte_t, digits)
24}
25
26// UNIX time からの経過秒数を返す
27fn current_time() -> Result<u64, String> {
28    match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
29        Ok(n) => Ok(n.as_secs()),
30        Err(_) => Err(String::from("SystemTime before UNIX EPOCH!")),
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37
38    // from RFC6238
39    // +---+-------------+--------------+------------------+----------+--------+
40    // | # |  Time (sec) |   UTC Time   | Value of T (hex) |   TOTP   |  Mode  |
41    // |---+-------------+--------------+------------------+----------+--------+
42    // | 1 |      59     |  1970-01-01  | 0000000000000001 | 94287082 |  SHA1  |
43    // |   |             |   00:00:59   |                  |          |        |
44    // | 2 |  1111111109 |  2005-03-18  | 00000000023523EC | 07081804 |  SHA1  |
45    // |   |             |   01:58:29   |                  |          |        |
46    // | 3 |  1111111111 |  2005-03-18  | 00000000023523ED | 14050471 |  SHA1  |
47    // |   |             |   01:58:31   |                  |          |        |
48    // | 4 |  1234567890 |  2009-02-13  | 000000000273EF07 | 89005924 |  SHA1  |
49    // |   |             |   23:31:30   |                  |          |        |
50    // | 5 |  2000000000 |  2033-05-18  | 0000000003F940AA | 69279037 |  SHA1  |
51    // |   |             |   03:33:20   |                  |          |        |
52    // | 6 | 20000000000 |  2603-10-11  | 0000000027BC86AA | 65353130 |  SHA1  |
53    // |   |             |   11:33:20   |                  |          |        |
54    // +---+-------------+--------------+------------------+----------+--------+
55
56    #[test]
57    fn rfc_6238_1() {
58        let totp = gen_totp(b"12345678901234567890", 59, 8).unwrap();
59        assert_eq!(totp, "94287082");
60    }
61
62    #[test]
63    fn rfc_6238_2() {
64        let totp = gen_totp(b"12345678901234567890", 1_111_111_109, 8).unwrap();
65        assert_eq!(totp, "07081804");
66    }
67
68    #[test]
69    fn rfc_6238_3() {
70        let totp = gen_totp(b"12345678901234567890", 1_111_111_111, 8).unwrap();
71        assert_eq!(totp, "14050471");
72    }
73
74    #[test]
75    fn rfc_6238_4() {
76        let totp = gen_totp(b"12345678901234567890", 1_234_567_890, 8).unwrap();
77        assert_eq!(totp, "89005924");
78    }
79
80    #[test]
81    fn rfc_6238_5() {
82        let totp = gen_totp(b"12345678901234567890", 2_000_000_000, 8).unwrap();
83        assert_eq!(totp, "69279037");
84    }
85
86    #[test]
87    fn rfc_6238_6() {
88        let totp = gen_totp(b"12345678901234567890", 20_000_000_000, 8).unwrap();
89        assert_eq!(totp, "65353130");
90    }
91}