Merge pull request #19 from huitseeker/simplify_types

Straightforward cleanups
This commit is contained in:
François Garillot
2020-07-03 17:37:28 -04:00
committed by GitHub
5 changed files with 40 additions and 50 deletions
+4 -4
View File
@@ -46,7 +46,7 @@ pub trait Group: Sized + for<'a> Mul<&'a <Self as Group>::Scalar, Output = Self>
element_bits: &GenericArray<u8, Self::ElemLen>,
) -> Result<Self, InternalPakeError>;
/// Serializes the `self` group element
fn to_bytes(&self) -> GenericArray<u8, Self::ElemLen>;
fn to_arr(&self) -> GenericArray<u8, Self::ElemLen>;
/// 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<u8, Self::ElemLen> {
fn to_arr(&self) -> GenericArray<u8, Self::ElemLen> {
let c = self.compress();
*GenericArray::from_slice(c.as_bytes())
}
type UniformBytesLen = U64;
fn hash_to_curve(uniform_bytes: &GenericArray<u8, Self::UniformBytesLen>) -> 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<u8, Self::ElemLen> {
fn to_arr(&self) -> GenericArray<u8, Self::ElemLen> {
let c = self.compress();
*GenericArray::from_slice(c.as_bytes())
}
+29 -38
View File
@@ -46,7 +46,7 @@ impl<Grp: Group> TryFrom<&[u8]> for RegisterFirstMessage<Grp> {
impl<Grp: Group> RegisterFirstMessage<Grp> {
pub fn to_bytes(&self) -> GenericArray<u8, Grp::ElemLen> {
self.alpha.to_bytes()
self.alpha.to_arr()
}
}
@@ -82,7 +82,7 @@ where
Grp: Group,
{
pub fn to_bytes(&self) -> Vec<u8> {
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<u8> {
[
&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<Grp: Group> TryFrom<&[u8]> for LoginFirstMessage<Grp> {
impl<Grp: Group> LoginFirstMessage<Grp> {
pub fn to_bytes(&self) -> Vec<u8> {
[
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<u8> {
[
&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<CS: CipherSuite> {
/// a blinding factor
pub(crate) blinding_factor: <<CS as CipherSuite>::Group as Group>::Scalar,
pub(crate) blinding_factor: <CS::Group as Group>::Scalar,
/// the client's password
password: Vec<u8>,
}
@@ -261,7 +253,7 @@ impl<CS: CipherSuite> TryFrom<&[u8]> for ClientRegistration<CS> {
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
// Check that the message is actually containing an element of the
// correct subgroup
let scalar_len = <<CS as CipherSuite>::Group as Group>::ScalarLen::to_usize();
let scalar_len = <CS::Group as Group>::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<CS: CipherSuite> TryFrom<&[u8]> for ClientRegistration<CS> {
impl<CS: CipherSuite> ClientRegistration<CS> {
pub fn to_bytes(&self) -> Vec<u8> {
let output: Vec<u8> = [
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<CS: CipherSuite> ClientRegistration<CS> {
pub fn finish<R: CryptoRng + RngCore>(
self,
r2: RegisterSecondMessage<CS::Group>,
server_s_pk: &<<CS as CipherSuite>::KeyFormat as KeyPair>::Repr,
server_s_pk: &<CS::KeyFormat as KeyPair>::Repr,
rng: &mut R,
) -> Result<ClientRegistrationFinishResult<CS::KeyFormat>, ProtocolError> {
let client_static_keypair = CS::KeyFormat::generate_random(rng)?;
@@ -424,24 +416,23 @@ impl<CS: CipherSuite> Drop for ClientLogin<CS> {
/// The state elements the server holds to record a registration
pub struct ServerRegistration<CS: CipherSuite> {
envelope: Option<Envelope>,
client_s_pk: Option<<<CS as CipherSuite>::KeyFormat as KeyPair>::Repr>,
pub(crate) oprf_key: <<CS as CipherSuite>::Group as Group>::Scalar,
client_s_pk: Option<<CS::KeyFormat as KeyPair>::Repr>,
pub(crate) oprf_key: <CS::Group as Group>::Scalar,
}
impl<CS: CipherSuite> TryFrom<&[u8]> for ServerRegistration<CS>
where
<<<CS as CipherSuite>::KeyFormat as KeyPair>::Repr as SizedBytes>::Len:
std::ops::Add<<<<CS as CipherSuite>::KeyFormat as KeyPair>::Repr as SizedBytes>::Len>,
<<CS::KeyFormat as KeyPair>::Repr as SizedBytes>::Len:
std::ops::Add<<<CS::KeyFormat as KeyPair>::Repr as SizedBytes>::Len>,
generic_array::typenum::Sum<
<<<CS as CipherSuite>::KeyFormat as KeyPair>::Repr as SizedBytes>::Len,
<<<CS as CipherSuite>::KeyFormat as KeyPair>::Repr as SizedBytes>::Len,
<<CS::KeyFormat as KeyPair>::Repr as SizedBytes>::Len,
<<CS::KeyFormat as KeyPair>::Repr as SizedBytes>::Len,
>: generic_array::ArrayLength<u8>,
{
type Error = ProtocolError;
fn try_from(server_registration_bytes: &[u8]) -> Result<Self, Self::Error> {
let key_len =
<<<CS as CipherSuite>::KeyFormat as KeyPair>::Repr as SizedBytes>::Len::to_usize();
let scalar_len = <<CS as CipherSuite>::Group as Group>::ScalarLen::to_usize();
let key_len = <<CS::KeyFormat as KeyPair>::Repr as SizedBytes>::Len::to_usize();
let scalar_len = <CS::Group as Group>::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 = <<CS as CipherSuite>::KeyFormat as KeyPair>::Repr::from_bytes(
let unchecked_client_s_pk = <CS::KeyFormat as KeyPair>::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<CS: CipherSuite> ServerRegistration<CS>
where
<<<CS as CipherSuite>::KeyFormat as KeyPair>::Repr as SizedBytes>::Len:
std::ops::Add<<<<CS as CipherSuite>::KeyFormat as KeyPair>::Repr as SizedBytes>::Len>,
<<CS::KeyFormat as KeyPair>::Repr as SizedBytes>::Len:
std::ops::Add<<<CS::KeyFormat as KeyPair>::Repr as SizedBytes>::Len>,
generic_array::typenum::Sum<
<<<CS as CipherSuite>::KeyFormat as KeyPair>::Repr as SizedBytes>::Len,
<<<CS as CipherSuite>::KeyFormat as KeyPair>::Repr as SizedBytes>::Len,
<<CS::KeyFormat as KeyPair>::Repr as SizedBytes>::Len,
<<CS::KeyFormat as KeyPair>::Repr as SizedBytes>::Len,
>: generic_array::ArrayLength<u8>,
{
pub fn to_bytes(&self) -> Vec<u8> {
@@ -594,7 +585,7 @@ pub struct ClientLogin<CS: CipherSuite> {
_key_format: PhantomData<CS::KeyFormat>,
/// A blinding factor, which is used to mask (and unmask) secret
/// information before transmission
blinding_factor: <<CS as CipherSuite>::Group as Group>::Scalar,
blinding_factor: <CS::Group as Group>::Scalar,
/// The user's password
password: Vec<u8>,
ke1_state: KE1State,
@@ -603,7 +594,7 @@ pub struct ClientLogin<CS: CipherSuite> {
impl<CS: CipherSuite> TryFrom<&[u8]> for ClientLogin<CS> {
type Error = ProtocolError;
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
let scalar_len = <<CS as CipherSuite>::Group as Group>::ScalarLen::to_usize();
let scalar_len = <CS::Group as Group>::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<CS: CipherSuite> TryFrom<&[u8]> for ClientLogin<CS> {
impl<CS: CipherSuite> ClientLogin<CS> {
pub fn to_bytes(&self) -> Vec<u8> {
let output: Vec<u8> = [
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<CS: CipherSuite> ClientLogin<CS> {
} = oprf::generate_oprf1::<R, CS::Group>(&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<CS: CipherSuite> ClientLogin<CS> {
pub fn finish<R: RngCore + CryptoRng>(
self,
l2: LoginSecondMessage<CS::Group, CS::KeyFormat>,
server_s_pk: &<<CS as CipherSuite>::KeyFormat as KeyPair>::Repr,
server_s_pk: &<CS::KeyFormat as KeyPair>::Repr,
_client_e_sk_rng: &mut R,
) -> Result<ClientLoginFinishResult, ProtocolError> {
let l2_bytes: Vec<u8> = [l2.beta.to_bytes().as_slice(), &l2.envelope.to_bytes()].concat();
let l2_bytes: Vec<u8> = [&l2.beta.to_arr()[..], &l2.envelope.to_bytes()].concat();
let password_derived_key = get_password_derived_key::<CS::Group, CS::SlowHash>(
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<u8> = [beta.to_bytes().as_slice(), &envelope.to_bytes()].concat();
let l2_component: Vec<u8> = [&beta.to_arr()[..], &envelope.to_bytes()].concat();
let (ke2_state, ke2_message) = generate_ke2::<_, CS::KeyFormat>(
rng,
+3 -3
View File
@@ -50,7 +50,7 @@ pub(crate) fn generate_oprf3<G: Group>(
blinding_factor: &G::Scalar,
) -> Result<GenericArray<u8, <Sha256 as Digest>::OutputSize>, InternalPakeError> {
let unblinded = point * &G::scalar_invert(&blinding_factor);
let ikm: Vec<u8> = [&unblinded.to_bytes(), input].concat();
let ikm: Vec<u8> = [&unblinded.to_arr()[..], input].concat();
let (prk, _) = Hkdf::<Sha256>::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<u8> = [res.to_bytes().as_slice(), &input].concat();
let ikm: Vec<u8> = [&res.to_arr()[..], &input].concat();
let (prk, _) = Hkdf::<Sha256>::extract(None, &ikm);
prk
@@ -126,7 +126,7 @@ mod tests {
let point = RistrettoPoint::from_uniform_bytes(&bits);
let mut ikm: Vec<u8> = Vec::new();
ikm.extend_from_slice(&point.to_bytes());
ikm.extend_from_slice(&point.to_arr());
ikm.extend_from_slice(&input);
let (prk, _) = Hkdf::<Sha256>::extract(None, &ikm);
+2 -3
View File
@@ -247,14 +247,13 @@ fn generate_parameters<CS: CipherSuite>() -> 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::<CS>::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<u8> = Vec::new();
+2 -2
View File
@@ -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::<RistrettoPoint>::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::<RistrettoPoint>::try_from(&message[..]).unwrap();