diff --git a/src/group.rs b/src/group.rs index 1aa4a77..44547d2 100644 --- a/src/group.rs +++ b/src/group.rs @@ -46,7 +46,7 @@ pub trait Group: Sized + for<'a> Mul<&'a ::Scalar, Output = Self> element_bits: &GenericArray, ) -> Result; /// Serializes the `self` group element - fn to_bytes(&self) -> GenericArray; + fn to_arr(&self) -> GenericArray; /// Hashes points presumed to be uniformly random to the curve. The /// impl is allowed to perform additional hashes if it needs to, but this @@ -87,14 +87,14 @@ impl Group for RistrettoPoint { .ok_or_else(|| InternalPakeError::PointError) } // serialization of a group element - fn to_bytes(&self) -> GenericArray { + fn to_arr(&self) -> GenericArray { let c = self.compress(); *GenericArray::from_slice(c.as_bytes()) } type UniformBytesLen = U64; fn hash_to_curve(uniform_bytes: &GenericArray) -> Self { - // This is because RistrettoPoint is on an obsolete sha2 version + // This is because RistrettoPoint is on an obsolete sha2 version, see https://github.com/dalek-cryptography/curve25519-dalek/pull/327 let mut bits = [0u8; 64]; let mut hasher = sha2::Sha512::new(); hasher.update(uniform_bytes); @@ -135,7 +135,7 @@ impl Group for EdwardsPoint { .ok_or_else(|| InternalPakeError::PointError) } // serialization of a group element - fn to_bytes(&self) -> GenericArray { + fn to_arr(&self) -> GenericArray { let c = self.compress(); *GenericArray::from_slice(c.as_bytes()) } diff --git a/src/opaque.rs b/src/opaque.rs index 6600407..5b306cc 100644 --- a/src/opaque.rs +++ b/src/opaque.rs @@ -46,7 +46,7 @@ impl TryFrom<&[u8]> for RegisterFirstMessage { impl RegisterFirstMessage { pub fn to_bytes(&self) -> GenericArray { - self.alpha.to_bytes() + self.alpha.to_arr() } } @@ -82,7 +82,7 @@ where Grp: Group, { pub fn to_bytes(&self) -> Vec { - self.beta.to_bytes().to_vec() + self.beta.to_arr().to_vec() } } @@ -101,11 +101,7 @@ where KeyFormat: KeyPair, { pub fn to_bytes(&self) -> Vec { - [ - &self.envelope.to_bytes(), - self.client_s_pk.to_arr().as_slice(), - ] - .concat() + [&self.envelope.to_bytes(), &self.client_s_pk.to_arr()[..]].concat() } } @@ -156,11 +152,7 @@ impl TryFrom<&[u8]> for LoginFirstMessage { impl LoginFirstMessage { pub fn to_bytes(&self) -> Vec { - [ - self.alpha.to_bytes().as_slice(), - &self.ke1_message.to_bytes(), - ] - .concat() + [&self.alpha.to_arr()[..], &self.ke1_message.to_bytes()].concat() } } @@ -182,7 +174,7 @@ where { pub fn to_bytes(&self) -> Vec { [ - &self.beta.to_bytes()[..], + &self.beta.to_arr()[..], &self.envelope.to_bytes()[..], &self.ke2_message.to_bytes()[..], ] @@ -251,7 +243,7 @@ impl LoginThirdMessage { /// The state elements the client holds to register itself pub struct ClientRegistration { /// a blinding factor - pub(crate) blinding_factor: <::Group as Group>::Scalar, + pub(crate) blinding_factor: ::Scalar, /// the client's password password: Vec, } @@ -261,7 +253,7 @@ impl TryFrom<&[u8]> for ClientRegistration { fn try_from(bytes: &[u8]) -> Result { // Check that the message is actually containing an element of the // correct subgroup - let scalar_len = <::Group as Group>::ScalarLen::to_usize(); + let scalar_len = ::ScalarLen::to_usize(); let blinding_factor_bytes = GenericArray::from_slice(&bytes[..scalar_len]); let blinding_factor = CS::Group::from_scalar_slice(blinding_factor_bytes)?; let password = bytes[scalar_len..].to_vec(); @@ -275,7 +267,7 @@ impl TryFrom<&[u8]> for ClientRegistration { impl ClientRegistration { pub fn to_bytes(&self) -> Vec { let output: Vec = [ - CS::Group::scalar_as_bytes(&self.blinding_factor).as_slice(), + &CS::Group::scalar_as_bytes(&self.blinding_factor)[..], &self.password, ] .concat(); @@ -365,7 +357,7 @@ impl ClientRegistration { pub fn finish( self, r2: RegisterSecondMessage, - server_s_pk: &<::KeyFormat as KeyPair>::Repr, + server_s_pk: &::Repr, rng: &mut R, ) -> Result, ProtocolError> { let client_static_keypair = CS::KeyFormat::generate_random(rng)?; @@ -424,24 +416,23 @@ impl Drop for ClientLogin { /// The state elements the server holds to record a registration pub struct ServerRegistration { envelope: Option, - client_s_pk: Option<<::KeyFormat as KeyPair>::Repr>, - pub(crate) oprf_key: <::Group as Group>::Scalar, + client_s_pk: Option<::Repr>, + pub(crate) oprf_key: ::Scalar, } impl TryFrom<&[u8]> for ServerRegistration where - <<::KeyFormat as KeyPair>::Repr as SizedBytes>::Len: - std::ops::Add<<<::KeyFormat as KeyPair>::Repr as SizedBytes>::Len>, + <::Repr as SizedBytes>::Len: + std::ops::Add<<::Repr as SizedBytes>::Len>, generic_array::typenum::Sum< - <<::KeyFormat as KeyPair>::Repr as SizedBytes>::Len, - <<::KeyFormat as KeyPair>::Repr as SizedBytes>::Len, + <::Repr as SizedBytes>::Len, + <::Repr as SizedBytes>::Len, >: generic_array::ArrayLength, { type Error = ProtocolError; fn try_from(server_registration_bytes: &[u8]) -> Result { - let key_len = - <<::KeyFormat as KeyPair>::Repr as SizedBytes>::Len::to_usize(); - let scalar_len = <::Group as Group>::ScalarLen::to_usize(); + let key_len = <::Repr as SizedBytes>::Len::to_usize(); + let scalar_len = ::ScalarLen::to_usize(); let envelope_size = key_len + Envelope::additional_size(); if server_registration_bytes.len() == scalar_len { @@ -461,7 +452,7 @@ where )?; let oprf_key_bytes = GenericArray::from_slice(&checked_bytes[..scalar_len]); let oprf_key = CS::Group::from_scalar_slice(oprf_key_bytes)?; - let unchecked_client_s_pk = <::KeyFormat as KeyPair>::Repr::from_bytes( + let unchecked_client_s_pk = ::Repr::from_bytes( &checked_bytes[scalar_len..scalar_len + key_len], )?; let client_s_pk = CS::KeyFormat::check_public_key(unchecked_client_s_pk)?; @@ -477,11 +468,11 @@ where impl ServerRegistration where - <<::KeyFormat as KeyPair>::Repr as SizedBytes>::Len: - std::ops::Add<<<::KeyFormat as KeyPair>::Repr as SizedBytes>::Len>, + <::Repr as SizedBytes>::Len: + std::ops::Add<<::Repr as SizedBytes>::Len>, generic_array::typenum::Sum< - <<::KeyFormat as KeyPair>::Repr as SizedBytes>::Len, - <<::KeyFormat as KeyPair>::Repr as SizedBytes>::Len, + <::Repr as SizedBytes>::Len, + <::Repr as SizedBytes>::Len, >: generic_array::ArrayLength, { pub fn to_bytes(&self) -> Vec { @@ -594,7 +585,7 @@ pub struct ClientLogin { _key_format: PhantomData, /// A blinding factor, which is used to mask (and unmask) secret /// information before transmission - blinding_factor: <::Group as Group>::Scalar, + blinding_factor: ::Scalar, /// The user's password password: Vec, ke1_state: KE1State, @@ -603,7 +594,7 @@ pub struct ClientLogin { impl TryFrom<&[u8]> for ClientLogin { type Error = ProtocolError; fn try_from(bytes: &[u8]) -> Result { - let scalar_len = <::Group as Group>::ScalarLen::to_usize(); + let scalar_len = ::ScalarLen::to_usize(); let blinding_factor_bytes = GenericArray::from_slice(&bytes[..scalar_len]); let blinding_factor = CS::Group::from_scalar_slice(blinding_factor_bytes)?; let ke1_state = KE1State::try_from(&bytes[scalar_len..scalar_len + KE1_STATE_LEN])?; @@ -620,7 +611,7 @@ impl TryFrom<&[u8]> for ClientLogin { impl ClientLogin { pub fn to_bytes(&self) -> Vec { let output: Vec = [ - CS::Group::scalar_as_bytes(&self.blinding_factor).as_slice(), + &CS::Group::scalar_as_bytes(&self.blinding_factor)[..], &self.ke1_state.to_bytes(), &self.password, ] @@ -665,7 +656,7 @@ impl ClientLogin { } = oprf::generate_oprf1::(&password, pepper, rng)?; let (ke1_state, ke1_message) = - generate_ke1::<_, CS::KeyFormat>(alpha.to_bytes().to_vec(), rng)?; + generate_ke1::<_, CS::KeyFormat>(alpha.to_arr().to_vec(), rng)?; let l1 = LoginFirstMessage { alpha, ke1_message }; @@ -716,10 +707,10 @@ impl ClientLogin { pub fn finish( self, l2: LoginSecondMessage, - server_s_pk: &<::KeyFormat as KeyPair>::Repr, + server_s_pk: &::Repr, _client_e_sk_rng: &mut R, ) -> Result { - let l2_bytes: Vec = [l2.beta.to_bytes().as_slice(), &l2.envelope.to_bytes()].concat(); + let l2_bytes: Vec = [&l2.beta.to_arr()[..], &l2.envelope.to_bytes()].concat(); let password_derived_key = get_password_derived_key::( self.password.clone(), @@ -822,7 +813,7 @@ impl ServerLogin { .ok_or(InternalPakeError::SealError)?; let envelope = password_file.envelope.ok_or(InternalPakeError::SealError)?; - let l2_component: Vec = [beta.to_bytes().as_slice(), &envelope.to_bytes()].concat(); + let l2_component: Vec = [&beta.to_arr()[..], &envelope.to_bytes()].concat(); let (ke2_state, ke2_message) = generate_ke2::<_, CS::KeyFormat>( rng, diff --git a/src/oprf.rs b/src/oprf.rs index 6e5be4b..98c066d 100644 --- a/src/oprf.rs +++ b/src/oprf.rs @@ -50,7 +50,7 @@ pub(crate) fn generate_oprf3( blinding_factor: &G::Scalar, ) -> Result::OutputSize>, InternalPakeError> { let unblinded = point * &G::scalar_invert(&blinding_factor); - let ikm: Vec = [&unblinded.to_bytes(), input].concat(); + let ikm: Vec = [&unblinded.to_arr()[..], input].concat(); let (prk, _) = Hkdf::::extract(None, &ikm); Ok(prk) } @@ -77,7 +77,7 @@ mod tests { let scalar = RistrettoPoint::from_scalar_slice(GenericArray::from_slice(&oprf_key[..])).unwrap(); let res = point * scalar; - let ikm: Vec = [res.to_bytes().as_slice(), &input].concat(); + let ikm: Vec = [&res.to_arr()[..], &input].concat(); let (prk, _) = Hkdf::::extract(None, &ikm); prk @@ -126,7 +126,7 @@ mod tests { let point = RistrettoPoint::from_uniform_bytes(&bits); let mut ikm: Vec = Vec::new(); - ikm.extend_from_slice(&point.to_bytes()); + ikm.extend_from_slice(&point.to_arr()); ikm.extend_from_slice(&input); let (prk, _) = Hkdf::::extract(None, &ikm); diff --git a/src/tests/opaque_ke_test.rs b/src/tests/opaque_ke_test.rs index fc13a3f..60c81e9 100644 --- a/src/tests/opaque_ke_test.rs +++ b/src/tests/opaque_ke_test.rs @@ -247,14 +247,13 @@ fn generate_parameters() -> TestVectorParameters { ) .unwrap(); let r1_bytes = r1.to_bytes().to_vec(); - let blinding_factor_bytes = - CS::Group::scalar_as_bytes(&client_registration.blinding_factor).clone(); + let blinding_factor_bytes = *CS::Group::scalar_as_bytes(&client_registration.blinding_factor); let client_registration_state = client_registration.to_bytes().to_vec(); let mut oprf_key_rng = CycleRng::new(oprf_key_raw.to_vec()); let (r2, server_registration) = ServerRegistration::::start(r1, &mut oprf_key_rng).unwrap(); let r2_bytes = r2.to_bytes().to_vec(); - let oprf_key_bytes = CS::Group::scalar_as_bytes(&server_registration.oprf_key).clone(); + let oprf_key_bytes = *CS::Group::scalar_as_bytes(&server_registration.oprf_key); let server_registration_state = server_registration.to_bytes().to_vec(); let mut client_s_sk_and_nonce: Vec = Vec::new(); diff --git a/src/tests/serialization.rs b/src/tests/serialization.rs index 7e51d22..0a755fe 100644 --- a/src/tests/serialization.rs +++ b/src/tests/serialization.rs @@ -85,7 +85,7 @@ fn server_registration_roundtrip() { #[test] fn register_first_message_roundtrip() { let pt = random_ristretto_point(); - let pt_bytes = pt.to_bytes(); + let pt_bytes = pt.to_arr(); let r1 = RegisterFirstMessage::::try_from(pt_bytes.as_slice()).unwrap(); let r1_bytes = r1.to_bytes(); assert_eq!(pt_bytes, r1_bytes); @@ -94,7 +94,7 @@ fn register_first_message_roundtrip() { #[test] fn register_second_message_roundtrip() { let pt = random_ristretto_point(); - let pt_bytes = pt.to_bytes(); + let pt_bytes = pt.to_arr(); let message = pt_bytes.to_vec(); let r2 = RegisterSecondMessage::::try_from(&message[..]).unwrap();