diff --git a/services/root-keys/Cargo.toml b/services/root-keys/Cargo.toml index 640e4c9ab..f37b2efe2 100644 --- a/services/root-keys/Cargo.toml +++ b/services/root-keys/Cargo.toml @@ -49,6 +49,7 @@ tts-frontend = {path="../tts"} aes-gcm-siv = {git="https://github.com/RustCrypto/AEADs.git", branch="master"} subtle = {version = "2.4.1", default-features = false} usb-device-xous = {path="../usb-device-xous"} +keyboard = {path="../keyboard"} # private keys #sha2 = {version = "0.9.5", default-features = false, features = []} diff --git a/services/root-keys/locales/i18n.json b/services/root-keys/locales/i18n.json index 3ed159b7f..1b3a7f043 100644 --- a/services/root-keys/locales/i18n.json +++ b/services/root-keys/locales/i18n.json @@ -71,6 +71,12 @@ "zh": "备份密钥不匹配。\n\n这是您的备份密钥,再次:", "en-tts": "Backup key did not match. This is your backup key, again:" }, + "rootkeys.backup_prepwait": { + "en": "Preparing backup state...", + "ja": "バックアップの準備中...", + "zh": "正在准备备份...", + "en-tts": "Preparing backup state..." + }, "rootkeys.backup_staged": { "en": "Backup data is prepared.\nConnect to a host computer by USB, and download the backup data bundle.\nScan the QR code for instructions:", "ja": "バックアップデータを用意します。 これで、ホストコンピューターに接続して、バックアップデータバンドルをダウンロードできます。", diff --git a/services/root-keys/src/api.rs b/services/root-keys/src/api.rs index 11832789d..06f47a1b2 100644 --- a/services/root-keys/src/api.rs +++ b/services/root-keys/src/api.rs @@ -131,6 +131,7 @@ pub enum RootkeyResult { /// AES operation definitions pub use cipher::{BlockCipher, consts::U16}; +use keyboard::KeyMap; use zeroize::Zeroize; /// 128-bit AES block @@ -265,6 +266,95 @@ pub enum BackupOp { Restore = 2, } +#[repr(u32)] +#[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[allow(dead_code)] +pub enum BackupLanguage { + En = 0, + EnTts = 1, + Ja = 2, + Zh = 3, +} +impl Default for BackupLanguage { + fn default() -> Self { + match xous::LANG { + "en" => BackupLanguage::En, + "en-tts" => BackupLanguage::EnTts, + "ja" => BackupLanguage::Ja, + "zh" => BackupLanguage::Zh, + _ => BackupLanguage::En, + } + } +} +impl From:: for [u8; 4] { + fn from(l: BackupLanguage) -> [u8; 4] { + (l as u32).to_le_bytes() + } +} +#[repr(u32)] +#[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[allow(dead_code)] +/// We keep a separate version of this for serialization/deserialization because +/// we need to handle "unknown/undefined" layouts in a different way from the keyboard +/// crate. Plus this helps avoid a dependency on the keyboard crate as well. +pub enum BackupKeyboardLayout { + Qwerty = 0, + Dvorak = 1, + Qwertz = 2, + Azerty = 3, + Braille = 4, + Hangul = 5, + // codes above 16384 are reserved for user layouts +} +impl From:: for BackupKeyboardLayout { + fn from(map: KeyMap) -> BackupKeyboardLayout { + match map { + KeyMap::Qwerty => BackupKeyboardLayout::Qwerty, + KeyMap::Azerty => BackupKeyboardLayout::Azerty, + KeyMap::Dvorak => BackupKeyboardLayout::Dvorak, + KeyMap::Qwertz => BackupKeyboardLayout::Qwertz, + KeyMap::Braille => BackupKeyboardLayout::Braille, + KeyMap::Undefined => BackupKeyboardLayout::Qwerty, + } + } +} +impl Into:: for BackupKeyboardLayout { + fn into(self) -> KeyMap { + match self { + BackupKeyboardLayout::Qwerty => KeyMap::Qwerty, + BackupKeyboardLayout::Braille => KeyMap::Braille, + BackupKeyboardLayout::Dvorak => KeyMap::Dvorak, + BackupKeyboardLayout::Qwertz => KeyMap::Qwertz, + BackupKeyboardLayout::Azerty => KeyMap::Azerty, + BackupKeyboardLayout::Hangul => KeyMap::Undefined, + } + } +} +impl Default for BackupKeyboardLayout { + fn default() -> Self { + BackupKeyboardLayout::Qwerty + } +} +impl From:: for [u8; 4] { + fn from(l: BackupKeyboardLayout) -> [u8; 4] { + (l as u32).to_le_bytes() + } +} +impl From::<[u8; 4]> for BackupKeyboardLayout { + fn from(b: [u8; 4]) -> BackupKeyboardLayout { + let code = u32::from_le_bytes(b); + match code { + 0 => BackupKeyboardLayout::Qwerty, + 1 => BackupKeyboardLayout::Dvorak, + 2 => BackupKeyboardLayout::Qwertz, + 3 => BackupKeyboardLayout::Azerty, + 4 => BackupKeyboardLayout::Braille, + 5 => BackupKeyboardLayout::Hangul, + _ => BackupKeyboardLayout::Qwerty, + } + } +} + #[repr(C, align(8))] #[derive(Copy, Clone, Debug)] pub struct BackupHeader { @@ -275,7 +365,9 @@ pub struct BackupHeader { pub ec_ver: [u8; 16], pub wf200_ver: [u8; 16], pub timestamp: u64, - pub _reserved: [u8; 64], + pub language: [u8; 4], + pub kbd_layout: [u8; 4], + pub _reserved: [u8; 56], pub op: BackupOp, } impl Default for BackupHeader { @@ -287,7 +379,9 @@ impl Default for BackupHeader { ec_ver: [0u8; 16], wf200_ver: [0u8; 16], timestamp: 0, - _reserved: [0u8; 64], + language: BackupLanguage::default().into(), // this is "correct by default" + kbd_layout: BackupKeyboardLayout::default().into(), // this has to be adjusted + _reserved: [0u8; 56], op: BackupOp::Archive, } } diff --git a/services/root-keys/src/backups.rs b/services/root-keys/src/backups.rs index 1d386ba78..1dee30633 100644 --- a/services/root-keys/src/backups.rs +++ b/services/root-keys/src/backups.rs @@ -162,6 +162,12 @@ pub(crate) fn create_backup( } ).expect("couldn't encrypt data"); + log::debug!("commit_nonce: {:?}", &kcom_nonce); + log::debug!("commit_nonce: {:x?}", &kcom_nonce); + log::debug!("commit_key: {:?}", &kcom.0); + log::debug!("commit_key: {:x?}", &kcom.0); + log::debug!("header {}, ptlen {}, ctlen {}", size_of::(), size_of::(), size_of::()); + // copy to a ciphertext record let mut backup_ct = BackupDataCt::default(); backup_ct.nonce.copy_from_slice(&nonce); diff --git a/services/root-keys/src/main.rs b/services/root-keys/src/main.rs index e77080cb6..9363b1175 100644 --- a/services/root-keys/src/main.rs +++ b/services/root-keys/src/main.rs @@ -371,7 +371,7 @@ fn main() -> ! { use crate::implementation::RootKeys; log_server::init_wait().unwrap(); - log::set_max_level(log::LevelFilter::Info); + log::set_max_level(log::LevelFilter::Debug); log::info!("my PID is {}", xous::process::id()); let xns = xous_names::XousNames::new().unwrap(); @@ -947,7 +947,11 @@ fn main() -> ! { match result { Ok(_) => { - modals.show_notification(t!("rootkeys.gwup.finished", xous::LANG), None).expect("modals error"); + send_message(main_cid, + xous::Message::new_scalar(Opcode::UxTryReboot.to_usize().unwrap(), 0, 0, 0, 0) + ).expect("couldn't initiate dialog box"); + // just do the reboot now. + // modals.show_notification(t!("rootkeys.gwup.finished", xous::LANG), None).expect("modals error"); } Err(RootkeyResult::AlignmentError) => { modals.show_notification(t!("rootkeys.init.fail_alignment", xous::LANG), None).expect("modals error"); @@ -1381,9 +1385,13 @@ fn main() -> ! { // this final statement has a take/unwrap to set backup_header back to None match keys.write_backup(backup_header.take().unwrap(), backup_ct) { Ok(_) => { + // this switchover takes a couple seconds, give some user feedback + modals.dynamic_notification(Some(t!("rootkeys.backup_prepwait", xous::LANG)), None).ok(); let usbd = usb_device_xous::UsbHid::new(); usbd.switch_to_core(usb_device_xous::UsbDeviceType::Debug).unwrap(); usbd.debug_usb(Some(false)).unwrap(); + modals.dynamic_notification_close().ok(); + // there will be a bit of a pause while the QR text renders, but we'll have to fix that with other optimizations... modals.show_notification(t!("rootkeys.backup_staged", xous::LANG), Some("https://github.com/betrusted-io/betrusted-wiki/wiki/Backups")).ok(); } Err(_) => {