Making message type parameters set to CipherSuite (#140)

This commit is contained in:
Kevin Lewi
2021-02-17 02:13:03 -08:00
committed by GitHub
parent e4636e0a97
commit 7f84984f61
3 changed files with 45 additions and 62 deletions
+29 -40
View File
@@ -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<Grp> {
pub struct RegistrationRequest<CS: CipherSuite> {
/// blinded password information
pub(crate) alpha: Grp,
pub(crate) alpha: CS::Group,
}
impl<Grp: Group> TryFrom<&[u8]> for RegistrationRequest<Grp> {
impl<CS: CipherSuite> TryFrom<&[u8]> for RegistrationRequest<CS> {
type Error = ProtocolError;
fn try_from(first_message_bytes: &[u8]) -> Result<Self, Self::Error> {
let elem_len = Grp::ElemLen::to_usize();
let elem_len = <CS::Group as Group>::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<Grp: Group> RegistrationRequest<Grp> {
impl<CS: CipherSuite> RegistrationRequest<CS> {
/// Byte representation for the registration request
pub fn to_bytes(&self) -> Vec<u8> {
self.alpha.to_arr().to_vec()
@@ -59,39 +57,36 @@ impl<Grp: Group> RegistrationRequest<Grp> {
/// Deserialization from bytes
pub fn deserialize(input: &[u8]) -> Result<Self, ProtocolError> {
let checked_slice =
check_slice_size(&input, Grp::ElemLen::to_usize(), "first_message_bytes")?;
let elem_len = <CS::Group as Group>::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<Grp> {
pub struct RegistrationResponse<CS: CipherSuite> {
/// 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<u8>,
}
impl<Grp> TryFrom<&[u8]> for RegistrationResponse<Grp>
where
Grp: Group,
{
impl<CS: CipherSuite> TryFrom<&[u8]> for RegistrationResponse<CS> {
type Error = ProtocolError;
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
let elem_len = Grp::ElemLen::to_usize();
let elem_len = <CS::Group as Group>::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<Grp> RegistrationResponse<Grp>
where
Grp: Group,
{
impl<CS: CipherSuite> RegistrationResponse<CS> {
/// Byte representation for the registration response message. This does not
/// include the envelope credentials format
pub fn to_bytes(&self) -> Vec<u8> {
@@ -120,15 +112,15 @@ where
/// Deserialization from bytes
pub fn deserialize(input: &[u8]) -> Result<Self, ProtocolError> {
let checked_slice =
check_slice_size_atleast(&input, Grp::ElemLen::to_usize(), "second_message_bytes")?;
let elem_len = <CS::Group as Group>::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<D: Hash, G: Group> {
pub struct RegistrationUpload<CS: CipherSuite> {
/// The "envelope" generated by the user, containing sealed
/// cryptographic identifiers
pub(crate) envelope: Envelope<D>,
pub(crate) envelope: Envelope<CS::Hash>,
/// The user's public key
pub(crate) client_s_pk: Key,
pub(crate) _g: PhantomData<G>,
}
impl<D: Hash, G: Group> TryFrom<&[u8]> for RegistrationUpload<D, G> {
impl<CS: CipherSuite> TryFrom<&[u8]> for RegistrationUpload<CS> {
type Error = ProtocolError;
fn try_from(third_message_bytes: &[u8]) -> Result<Self, Self::Error> {
let key_len = <Key as SizedBytes>::Len::to_usize();
let envelope_size = key_len + Envelope::<D>::additional_size();
let envelope_size = key_len + Envelope::<CS::Hash>::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::<G>::check_public_key(unchecked_client_s_pk)?;
let client_s_pk = KeyPair::<CS::Group>::check_public_key(unchecked_client_s_pk)?;
Ok(Self {
envelope: Envelope::<D>::from_bytes(&checked_bytes[..envelope_size])?,
envelope: Envelope::<CS::Hash>::from_bytes(&checked_bytes[..envelope_size])?,
client_s_pk,
_g: PhantomData,
})
}
}
impl<D: Hash, G: Group> RegistrationUpload<D, G> {
impl<CS: CipherSuite> RegistrationUpload<CS> {
/// Serialization into bytes
pub fn serialize(&self) -> Vec<u8> {
let mut message: Vec<u8> = Vec::new();
@@ -182,7 +172,7 @@ impl<D: Hash, G: Group> RegistrationUpload<D, G> {
/// Deserialization from bytes
pub fn deserialize(input: &[u8]) -> Result<Self, ProtocolError> {
let (client_s_pk, remainder) = tokenize(&input, 2)?;
let (envelope, remainder) = Envelope::<D>::deserialize(&remainder)?;
let (envelope, remainder) = Envelope::<CS::Hash>::deserialize(&remainder)?;
if !remainder.is_empty() {
return Err(PakeError::SerializationError.into());
@@ -190,8 +180,7 @@ impl<D: Hash, G: Group> RegistrationUpload<D, G> {
Ok(Self {
envelope,
client_s_pk: KeyPair::<G>::check_public_key(Key::from_bytes(&client_s_pk)?)?,
_g: PhantomData,
client_s_pk: KeyPair::<CS::Group>::check_public_key(Key::from_bytes(&client_s_pk)?)?,
})
}
}
+10 -16
View File
@@ -96,7 +96,7 @@ impl Default for ClientRegistrationFinishParameters {
/// Contains the fields that are returned by a client registration start
pub struct ClientRegistrationStartResult<CS: CipherSuite> {
/// The registration request message to be sent to the server
pub message: RegistrationRequest<CS::Group>,
pub message: RegistrationRequest<CS>,
/// The client state that must be persisted in order to complete registration
pub state: ClientRegistration<CS>,
}
@@ -132,19 +132,18 @@ impl<CS: CipherSuite> ClientRegistration<CS> {
let (token, alpha) = oprf::blind::<R, CS::Group, CS::Hash>(&password, blinding_factor_rng)?;
Ok(ClientRegistrationStartResult {
message: RegistrationRequest::<CS::Group> { alpha },
message: RegistrationRequest::<CS> { alpha },
state: Self { token },
})
}
}
/// Contains the fields that are returned by a client registration finish
pub struct ClientRegistrationFinishResult<D: Hash, G: Group> {
pub struct ClientRegistrationFinishResult<CS: CipherSuite> {
/// The registration upload message to be sent to the server
pub message: RegistrationUpload<D, G>,
pub message: RegistrationUpload<CS>,
/// The export key output by client registration
pub export_key: GenericArray<u8, <D as Digest>::OutputSize>,
_g: PhantomData<G>,
pub export_key: GenericArray<u8, <CS::Hash as Digest>::OutputSize>,
}
impl<CS: CipherSuite> ClientRegistration<CS> {
@@ -182,9 +181,9 @@ impl<CS: CipherSuite> ClientRegistration<CS> {
pub fn finish<R: CryptoRng + RngCore>(
self,
rng: &mut R,
r2: RegistrationResponse<CS::Group>,
r2: RegistrationResponse<CS>,
params: ClientRegistrationFinishParameters,
) -> Result<ClientRegistrationFinishResult<CS::Hash, CS::Group>, ProtocolError> {
) -> Result<ClientRegistrationFinishResult<CS>, 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<CS: CipherSuite> ClientRegistration<CS> {
message: RegistrationUpload {
envelope,
client_s_pk: client_static_keypair.public().clone(),
_g: PhantomData,
},
export_key,
_g: PhantomData,
})
}
}
@@ -245,7 +242,7 @@ impl<CS: CipherSuite> Drop for ClientLogin<CS> {
/// Contains the fields that are returned by a server registration start
pub struct ServerRegistrationStartResult<CS: CipherSuite> {
/// The registration resposne message to send to the client
pub message: RegistrationResponse<CS::Group>,
pub message: RegistrationResponse<CS>,
/// The state that the server must keep in order to complete registration
pub state: ServerRegistration<CS>,
}
@@ -336,7 +333,7 @@ impl<CS: CipherSuite> ServerRegistration<CS> {
/// ```
pub fn start<R: RngCore + CryptoRng>(
rng: &mut R,
message: RegistrationRequest<CS::Group>,
message: RegistrationRequest<CS>,
server_s_pk: &Key,
) -> Result<ServerRegistrationStartResult<CS>, ProtocolError> {
// RFC: generate oprf_key (salt) and v_u = g^oprf_key
@@ -388,10 +385,7 @@ impl<CS: CipherSuite> ServerRegistration<CS> {
/// let client_record = server_registration_start_result.state.finish(client_registration_finish_result.message)?;
/// # Ok::<(), ProtocolError>(())
/// ```
pub fn finish(
self,
message: RegistrationUpload<CS::Hash, CS::Group>,
) -> Result<Self, ProtocolError> {
pub fn finish(self, message: RegistrationUpload<CS>) -> Result<Self, ProtocolError> {
Ok(Self {
envelope: Some(message.envelope),
client_s_pk: Some(message.client_s_pk),
+6 -6
View File
@@ -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::<RistrettoPoint>::deserialize(input.as_slice()).unwrap();
let r1 = RegistrationRequest::<Default>::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::<usize>() - 2..]);
input.extend_from_slice(&pubkey_bytes.as_slice());
let r2 = RegistrationResponse::<RistrettoPoint>::deserialize(input.as_slice()).unwrap();
let r2 = RegistrationResponse::<Default>::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::<sha2::Sha512, RistrettoPoint>::deserialize(&input[..]).unwrap();
let r3 = RegistrationUpload::<Default>::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::<u8>(), 0..std::mem::size_of::<usize>()))
#[test]
fn test_nocrash_register_first_message(bytes in vec(any::<u8>(), 0..200)) {
RegistrationRequest::<RistrettoPoint>::try_from(&bytes[..]).map_or(true, |_| true);
RegistrationRequest::<Default>::try_from(&bytes[..]).map_or(true, |_| true);
}
#[test]
fn test_nocrash_register_second_message(bytes in vec(any::<u8>(), 0..200)) {
RegistrationResponse::<RistrettoPoint>::try_from(&bytes[..]).map_or(true, |_| true);
RegistrationResponse::<Default>::try_from(&bytes[..]).map_or(true, |_| true);
}
#[test]
fn test_nocrash_register_third_message(bytes in vec(any::<u8>(), 0..200)) {
RegistrationUpload::<sha2::Sha512, RistrettoPoint>::try_from(&bytes[..]).map_or(true, |_| true);
RegistrationUpload::<Default>::try_from(&bytes[..]).map_or(true, |_| true);
}
#[test]