totp-rs/src/lib.rs

289 lines
10 KiB
Rust
Raw Normal View History

//! This library permits the creation of 2FA authentification tokens per TOTP, the verification of said tokens, with configurable time skew, validity time of each token, algorithm and number of digits! Default features are kept as low-dependency as possible to ensure small binaries and short compilation time
2020-04-13 16:57:06 -04:00
//!
2022-02-10 04:00:33 -05:00
//! Be aware that some authenticator apps will accept the `SHA256`
//! and `SHA512` algorithms but silently fallback to `SHA1` which will
//! make the `check()` function fail due to mismatched algorithms.
//!
//! Use the `SHA1` algorithm to avoid this problem.
//!
2020-04-13 16:57:06 -04:00
//! # Examples
//!
2020-06-21 13:08:52 -04:00
//! ```rust
2020-04-13 16:57:06 -04:00
//! use std::time::SystemTime;
2020-06-21 09:57:16 -04:00
//! use totp_rs::{Algorithm, TOTP};
//!
2020-06-21 09:57:16 -04:00
//! let totp = TOTP::new(
//! Algorithm::SHA1,
//! 6,
//! 1,
//! 30,
//! "supersecret",
2020-06-21 09:57:16 -04:00
//! );
//! let time = SystemTime::now()
//! .duration_since(SystemTime::UNIX_EPOCH).unwrap()
//! .as_secs();
//! let url = totp.get_url("user@example.com", "my-org.com");
2020-04-13 16:57:06 -04:00
//! println!("{}", url);
//! let token = totp.generate(time);
//! println!("{}", token);
//! ```
2020-06-21 09:57:16 -04:00
//!
2020-06-21 13:08:52 -04:00
//! ```rust
2022-01-20 11:58:27 -05:00
//! # #[cfg(feature = "qr")] {
2020-06-21 09:57:16 -04:00
//! use totp_rs::{Algorithm, TOTP};
//!
//! let totp = TOTP::new(
//! Algorithm::SHA1,
//! 6,
//! 1,
//! 30,
//! "supersecret",
2020-06-21 09:57:16 -04:00
//! );
//! let code = totp.get_qr("user@example.com", "my-org.com").unwrap();
2020-06-21 09:57:16 -04:00
//! println!("{}", code);
2022-01-20 11:58:27 -05:00
//! # }
2020-06-21 09:57:16 -04:00
//! ```
2020-04-11 15:24:50 -04:00
#[cfg(feature = "serde_support")]
2020-04-25 13:20:11 -04:00
use serde::{Deserialize, Serialize};
2022-01-13 15:52:55 -05:00
use core::fmt;
2020-04-11 15:24:50 -04:00
2020-06-21 09:57:16 -04:00
#[cfg(feature = "qr")]
use {base64, image::Luma, qrcode::QrCode};
2020-04-11 15:24:50 -04:00
2022-01-13 15:52:55 -05:00
use hmac::Mac;
2022-01-13 15:52:55 -05:00
type HmacSha1 = hmac::Hmac<sha1::Sha1>;
type HmacSha256 = hmac::Hmac<sha2::Sha256>;
type HmacSha512 = hmac::Hmac<sha2::Sha512>;
/// Algorithm enum holds the three standards algorithms for TOTP as per the [reference implementation](https://tools.ietf.org/html/rfc6238#appendix-A)
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub enum Algorithm {
SHA1,
SHA256,
SHA512,
}
2022-01-13 15:52:55 -05:00
impl fmt::Display for Algorithm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Algorithm::SHA1 => {
return f.write_str("SHA1");
}
Algorithm::SHA256 => {
return f.write_str("SHA256");
}
Algorithm::SHA512 => {
return f.write_str("SHA512");
}
}
}
}
impl Algorithm {
fn hash<D>(mut digest: D, data: &[u8]) -> Vec<u8>
where
D: hmac::Mac,
{
digest.update(data);
digest.finalize().into_bytes().to_vec()
}
fn sign(&self, key: &[u8], data: &[u8]) -> Vec<u8> {
match *self {
Algorithm::SHA1 => Algorithm::hash(HmacSha1::new_from_slice(key).unwrap(), data),
Algorithm::SHA256 => Algorithm::hash(HmacSha256::new_from_slice(key).unwrap(), data),
Algorithm::SHA512 => Algorithm::hash(HmacSha512::new_from_slice(key).unwrap(), data),
}
}
}
/// TOTP holds informations as to how to generate an auth code and validate it. Its [secret](struct.TOTP.html#structfield.secret) field is sensitive data, treat it accordingly
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct TOTP<T = Vec<u8>> {
/// SHA-1 is the most widespread algorithm used, and for totp pursposes, SHA-1 hash collisions are [not a problem](https://tools.ietf.org/html/rfc4226#appendix-B.2) as HMAC-SHA-1 is not impacted. It's also the main one cited in [rfc-6238](https://tools.ietf.org/html/rfc6238#section-3) even though the [reference implementation](https://tools.ietf.org/html/rfc6238#appendix-A) permits the use of SHA-1, SHA-256 and SHA-512. Not all clients support other algorithms then SHA-1
pub algorithm: Algorithm,
/// The number of digits composing the auth code. Per [rfc-4226](https://tools.ietf.org/html/rfc4226#section-5.3), this can oscilate between 6 and 8 digits
pub digits: usize,
2020-04-13 12:09:02 -04:00
/// Number of steps allowed as network delay. 1 would mean one step before current step and one step after are valids. The recommended value per [rfc-6238](https://tools.ietf.org/html/rfc6238#section-5.2) is 1. Anything more is sketchy, and anyone recommending more is, by definition, ugly and stupid
pub skew: u8,
/// Duration in seconds of a step. The recommended value per [rfc-6238](https://tools.ietf.org/html/rfc6238#section-5.2) is 30 seconds
pub step: u64,
/// As per [rfc-4226](https://tools.ietf.org/html/rfc4226#section-4) the secret should come from a strong source, most likely a CSPRNG. It should be at least 128 bits, but 160 are recommended
pub secret: T,
}
impl<T: AsRef<[u8]>> TOTP<T> {
/// Will create a new instance of TOTP with given parameters. See [the doc](struct.TOTP.html#fields) for reference as to how to choose those values
pub fn new(algorithm: Algorithm, digits: usize, skew: u8, step: u64, secret: T) -> TOTP<T> {
TOTP {
algorithm,
digits,
skew,
step,
secret,
2020-04-11 15:24:50 -04:00
}
}
/// Will sign the given timestamp
pub fn sign(&self, time: u64) -> Vec<u8> {
2022-01-13 15:52:55 -05:00
self.algorithm.sign(
self.secret.as_ref(),
(time / self.step).to_be_bytes().as_ref(),
)
}
/// Will generate a token according to the provided timestamp in seconds
pub fn generate(&self, time: u64) -> String {
let result: &[u8] = &self.sign(time);
let offset = (result[19] & 15) as usize;
2022-01-20 11:58:27 -05:00
let result = u32::from_be_bytes(result[offset..offset + 4].try_into().unwrap()) & 0x7fff_ffff;
2020-04-13 11:02:00 -04:00
format!(
"{1:00$}",
self.digits,
result % (10 as u32).pow(self.digits as u32)
)
}
/// Will check if token is valid by current time, accounting [skew](struct.TOTP.html#structfield.skew)
pub fn check(&self, token: &str, time: u64) -> bool {
let basestep = time / self.step - (self.skew as u64);
for i in 0..self.skew * 2 + 1 {
let step_time = (basestep + (i as u64)) * (self.step as u64);
if self.generate(step_time) == token {
return true;
}
}
false
}
/// Will return the base32 representation of the secret, which might be useful when users want to manually add the secret to their authenticator
pub fn get_secret_base32(&self) -> String {
base32::encode(
base32::Alphabet::RFC4648 { padding: false },
self.secret.as_ref(),
)
}
/// Will generate a standard URL used to automatically add TOTP auths. Usually used with qr codes
pub fn get_url(&self, label: &str, issuer: &str) -> String {
2020-04-13 11:02:00 -04:00
format!(
"otpauth://totp/{}?secret={}&issuer={}&digits={}&algorithm={}",
2022-01-13 15:52:55 -05:00
label.to_string(),
self.get_secret_base32(),
2022-01-13 15:52:55 -05:00
issuer.to_string(),
self.digits.to_string(),
self.algorithm,
)
}
2020-06-21 13:08:52 -04:00
/// Will return a qrcode to automatically add a TOTP as a base64 string. Needs feature `qr` to be enabled!
2020-04-13 16:57:06 -04:00
///
/// # Errors
///
/// This will return an error in case the URL gets too long to encode into a QR code
///
/// It will also return an error in case it can't encode the qr into a png. This shouldn't happen unless either the qrcode library returns malformed data, or the image library doesn't encode the data correctly
2020-06-21 09:57:16 -04:00
#[cfg(feature = "qr")]
pub fn get_qr(&self, label: &str, issuer: &str) -> Result<String, Box<dyn std::error::Error>> {
let url = self.get_url(label, issuer);
let code = QrCode::new(&url)?;
let mut vec = Vec::new();
let encoder = image::png::PngEncoder::new(&mut vec);
2020-04-13 11:02:00 -04:00
encoder.encode(
code.render::<Luma<u8>>().build().as_ref(),
2022-01-13 15:52:55 -05:00
((code.width() + 8) * 8) as u32,
((code.width() + 8) * 8) as u32,
2020-04-13 11:02:00 -04:00
image::ColorType::L8,
)?;
Ok(base64::encode(vec))
}
2020-04-11 15:24:50 -04:00
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
2022-01-13 15:52:55 -05:00
fn url_for_secret_matches_sha1() {
let totp = TOTP::new(Algorithm::SHA1, 6, 1, 1, "TestSecret");
let url = totp.get_url("test_url", "totp-rs");
assert_eq!(url.as_str(), "otpauth://totp/test_url?secret=KRSXG5CTMVRXEZLU&issuer=totp-rs&digits=6&algorithm=SHA1");
}
2022-01-13 15:52:55 -05:00
#[test]
fn url_for_secret_matches_sha256() {
let totp = TOTP::new(Algorithm::SHA256, 6, 1, 1, "TestSecret");
let url = totp.get_url("test_url", "totp-rs");
assert_eq!(url.as_str(), "otpauth://totp/test_url?secret=KRSXG5CTMVRXEZLU&issuer=totp-rs&digits=6&algorithm=SHA256");
}
#[test]
fn url_for_secret_matches_sha512() {
let totp = TOTP::new(Algorithm::SHA512, 6, 1, 1, "TestSecret");
let url = totp.get_url("test_url", "totp-rs");
assert_eq!(url.as_str(), "otpauth://totp/test_url?secret=KRSXG5CTMVRXEZLU&issuer=totp-rs&digits=6&algorithm=SHA512");
}
#[test]
fn returns_base32() {
let totp = TOTP::new(Algorithm::SHA1, 6, 1, 1, "TestSecret");
assert_eq!(totp.get_secret_base32().as_str(), "KRSXG5CTMVRXEZLU");
}
#[test]
fn generates_token() {
let totp = TOTP::new(Algorithm::SHA1, 6, 1, 1, "TestSecret");
assert_eq!(totp.generate(1000).as_str(), "718996");
}
#[test]
fn generates_token_sha256() {
let totp = TOTP::new(Algorithm::SHA256, 6, 1, 1, "TestSecret");
assert_eq!(totp.generate(1000).as_str(), "423657");
}
#[test]
fn generates_token_sha512() {
let totp = TOTP::new(Algorithm::SHA512, 6, 1, 1, "TestSecret");
assert_eq!(totp.generate(1000).as_str(), "416767");
}
#[test]
fn checks_token() {
2022-01-13 15:52:55 -05:00
let totp = TOTP::new(Algorithm::SHA1, 6, 0, 1, "TestSecret");
assert!(totp.check("718996", 1000));
2022-01-13 15:52:55 -05:00
assert!(totp.check("712039", 2000));
assert!(!totp.check("527544", 2000));
assert!(!totp.check("714250", 2000));
}
#[test]
fn checks_token_with_skew() {
let totp = TOTP::new(Algorithm::SHA1, 6, 1, 1, "TestSecret");
assert!(
totp.check("527544", 2000) && totp.check("712039", 2000) && totp.check("714250", 2000)
);
}
#[test]
#[cfg(feature = "qr")]
fn generates_qr() {
use sha1::{Digest, Sha1};
let totp = TOTP::new(Algorithm::SHA1, 6, 1, 1, "TestSecret");
let qr = totp.get_qr("test_url", "totp-rs").unwrap();
// Create hash from image
let hash_digest = Sha1::digest(qr.as_bytes());
assert_eq!(
format!("{:x}", hash_digest).as_str(),
"3abc0127e7a2b1013fb25c97ef14422c1fe9e878"
);
}
}