[vault] The recovery key is printed as 8 groups of 8
This commit is contained in:
@@ -29,8 +29,8 @@ pub mod security;
|
||||
pub mod signing;
|
||||
|
||||
pub use item::{Attachment, Field, HISTORY_LIMIT, HistoryEntry, Item, ItemType, new_id, now_ms};
|
||||
pub use save::{CrashPoint, save_atomic, save_atomic_crashing};
|
||||
pub use recovery::RecoveryKey;
|
||||
pub use save::{CrashPoint, save_atomic, save_atomic_crashing};
|
||||
pub use secret::{SecretBytes, SecretString};
|
||||
|
||||
use crypto::KEY_LEN;
|
||||
|
||||
@@ -13,8 +13,9 @@ pub(crate) const ID_LEN: usize = 4;
|
||||
pub(crate) const MATERIAL_LEN: usize = 32;
|
||||
const CHECK_LEN: usize = 4;
|
||||
const CHECK_CONTEXT: &[u8] = b"custos recovery key v1";
|
||||
/// 40 bytes as base32: 64 characters, written as 16 groups of 4.
|
||||
/// 40 bytes as base32: 64 characters, written as 8 groups of 8.
|
||||
const TEXT_LEN: usize = 64;
|
||||
const GROUP: usize = 8;
|
||||
|
||||
/// A recovery key as it is printed. `Debug` never shows it.
|
||||
pub struct RecoveryKey {
|
||||
@@ -28,7 +29,7 @@ impl RecoveryKey {
|
||||
&self.id
|
||||
}
|
||||
|
||||
/// The key: 16 groups of 4 characters, `A-Z` and `2-7`.
|
||||
/// The key: 8 groups of 8 characters, `A-Z` and `2-7`.
|
||||
pub fn expose(&self) -> &str {
|
||||
&self.text
|
||||
}
|
||||
@@ -59,9 +60,9 @@ pub(crate) fn encode(id: &[u8; ID_LEN], material: &[u8; MATERIAL_LEN]) -> Recove
|
||||
raw[ID_LEN..ID_LEN + MATERIAL_LEN].copy_from_slice(material);
|
||||
raw[ID_LEN + MATERIAL_LEN..].copy_from_slice(&check(id, material));
|
||||
let flat = Zeroizing::new(BASE32_NOPAD.encode(raw.as_ref()));
|
||||
let mut text = Zeroizing::new(String::with_capacity(TEXT_LEN + TEXT_LEN / 4));
|
||||
let mut text = Zeroizing::new(String::with_capacity(TEXT_LEN + TEXT_LEN / GROUP));
|
||||
for (i, c) in flat.chars().enumerate() {
|
||||
if i > 0 && i % 4 == 0 {
|
||||
if i > 0 && i % GROUP == 0 {
|
||||
text.push('-');
|
||||
}
|
||||
text.push(c);
|
||||
@@ -105,9 +106,14 @@ mod tests {
|
||||
let key = encode(&[0xa1, 0xb2, 0xc3, 0xd4], &[9; 32]);
|
||||
assert_eq!(key.id(), "a1b2c3d4");
|
||||
let text = key.expose().to_owned();
|
||||
assert_eq!(text.len(), 64 + 15);
|
||||
assert!(text.split('-').all(|g| g.len() == 4 && g.bytes().all(|b| b.is_ascii_uppercase() || (b'2'..=b'7').contains(&b))));
|
||||
for typed in [text.clone(), text.to_lowercase(), text.replace('-', " "), text.replace('-', ""), format!(" {text}\r\n")] {
|
||||
assert_eq!(text.len(), 64 + 7);
|
||||
assert!(
|
||||
text.split('-')
|
||||
.all(|g| g.len() == 8 && g.bytes().all(|b| b.is_ascii_uppercase() || (b'2'..=b'7').contains(&b)))
|
||||
);
|
||||
for typed in
|
||||
[text.clone(), text.to_lowercase(), text.replace('-', " "), text.replace('-', ""), format!(" {text}\r\n")]
|
||||
{
|
||||
let (id, material) = decode(&SecretString::from(typed.as_str())).unwrap();
|
||||
assert_eq!((id, *material), ([0xa1, 0xb2, 0xc3, 0xd4], [9; 32]));
|
||||
}
|
||||
@@ -119,7 +125,7 @@ mod tests {
|
||||
let text = encode(&[1, 2, 3, 4], &[9; 32]).expose().to_owned();
|
||||
let mistyped = |s: String| matches!(decode(&SecretString::from(s.as_str())), Err(Error::RecoveryKeyMistyped));
|
||||
// One character changed, anywhere: the check catches it.
|
||||
for at in [0usize, 7, 40, 78] {
|
||||
for at in [0usize, 7, 40, 70] {
|
||||
let mut chars: Vec<char> = text.chars().collect();
|
||||
if chars[at] == '-' {
|
||||
continue;
|
||||
@@ -130,12 +136,12 @@ mod tests {
|
||||
// Two groups swapped, a group missing, one too many, a character that is never in a key.
|
||||
let groups: Vec<&str> = text.split('-').collect();
|
||||
let mut swapped = groups.clone();
|
||||
swapped.swap(2, 9);
|
||||
swapped.swap(2, 6);
|
||||
if swapped != groups {
|
||||
assert!(mistyped(swapped.join("-")));
|
||||
}
|
||||
assert!(mistyped(groups[1..].join("-")));
|
||||
assert!(mistyped(format!("{text}-AAAA")));
|
||||
assert!(mistyped(format!("{text}-AAAAAAAA")));
|
||||
assert!(mistyped(text.replacen(|c: char| c != '-', "1", 1)));
|
||||
assert!(mistyped(text.replacen('-', "_", 1)));
|
||||
assert!(mistyped(String::new()));
|
||||
|
||||
Reference in New Issue
Block a user