Files
opaque-vx/src/messages.rs
T

349 lines
12 KiB
Rust
Raw Normal View History

2020-11-16 14:05:43 -08:00
// Copyright (c) Facebook, Inc. and its affiliates.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
//! Contains the messages used for OPAQUE
use crate::{
ciphersuite::CipherSuite,
envelope::Envelope,
2020-11-16 14:05:43 -08:00
errors::{
utils::{check_slice_size, check_slice_size_atleast},
PakeError, ProtocolError,
},
group::Group,
hash::Hash,
key_exchange::traits::{KeyExchange, ToBytes},
2021-01-25 13:20:36 -08:00
keypair::{Key, KeyPair, SizedBytesExt},
serialization::{serialize, tokenize},
2020-11-16 14:05:43 -08:00
};
use generic_array::{typenum::Unsigned, GenericArray};
use generic_bytes::SizedBytes;
use std::convert::TryFrom;
2021-01-25 13:20:36 -08:00
use std::marker::PhantomData;
2020-11-16 14:05:43 -08:00
// Messages
// =========
/// The message sent by the client to the server, to initiate registration
2020-12-12 21:53:33 -08:00
pub struct RegistrationRequest<Grp> {
2020-11-16 14:05:43 -08:00
/// blinded password information
pub(crate) alpha: Grp,
}
2020-12-12 21:53:33 -08:00
impl<Grp: Group> TryFrom<&[u8]> for RegistrationRequest<Grp> {
2020-11-16 14:05:43 -08:00
type Error = ProtocolError;
fn try_from(first_message_bytes: &[u8]) -> Result<Self, Self::Error> {
let elem_len = Grp::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)?;
Ok(Self { alpha })
}
}
2020-12-12 21:53:33 -08:00
impl<Grp: Group> RegistrationRequest<Grp> {
2020-11-16 14:05:43 -08:00
/// Byte representation for the registration request
pub fn to_bytes(&self) -> Vec<u8> {
self.alpha.to_arr().to_vec()
}
/// Serialization into bytes
pub fn serialize(&self) -> Vec<u8> {
self.alpha.to_arr().to_vec()
2020-11-16 14:05:43 -08:00
}
/// 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")?;
2020-11-16 14:05:43 -08:00
// 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)?;
Ok(Self { alpha })
}
}
/// The answer sent by the server to the user, upon reception of the
/// registration attempt
2020-12-12 21:53:33 -08:00
pub struct RegistrationResponse<Grp> {
2020-11-16 14:05:43 -08:00
/// The server's oprf output
pub(crate) beta: Grp,
/// Server's static public key
pub(crate) server_s_pk: Vec<u8>,
}
2020-12-12 21:53:33 -08:00
impl<Grp> TryFrom<&[u8]> for RegistrationResponse<Grp>
2020-11-16 14:05:43 -08:00
where
Grp: Group,
{
type Error = ProtocolError;
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
let elem_len = Grp::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)?;
// FIXME check public key bytes
2020-11-16 14:05:43 -08:00
let server_s_pk = checked_slice[elem_len..].to_vec();
Ok(Self { beta, server_s_pk })
2020-11-16 14:05:43 -08:00
}
}
2020-12-12 21:53:33 -08:00
impl<Grp> RegistrationResponse<Grp>
2020-11-16 14:05:43 -08:00
where
Grp: Group,
{
/// Byte representation for the registration response message. This does not
/// include the envelope credentials format
pub fn to_bytes(&self) -> Vec<u8> {
[&self.beta.to_arr().to_vec()[..], &self.server_s_pk[..]].concat()
}
/// Serialization into bytes
pub fn serialize(&self) -> Vec<u8> {
let mut registration_response: Vec<u8> = Vec::new();
registration_response.extend_from_slice(&self.beta.to_arr());
2020-11-16 14:05:43 -08:00
registration_response.extend_from_slice(&serialize(&self.server_s_pk, 2));
2020-11-30 11:37:32 -08:00
registration_response
2020-11-16 14:05:43 -08:00
}
/// 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")?;
// 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)?;
2020-11-16 14:05:43 -08:00
let (server_s_pk, remainder) = tokenize(&checked_slice[Grp::ElemLen::to_usize()..], 2)?;
2020-11-16 14:05:43 -08:00
if !remainder.is_empty() {
return Err(PakeError::SerializationError.into());
}
Ok(Self { server_s_pk, beta })
2020-11-16 14:05:43 -08:00
}
}
/// The final message from the client, containing sealed cryptographic
/// identifiers
2021-01-25 13:20:36 -08:00
pub struct RegistrationUpload<D: Hash, G: Group> {
2020-11-16 14:05:43 -08:00
/// The "envelope" generated by the user, containing sealed
/// cryptographic identifiers
pub(crate) envelope: Envelope<D>,
/// The user's public key
2021-01-25 13:20:36 -08:00
pub(crate) client_s_pk: Key,
pub(crate) _g: PhantomData<G>,
2020-11-16 14:05:43 -08:00
}
2021-01-25 13:20:36 -08:00
impl<D: Hash, G: Group> TryFrom<&[u8]> for RegistrationUpload<D, G> {
2020-11-16 14:05:43 -08:00
type Error = ProtocolError;
fn try_from(third_message_bytes: &[u8]) -> Result<Self, Self::Error> {
2021-01-25 13:20:36 -08:00
let key_len = <Key as SizedBytes>::Len::to_usize();
2020-11-16 14:05:43 -08:00
let envelope_size = key_len + Envelope::<D>::additional_size();
let checked_bytes = check_slice_size(
third_message_bytes,
envelope_size + key_len,
"third_message",
)?;
2021-01-25 13:20:36 -08:00
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)?;
2020-11-16 14:05:43 -08:00
Ok(Self {
envelope: Envelope::<D>::from_bytes(&checked_bytes[..envelope_size])?,
client_s_pk,
2021-01-25 13:20:36 -08:00
_g: PhantomData,
2020-11-16 14:05:43 -08:00
})
}
}
2021-01-25 13:20:36 -08:00
impl<D: Hash, G: Group> RegistrationUpload<D, G> {
2020-11-16 14:05:43 -08:00
/// Serialization into bytes
pub fn serialize(&self) -> Vec<u8> {
2020-12-12 21:53:33 -08:00
let mut message: Vec<u8> = Vec::new();
message.extend_from_slice(&serialize(&self.client_s_pk.to_arr(), 2));
message.extend_from_slice(&self.envelope.serialize());
2020-12-12 21:53:33 -08:00
message
2020-11-16 14:05:43 -08:00
}
/// 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)?;
2020-11-16 14:05:43 -08:00
if !remainder.is_empty() {
return Err(PakeError::SerializationError.into());
}
Ok(Self {
envelope,
2021-01-25 13:20:36 -08:00
client_s_pk: KeyPair::<G>::check_public_key(Key::from_bytes(&client_s_pk)?)?,
_g: PhantomData,
2020-11-16 14:05:43 -08:00
})
}
}
/// The message sent by the user to the server, to initiate registration
2020-12-12 21:53:33 -08:00
pub struct CredentialRequest<CS: CipherSuite> {
2020-11-16 14:05:43 -08:00
/// blinded password information
pub(crate) alpha: CS::Group,
2021-01-25 13:20:36 -08:00
pub(crate) ke1_message: <CS::KeyExchange as KeyExchange<CS::Hash, CS::Group>>::KE1Message,
2020-11-16 14:05:43 -08:00
}
2020-12-12 21:53:33 -08:00
impl<CS: CipherSuite> TryFrom<&[u8]> for CredentialRequest<CS> {
2020-11-16 14:05:43 -08:00
type Error = ProtocolError;
fn try_from(first_message_bytes: &[u8]) -> Result<Self, Self::Error> {
Self::deserialize(first_message_bytes)
}
}
2020-12-12 21:53:33 -08:00
impl<CS: CipherSuite> CredentialRequest<CS> {
2020-11-16 14:05:43 -08:00
/// byte representation for the login request
pub(crate) fn to_bytes(&self) -> Vec<u8> {
[&self.alpha.to_arr()[..], &self.ke1_message.to_bytes()].concat()
}
/// Serialization into bytes
pub fn serialize(&self) -> Vec<u8> {
let mut credential_request: Vec<u8> = Vec::new();
credential_request.extend_from_slice(&self.alpha.to_arr());
2020-11-30 11:37:32 -08:00
credential_request.extend_from_slice(&self.ke1_message.to_bytes());
credential_request
2020-11-16 14:05:43 -08:00
}
/// Deserialization from bytes
pub fn deserialize(input: &[u8]) -> Result<Self, ProtocolError> {
let elem_len = <CS::Group as Group>::ElemLen::to_usize();
let checked_slice =
check_slice_size_atleast(&input, elem_len, "login_first_message_bytes")?;
// Check that the message is actually containing an element of the
// correct subgroup
2020-11-16 14:05:43 -08:00
let arr = GenericArray::from_slice(&checked_slice[..elem_len]);
let alpha = CS::Group::from_element_slice(arr)?;
2020-11-16 14:05:43 -08:00
let ke1_message =
<CS::KeyExchange as KeyExchange<CS::Hash, CS::Group>>::KE1Message::try_from(
&checked_slice[elem_len..],
)?;
2020-11-16 14:05:43 -08:00
Ok(Self { alpha, ke1_message })
}
}
/// The answer sent by the server to the user, upon reception of the
2020-12-12 21:53:33 -08:00
/// login attempt
pub struct CredentialResponse<CS: CipherSuite> {
2020-11-16 14:05:43 -08:00
/// the server's oprf output
pub(crate) beta: CS::Group,
2021-01-25 13:20:36 -08:00
pub(crate) server_s_pk: Key,
2020-11-16 14:05:43 -08:00
/// the user's sealed information,
pub(crate) envelope: Envelope<CS::Hash>,
2021-01-25 13:20:36 -08:00
pub(crate) ke2_message: <CS::KeyExchange as KeyExchange<CS::Hash, CS::Group>>::KE2Message,
2020-11-16 14:05:43 -08:00
}
2020-12-12 21:53:33 -08:00
impl<CS: CipherSuite> CredentialResponse<CS> {
2020-11-16 14:05:43 -08:00
/// Serialization into bytes
pub fn serialize(&self) -> Vec<u8> {
let mut credential_response: Vec<u8> = Vec::new();
credential_response.extend_from_slice(&self.beta.to_arr());
credential_response.extend_from_slice(&serialize(&self.server_s_pk.to_arr().to_vec(), 2));
2020-11-16 14:05:43 -08:00
credential_response.extend_from_slice(&self.envelope.to_bytes());
2020-11-30 11:37:32 -08:00
credential_response.extend_from_slice(&self.ke2_message.to_bytes());
credential_response
2020-11-16 14:05:43 -08:00
}
/// Deserialization from bytes
pub fn deserialize(input: &[u8]) -> Result<Self, ProtocolError> {
Self::try_from(input)
2020-11-16 14:05:43 -08:00
}
}
2020-12-12 21:53:33 -08:00
impl<CS: CipherSuite> TryFrom<&[u8]> for CredentialResponse<CS> {
2020-11-16 14:05:43 -08:00
type Error = ProtocolError;
fn try_from(second_message_bytes: &[u8]) -> Result<Self, Self::Error> {
let elem_len = <CS::Group as Group>::ElemLen::to_usize();
let checked_slice =
check_slice_size_atleast(second_message_bytes, elem_len, "login_second_message_bytes")?;
// Check that the message is actually containing an element of the
// correct subgroup
let beta_bytes = &checked_slice[..elem_len];
let arr = GenericArray::from_slice(beta_bytes);
let beta = CS::Group::from_element_slice(arr)?;
let (serialized_server_s_pk, remainder) = tokenize(&checked_slice[elem_len..], 2)?;
let sized_server_s_pk = check_slice_size(
&serialized_server_s_pk[..],
2021-01-25 13:20:36 -08:00
<Key as SizedBytes>::Len::to_usize(),
"server_s_pk in credential_response",
)?;
2021-01-25 13:20:36 -08:00
let unchecked_server_s_pk = Key::from_bytes(&sized_server_s_pk[..])?;
let server_s_pk = KeyPair::<CS::Group>::check_public_key(unchecked_server_s_pk)?;
let (envelope, remainder) = Envelope::<CS::Hash>::deserialize(&remainder)?;
2020-11-16 14:05:43 -08:00
let ke2_message_size = CS::KeyExchange::ke2_message_size();
let checked_remainder =
check_slice_size_atleast(&remainder, ke2_message_size, "login_second_message_bytes")?;
let ke2_message =
2021-01-25 13:20:36 -08:00
<CS::KeyExchange as KeyExchange<CS::Hash, CS::Group>>::KE2Message::try_from(
2020-11-16 14:05:43 -08:00
&checked_remainder,
)?;
Ok(Self {
beta,
server_s_pk,
2020-11-16 14:05:43 -08:00
envelope,
ke2_message,
})
}
}
/// The answer sent by the client to the server, upon reception of the
/// sealed envelope
2020-12-12 21:53:33 -08:00
pub struct CredentialFinalization<CS: CipherSuite> {
2021-01-25 13:20:36 -08:00
pub(crate) ke3_message: <CS::KeyExchange as KeyExchange<CS::Hash, CS::Group>>::KE3Message,
2020-11-16 14:05:43 -08:00
}
2020-12-12 21:53:33 -08:00
impl<CS: CipherSuite> TryFrom<&[u8]> for CredentialFinalization<CS> {
2020-11-16 14:05:43 -08:00
type Error = ProtocolError;
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
let ke3_message =
2021-01-25 13:20:36 -08:00
<CS::KeyExchange as KeyExchange<CS::Hash, CS::Group>>::KE3Message::try_from(bytes)?;
2020-11-16 14:05:43 -08:00
Ok(Self { ke3_message })
}
}
2020-12-12 21:53:33 -08:00
impl<CS: CipherSuite> CredentialFinalization<CS> {
2020-11-16 14:05:43 -08:00
/// Serialization into bytes
pub fn serialize(&self) -> Vec<u8> {
2020-11-30 11:37:32 -08:00
self.ke3_message.to_bytes()
2020-11-16 14:05:43 -08:00
}
/// Deserialization from bytes
pub fn deserialize(input: &[u8]) -> Result<Self, ProtocolError> {
2020-11-30 11:37:32 -08:00
Self::try_from(&input[..])
2020-11-16 14:05:43 -08:00
}
/// byte representation for the login finalization
pub fn to_bytes(&self) -> Vec<u8> {
self.ke3_message.to_bytes()
}
}