slauth 0.7.21

oath HOTP and TOTP complient implementation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
use super::*;

pub const TOTP_DEFAULT_PERIOD_VALUE: u64 = 30;
pub const TOTP_DEFAULT_BACK_RESYNC_VALUE: u64 = 1;
pub const TOTP_DEFAULT_FORWARD_RESYNC_VALUE: u64 = 1;

#[derive(Default)]
pub struct TOTPBuilder {
    alg: Option<HashesAlgorithm>,
    period: Option<u64>,
    backward_resync: Option<u64>,
    forward_resync: Option<u64>,
    initial_time: Option<u64>,
    digits: Option<usize>,
    secret: Option<Vec<u8>>,
}

impl TOTPBuilder {
    pub fn new() -> Self {
        TOTPBuilder::default()
    }

    pub fn algorithm(mut self, alg: HashesAlgorithm) -> Self {
        self.alg = Some(alg);
        self
    }

    pub fn period(mut self, p: u64) -> Self {
        self.period = Some(p);
        self
    }

    pub fn re_sync_parameter(mut self, backward: u64, forward: u64) -> Self {
        self.backward_resync = Some(backward);
        self.forward_resync = Some(forward);
        self
    }

    pub fn digits(mut self, d: usize) -> Self {
        self.digits = Some(d);
        self
    }

    pub fn initial_time(mut self, t: u64) -> Self {
        self.initial_time = Some(t);
        self
    }

    pub fn secret(mut self, secret: &[u8]) -> Self {
        self.secret = Some(secret.to_vec());
        self
    }

    pub fn build(self) -> TOTPContext {
        let TOTPBuilder {
            alg,
            period,
            backward_resync,
            forward_resync,
            digits,
            secret,
            initial_time,
        } = self;

        let alg = alg.unwrap_or(OTP_DEFAULT_ALG_VALUE);
        let secret = secret.unwrap_or_default();
        let secret_key = alg.to_mac_hash_key(secret.as_slice());

        TOTPContext {
            alg,
            period: period.unwrap_or(TOTP_DEFAULT_PERIOD_VALUE),
            backward_resync: backward_resync.unwrap_or(TOTP_DEFAULT_BACK_RESYNC_VALUE),
            forward_resync: forward_resync.unwrap_or(TOTP_DEFAULT_FORWARD_RESYNC_VALUE),
            digits: digits.unwrap_or(OTP_DEFAULT_DIGITS_VALUE),
            secret,
            secret_key,
            initial_time: initial_time.unwrap_or(0),
            clock_drift: 0,
        }
    }
}

#[derive(Clone)]
pub struct TOTPContext {
    alg: HashesAlgorithm,
    period: u64,
    backward_resync: u64,
    forward_resync: u64,
    initial_time: u64,
    clock_drift: i64,
    digits: usize,
    secret: Vec<u8>,
    secret_key: MacHashKey,
}

impl TOTPContext {
    /// Create a new HOTP builder
    pub fn builder() -> TOTPBuilder {
        TOTPBuilder::new()
    }

    /// Generate the current TOTP code corresponding to the counter value
    pub fn gen(&self) -> String {
        self.gen_past(0)
    }

    /// Generate the current TOTP code corresponding to the counter value
    /// Note that elapsed represent the time between the actual time you want in to be generated for and now.
    /// E.g. if you want a code valid exactly 68 sec ago, elapsed value would be 68
    pub fn gen_past(&self, elapsed: u64) -> String {
        self.gen_time_diff(get_time() - elapsed)
    }

    /// Generate the current TOTP code corresponding to the counter value
    /// Note that elapsed represent the time between the actual time you want in to be generated for and now.
    /// E.g. if you want a code valid exactly 68 sec ago, elapsed value would be 68
    /// Deprecated: Use gen_past
    pub fn gen_with(&self, elapsed: u64) -> String {
        self.gen_past(elapsed)
    }

    /// Generate a TOTP code in the future
    pub fn gen_future(&self, seconds: u64) -> String {
        self.gen_time_diff(get_time() + seconds)
    }

    /// Generate a TOTP code in a given time
    fn gen_time_diff(&self, time: u64) -> String {
        let mut counter = (time - self.initial_time) / self.period;

        match self.clock_drift {
            d if d > 0 => counter += d.unsigned_abs(),
            d if d < 0 => counter -= d.unsigned_abs(),
            _ => {}
        }

        self.gen_at(counter)
    }

    /// Check if a code equal the current value at the counter
    pub fn validate_current(&self, value: &str) -> bool {
        if value.len() != self.digits {
            return false;
        }

        self.gen().as_str().eq(value)
    }

    /// Check if a code is valid, if yes icrements the counter, if not begins the resync procedure.
    /// The counter won't be altered if the value is invalidated.
    pub fn verify(&mut self, value: &str) -> bool {
        if value.len() != self.digits {
            return false;
        }

        let mut counter = (get_time() - self.initial_time) / self.period;

        match self.clock_drift {
            d if d > 0 => counter += d.unsigned_abs(),
            d if d < 0 => counter -= d.unsigned_abs(),
            _ => {}
        }

        for i in (counter - self.backward_resync)..(counter + self.forward_resync) {
            if self.gen_at(i).as_str().eq(value) {
                match i {
                    i if i > counter => {
                        let drift = (i - counter) as i64;
                        self.clock_drift += drift;
                    }
                    i if i < counter => {
                        let drift = (counter - i) as i64;
                        self.clock_drift -= drift;
                    }
                    _ => {}
                }
                return true;
            }
        }

        false
    }

    fn gen_at(&self, t: u64) -> String {
        let c_b_e = t.to_be_bytes();

        let hs_sig = self
            .secret_key
            .sign(&c_b_e[..])
            .expect("This should not happen since HMAC can take key of any size")
            .into_vec();
        let s_bits = dt(hs_sig.as_ref());

        let s_num = s_bits % 10_u32.pow(self.digits as u32);

        format!("{:0>6}", s_num)
    }
}

impl OtpAuth for TOTPContext {
    fn to_uri(&self, label: Option<&str>, issuer: Option<&str>) -> String {
        let mut uri = format!(
            "otpauth://totp/{}?secret={}&algorithm={}&digits={}&period={}",
            label.unwrap_or("slauth"),
            base32::encode(base32::Alphabet::Rfc4648 { padding: false }, self.secret.as_slice()),
            self.alg,
            self.digits,
            self.period
        );

        if let Some(iss) = issuer {
            uri.push_str("&issuer=");
            uri.push_str(iss);
        }

        uri
    }

    fn from_uri(uri: &str) -> Result<Self, String>
    where
        Self: Sized,
    {
        let mut uri_it = uri.split("://");

        uri_it
            .next()
            .filter(|scheme| scheme.eq(&"otpauth"))
            .ok_or_else(|| "Otpauth uri is malformed".to_string())?;

        let type_label_it_opt = uri_it.next().map(|type_label_param| type_label_param.split('/'));

        if let Some(mut type_label_it) = type_label_it_opt {
            type_label_it
                .next()
                .filter(|otp_type| otp_type.eq(&"totp"))
                .ok_or_else(|| "Otpauth uri is malformed, bad type".to_string())?;

            let param_it_opt = type_label_it
                .next()
                .and_then(|label_param| label_param.split('?').next_back().map(|s| s.split('&')));

            param_it_opt
                .ok_or_else(|| "Otpauth uri is malformed, missing parameters".to_string())
                .and_then(|param_it| {
                    let mut secret = Vec::<u8>::new();
                    let mut period = TOTP_DEFAULT_PERIOD_VALUE;
                    let mut alg = OTP_DEFAULT_ALG_VALUE;
                    let mut digits = OTP_DEFAULT_DIGITS_VALUE;

                    for s_param in param_it {
                        let mut s_param_it = s_param.split('=');

                        match s_param_it.next() {
                            Some("secret") => {
                                secret = s_param_it
                                    .next()
                                    .and_then(decode_hex_or_base_32)
                                    .ok_or_else(|| "Otpauth uri is malformed, missing secret value".to_string())?;
                                continue;
                            }
                            Some("algorithm") => {
                                alg = match s_param_it
                                    .next()
                                    .ok_or_else(|| "Otpauth uri is malformed, missing algorithm value".to_string())?
                                {
                                    "SHA256" => HashesAlgorithm::SHA256,
                                    "SHA512" => HashesAlgorithm::SHA512,
                                    _ => HashesAlgorithm::SHA1,
                                };
                                continue;
                            }
                            Some("digits") => {
                                digits = s_param_it
                                    .next()
                                    .ok_or_else(|| "Otpauth uri is malformed, missing digits value".to_string())?
                                    .parse::<usize>()
                                    .map_err(|_| "Otpauth uri is malformed, bad digits value".to_string())?;
                                continue;
                            }
                            Some("period") => {
                                period = s_param_it
                                    .next()
                                    .ok_or_else(|| "Otpauth uri is malformed, missing period value".to_string())?
                                    .parse::<u64>()
                                    .map_err(|_| "Otpauth uri is malformed, bad period value".to_string())?;
                                continue;
                            }
                            _ => {}
                        }
                    }

                    if secret.is_empty() {
                        return Err("Otpauth uri is malformed".to_string());
                    }

                    let secret_key = alg.to_mac_hash_key(secret.as_slice());

                    Ok(TOTPContext {
                        alg,
                        period,
                        digits,
                        secret,
                        secret_key,
                        backward_resync: TOTP_DEFAULT_BACK_RESYNC_VALUE,
                        forward_resync: TOTP_DEFAULT_FORWARD_RESYNC_VALUE,
                        initial_time: 0,
                        clock_drift: 0,
                    })
                })
        } else {
            Err("Otpauth uri is malformed, missing parts".to_string())
        }
    }
}

#[cfg(feature = "native-bindings")]
mod native_bindings {
    use crate::strings;
    use std::{
        os::raw::{c_char, c_ulong},
        ptr::null_mut,
    };

    use super::*;

    #[no_mangle]
    pub unsafe extern "C" fn totp_from_uri(uri: *const c_char) -> *mut TOTPContext {
        let uri_str = strings::c_char_to_string(uri);
        let totp = TOTPContext::from_uri(&uri_str).map(Box::new);

        match totp {
            Ok(totp) => Box::into_raw(totp),
            Err(_) => null_mut(),
        }
    }

    #[no_mangle]
    pub unsafe extern "C" fn totp_free(totp: *mut TOTPContext) {
        let _ = Box::from_raw(totp);
    }

    #[no_mangle]
    pub unsafe extern "C" fn totp_to_uri(totp: *mut TOTPContext, label: *const c_char, issuer: *const c_char) -> *mut c_char {
        let totp = &*totp;
        let label = strings::c_char_to_string(label);
        let label_opt = if !label.is_empty() { Some(label.as_str()) } else { None };
        let issuer = strings::c_char_to_string(issuer);
        let issuer_opt = if !issuer.is_empty() { Some(issuer.as_str()) } else { None };
        strings::string_to_c_char(totp.to_uri(label_opt, issuer_opt))
    }

    #[no_mangle]
    pub unsafe extern "C" fn totp_gen(totp: *mut TOTPContext) -> *mut c_char {
        let totp = &*totp;
        strings::string_to_c_char(totp.gen())
    }

    #[no_mangle]
    pub unsafe extern "C" fn totp_gen_with(totp: *mut TOTPContext, elapsed: c_ulong) -> *mut c_char {
        let totp = &*totp;

        // The conversion is useless on most platforms but it is needed for Windows
        #[allow(clippy::useless_conversion)]
        strings::string_to_c_char(totp.gen_with(elapsed.into()))
    }

    #[no_mangle]
    pub unsafe extern "C" fn totp_verify(totp: *mut TOTPContext, code: *const c_char) -> bool {
        let totp = &mut *totp;
        let value = strings::c_char_to_string(code);
        totp.verify(&value)
    }

    #[no_mangle]
    pub unsafe extern "C" fn totp_validate_current(totp: *mut TOTPContext, code: *const c_char) -> bool {
        let totp = &*totp;
        let value = strings::c_char_to_string(code);
        totp.validate_current(&value)
    }
}

#[test]
fn test_multiple() {
    const MK_ULTRA: &str = "patate";

    let mut server = TOTPContext::builder().period(5).secret(MK_ULTRA.as_bytes()).build();

    let client = TOTPContext::from_uri(server.to_uri(None, None).as_str()).unwrap();

    for _ in 0..10 {
        use std::{thread::sleep, time::Duration};
        assert!(server.verify(&client.gen()));
        sleep(Duration::from_secs(5));
    }
}

#[test]
fn test_clock_drifting() {
    const MK_ULTRA: &str = "patate";

    let mut server = TOTPContext::builder()
        .period(5)
        .secret(MK_ULTRA.as_bytes())
        .re_sync_parameter(3, 3)
        .build();

    let client = TOTPContext::from_uri(server.to_uri(None, None).as_str()).unwrap();

    for _ in 0..10 {
        let client_code = client.gen();
        use std::{thread::sleep, time::Duration};
        sleep(Duration::from_secs(6));
        assert!(server.verify(&client_code));
    }
}

#[test]
fn test_gen_past() {
    let totp = TOTPContext::from_uri("otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Example").unwrap();

    let code1 = totp.gen();

    std::thread::sleep(std::time::Duration::from_secs(31));

    let code2 = totp.gen_past(31);

    assert_eq!(code1, code2);
}

#[test]
fn test_gen_future() {
    let totp = TOTPContext::from_uri("otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Example").unwrap();

    let code1 = totp.gen_future(31);

    std::thread::sleep(std::time::Duration::from_secs(31));

    let code2 = totp.gen();

    assert_eq!(code1, code2);
}