diff --git a/services/root-keys/locales/i18n.json b/services/root-keys/locales/i18n.json index 04ef68352..9b2c16f78 100644 --- a/services/root-keys/locales/i18n.json +++ b/services/root-keys/locales/i18n.json @@ -95,18 +95,24 @@ "zh": "执行备份的内部错误。 进程中止。", "en-tts": "Internal error performing backup. Process aborted." }, - "rootkeys.bootpass": { - "en": "Create a PIN code for device unlock.", - "ja": "デバイスのロックを解除するためのPINコードを作成します。", - "zh": "输入密码 解锁 安全含义:低", - "en-tts": "Enter unlock password" - }, - "rootkeys.updatepass": { + "rootkeys.updatefirstpass": { "en": "⚠ Critical Password ⚠\nCreate a strong root password for authorizing system updates.", "ja": "重要なパスワード\nシステムアップデートを認証するための強力なパスフレーズを作成します。", "zh": "输入密码 软件更新 安全含义:至关重要 ", "en-tts": "Enter a root password. This should be a strong password." }, + "rootkeys.updatepass": { + "en": "⚠ Critical Password ⚠\nPlease confirm the root password.", + "ja": "ルートパスワードを確認してください。", + "zh": "请确认root密码。", + "en-tts": "Please confirm the root password." + }, + "rootkeys.updatepass_fail": { + "en": "Passwords did not match. Try again?", + "ja": "パスワードが間違っています。 再試行?", + "zh": "密码不匹配。 再试一次?", + "en-tts": "Passwords did not match. Try again?" + }, "rootkeys.blind_update": { "en": "No device keys detected. Shall we copy the staged gateware anyways, overwriting the current image?", "ja": "ルートキーが検出されませんでした。 とにかくステージングされたゲートウェアをコピーして、現在のイメージを上書きしませんか?", @@ -126,10 +132,10 @@ "en-tts": "Device keys already initialized." }, "rootkeys.confirm": { - "en": "🔐 Setup passwords for Precursor 🔐\n\nYou will be prompted for two passwords:\n\t1. Device unlock PIN code\n\t2. Root passphrase.\n\nAre you ready?", - "ja": "これにより、Precursorデバイスのルートキーが初期化されます。\n\n2つのパスワードの入力を求められます。\n\t1.デバイスのロック解除PINコード\n\t2.システムアップデートパスフレーズ。\n\n準備はいいですか?", - "zh": "仅当您准备好信 任此设备时才 应继续", - "en-tts": "Setup passwords for Precursor. You will create two passwords, a PIN code, and a root password." + "en": "🔐 Setup passwords for Precursor 🔐\n\nYou will be prompted for a root passphrase that must be secure and cannot be changed.\n\nAre you ready?", + "ja": "🔐 Precursor のパスワードを設定する 🔐\n\nルート パスフレーズの入力を求めるメッセージが表示されます。ルート パスフレーズは安全である必要があり、変更することはできません。\n\n準備はできていますか?", + "zh": "🔐 为 Precursor 设置密码 🔐\n\n系统将提示您输入必须安全且无法更改的根密码。\n\n您准备好了吗?", + "en-tts": "Setup passwords for Precursor\n\nYou will be prompted for a root passphrase that must be secure and cannot be changed.\n\nAre you ready?" }, "rootkeys.confirm.yes": { "en": "I'm ready!", diff --git a/services/root-keys/src/api.rs b/services/root-keys/src/api.rs index 81c35a058..811bf42b2 100644 --- a/services/root-keys/src/api.rs +++ b/services/root-keys/src/api.rs @@ -29,7 +29,7 @@ pub(crate) enum Opcode { /// attempt to initialize keys on a brand new system. Does nothing if the keys are already provisioned. UxTryInitKeys = 9, - UxInitBootPasswordReturn = 10, + UxInitUpdateFirstPasswordReturn = 10, UxInitUpdatePasswordReturn = 11, /// provision a gateware update with our secret data diff --git a/services/root-keys/src/implementation.rs b/services/root-keys/src/implementation.rs index e30364ed8..7b2f6a950 100644 --- a/services/root-keys/src/implementation.rs +++ b/services/root-keys/src/implementation.rs @@ -722,12 +722,12 @@ impl<'a> RootKeys { /// Plaintext password is passed as a &str. Any copies internally are destroyed. Caller is responsible for destroying the &str original. /// Performs a bcrypt hash of the password, with the currently set salt; does not store the plaintext after exit. - pub fn hash_and_save_password(&mut self, pw: &str) { + pub fn hash_and_save_password(&mut self, pw: &str, verify: bool) -> bool { let pw_type = if let Some(cur_type) = self.cur_password_type { cur_type } else { log::error!("got an unexpected password from the UX"); - return; + return false; }; let mut hashed_password: [u8; 24] = [0; 24]; let mut salt = self.get_salt(); @@ -751,19 +751,51 @@ impl<'a> RootKeys { let digest = hasher.finalize(); let pcache_ptr: *mut PasswordCache = self.pass_cache.as_mut_ptr() as *mut PasswordCache; - unsafe { - match pw_type { - PasswordType::Boot => { - for (&src, dst) in digest.iter().zip((*pcache_ptr).hashed_boot_pw.iter_mut()) { - *dst = src; + if !verify { + unsafe { + match pw_type { + PasswordType::Boot => { + for (&src, dst) in digest.iter().zip((*pcache_ptr).hashed_boot_pw.iter_mut()) { + *dst = src; + } + (*pcache_ptr).hashed_boot_pw_valid = 1; + } + PasswordType::Update => { + for (&src, dst) in digest.iter().zip((*pcache_ptr).hashed_update_pw.iter_mut()) { + *dst = src; + } + (*pcache_ptr).hashed_update_pw_valid = 1; } - (*pcache_ptr).hashed_boot_pw_valid = 1; } - PasswordType::Update => { - for (&src, dst) in digest.iter().zip((*pcache_ptr).hashed_update_pw.iter_mut()) { - *dst = src; + } + true + } else { + unsafe { + match pw_type { + PasswordType::Boot => { + if (*pcache_ptr).hashed_boot_pw_valid == 1 { + for (&src, &dst) in digest.iter().zip((*pcache_ptr).hashed_boot_pw.iter()) { + if dst != src { + return false + } + } + true + } else { + false + } + } + PasswordType::Update => { + if (*pcache_ptr).hashed_update_pw_valid == 1 { + for (&src, &dst) in digest.iter().zip((*pcache_ptr).hashed_update_pw.iter()) { + if dst != src { + return false + } + } + true + } else { + false + } } - (*pcache_ptr).hashed_update_pw_valid = 1; } } } @@ -1063,7 +1095,6 @@ impl<'a> RootKeys { // extract the update password key from the cache, and apply it to the private key #[cfg(feature = "hazardous-debug")] { - log::debug!("cached boot passwords {:x?}", pcache.hashed_boot_pw); log::debug!("cached update password: {:x?}", pcache.hashed_update_pw); } // private key must XOR with password before storing @@ -1081,30 +1112,28 @@ impl<'a> RootKeys { .zip(self.sensitive_data.borrow_mut().as_slice_mut::()[KeyRomLocs::SELFSIGN_PRIVKEY as usize..KeyRomLocs::SELFSIGN_PRIVKEY as usize + 256/(size_of::()*8)].iter_mut()) { *dst = u32::from_be_bytes(src.try_into().unwrap()) } + #[cfg(feature = "hazardous-debug")] + log::debug!("private_key_enc: {:?}", private_key_enc); pb.set_percentage(10); - // generate and store a root key (aka boot key), this is what is unlocked by the "boot password" - // ironically, it's a "lower security" key because it just acts as a gatekeeper to further - // keys that would have a stronger password applied to them, based upon the importance of the secret - // think of this more as a user PIN login confirmation, than as a significant cryptographic event + // generate the "boot key". This is just a random number that eventually gets XOR'd with a PIN + // by the PDDB to derive the final PDDB .System unlock key. let mut boot_key_enc: [u8; 32] = [0; 32]; - for (dst, key) in - boot_key_enc.chunks_mut(4).into_iter() - .zip(pcache.hashed_boot_pw.chunks(4).into_iter()) { + for dst in + boot_key_enc.chunks_mut(4).into_iter() { let key_word = self.trng.get_u32().unwrap().to_be_bytes(); // just unroll this loop, it's fast and easy enough - (*dst)[0] = key[0] ^ key_word[0]; - (*dst)[1] = key[1] ^ key_word[1]; - (*dst)[2] = key[2] ^ key_word[2]; - (*dst)[3] = key[3] ^ key_word[3]; - // also note that interestingly, we don't have to XOR it with the hashed boot password -- - // this key isn't used by this routine, just initialized, so really, it only matters to - // XOR it with the password when you use it the first time to encrypt something. + (*dst)[0] = key_word[0]; + (*dst)[1] = key_word[1]; + (*dst)[2] = key_word[2]; + (*dst)[3] = key_word[3]; } + #[cfg(feature = "hazardous-debug")] + log::debug!("boot_key_enc: {:?}", boot_key_enc); // store the boot key to the keyrom staging area - for (src, dst) in private_key_enc.chunks(4).into_iter() + for (src, dst) in boot_key_enc.chunks(4).into_iter() .zip(self.sensitive_data.borrow_mut().as_slice_mut::()[KeyRomLocs::USER_KEY as usize..KeyRomLocs::USER_KEY as usize + 256/(size_of::()*8)].iter_mut()) { *dst = u32::from_be_bytes(src.try_into().unwrap()) } diff --git a/services/root-keys/src/main.rs b/services/root-keys/src/main.rs index 5c0cbb792..2c085c84b 100644 --- a/services/root-keys/src/main.rs +++ b/services/root-keys/src/main.rs @@ -147,7 +147,7 @@ mod implementation { pub fn update_policy(&mut self, policy: Option) { log::info!("policy updated: {:?}", policy); } - pub fn hash_and_save_password(&mut self, pw: &str) { + pub fn hash_and_save_password(&mut self, pw: &str, _verify: bool) { log::info!("got password plaintext: {}", pw); } pub fn set_ux_password_type(&mut self, cur_type: Option) { @@ -405,7 +405,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(); @@ -470,7 +470,7 @@ fn main() -> ! { true, TextEntryVisibility::LastChars, main_cid, - Opcode::UxInitBootPasswordReturn.to_u32().unwrap(), + Opcode::UxInitUpdateFirstPasswordReturn.to_u32().unwrap(), vec![TextEntryPayload::new()], None, ); @@ -481,7 +481,7 @@ fn main() -> ! { let mut rootkeys_modal = Modal::new( gam::ROOTKEY_MODAL_NAME, ActionType::TextEntry(password_action.clone()), - Some(t!("rootkeys.bootpass", xous::LANG)), + Some(t!("rootkeys.updatefirstpass", xous::LANG)), None, GlyphStyle::Regular, 8 @@ -624,29 +624,29 @@ fn main() -> ! { } // setup_key_init() prepares the salt and other items necessary to receive a password safely keys.setup_key_init(); - // request the boot password first - keys.set_ux_password_type(Some(PasswordType::Boot)); + // request the update password first + keys.set_ux_password_type(Some(PasswordType::Update)); // pop up our private password dialog box - password_action.set_action_opcode(Opcode::UxInitBootPasswordReturn.to_u32().unwrap()); + password_action.set_action_opcode(Opcode::UxInitUpdateFirstPasswordReturn.to_u32().unwrap()); rootkeys_modal.modify( Some(ActionType::TextEntry(password_action.clone())), - Some(t!("rootkeys.bootpass", xous::LANG)), false, + Some(t!("rootkeys.updatefirstpass", xous::LANG)), false, None, true, None ); #[cfg(feature="tts")] - tts.tts_blocking(t!("rootkeys.bootpass", xous::LANG)).unwrap(); - log::info!("{}ROOTKEY.BOOTPW,{}", xous::BOOKEND_START, xous::BOOKEND_END); + tts.tts_blocking(t!("rootkeys.updatefirstpass", xous::LANG)).unwrap(); + log::info!("{}ROOTKEY.UPDPW,{}", xous::BOOKEND_START, xous::BOOKEND_END); rootkeys_modal.activate(); } }, - Some(Opcode::UxInitBootPasswordReturn) => { + Some(Opcode::UxInitUpdateFirstPasswordReturn) => { // assume: // - setup_key_init has also been called (exactly once, before anything happens) // - set_ux_password_type has been called already let mut buf = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; let plaintext_pw = buf.to_original::().unwrap(); - keys.hash_and_save_password(plaintext_pw.first().as_str()); + keys.hash_and_save_password(plaintext_pw.first().as_str(), false); plaintext_pw.first().volatile_clear(); // ensure the data is destroyed after sending to the keys enclave buf.volatile_clear(); @@ -667,7 +667,56 @@ fn main() -> ! { let mut buf = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; let plaintext_pw = buf.to_original::().unwrap(); - keys.hash_and_save_password(plaintext_pw.first().as_str()); + if !keys.hash_and_save_password(plaintext_pw.first().as_str(), true) { + // password did not verify + modals.add_list_item(t!("rootkeys.gwup.yes", xous::LANG)).expect("modals error"); + modals.add_list_item(t!("rootkeys.gwup.no", xous::LANG)).expect("modals error"); + match modals.get_radiobutton(t!("rootkeys.updatepass_fail", xous::LANG)) { + Ok(response) => { + if response == t!("rootkeys.gwup.yes", xous::LANG) { + keys.purge_password(PasswordType::Update); + // request the update password first + keys.set_ux_password_type(Some(PasswordType::Update)); + // pop up our private password dialog box + password_action.set_action_opcode(Opcode::UxInitUpdateFirstPasswordReturn.to_u32().unwrap()); + rootkeys_modal.modify( + Some(ActionType::TextEntry(password_action.clone())), + Some(t!("rootkeys.updatefirstpass", xous::LANG)), false, + None, true, None + ); + #[cfg(feature="tts")] + tts.tts_blocking(t!("rootkeys.updatefirstpass", xous::LANG)).unwrap(); + log::info!("{}ROOTKEY.UPDPW,{}", xous::BOOKEND_START, xous::BOOKEND_END); + rootkeys_modal.activate(); + continue; + } else { + // abort + plaintext_pw.first().volatile_clear(); // ensure the data is destroyed after sending to the keys enclave + buf.volatile_clear(); + keys.purge_password(PasswordType::Update); + keys.purge_sensitive_data(); + susres.set_suspendable(true).expect("couldn't unblock suspend/resume"); + if let Some(dr) = deferred_response.take() { + xous::return_scalar(dr, 0).unwrap(); + } + continue; + } + } + _ => { + log::error!("get_radiobutton failed"); + plaintext_pw.first().volatile_clear(); // ensure the data is destroyed after sending to the keys enclave + buf.volatile_clear(); + keys.purge_password(PasswordType::Update); + keys.purge_sensitive_data(); + susres.set_suspendable(true).expect("couldn't unblock suspend/resume"); + if let Some(dr) = deferred_response.take() { + xous::return_scalar(dr, 0).unwrap(); + } + continue; + }, + } + } + plaintext_pw.first().volatile_clear(); // ensure the data is destroyed after sending to the keys enclave buf.volatile_clear(); @@ -946,7 +995,7 @@ fn main() -> ! { let mut buf = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; let plaintext_pw = buf.to_original::().unwrap(); - keys.hash_and_save_password(plaintext_pw.first().as_str()); + keys.hash_and_save_password(plaintext_pw.first().as_str(), false); plaintext_pw.first().volatile_clear(); // ensure the data is destroyed after sending to the keys enclave buf.volatile_clear(); // indicate that there should be no change to the policy @@ -1034,7 +1083,7 @@ fn main() -> ! { let mut buf = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; let plaintext_pw = buf.to_original::().unwrap(); - keys.hash_and_save_password(plaintext_pw.first().as_str()); + keys.hash_and_save_password(plaintext_pw.first().as_str(), false); plaintext_pw.first().volatile_clear(); // ensure the data is destroyed after sending to the keys enclave buf.volatile_clear(); @@ -1130,7 +1179,7 @@ fn main() -> ! { let mut buf = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; let plaintext_pw = buf.to_original::().unwrap(); - keys.hash_and_save_password(plaintext_pw.first().as_str()); + keys.hash_and_save_password(plaintext_pw.first().as_str(), false); plaintext_pw.first().volatile_clear(); // ensure the data is destroyed after sending to the keys enclave buf.volatile_clear(); @@ -1170,7 +1219,7 @@ fn main() -> ! { { let mut buf = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; let plaintext_pw = buf.to_original::().unwrap(); - keys.hash_and_save_password(plaintext_pw.first().as_str()); + keys.hash_and_save_password(plaintext_pw.first().as_str(), false); plaintext_pw.first().volatile_clear(); // ensure the data is destroyed after sending to the keys enclave buf.volatile_clear(); @@ -1259,7 +1308,7 @@ fn main() -> ! { let mut buf = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; let plaintext_pw = buf.to_original::().unwrap(); - keys.hash_and_save_password(plaintext_pw.first().as_str()); + keys.hash_and_save_password(plaintext_pw.first().as_str(), false); plaintext_pw.first().volatile_clear(); // ensure the data is destroyed after sending to the keys enclave buf.volatile_clear(); send_message(main_cid, @@ -1373,7 +1422,7 @@ fn main() -> ! { let mut buf = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; let plaintext_pw = buf.to_original::().unwrap(); - keys.hash_and_save_password(plaintext_pw.first().as_str()); + keys.hash_and_save_password(plaintext_pw.first().as_str(), false); plaintext_pw.first().volatile_clear(); // ensure the data is destroyed after sending to the keys enclave buf.volatile_clear(); keys.set_ux_password_type(None); @@ -1554,7 +1603,7 @@ fn main() -> ! { let mut buf = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; let plaintext_pw = buf.to_original::().unwrap(); - keys.hash_and_save_password(plaintext_pw.first().as_str()); + keys.hash_and_save_password(plaintext_pw.first().as_str(), false); plaintext_pw.first().volatile_clear(); // ensure the data is destroyed after sending to the keys enclave buf.volatile_clear(); keys.set_ux_password_type(None);