use std::path::Path;
use anyhow::{Context as _, Result, bail};
use zeroize::Zeroizing;
const HEADER: &str = "openmoat-gate-totp-v1";
pub const LEN: usize = 20;
const ALPHABET: &[u8; 32] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
pub fn generate() -> Result<Zeroizing<Vec<u8>>> {
let mut raw = Zeroizing::new(vec![0u8; LEN]);
getrandom::fill(raw.as_mut_slice())
.map_err(|e| anyhow::Error::msg(e.to_string()))
.context("reading random bytes for the gate's secret")?;
Ok(raw)
}
pub fn encode_file(secret: &[u8]) -> Zeroizing<String> {
Zeroizing::new(format!("{HEADER}\n{}\n", base32(secret).as_str()))
}
pub fn read(path: &Path) -> Result<Zeroizing<Vec<u8>>> {
let text = crate::key_file::read(path)?;
parse(&text).with_context(|| {
format!(
"reading {}: the gate's secret file is not valid; run `openmoat doctor`",
path.display()
)
})
}
pub(super) fn parse(text: &str) -> Result<Zeroizing<Vec<u8>>> {
let mut lines = text.lines();
if lines.next() != Some(HEADER) {
bail!("the first line is not `{HEADER}`");
}
let Some(encoded) = lines.next() else {
bail!("there is no secret line");
};
let secret = unbase32(encoded.trim()).context("the secret is not base32")?;
if secret.len() < 16 {
bail!("the secret is shorter than 128 bits");
}
Ok(secret)
}
pub fn base32(bytes: &[u8]) -> Zeroizing<String> {
let mut out = Zeroizing::new(String::with_capacity(bytes.len().div_ceil(5) * 8));
for chunk in bytes.chunks(5) {
let mut block = [0u8; 5];
for (slot, byte) in block.iter_mut().zip(chunk) {
*slot = *byte;
}
let bits = block.iter().fold(0u64, |acc, b| (acc << 8) | u64::from(*b));
let chars = (chunk.len() * 8).div_ceil(5);
for i in 0..chars {
let index = (bits >> (35 - 5 * i)) & 0x1f;
out.push(char::from(
ALPHABET[usize::try_from(index).unwrap_or_default()],
));
}
}
out
}
pub fn unbase32(text: &str) -> Option<Zeroizing<Vec<u8>>> {
let mut out = Zeroizing::new(Vec::with_capacity(text.len() * 5 / 8));
let mut bits: u64 = 0;
let mut count = 0u32;
for c in text.bytes().filter(|b| !matches!(b, b' ' | b'=')) {
let upper = c.to_ascii_uppercase();
let value = ALPHABET.iter().position(|a| *a == upper)?;
bits = (bits << 5) | u64::try_from(value).ok()?;
count += 5;
if count >= 8 {
count -= 8;
out.push(u8::try_from((bits >> count) & 0xff).ok()?);
}
}
Some(out)
}
pub fn uri(secret: &[u8]) -> Zeroizing<String> {
Zeroizing::new(format!(
"otpauth://totp/OpenMoat:policy-changes?secret={}&issuer=OpenMoat&algorithm=SHA1\
&digits={}&period={}",
base32(secret).as_str(),
super::totp::DIGITS,
super::totp::STEP_SECS
))
}
pub fn grouped(secret: &[u8]) -> Zeroizing<String> {
let plain = base32(secret);
let mut out = Zeroizing::new(String::with_capacity(plain.len() + plain.len() / 4));
for (i, c) in plain.chars().enumerate() {
if i > 0 && i % 4 == 0 {
out.push(' ');
}
out.push(c);
}
out
}