diff --git a/services/keystore/Cargo.toml b/services/keystore/Cargo.toml index efb17a6cd..4488ccf8a 100644 --- a/services/keystore/Cargo.toml +++ b/services/keystore/Cargo.toml @@ -80,7 +80,10 @@ debug-hal = ["bao1x-hal/debug-print-duart"] # Enables incrementing one way counters. Off by default because this API can be, at the very least, # used to grief the security layer if not worse -owc-inc = [] +owc-inc = ["keystore-api/owc-inc"] + +# Enable to allow users to access the application keystore slots +app-keys = ["keystore-api/app-keys"] board-baosec = [ "utralib/bao1x", @@ -109,6 +112,7 @@ board-dabao = [ "bao1x-hal", "bao1x-hal-service/board-dabao", "bao1x-hal/security", + "app-keys", ] # Dev board form factor loader-dabao = [] diff --git a/services/keystore/src/lib.rs b/services/keystore/src/lib.rs index 434315142..269893877 100644 --- a/services/keystore/src/lib.rs +++ b/services/keystore/src/lib.rs @@ -381,6 +381,32 @@ impl Keystore { } Ok(key) } + + #[cfg(feature = "app-keys")] + pub fn read_app_key(&self, index: usize) -> Result<[u8; 32], xous::Error> { + let app_key = AppKey::ReadRequest { guard: APPKEY_GUARD, index }; + let mut buf = Buffer::into_buf(app_key).or(Err(xous::Error::InternalError))?; + buf.lend_mut(self.conn, Opcode::AppKeyOp.to_u32().unwrap()).or(Err(xous::Error::InternalError))?; + match buf.to_original::().unwrap() { + AppKey::ReadResponse { data } => Ok(data), + AppKey::AccessDenied => Err(xous::Error::AccessDenied), + _ => Err(xous::Error::InternalError), + } + } + + #[cfg(feature = "app-keys")] + pub fn write_app_key(&self, index: usize, data: &[u8; 32]) -> Result<(), xous::Error> { + let mut storage = [0u8; 32]; + storage.copy_from_slice(data); + let app_key = AppKey::Write { guard: APPKEY_GUARD, index, data: storage }; + let mut buf = Buffer::into_buf(app_key).or(Err(xous::Error::InternalError))?; + buf.lend_mut(self.conn, Opcode::AppKeyOp.to_u32().unwrap()).or(Err(xous::Error::InternalError))?; + match buf.to_original::().unwrap() { + AppKey::Success => Ok(()), + AppKey::AccessDenied => Err(xous::Error::AccessDenied), + _ => Err(xous::Error::InternalError), + } + } } use core::sync::atomic::{AtomicU32, Ordering}; diff --git a/services/keystore/src/platform/baosec/server.rs b/services/keystore/src/platform/baosec/server.rs index d262421ae..574f0685a 100644 --- a/services/keystore/src/platform/baosec/server.rs +++ b/services/keystore/src/platform/baosec/server.rs @@ -37,23 +37,25 @@ pub fn keystore(sid: SID) -> ! { log::debug!("{:?}", opcode); match opcode { Opcode::AesOracle => { - let mut buffer = - unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; - // as_flat saves a copy step, but we have to deserialize some enums manually - let mut aes_op = buffer.to_original::().unwrap(); - // TODO: add hardening checks to the deserialization of AesOp. Can't assume the - // values are correct coming into this server. - store.aes_op(&mut aes_op).expect("couldn't perform AES op"); - buffer.replace(aes_op).unwrap(); + if let Some(mem) = msg.body.memory_message_mut() { + let mut buffer = unsafe { Buffer::from_memory_message_mut(mem) }; + // as_flat saves a copy step, but we have to deserialize some enums manually + let mut aes_op = buffer.to_original::().unwrap(); + // TODO: add hardening checks to the deserialization of AesOp. Can't assume the + // values are correct coming into this server. + store.aes_op(&mut aes_op).expect("couldn't perform AES op"); + buffer.replace(aes_op).unwrap(); + } } Opcode::AesKwp => { - let mut buffer = - unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; - let mut kwp = buffer.to_original::().unwrap(); - // TODO: add hardening checks to the deserialization of AesOp. Can't assume the - // values are correct coming into this server. - store.aes_kwp(&mut kwp).expect("couldn't wrap key"); - buffer.replace(kwp).unwrap(); + if let Some(mem) = msg.body.memory_message_mut() { + let mut buffer = unsafe { Buffer::from_memory_message_mut(mem) }; + let mut kwp = buffer.to_original::().unwrap(); + // TODO: add hardening checks to the deserialization of AesOp. Can't assume the + // values are correct coming into this server. + store.aes_kwp(&mut kwp).expect("couldn't wrap key"); + buffer.replace(kwp).unwrap(); + } } Opcode::Ephemeral => { if let Some(scalar) = msg.body.scalar_message_mut() { @@ -121,6 +123,7 @@ pub fn keystore(sid: SID) -> ! { if store.is_developer() { scalar.arg1 = 0 } else { scalar.arg1 = 1 } } } + #[cfg(feature = "owc-inc")] Opcode::IncOneWayCounter => { if let Some(scalar) = msg.body.scalar_message_mut() { if [scalar.arg2, scalar.arg3, scalar.arg4] == OWC_MAGIC_INC @@ -154,6 +157,41 @@ pub fn keystore(sid: SID) -> ! { } } } + #[cfg(feature = "app-keys")] + Opcode::AppKeyOp => { + if let Some(mem) = msg.body.memory_message_mut() { + let mut buffer = unsafe { Buffer::from_memory_message_mut(mem) }; + // as_flat saves a copy step, but we have to deserialize some enums manually + let mut app_key = buffer.to_original::().unwrap(); + match app_key { + AppKey::ReadRequest { guard, index } => { + if guard != APPKEY_GUARD { + app_key = AppKey::InternalError; + } else { + match store.read_app_key(index) { + Ok(d) => app_key = AppKey::ReadResponse { data: d }, + Err(xous::Error::AccessDenied) => app_key = AppKey::AccessDenied, + _ => app_key = AppKey::InternalError, + } + } + } + AppKey::Write { guard, index, data } => { + if guard != APPKEY_GUARD { + app_key = AppKey::InternalError + } else { + match store.write_app_key(&mut rram, index, data) { + Ok(_) => app_key = AppKey::Success, + Err(xous::Error::AccessDenied) => app_key = AppKey::AccessDenied, + Err(xous::Error::HardwareError) => app_key = AppKey::WriteError, + _ => app_key = AppKey::InternalError, + } + } + } + _ => app_key = AppKey::InternalError, + } + buffer.replace(app_key).unwrap(); + } + } Opcode::InvalidCall => { log::error!("Invalid call in keystore: {:?}", opcode); } diff --git a/services/keystore/src/platform/baosec/store.rs b/services/keystore/src/platform/baosec/store.rs index acbc618de..7be8c9fee 100644 --- a/services/keystore/src/platform/baosec/store.rs +++ b/services/keystore/src/platform/baosec/store.rs @@ -386,4 +386,53 @@ impl KeyStore { } pub fn is_developer(&self) -> bool { self.owc.get(bao1x_api::DEVELOPER_MODE).unwrap() != 0 } + + #[cfg(feature = "app-keys")] + pub fn read_app_key(&self, index: usize) -> Result<[u8; 32], xous::Error> { + use bao1x_api::common::APPLICATION; + let real_index = index + bao1x_api::common::APPLICATION.get_base(); + if real_index < APPLICATION.get_base() || real_index >= APPLICATION.get_base() + APPLICATION.len() { + return Err(xous::Error::AccessDenied); + } else { + use bao1x_api::SlotIndex; + + let slot = + SlotIndex::Data(real_index, bao1x_api::PartitionAccess::Open, bao1x_api::RwPerms::ReadWrite); + if let Ok(result) = self.slot_mgr.read(&slot) { + let mut storage = [0u8; 32]; + storage.copy_from_slice(result); + Ok(storage) + } else { + Err(xous::Error::AccessDenied) + } + } + } + + #[cfg(feature = "app-keys")] + // indices are absolute offsets + pub fn write_app_key(&self, rram: &mut Reram, index: usize, data: [u8; 32]) -> Result<(), xous::Error> { + use bao1x_api::common::APPLICATION; + let real_index = index + bao1x_api::common::APPLICATION.get_base(); + if real_index < APPLICATION.get_base() || real_index >= APPLICATION.get_base() + APPLICATION.len() { + return Err(xous::Error::AccessDenied); + } else { + use bao1x_api::SlotIndex; + + let slot = + SlotIndex::Data(real_index, bao1x_api::PartitionAccess::Open, bao1x_api::RwPerms::ReadWrite); + match self.slot_mgr.write(rram, &slot, &data) { + Ok(_) => { + // log::info!("write success: {:?} {:x?}", slot, data); + Ok(()) + } + Err(e) => { + log::warn!("Write app key error: {:?}", e); + match e { + bao1x_api::AccessError::WriteError => Err(xous::Error::HardwareError), + _ => Err(xous::Error::AccessDenied), + } + } + } + } + } }