diff --git a/libs/keystore-api/src/common.rs b/libs/keystore-api/src/common.rs index 5cee0bca6..ab8c8b2d0 100644 --- a/libs/keystore-api/src/common.rs +++ b/libs/keystore-api/src/common.rs @@ -33,7 +33,10 @@ pub enum AesOpType { #[zeroize(drop)] pub struct AesOp { /// the caller can try to request "any" index, but it's checked inside the oracle first. + #[cfg(feature = "gen1")] pub key_index: u8, + #[cfg(feature = "gen2")] + pub domain: String, pub block: AesBlockType, pub aes_op: AesOpType, } @@ -65,7 +68,7 @@ pub enum KeyWrapOp { use std::error::Error; impl Error for KeywrapError {} -use std::fmt; +use std::{fmt, u64}; impl fmt::Display for KeywrapError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { @@ -91,7 +94,10 @@ pub struct KeyWrapper { pub data: [u8; MAX_WRAP_DATA + 8], // used to specify the length of the data used in the fixed-length array above pub len: u32, + #[cfg(feature = "gen1")] pub key_index: u8, + #[cfg(feature = "gen2")] + pub domain: String, pub op: KeyWrapOp, pub result: Option, // used by the unwrap side @@ -140,3 +146,44 @@ impl DerefMut for Checksums { } } } + +#[derive(Debug, PartialEq, Eq)] +pub enum PasswordState { + /// Mounted successfully. + Correct, + /// User-initiated aborted. The main purpose for this path is to facilitate + /// developers who want shellchat access but don't want to mount the PDDB. + Incorrect(u64), + /// Abort initiated by system policy due to too many failed attempts + ForcedAbort(u64), + /// Failure because the PDDB hasn't been initialized yet (can't mount because nothing to mount) + Uninit, +} + +impl From<(usize, usize, usize)> for PasswordState { + fn from(value: (usize, usize, usize)) -> Self { + let (arg1, arg2, arg3) = value; + match arg1 { + 0 => PasswordState::Correct, + 1 => PasswordState::Incorrect(arg2 as u64 | (arg3 as u64) << 32), + 2 => PasswordState::ForcedAbort(arg2 as u64 | (arg3 as u64) << 32), + 3 => PasswordState::Uninit, + _ => PasswordState::ForcedAbort(u64::MAX), + } + } +} + +impl Into<(usize, usize, usize)> for PasswordState { + fn into(self) -> (usize, usize, usize) { + match self { + PasswordState::Correct => (0, 0, 0), + PasswordState::Incorrect(arg) => { + (1, (arg as usize) & 0xFFFF_FFFF, (arg >> 32) as usize & 0xFFFF_FFFF) + } + PasswordState::ForcedAbort(arg) => { + (2, (arg as usize) & 0xFFFF_FFFF, (arg >> 32) as usize & 0xFFFF_FFFF) + } + PasswordState::Uninit => (3, 0, 0), + } + } +} diff --git a/libs/keystore-api/src/keystore_api.rs b/libs/keystore-api/src/gen2_api.rs similarity index 51% rename from libs/keystore-api/src/keystore_api.rs rename to libs/keystore-api/src/gen2_api.rs index abd883df9..7f6ff0415 100644 --- a/libs/keystore-api/src/keystore_api.rs +++ b/libs/keystore-api/src/gen2_api.rs @@ -1,3 +1,5 @@ +pub const SERVER_NAME_KEYS: &str = "_Keystore server_"; + /// Size of a checksummed block in pages. 0x100 = 256 pages, /// or 1 MiB for a checksummed block. This is specified in 4kiB pages /// because it really doesn't make sense to checksum anything smaller @@ -6,3 +8,18 @@ pub const CHECKSUM_BLOCKLEN_PAGE: u32 = 0x100; /// TODO: set PDDB length based on board-specific config params pub const TOTAL_CHECKSUMS: u32 = 4096 * 1024 / (CHECKSUM_BLOCKLEN_PAGE * 4096); + +#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] +pub enum Opcode { + /// initiate an AES oracle operation + AesOracle = 4, + /// initiate key wrapper operation + AesKwp = 5, + /// clear a cached password + ClearPasswordCacheEntry = 7, + + // Gen-2 extensions are at 256 and up + /// Get a non-secret 64-bit random identifier + GetDna = 256, + EnsurePassword = 257, +} diff --git a/libs/keystore-api/src/lib.rs b/libs/keystore-api/src/lib.rs index 4d12ddedf..58e170ee8 100644 --- a/libs/keystore-api/src/lib.rs +++ b/libs/keystore-api/src/lib.rs @@ -5,9 +5,9 @@ pub mod rootkeys_api; pub use rootkeys_api::*; // gen2 API #[cfg(feature = "gen2")] -pub mod keystore_api; +pub mod gen2_api; #[cfg(feature = "gen2")] -pub use keystore_api::*; +pub use gen2_api::*; pub mod common; pub use common::*;