diff --git a/src/messages.rs b/src/messages.rs index fe97e93..090a3b3 100644 --- a/src/messages.rs +++ b/src/messages.rs @@ -13,7 +13,6 @@ use crate::{ PakeError, ProtocolError, }, group::Group, - hash::Hash, key_exchange::traits::{KeyExchange, ToBytes}, keypair::{Key, KeyPair, SizedBytesExt}, serialization::{serialize, tokenize}, @@ -21,32 +20,31 @@ use crate::{ use generic_array::{typenum::Unsigned, GenericArray}; use generic_bytes::SizedBytes; use std::convert::TryFrom; -use std::marker::PhantomData; // Messages // ========= /// The message sent by the client to the server, to initiate registration -pub struct RegistrationRequest { +pub struct RegistrationRequest { /// blinded password information - pub(crate) alpha: Grp, + pub(crate) alpha: CS::Group, } -impl TryFrom<&[u8]> for RegistrationRequest { +impl TryFrom<&[u8]> for RegistrationRequest { type Error = ProtocolError; fn try_from(first_message_bytes: &[u8]) -> Result { - let elem_len = Grp::ElemLen::to_usize(); + let elem_len = ::ElemLen::to_usize(); let checked_slice = check_slice_size(first_message_bytes, elem_len, "first_message_bytes")?; // Check that the message is actually containing an element of the // correct subgroup let arr = GenericArray::from_slice(&checked_slice[checked_slice.len() - elem_len..]); - let alpha = Grp::from_element_slice(arr)?; + let alpha = CS::Group::from_element_slice(arr)?; Ok(Self { alpha }) } } -impl RegistrationRequest { +impl RegistrationRequest { /// Byte representation for the registration request pub fn to_bytes(&self) -> Vec { self.alpha.to_arr().to_vec() @@ -59,39 +57,36 @@ impl RegistrationRequest { /// Deserialization from bytes pub fn deserialize(input: &[u8]) -> Result { - let checked_slice = - check_slice_size(&input, Grp::ElemLen::to_usize(), "first_message_bytes")?; + let elem_len = ::ElemLen::to_usize(); + let checked_slice = check_slice_size(&input, elem_len, "first_message_bytes")?; // Check that the message is actually containing an element of the // correct subgroup let arr = GenericArray::from_slice(checked_slice); - let alpha = Grp::from_element_slice(arr)?; + let alpha = CS::Group::from_element_slice(arr)?; Ok(Self { alpha }) } } /// The answer sent by the server to the user, upon reception of the /// registration attempt -pub struct RegistrationResponse { +pub struct RegistrationResponse { /// The server's oprf output - pub(crate) beta: Grp, + pub(crate) beta: CS::Group, /// Server's static public key pub(crate) server_s_pk: Vec, } -impl TryFrom<&[u8]> for RegistrationResponse -where - Grp: Group, -{ +impl TryFrom<&[u8]> for RegistrationResponse { type Error = ProtocolError; fn try_from(bytes: &[u8]) -> Result { - let elem_len = Grp::ElemLen::to_usize(); + let elem_len = ::ElemLen::to_usize(); let checked_slice = check_slice_size_atleast(bytes, elem_len, "second_message_bytes")?; // Check that the message is actually containing an element of the // correct subgroup let arr = GenericArray::from_slice(&checked_slice[..elem_len]); - let beta = Grp::from_element_slice(arr)?; + let beta = CS::Group::from_element_slice(arr)?; // FIXME check public key bytes let server_s_pk = checked_slice[elem_len..].to_vec(); @@ -100,10 +95,7 @@ where } } -impl RegistrationResponse -where - Grp: Group, -{ +impl RegistrationResponse { /// Byte representation for the registration response message. This does not /// include the envelope credentials format pub fn to_bytes(&self) -> Vec { @@ -120,15 +112,15 @@ where /// Deserialization from bytes pub fn deserialize(input: &[u8]) -> Result { - let checked_slice = - check_slice_size_atleast(&input, Grp::ElemLen::to_usize(), "second_message_bytes")?; + let elem_len = ::ElemLen::to_usize(); + let checked_slice = check_slice_size_atleast(&input, elem_len, "second_message_bytes")?; // Check that the message is actually containing an element of the // correct subgroup - let arr = GenericArray::from_slice(&checked_slice[..Grp::ElemLen::to_usize()]); - let beta = Grp::from_element_slice(arr)?; + let arr = GenericArray::from_slice(&checked_slice[..elem_len]); + let beta = CS::Group::from_element_slice(arr)?; - let (server_s_pk, remainder) = tokenize(&checked_slice[Grp::ElemLen::to_usize()..], 2)?; + let (server_s_pk, remainder) = tokenize(&checked_slice[elem_len..], 2)?; if !remainder.is_empty() { return Err(PakeError::SerializationError.into()); } @@ -139,38 +131,36 @@ where /// The final message from the client, containing sealed cryptographic /// identifiers -pub struct RegistrationUpload { +pub struct RegistrationUpload { /// The "envelope" generated by the user, containing sealed /// cryptographic identifiers - pub(crate) envelope: Envelope, + pub(crate) envelope: Envelope, /// The user's public key pub(crate) client_s_pk: Key, - pub(crate) _g: PhantomData, } -impl TryFrom<&[u8]> for RegistrationUpload { +impl TryFrom<&[u8]> for RegistrationUpload { type Error = ProtocolError; fn try_from(third_message_bytes: &[u8]) -> Result { let key_len = ::Len::to_usize(); - let envelope_size = key_len + Envelope::::additional_size(); + let envelope_size = key_len + Envelope::::additional_size(); let checked_bytes = check_slice_size( third_message_bytes, envelope_size + key_len, "third_message", )?; let unchecked_client_s_pk = Key::from_bytes(&checked_bytes[envelope_size..])?; - let client_s_pk = KeyPair::::check_public_key(unchecked_client_s_pk)?; + let client_s_pk = KeyPair::::check_public_key(unchecked_client_s_pk)?; Ok(Self { - envelope: Envelope::::from_bytes(&checked_bytes[..envelope_size])?, + envelope: Envelope::::from_bytes(&checked_bytes[..envelope_size])?, client_s_pk, - _g: PhantomData, }) } } -impl RegistrationUpload { +impl RegistrationUpload { /// Serialization into bytes pub fn serialize(&self) -> Vec { let mut message: Vec = Vec::new(); @@ -182,7 +172,7 @@ impl RegistrationUpload { /// Deserialization from bytes pub fn deserialize(input: &[u8]) -> Result { let (client_s_pk, remainder) = tokenize(&input, 2)?; - let (envelope, remainder) = Envelope::::deserialize(&remainder)?; + let (envelope, remainder) = Envelope::::deserialize(&remainder)?; if !remainder.is_empty() { return Err(PakeError::SerializationError.into()); @@ -190,8 +180,7 @@ impl RegistrationUpload { Ok(Self { envelope, - client_s_pk: KeyPair::::check_public_key(Key::from_bytes(&client_s_pk)?)?, - _g: PhantomData, + client_s_pk: KeyPair::::check_public_key(Key::from_bytes(&client_s_pk)?)?, }) } } diff --git a/src/opaque.rs b/src/opaque.rs index 5b053e7..9f41bd2 100644 --- a/src/opaque.rs +++ b/src/opaque.rs @@ -96,7 +96,7 @@ impl Default for ClientRegistrationFinishParameters { /// Contains the fields that are returned by a client registration start pub struct ClientRegistrationStartResult { /// The registration request message to be sent to the server - pub message: RegistrationRequest, + pub message: RegistrationRequest, /// The client state that must be persisted in order to complete registration pub state: ClientRegistration, } @@ -132,19 +132,18 @@ impl ClientRegistration { let (token, alpha) = oprf::blind::(&password, blinding_factor_rng)?; Ok(ClientRegistrationStartResult { - message: RegistrationRequest:: { alpha }, + message: RegistrationRequest:: { alpha }, state: Self { token }, }) } } /// Contains the fields that are returned by a client registration finish -pub struct ClientRegistrationFinishResult { +pub struct ClientRegistrationFinishResult { /// The registration upload message to be sent to the server - pub message: RegistrationUpload, + pub message: RegistrationUpload, /// The export key output by client registration - pub export_key: GenericArray::OutputSize>, - _g: PhantomData, + pub export_key: GenericArray::OutputSize>, } impl ClientRegistration { @@ -182,9 +181,9 @@ impl ClientRegistration { pub fn finish( self, rng: &mut R, - r2: RegistrationResponse, + r2: RegistrationResponse, params: ClientRegistrationFinishParameters, - ) -> Result, ProtocolError> { + ) -> Result, ProtocolError> { let optional_ids = match params { ClientRegistrationFinishParameters::WithIdentifiers(id_u, id_s) => Some((id_u, id_s)), ClientRegistrationFinishParameters::Default => None, @@ -206,10 +205,8 @@ impl ClientRegistration { message: RegistrationUpload { envelope, client_s_pk: client_static_keypair.public().clone(), - _g: PhantomData, }, export_key, - _g: PhantomData, }) } } @@ -245,7 +242,7 @@ impl Drop for ClientLogin { /// Contains the fields that are returned by a server registration start pub struct ServerRegistrationStartResult { /// The registration resposne message to send to the client - pub message: RegistrationResponse, + pub message: RegistrationResponse, /// The state that the server must keep in order to complete registration pub state: ServerRegistration, } @@ -336,7 +333,7 @@ impl ServerRegistration { /// ``` pub fn start( rng: &mut R, - message: RegistrationRequest, + message: RegistrationRequest, server_s_pk: &Key, ) -> Result, ProtocolError> { // RFC: generate oprf_key (salt) and v_u = g^oprf_key @@ -388,10 +385,7 @@ impl ServerRegistration { /// let client_record = server_registration_start_result.state.finish(client_registration_finish_result.message)?; /// # Ok::<(), ProtocolError>(()) /// ``` - pub fn finish( - self, - message: RegistrationUpload, - ) -> Result { + pub fn finish(self, message: RegistrationUpload) -> Result { Ok(Self { envelope: Some(message.envelope), client_s_pk: Some(message.client_s_pk), diff --git a/src/serialization/tests.rs b/src/serialization/tests.rs index ba2972e..22bb3c9 100644 --- a/src/serialization/tests.rs +++ b/src/serialization/tests.rs @@ -103,7 +103,7 @@ fn register_first_message_roundtrip() { let mut input = Vec::new(); input.extend_from_slice(pt_bytes.as_slice()); - let r1 = RegistrationRequest::::deserialize(input.as_slice()).unwrap(); + let r1 = RegistrationRequest::::deserialize(input.as_slice()).unwrap(); let r1_bytes = r1.serialize(); assert_eq!(input, r1_bytes); } @@ -123,7 +123,7 @@ fn register_second_message_roundtrip() { input.extend_from_slice(&pubkey_length.to_be_bytes()[std::mem::size_of::() - 2..]); input.extend_from_slice(&pubkey_bytes.as_slice()); - let r2 = RegistrationResponse::::deserialize(input.as_slice()).unwrap(); + let r2 = RegistrationResponse::::deserialize(input.as_slice()).unwrap(); let r2_bytes = r2.serialize(); assert_eq!(input, r2_bytes); } @@ -157,7 +157,7 @@ fn register_third_message_roundtrip() { input.extend_from_slice(&pubkey_bytes[..]); input.extend_from_slice(&envelope_bytes); - let r3 = RegistrationUpload::::deserialize(&input[..]).unwrap(); + let r3 = RegistrationUpload::::deserialize(&input[..]).unwrap(); let r3_bytes = r3.serialize(); assert_eq!(input, r3_bytes); } @@ -359,17 +359,17 @@ fn test_i2osp_os2ip(bytes in vec(any::(), 0..std::mem::size_of::())) #[test] fn test_nocrash_register_first_message(bytes in vec(any::(), 0..200)) { - RegistrationRequest::::try_from(&bytes[..]).map_or(true, |_| true); + RegistrationRequest::::try_from(&bytes[..]).map_or(true, |_| true); } #[test] fn test_nocrash_register_second_message(bytes in vec(any::(), 0..200)) { - RegistrationResponse::::try_from(&bytes[..]).map_or(true, |_| true); + RegistrationResponse::::try_from(&bytes[..]).map_or(true, |_| true); } #[test] fn test_nocrash_register_third_message(bytes in vec(any::(), 0..200)) { - RegistrationUpload::::try_from(&bytes[..]).map_or(true, |_| true); + RegistrationUpload::::try_from(&bytes[..]).map_or(true, |_| true); } #[test]