From e188ab2552524d705941d9bf904c95c949bda44e Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Tue, 18 Jun 2024 09:16:12 +0200 Subject: [PATCH] Use references for ctap1 --- CHANGELOG.md | 2 +- src/authenticator.rs | 2 +- src/ctap1.rs | 51 +++++++++++++++++++++++--------------------- 3 files changed, 29 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8d6230..6c2e338 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Replace `cose` module with `cosey` dependency ([#36][]) - Mark `get_assertion::{ExtensionsInput, ExtensionsOutput}` and `make_credential::Extensions` as non-exhaustive and implement `Default` - Mark CTAP2 request and response types as non-exhaustive where possible -- Use references for `AuthenticatorData` and `get_assertion::Request` where possible +- Use references where possible [#8]: https://github.com/trussed-dev/ctap-types/pull/8 [#9]: https://github.com/solokeys/ctap-types/issues/9 diff --git a/src/authenticator.rs b/src/authenticator.rs index 4e26da8..06dd272 100644 --- a/src/authenticator.rs +++ b/src/authenticator.rs @@ -12,7 +12,7 @@ pub use ctap2::Authenticator as Ctap2Authenticator; // - second is 10456 bytes #[allow(clippy::large_enum_variant)] pub enum Request<'a> { - Ctap1(ctap1::Request), + Ctap1(ctap1::Request<'a>), Ctap2(ctap2::Request<'a>), } diff --git a/src/ctap1.rs b/src/ctap1.rs index 19e7ef2..ede7de1 100644 --- a/src/ctap1.rs +++ b/src/ctap1.rs @@ -13,11 +13,11 @@ pub mod authenticate { use super::{Bytes, ControlByte}; #[derive(Clone, Debug, Eq, PartialEq)] - pub struct Request { + pub struct Request<'a> { pub control_byte: ControlByte, - pub challenge: Bytes<32>, - pub app_id: Bytes<32>, - pub key_handle: Bytes<255>, + pub challenge: &'a [u8], + pub app_id: &'a [u8], + pub key_handle: &'a [u8], } #[derive(Clone, Debug, Eq, PartialEq)] @@ -32,9 +32,9 @@ pub mod register { use super::Bytes; #[derive(Clone, Debug, Eq, PartialEq)] - pub struct Request { - pub challenge: Bytes<32>, - pub app_id: Bytes<32>, + pub struct Request<'a> { + pub challenge: &'a [u8], + pub app_id: &'a [u8], } #[derive(Clone, Debug, Eq, PartialEq)] @@ -112,9 +112,9 @@ impl TryFrom for ControlByte { pub type Result = core::result::Result; /// Type alias for convenience. -pub type Register = register::Request; +pub type Register<'a> = register::Request<'a>; /// Type alias for convenience. -pub type Authenticate = authenticate::Request; +pub type Authenticate<'a> = authenticate::Request<'a>; /// Type alias for convenience. pub type RegisterResponse = register::Response; @@ -124,9 +124,9 @@ pub type AuthenticateResponse = authenticate::Response; #[derive(Clone, Debug, Eq, PartialEq)] #[allow(clippy::large_enum_variant)] /// Enum of all CTAP1 requests. -pub enum Request { - Register(register::Request), - Authenticate(authenticate::Request), +pub enum Request<'a> { + Register(register::Request<'a>), + Authenticate(authenticate::Request<'a>), Version, } @@ -165,10 +165,10 @@ impl Response { } } -impl TryFrom<&iso7816::Command> for Request { +impl<'a, const S: usize> TryFrom<&'a iso7816::Command> for Request<'a> { type Error = Error; #[inline(never)] - fn try_from(apdu: &iso7816::Command) -> Result { + fn try_from(apdu: &'a iso7816::Command) -> Result { let cla = apdu.class().into_inner(); let ins = match apdu.instruction() { iso7816::Instruction::Unknown(ins) => ins, @@ -196,8 +196,8 @@ impl TryFrom<&iso7816::Command> for Request { return Err(Error::IncorrectDataParameter); } Ok(Request::Register(Register { - challenge: Bytes::from_slice(&request[..32]).unwrap(), - app_id: Bytes::from_slice(&request[32..]).unwrap(), + challenge: &request[..32], + app_id: &request[32..], })) } @@ -213,9 +213,9 @@ impl TryFrom<&iso7816::Command> for Request { } Ok(Request::Authenticate(Authenticate { control_byte, - challenge: Bytes::from_slice(&request[..32]).unwrap(), - app_id: Bytes::from_slice(&request[32..64]).unwrap(), - key_handle: Bytes::from_slice(&request[65..]).unwrap(), + challenge: &request[..32], + app_id: &request[32..64], + key_handle: &request[65..], })) } @@ -233,16 +233,19 @@ impl TryFrom<&iso7816::Command> for Request { /// [`Response`]. pub trait Authenticator { /// Register a U2F credential. - fn register(&mut self, request: ®ister::Request) -> Result; + fn register(&mut self, request: ®ister::Request<'_>) -> Result; /// Authenticate with a U2F credential. - fn authenticate(&mut self, request: &authenticate::Request) -> Result; + fn authenticate( + &mut self, + request: &authenticate::Request<'_>, + ) -> Result; /// Supported U2F version. fn version() -> [u8; 6] { *b"U2F_V2" } #[inline(never)] - fn call_ctap1(&mut self, request: &Request) -> Result { + fn call_ctap1(&mut self, request: &Request<'_>) -> Result { match request { Request::Register(reg) => { debug_now!("CTAP1.REG"); @@ -257,9 +260,9 @@ pub trait Authenticator { } } -impl crate::Rpc for A { +impl crate::Rpc, Response> for A { /// Dispatches the enum of possible requests into the appropriate trait method. - fn call(&mut self, request: &Request) -> Result { + fn call(&mut self, request: &Request<'_>) -> Result { self.call_ctap1(request) } }