Files
opaque-vx/src/messages.rs
T

402 lines
13 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},
2021-08-22 12:28:19 -07:00
ProtocolError,
2020-11-16 14:05:43 -08:00
},
2021-10-25 02:54:32 -07:00
key_exchange::{
group::KeGroup,
traits::{FromBytes, KeyExchange, ToBytes},
},
2021-08-17 05:11:53 +02:00
keypair::{KeyPair, PublicKey, SecretKey},
opaque::ServerSetup,
2020-11-16 14:05:43 -08:00
};
2021-08-12 06:25:07 +02:00
use alloc::vec::Vec;
use digest::Digest;
2020-11-16 14:05:43 -08:00
use generic_array::{typenum::Unsigned, GenericArray};
use rand::{CryptoRng, RngCore};
2021-10-25 02:54:32 -07:00
use voprf::group::Group;
2020-11-16 14:05:43 -08:00
////////////////////////////
// High-level API Structs //
// ====================== //
////////////////////////////
2020-11-16 14:05:43 -08:00
/// The message sent by the client to the server, to initiate registration
pub struct RegistrationRequest<CS: CipherSuite> {
2020-11-16 14:05:43 -08:00
/// blinded password information
2021-10-25 02:54:32 -07:00
pub(crate) blinded_element: voprf::BlindedElement<CS::OprfGroup, CS::Hash>,
2020-11-16 14:05:43 -08:00
}
/// The answer sent by the server to the user, upon reception of the
/// registration attempt
pub struct RegistrationResponse<CS: CipherSuite> {
/// The server's oprf output
2021-10-25 02:54:32 -07:00
pub(crate) evaluation_element: voprf::EvaluationElement<CS::OprfGroup, CS::Hash>,
/// Server's static public key
pub(crate) server_s_pk: PublicKey<CS::KeGroup>,
}
/// The final message from the client, containing sealed cryptographic
/// identifiers
pub struct RegistrationUpload<CS: CipherSuite> {
/// The "envelope" generated by the user, containing sealed
/// cryptographic identifiers
pub(crate) envelope: Envelope<CS>,
/// The masking key used to mask the envelope
pub(crate) masking_key: GenericArray<u8, <CS::Hash as Digest>::OutputSize>,
/// The user's public key
pub(crate) client_s_pk: PublicKey<CS::KeGroup>,
}
/// The message sent by the user to the server, to initiate registration
pub struct CredentialRequest<CS: CipherSuite> {
2021-10-25 02:54:32 -07:00
pub(crate) blinded_element: voprf::BlindedElement<CS::OprfGroup, CS::Hash>,
pub(crate) ke1_message: <CS::KeyExchange as KeyExchange<CS::Hash, CS::KeGroup>>::KE1Message,
}
/// The answer sent by the server to the user, upon reception of the
/// login attempt
pub struct CredentialResponse<CS: CipherSuite> {
/// the server's oprf output
2021-10-25 02:54:32 -07:00
pub(crate) evaluation_element: voprf::EvaluationElement<CS::OprfGroup, CS::Hash>,
pub(crate) masking_nonce: Vec<u8>,
pub(crate) masked_response: Vec<u8>,
pub(crate) ke2_message: <CS::KeyExchange as KeyExchange<CS::Hash, CS::KeGroup>>::KE2Message,
}
/// The answer sent by the client to the server, upon reception of the
/// sealed envelope
pub struct CredentialFinalization<CS: CipherSuite> {
pub(crate) ke3_message: <CS::KeyExchange as KeyExchange<CS::Hash, CS::KeGroup>>::KE3Message,
}
////////////////////////////////
// High-level Implementations //
// ========================== //
////////////////////////////////
impl<CS: CipherSuite> RegistrationRequest<CS> {
/// Only used for testing purposes
#[cfg(test)]
2021-10-25 02:54:32 -07:00
pub fn get_blinded_element_for_testing(
&self,
) -> voprf::BlindedElement<CS::OprfGroup, CS::Hash> {
self.blinded_element.clone()
}
2021-06-22 15:48:36 +02:00
2020-11-16 14:05:43 -08:00
/// Serialization into bytes
2021-10-25 02:54:32 -07:00
pub fn serialize(&self) -> Result<Vec<u8>, ProtocolError> {
Ok(self.blinded_element.serialize())
2020-11-16 14:05:43 -08:00
}
/// Deserialization from bytes
pub fn deserialize(input: &[u8]) -> Result<Self, ProtocolError> {
2021-10-25 02:54:32 -07:00
Ok(Self {
blinded_element: voprf::BlindedElement::deserialize(input)?,
})
2020-11-16 14:05:43 -08:00
}
}
impl<CS: CipherSuite> RegistrationResponse<CS> {
2020-11-16 14:05:43 -08:00
/// Serialization into bytes
2021-10-25 02:54:32 -07:00
pub fn serialize(&self) -> Result<Vec<u8>, ProtocolError> {
Ok([
self.evaluation_element.serialize(),
self.server_s_pk.to_vec(),
]
.concat())
2020-11-16 14:05:43 -08:00
}
/// Deserialization from bytes
pub fn deserialize(input: &[u8]) -> Result<Self, ProtocolError> {
2021-08-17 05:11:53 +02:00
let elem_len = <CS::OprfGroup as Group>::ElemLen::USIZE;
2021-10-25 02:54:32 -07:00
let key_len = <CS::KeGroup as KeGroup>::PkLen::USIZE;
let checked_slice =
2021-06-12 23:18:08 -07:00
check_slice_size(input, elem_len + key_len, "registration_response_bytes")?;
// Ensure that public key is valid
2021-08-04 21:24:46 +02:00
let server_s_pk = KeyPair::<CS::KeGroup>::check_public_key(PublicKey::from_bytes(
2021-06-15 10:48:29 +02:00
&checked_slice[elem_len..],
)?)?;
2020-11-16 14:05:43 -08:00
2021-10-25 02:54:32 -07:00
Ok(Self {
evaluation_element: voprf::EvaluationElement::deserialize(&checked_slice[..elem_len])?,
server_s_pk,
})
2020-11-16 14:05:43 -08:00
}
2021-07-12 12:33:19 -07:00
#[cfg(test)]
/// Only used for tests, where we can set the beta value to test for the reflection
/// error case
2021-10-25 02:54:32 -07:00
pub fn set_evaluation_element_for_testing(&self, beta: CS::OprfGroup) -> Self {
2021-07-12 12:33:19 -07:00
Self {
2021-10-25 02:54:32 -07:00
evaluation_element: voprf::EvaluationElement::from_value_unchecked(beta),
2021-07-12 12:33:19 -07:00
server_s_pk: self.server_s_pk.clone(),
}
}
2020-11-16 14:05:43 -08:00
}
impl<CS: CipherSuite> RegistrationUpload<CS> {
2020-11-16 14:05:43 -08:00
/// Serialization into bytes
2021-10-25 02:54:32 -07:00
pub fn serialize(&self) -> Result<Vec<u8>, ProtocolError> {
Ok([
self.client_s_pk.to_arr().to_vec(),
self.masking_key.to_vec(),
self.envelope.serialize(),
]
2021-10-25 02:54:32 -07:00
.concat())
2020-11-16 14:05:43 -08:00
}
/// Deserialization from bytes
pub fn deserialize(input: &[u8]) -> Result<Self, ProtocolError> {
2021-10-25 02:54:32 -07:00
let key_len = <CS::KeGroup as KeGroup>::PkLen::USIZE;
2021-08-17 05:11:53 +02:00
let hash_len = <CS::Hash as Digest>::OutputSize::USIZE;
let checked_slice =
check_slice_size_atleast(input, key_len + hash_len, "registration_upload_bytes")?;
let envelope = Envelope::<CS>::deserialize(&checked_slice[key_len + hash_len..])?;
2020-11-16 14:05:43 -08:00
Ok(Self {
envelope,
masking_key: GenericArray::clone_from_slice(
&checked_slice[key_len..key_len + hash_len],
),
2021-08-04 21:24:46 +02:00
client_s_pk: KeyPair::<CS::KeGroup>::check_public_key(PublicKey::from_bytes(
&checked_slice[..key_len],
)?)?,
2020-11-16 14:05:43 -08:00
})
}
// Creates a dummy instance used for faking a [CredentialResponse]
2021-08-04 21:24:46 +02:00
pub(crate) fn dummy<R: RngCore + CryptoRng, S: SecretKey<CS::KeGroup>>(
rng: &mut R,
2021-07-20 11:49:37 +02:00
server_setup: &ServerSetup<CS, S>,
) -> Self {
2021-08-17 05:11:53 +02:00
let mut masking_key = alloc::vec![0u8; <CS::Hash as Digest>::OutputSize::USIZE];
rng.fill_bytes(&mut masking_key);
Self {
envelope: Envelope::<CS>::dummy(),
masking_key: GenericArray::clone_from_slice(&masking_key),
client_s_pk: server_setup.fake_keypair.public().clone(),
}
}
2020-11-16 14:05:43 -08:00
}
2020-12-12 21:53:33 -08:00
impl<CS: CipherSuite> CredentialRequest<CS> {
2020-11-16 14:05:43 -08:00
/// Serialization into bytes
2021-10-25 02:54:32 -07:00
pub fn serialize(&self) -> Result<Vec<u8>, ProtocolError> {
Ok([
self.blinded_element.serialize(),
self.ke1_message.to_bytes(),
]
.concat())
2020-11-16 14:05:43 -08:00
}
/// Deserialization from bytes
pub fn deserialize(input: &[u8]) -> Result<Self, ProtocolError> {
2021-08-17 05:11:53 +02:00
let elem_len = <CS::OprfGroup as Group>::ElemLen::USIZE;
2021-06-12 23:18:08 -07:00
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
2021-10-25 02:54:32 -07:00
let blinded_element = voprf::BlindedElement::<CS::OprfGroup, CS::Hash>::deserialize(
&checked_slice[..elem_len],
)?;
2020-11-16 14:05:43 -08:00
// Throw an error if the identity group element is encountered
2021-10-25 02:54:32 -07:00
if blinded_element.value().is_identity() {
2021-08-22 12:28:19 -07:00
return Err(ProtocolError::IdentityGroupElementError);
}
2020-11-16 14:05:43 -08:00
let ke1_message =
2021-08-04 21:24:46 +02:00
<CS::KeyExchange as KeyExchange<CS::Hash, CS::KeGroup>>::KE1Message::from_bytes::<CS>(
&checked_slice[elem_len..],
)?;
2020-11-16 14:05:43 -08:00
2021-10-25 02:54:32 -07:00
Ok(Self {
blinded_element,
ke1_message,
})
2020-11-16 14:05:43 -08:00
}
2021-07-12 12:33:19 -07:00
/// Only used for testing purposes
#[cfg(test)]
2021-10-25 02:54:32 -07:00
pub fn get_blinded_element_for_testing(
&self,
) -> voprf::BlindedElement<CS::OprfGroup, CS::Hash> {
self.blinded_element.clone()
2021-07-12 12:33:19 -07:00
}
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
2021-10-25 02:54:32 -07:00
pub fn serialize(&self) -> Result<Vec<u8>, ProtocolError> {
Ok([
Self::serialize_without_ke(
&self.evaluation_element.value(),
&self.masking_nonce,
&self.masked_response,
),
self.ke2_message.to_bytes(),
]
2021-10-25 02:54:32 -07:00
.concat())
}
pub(crate) fn serialize_without_ke(
2021-08-04 21:24:46 +02:00
beta: &CS::OprfGroup,
masking_nonce: &[u8],
masked_response: &[u8],
) -> Vec<u8> {
[&beta.to_arr(), masking_nonce, masked_response].concat()
2020-11-16 14:05:43 -08:00
}
/// Deserialization from bytes
pub fn deserialize(input: &[u8]) -> Result<Self, ProtocolError> {
2021-08-17 05:11:53 +02:00
let elem_len = <CS::OprfGroup as Group>::ElemLen::USIZE;
2021-10-25 02:54:32 -07:00
let key_len = <CS::KeGroup as KeGroup>::PkLen::USIZE;
let nonce_len: usize = 32;
let envelope_len = Envelope::<CS>::len();
let masked_response_len = key_len + envelope_len;
let ke2_message_len = CS::KeyExchange::ke2_message_size();
let checked_slice = check_slice_size_atleast(
input,
elem_len + nonce_len + masked_response_len + ke2_message_len,
"credential_response_bytes",
)?;
2020-11-16 14:05:43 -08:00
// Check that the message is actually containing an element of the
// correct subgroup
let beta_bytes = &checked_slice[..elem_len];
2021-10-25 02:54:32 -07:00
let evaluation_element =
voprf::EvaluationElement::<CS::OprfGroup, CS::Hash>::deserialize(beta_bytes)?;
2020-11-16 14:05:43 -08:00
// Throw an error if the identity group element is encountered
2021-10-25 02:54:32 -07:00
if evaluation_element.value().is_identity() {
2021-08-22 12:28:19 -07:00
return Err(ProtocolError::IdentityGroupElementError);
}
let masking_nonce = checked_slice[elem_len..elem_len + nonce_len].to_vec();
let masked_response = checked_slice
[elem_len + nonce_len..elem_len + nonce_len + masked_response_len]
.to_vec();
2020-11-16 14:05:43 -08:00
let ke2_message =
2021-08-04 21:24:46 +02:00
<CS::KeyExchange as KeyExchange<CS::Hash, CS::KeGroup>>::KE2Message::from_bytes::<CS>(
&checked_slice[elem_len + nonce_len + masked_response_len..],
2020-11-16 14:05:43 -08:00
)?;
Ok(Self {
2021-10-25 02:54:32 -07:00
evaluation_element,
masking_nonce,
masked_response,
2020-11-16 14:05:43 -08:00
ke2_message,
})
}
2021-07-12 12:33:19 -07:00
#[cfg(test)]
/// Only used for tests, where we can set the beta value to test for the reflection
/// error case
2021-10-25 02:54:32 -07:00
pub fn set_evaluation_element_for_testing(&self, beta: CS::OprfGroup) -> Self {
2021-07-12 12:33:19 -07:00
Self {
2021-10-25 02:54:32 -07:00
evaluation_element: voprf::EvaluationElement::from_value_unchecked(beta),
2021-07-12 12:33:19 -07:00
masking_nonce: self.masking_nonce.clone(),
masked_response: self.masked_response.clone(),
ke2_message: self.ke2_message.clone(),
}
}
2020-11-16 14:05:43 -08:00
}
2020-12-12 21:53:33 -08:00
impl<CS: CipherSuite> CredentialFinalization<CS> {
2020-11-16 14:05:43 -08:00
/// Serialization into bytes
2021-10-25 02:54:32 -07:00
pub fn serialize(&self) -> Result<Vec<u8>, ProtocolError> {
Ok(self.ke3_message.to_bytes())
2020-11-16 14:05:43 -08:00
}
/// Deserialization from bytes
pub fn deserialize(input: &[u8]) -> Result<Self, ProtocolError> {
let ke3_message =
2021-08-04 21:24:46 +02:00
<CS::KeyExchange as KeyExchange<CS::Hash, CS::KeGroup>>::KE3Message::from_bytes::<CS>(
input,
)?;
Ok(Self { ke3_message })
2020-11-16 14:05:43 -08:00
}
}
///////////////////////////
// Trait Implementations //
// ===================== //
///////////////////////////
impl_clone_for!(
struct RegistrationRequest<CS: CipherSuite>,
2021-10-25 02:54:32 -07:00
[blinded_element],
);
2021-10-25 02:54:32 -07:00
impl_debug_eq_hash_for!(struct RegistrationRequest<CS: CipherSuite>, [blinded_element], [CS::OprfGroup, CS::Hash]);
impl_serialize_and_deserialize_for!(RegistrationRequest);
impl_clone_for!(
struct RegistrationResponse<CS: CipherSuite>,
2021-10-25 02:54:32 -07:00
[evaluation_element, server_s_pk],
);
impl_debug_eq_hash_for!(
struct RegistrationResponse<CS: CipherSuite>,
2021-10-25 02:54:32 -07:00
[evaluation_element, server_s_pk],
[CS::OprfGroup, CS::Hash],
);
impl_serialize_and_deserialize_for!(RegistrationResponse);
impl_clone_for!(
struct RegistrationUpload<CS: CipherSuite>,
[envelope, masking_key, client_s_pk],
);
impl_debug_eq_hash_for!(
struct RegistrationUpload<CS: CipherSuite>,
[envelope, masking_key, client_s_pk],
);
impl_serialize_and_deserialize_for!(RegistrationUpload);
impl_clone_for!(
struct CredentialRequest<CS: CipherSuite>,
2021-10-25 02:54:32 -07:00
[blinded_element, ke1_message],
);
impl_debug_eq_hash_for!(
struct CredentialRequest<CS: CipherSuite>,
2021-10-25 02:54:32 -07:00
[blinded_element, ke1_message],
[
CS::OprfGroup,
<CS::KeyExchange as KeyExchange<CS::Hash, CS::KeGroup>>::KE1Message
],
);
impl_serialize_and_deserialize_for!(CredentialRequest);
impl_clone_for!(
struct CredentialResponse<CS: CipherSuite>,
2021-10-25 02:54:32 -07:00
[evaluation_element, masking_nonce, masked_response, ke2_message],
);
impl_debug_eq_hash_for!(
struct CredentialResponse<CS: CipherSuite>,
2021-10-25 02:54:32 -07:00
[evaluation_element, masking_nonce, masked_response, ke2_message],
[
CS::OprfGroup,
<CS::KeyExchange as KeyExchange<CS::Hash, CS::KeGroup>>::KE2Message,
],
);
impl_serialize_and_deserialize_for!(CredentialResponse);
impl_clone_for!(struct CredentialFinalization<CS: CipherSuite>, [ke3_message]);
impl_debug_eq_hash_for!(
struct CredentialFinalization<CS: CipherSuite>,
[ke3_message],
[<CS::KeyExchange as KeyExchange<CS::Hash, CS::KeGroup>>::KE3Message],
);
impl_serialize_and_deserialize_for!(CredentialFinalization);