Files
opaque-vx/src/messages.rs
T

561 lines
22 KiB
Rust
Raw Normal View History

2020-11-16 14:05:43 -08:00
// Copyright (c) Facebook, Inc. and its affiliates.
//
2021-12-03 14:38:11 -08:00
// This source code is licensed under both the MIT license found in the
// LICENSE-MIT file in the root directory of this source tree and the Apache
// License, Version 2.0 found in the LICENSE-APACHE file in the root directory
// of this source tree.
2020-11-16 14:05:43 -08:00
//! Contains the messages used for OPAQUE
2022-01-04 00:50:40 +01:00
use core::ops::Add;
2022-01-06 06:19:02 +01:00
2022-02-25 07:13:22 +01:00
use derive_where::derive_where;
2022-01-06 00:10:57 +01:00
use digest::core_api::{BlockSizeUser, CoreProxy};
2022-02-25 07:13:22 +01:00
use digest::{Output, OutputSizeUser};
2022-01-04 00:50:40 +01:00
use generic_array::sequence::Concat;
2022-02-25 07:13:22 +01:00
use generic_array::typenum::{IsLess, IsLessOrEqual, Le, NonZero, Sum, Unsigned, U256};
2022-01-06 06:19:02 +01:00
use generic_array::{ArrayLength, GenericArray};
use rand::{CryptoRng, RngCore};
2022-02-25 07:13:22 +01:00
use subtle::ConstantTimeEq;
2022-01-06 00:10:57 +01:00
use voprf::Group;
2020-11-16 14:05:43 -08:00
2022-02-25 07:13:22 +01:00
use crate::ciphersuite::{CipherSuite, OprfGroup, OprfHash};
2022-01-06 06:19:02 +01:00
use crate::envelope::{Envelope, EnvelopeLen};
use crate::errors::utils::{check_slice_size, check_slice_size_atleast};
use crate::errors::ProtocolError;
2022-02-25 07:13:22 +01:00
use crate::hash::{Hash, OutputSize, ProxyHash};
2022-01-06 06:19:02 +01:00
use crate::key_exchange::group::KeGroup;
use crate::key_exchange::traits::{
2022-04-02 01:10:00 +02:00
Deserialize, Ke1MessageLen, Ke2MessageLen, Ke3MessageLen, KeyExchange, Serialize,
2022-01-06 06:19:02 +01:00
};
use crate::key_exchange::tripledh::NonceLen;
2022-02-25 07:13:22 +01:00
use crate::keypair::{PublicKey, SecretKey};
2022-01-06 06:19:02 +01:00
use crate::opaque::{MaskedResponse, MaskedResponseLen, ServerSetup};
////////////////////////////
// High-level API Structs //
// ====================== //
////////////////////////////
2020-11-16 14:05:43 -08:00
/// The message sent by the client to the server, to initiate registration
2022-04-02 01:10:00 +02:00
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize, serde::Serialize),
serde(bound = "", crate = "serde")
)]
2022-01-04 00:50:40 +01:00
#[derive_where(Clone)]
2022-04-02 01:10:00 +02:00
#[derive_where(Debug, Eq, Hash, Ord, PartialEq, PartialOrd; voprf::BlindedElement<CS::OprfCs>)]
2022-01-06 00:10:57 +01:00
pub struct RegistrationRequest<CS: CipherSuite>
where
2022-02-25 07:13:22 +01:00
<OprfHash<CS> as OutputSizeUser>::OutputSize:
IsLess<U256> + IsLessOrEqual<<OprfHash<CS> as BlockSizeUser>::BlockSize>,
OprfHash<CS>: Hash,
<OprfHash<CS> as CoreProxy>::Core: ProxyHash,
<<OprfHash<CS> as CoreProxy>::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<<OprfHash<CS> as CoreProxy>::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
2022-01-06 00:10:57 +01:00
{
2020-11-16 14:05:43 -08:00
/// blinded password information
2022-04-02 01:10:00 +02:00
pub(crate) blinded_element: voprf::BlindedElement<CS::OprfCs>,
2020-11-16 14:05:43 -08:00
}
/// The answer sent by the server to the user, upon reception of the
/// registration attempt
2022-04-02 01:10:00 +02:00
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize, serde::Serialize),
serde(bound = "", crate = "serde")
)]
2022-01-04 00:50:40 +01:00
#[derive_where(Clone)]
2022-04-02 01:10:00 +02:00
#[derive_where(Debug, Eq, Hash, Ord, PartialEq, PartialOrd; voprf::EvaluationElement<CS::OprfCs>, <CS::KeGroup as KeGroup>::Pk)]
2022-01-06 00:10:57 +01:00
pub struct RegistrationResponse<CS: CipherSuite>
where
2022-02-25 07:13:22 +01:00
<OprfHash<CS> as OutputSizeUser>::OutputSize:
IsLess<U256> + IsLessOrEqual<<OprfHash<CS> as BlockSizeUser>::BlockSize>,
OprfHash<CS>: Hash,
<OprfHash<CS> as CoreProxy>::Core: ProxyHash,
<<OprfHash<CS> as CoreProxy>::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<<OprfHash<CS> as CoreProxy>::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
2022-01-06 00:10:57 +01:00
{
/// The server's oprf output
2022-04-02 01:10:00 +02:00
pub(crate) evaluation_element: voprf::EvaluationElement<CS::OprfCs>,
/// Server's static public key
pub(crate) server_s_pk: PublicKey<CS::KeGroup>,
}
/// The final message from the client, containing sealed cryptographic
/// identifiers
2022-04-02 01:10:00 +02:00
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize, serde::Serialize),
serde(bound = "", crate = "serde")
)]
#[derive_where(Clone, ZeroizeOnDrop)]
2022-02-25 07:13:22 +01:00
#[derive_where(Debug, Eq, Hash, Ord, PartialEq, PartialOrd; <CS::KeGroup as KeGroup>::Pk)]
2022-01-06 00:10:57 +01:00
pub struct RegistrationUpload<CS: CipherSuite>
where
2022-02-25 07:13:22 +01:00
<OprfHash<CS> as OutputSizeUser>::OutputSize:
IsLess<U256> + IsLessOrEqual<<OprfHash<CS> as BlockSizeUser>::BlockSize>,
OprfHash<CS>: Hash,
<OprfHash<CS> as CoreProxy>::Core: ProxyHash,
<<OprfHash<CS> as CoreProxy>::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<<OprfHash<CS> as CoreProxy>::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
2022-01-06 00:10:57 +01:00
{
2022-01-06 06:19:02 +01:00
/// The "envelope" generated by the user, containing sealed cryptographic
/// identifiers
pub(crate) envelope: Envelope<CS>,
/// The masking key used to mask the envelope
2022-02-25 07:13:22 +01:00
pub(crate) masking_key: Output<OprfHash<CS>>,
/// 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
2022-04-02 01:10:00 +02:00
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize, serde::Serialize),
serde(
bound(
deserialize = "<CS::KeyExchange as KeyExchange<OprfHash<CS>, \
CS::KeGroup>>::KE1Message: serde::Deserialize<'de>",
serialize = "<CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE1Message: \
serde::Serialize"
),
crate = "serde"
)
)]
2022-02-25 07:13:22 +01:00
#[derive_where(Clone, ZeroizeOnDrop)]
2022-01-04 00:50:40 +01:00
#[derive_where(
Debug, Eq, Hash, PartialEq;
2022-04-02 01:10:00 +02:00
voprf::BlindedElement<CS::OprfCs>,
2022-02-25 07:13:22 +01:00
<CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE1Message,
2022-01-04 00:50:40 +01:00
)]
2022-01-06 00:10:57 +01:00
pub struct CredentialRequest<CS: CipherSuite>
where
2022-02-25 07:13:22 +01:00
<OprfHash<CS> as OutputSizeUser>::OutputSize:
IsLess<U256> + IsLessOrEqual<<OprfHash<CS> as BlockSizeUser>::BlockSize>,
OprfHash<CS>: Hash,
<OprfHash<CS> as CoreProxy>::Core: ProxyHash,
<<OprfHash<CS> as CoreProxy>::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<<OprfHash<CS> as CoreProxy>::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
2022-01-06 00:10:57 +01:00
{
2022-04-02 01:10:00 +02:00
pub(crate) blinded_element: voprf::BlindedElement<CS::OprfCs>,
2022-02-25 07:13:22 +01:00
pub(crate) ke1_message: <CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE1Message,
}
2022-01-06 06:19:02 +01:00
/// The answer sent by the server to the user, upon reception of the login
/// attempt
2022-04-02 01:10:00 +02:00
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize, serde::Serialize),
serde(
bound(
deserialize = "<CS::KeyExchange as KeyExchange<OprfHash<CS>, \
CS::KeGroup>>::KE2Message: serde::Deserialize<'de>",
serialize = "<CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE2Message: \
serde::Serialize"
),
crate = "serde"
)
)]
2022-01-04 00:50:40 +01:00
#[derive_where(Clone)]
#[derive_where(
Debug, Eq, Hash, PartialEq;
2022-04-02 01:10:00 +02:00
voprf::EvaluationElement<CS::OprfCs>,
2022-02-25 07:13:22 +01:00
<CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE2Message,
2022-01-04 00:50:40 +01:00
)]
2022-01-06 00:10:57 +01:00
pub struct CredentialResponse<CS: CipherSuite>
where
2022-02-25 07:13:22 +01:00
<OprfHash<CS> as OutputSizeUser>::OutputSize:
IsLess<U256> + IsLessOrEqual<<OprfHash<CS> as BlockSizeUser>::BlockSize>,
OprfHash<CS>: Hash,
<OprfHash<CS> as CoreProxy>::Core: ProxyHash,
<<OprfHash<CS> as CoreProxy>::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<<OprfHash<CS> as CoreProxy>::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
2022-01-06 00:10:57 +01:00
{
/// the server's oprf output
2022-04-02 01:10:00 +02:00
pub(crate) evaluation_element: voprf::EvaluationElement<CS::OprfCs>,
2022-01-04 00:50:40 +01:00
pub(crate) masking_nonce: GenericArray<u8, NonceLen>,
pub(crate) masked_response: MaskedResponse<CS>,
2022-02-25 07:13:22 +01:00
pub(crate) ke2_message: <CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE2Message,
}
2022-01-06 06:19:02 +01:00
/// The answer sent by the client to the server, upon reception of the sealed
/// envelope
2022-04-02 01:10:00 +02:00
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize, serde::Serialize),
serde(
bound(
deserialize = "<CS::KeyExchange as KeyExchange<OprfHash<CS>, \
CS::KeGroup>>::KE3Message: serde::Deserialize<'de>",
serialize = "<CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE3Message: \
serde::Serialize"
),
crate = "serde"
)
)]
2022-01-04 00:50:40 +01:00
#[derive_where(Clone)]
#[derive_where(
Debug, Eq, Hash, PartialEq;
2022-02-25 07:13:22 +01:00
<CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE3Message,
2022-01-04 00:50:40 +01:00
)]
2022-01-06 00:10:57 +01:00
pub struct CredentialFinalization<CS: CipherSuite>
where
2022-02-25 07:13:22 +01:00
<OprfHash<CS> as OutputSizeUser>::OutputSize:
IsLess<U256> + IsLessOrEqual<<OprfHash<CS> as BlockSizeUser>::BlockSize>,
OprfHash<CS>: Hash,
<OprfHash<CS> as CoreProxy>::Core: ProxyHash,
<<OprfHash<CS> as CoreProxy>::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<<OprfHash<CS> as CoreProxy>::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
2022-01-06 00:10:57 +01:00
{
2022-02-25 07:13:22 +01:00
pub(crate) ke3_message: <CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE3Message,
}
////////////////////////////////
// High-level Implementations //
// ========================== //
////////////////////////////////
2022-01-04 00:50:40 +01:00
/// Length of [`RegistrationRequest`] in bytes for serialization.
2022-02-25 07:13:22 +01:00
pub type RegistrationRequestLen<CS: CipherSuite> = <OprfGroup<CS> as Group>::ElemLen;
2022-01-04 00:50:40 +01:00
2022-01-06 00:10:57 +01:00
impl<CS: CipherSuite> RegistrationRequest<CS>
where
2022-02-25 07:13:22 +01:00
<OprfHash<CS> as OutputSizeUser>::OutputSize:
IsLess<U256> + IsLessOrEqual<<OprfHash<CS> as BlockSizeUser>::BlockSize>,
OprfHash<CS>: Hash,
<OprfHash<CS> as CoreProxy>::Core: ProxyHash,
<<OprfHash<CS> as CoreProxy>::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<<OprfHash<CS> as CoreProxy>::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
2022-01-06 00:10:57 +01:00
{
/// Only used for testing purposes
#[cfg(test)]
2022-04-02 01:10:00 +02:00
pub fn get_blinded_element_for_testing(&self) -> voprf::BlindedElement<CS::OprfCs> {
2021-10-25 02:54:32 -07:00
self.blinded_element.clone()
}
2021-06-22 15:48:36 +02:00
2020-11-16 14:05:43 -08:00
/// Serialization into bytes
2022-01-04 00:50:40 +01:00
pub fn serialize(&self) -> GenericArray<u8, RegistrationRequestLen<CS>> {
2022-02-25 07:13:22 +01:00
<OprfGroup<CS> as Group>::serialize_elem(self.blinded_element.value())
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
}
}
2022-01-04 00:50:40 +01:00
/// Length of [`RegistrationResponse`] in bytes for serialization.
pub type RegistrationResponseLen<CS: CipherSuite> =
2022-02-25 07:13:22 +01:00
Sum<<OprfGroup<CS> as Group>::ElemLen, <CS::KeGroup as KeGroup>::PkLen>;
2022-01-04 00:50:40 +01:00
2022-01-06 00:10:57 +01:00
impl<CS: CipherSuite> RegistrationResponse<CS>
where
2022-02-25 07:13:22 +01:00
<OprfHash<CS> as OutputSizeUser>::OutputSize:
IsLess<U256> + IsLessOrEqual<<OprfHash<CS> as BlockSizeUser>::BlockSize>,
OprfHash<CS>: Hash,
<OprfHash<CS> as CoreProxy>::Core: ProxyHash,
<<OprfHash<CS> as CoreProxy>::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<<OprfHash<CS> as CoreProxy>::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
2022-01-06 00:10:57 +01:00
{
2020-11-16 14:05:43 -08:00
/// Serialization into bytes
2022-01-04 00:50:40 +01:00
pub fn serialize(&self) -> GenericArray<u8, RegistrationResponseLen<CS>>
where
// RegistrationResponse: KgPk + KePk
2022-02-25 07:13:22 +01:00
<OprfGroup<CS> as Group>::ElemLen: Add<<CS::KeGroup as KeGroup>::PkLen>,
2022-01-04 00:50:40 +01:00
RegistrationResponseLen<CS>: ArrayLength<u8>,
{
2022-02-25 07:13:22 +01:00
<OprfGroup<CS> as Group>::serialize_elem(self.evaluation_element.value())
2022-04-02 01:10:00 +02:00
.concat(self.server_s_pk.serialize())
2020-11-16 14:05:43 -08:00
}
/// Deserialization from bytes
pub fn deserialize(input: &[u8]) -> Result<Self, ProtocolError> {
2022-02-25 07:13:22 +01:00
let elem_len = <OprfGroup<CS> 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
2022-02-25 07:13:22 +01:00
let server_s_pk = PublicKey::deserialize(&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)]
2022-01-06 06:19:02 +01:00
/// Only used for tests, where we can set the beta value to test for the
/// reflection error case
2022-02-25 07:13:22 +01:00
pub fn set_evaluation_element_for_testing(&self, beta: <OprfGroup<CS> as Group>::Elem) -> 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
}
2022-01-04 00:50:40 +01:00
/// Length of [`RegistrationUpload`] in bytes for serialization.
2022-01-06 00:10:57 +01:00
pub type RegistrationUploadLen<CS: CipherSuite> =
2022-02-25 07:13:22 +01:00
Sum<Sum<<CS::KeGroup as KeGroup>::PkLen, OutputSize<OprfHash<CS>>>, EnvelopeLen<CS>>;
2022-01-06 00:10:57 +01:00
impl<CS: CipherSuite> RegistrationUpload<CS>
where
2022-02-25 07:13:22 +01:00
<OprfHash<CS> as OutputSizeUser>::OutputSize:
IsLess<U256> + IsLessOrEqual<<OprfHash<CS> as BlockSizeUser>::BlockSize>,
OprfHash<CS>: Hash,
<OprfHash<CS> as CoreProxy>::Core: ProxyHash,
<<OprfHash<CS> as CoreProxy>::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<<OprfHash<CS> as CoreProxy>::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
2022-01-06 00:10:57 +01:00
{
2020-11-16 14:05:43 -08:00
/// Serialization into bytes
2022-01-04 00:50:40 +01:00
pub fn serialize(&self) -> GenericArray<u8, RegistrationUploadLen<CS>>
where
// Envelope: Nonce + Hash
2022-02-25 07:13:22 +01:00
NonceLen: Add<OutputSize<OprfHash<CS>>>,
2022-01-04 00:50:40 +01:00
EnvelopeLen<CS>: ArrayLength<u8>,
// RegistrationUpload: (KePk + Hash) + Envelope
2022-02-25 07:13:22 +01:00
<CS::KeGroup as KeGroup>::PkLen: Add<OutputSize<OprfHash<CS>>>,
Sum<<CS::KeGroup as KeGroup>::PkLen, OutputSize<OprfHash<CS>>>:
2022-01-04 00:50:40 +01:00
ArrayLength<u8> + Add<EnvelopeLen<CS>>,
RegistrationUploadLen<CS>: ArrayLength<u8>,
{
self.client_s_pk
2022-04-02 01:10:00 +02:00
.serialize()
2022-01-04 00:50:40 +01:00
.concat(self.masking_key.clone())
.concat(self.envelope.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
let key_len = <CS::KeGroup as KeGroup>::PkLen::USIZE;
2022-02-25 07:13:22 +01:00
let hash_len = OutputSize::<OprfHash<CS>>::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],
),
2022-02-25 07:13:22 +01:00
client_s_pk: PublicKey::deserialize(&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 {
2022-02-25 07:13:22 +01:00
let mut masking_key = Output::<OprfHash<CS>>::default();
rng.fill_bytes(&mut masking_key);
Self {
envelope: Envelope::<CS>::dummy(),
2022-01-04 00:50:40 +01:00
masking_key,
client_s_pk: server_setup.fake_keypair.public().clone(),
}
}
2020-11-16 14:05:43 -08:00
}
2022-01-04 00:50:40 +01:00
/// Length of [`CredentialRequest`] in bytes for serialization.
pub type CredentialRequestLen<CS: CipherSuite> =
2022-02-25 07:13:22 +01:00
Sum<<OprfGroup<CS> as Group>::ElemLen, Ke1MessageLen<CS>>;
2022-01-04 00:50:40 +01:00
2022-01-06 00:10:57 +01:00
impl<CS: CipherSuite> CredentialRequest<CS>
where
2022-02-25 07:13:22 +01:00
<OprfHash<CS> as OutputSizeUser>::OutputSize:
IsLess<U256> + IsLessOrEqual<<OprfHash<CS> as BlockSizeUser>::BlockSize>,
OprfHash<CS>: Hash,
<OprfHash<CS> as CoreProxy>::Core: ProxyHash,
<<OprfHash<CS> as CoreProxy>::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<<OprfHash<CS> as CoreProxy>::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
2022-01-06 00:10:57 +01:00
{
2020-11-16 14:05:43 -08:00
/// Serialization into bytes
2022-01-04 00:50:40 +01:00
pub fn serialize(&self) -> GenericArray<u8, CredentialRequestLen<CS>>
where
// CredentialRequest: KgPk + Ke1Message
2022-02-25 07:13:22 +01:00
<OprfGroup<CS> as Group>::ElemLen: Add<Ke1MessageLen<CS>>,
2022-01-04 00:50:40 +01:00
CredentialRequestLen<CS>: ArrayLength<u8>,
{
2022-02-25 07:13:22 +01:00
<OprfGroup<CS> as Group>::serialize_elem(self.blinded_element.value())
2022-04-02 01:10:00 +02:00
.concat(self.ke1_message.serialize())
2022-01-04 00:50:40 +01:00
}
pub(crate) fn serialize_iter<'a>(
2022-02-25 07:13:22 +01:00
blinded_element: &'a GenericArray<u8, <OprfGroup<CS> as Group>::ElemLen>,
2022-01-04 00:50:40 +01:00
ke1_message: &'a GenericArray<u8, Ke1MessageLen<CS>>,
) -> impl Iterator<Item = &'a [u8]> {
2022-01-06 00:10:57 +01:00
[blinded_element.as_slice(), ke1_message].into_iter()
2020-11-16 14:05:43 -08:00
}
/// Deserialization from bytes
pub fn deserialize(input: &[u8]) -> Result<Self, ProtocolError> {
2022-02-25 07:13:22 +01:00
let elem_len = <OprfGroup<CS> 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")?;
2022-01-06 06:19:02 +01:00
// Check that the message is actually containing an element of the correct
// subgroup
2022-02-25 07:13:22 +01:00
let blinded_element =
2022-04-02 01:10:00 +02:00
voprf::BlindedElement::<CS::OprfCs>::deserialize(&checked_slice[..elem_len])?;
2020-11-16 14:05:43 -08:00
// Throw an error if the identity group element is encountered
2022-02-25 07:13:22 +01:00
if bool::from(<OprfGroup<CS> as Group>::identity_elem().ct_eq(&blinded_element.value())) {
2021-08-22 12:28:19 -07:00
return Err(ProtocolError::IdentityGroupElementError);
}
2020-11-16 14:05:43 -08:00
let ke1_message =
2022-04-02 01:10:00 +02:00
<CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE1Message::deserialize(
&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)]
2022-04-02 01:10:00 +02:00
pub fn get_blinded_element_for_testing(&self) -> voprf::BlindedElement<CS::OprfCs> {
2021-10-25 02:54:32 -07:00
self.blinded_element.clone()
2021-07-12 12:33:19 -07:00
}
2020-11-16 14:05:43 -08:00
}
2022-01-04 00:50:40 +01:00
/// Length of [`CredentialResponse`] in bytes for serialization.
pub type CredentialResponseLen<CS: CipherSuite> =
Sum<CredentialResponseWithoutKeLen<CS>, Ke2MessageLen<CS>>;
pub(crate) type CredentialResponseWithoutKeLen<CS: CipherSuite> =
2022-02-25 07:13:22 +01:00
Sum<Sum<<OprfGroup<CS> as Group>::ElemLen, NonceLen>, MaskedResponseLen<CS>>;
2022-01-04 00:50:40 +01:00
2022-01-06 00:10:57 +01:00
impl<CS: CipherSuite> CredentialResponse<CS>
where
2022-02-25 07:13:22 +01:00
<OprfHash<CS> as OutputSizeUser>::OutputSize:
IsLess<U256> + IsLessOrEqual<<OprfHash<CS> as BlockSizeUser>::BlockSize>,
OprfHash<CS>: Hash,
<OprfHash<CS> as CoreProxy>::Core: ProxyHash,
<<OprfHash<CS> as CoreProxy>::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<<OprfHash<CS> as CoreProxy>::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
2022-01-06 00:10:57 +01:00
{
2020-11-16 14:05:43 -08:00
/// Serialization into bytes
2022-01-04 00:50:40 +01:00
pub fn serialize(&self) -> GenericArray<u8, CredentialResponseLen<CS>>
where
// CredentialResponseWithoutKeLen: (KgPk + Nonce) + MaskedResponse
2022-02-25 07:13:22 +01:00
<OprfGroup<CS> as Group>::ElemLen: Add<NonceLen>,
Sum<<OprfGroup<CS> as Group>::ElemLen, NonceLen>:
2022-01-04 00:50:40 +01:00
ArrayLength<u8> + Add<MaskedResponseLen<CS>>,
CredentialResponseWithoutKeLen<CS>: ArrayLength<u8>,
// MaskedResponse: (Nonce + Hash) + KePk
2022-02-25 07:13:22 +01:00
NonceLen: Add<OutputSize<OprfHash<CS>>>,
Sum<NonceLen, OutputSize<OprfHash<CS>>>:
ArrayLength<u8> + Add<<CS::KeGroup as KeGroup>::PkLen>,
2022-01-04 00:50:40 +01:00
MaskedResponseLen<CS>: ArrayLength<u8>,
// CredentialResponse: CredentialResponseWithoutKeLen + Ke2Message
CredentialResponseWithoutKeLen<CS>: Add<Ke2MessageLen<CS>>,
CredentialResponseLen<CS>: ArrayLength<u8>,
{
2022-02-25 07:13:22 +01:00
<OprfGroup<CS> as Group>::serialize_elem(self.evaluation_element.value())
2022-01-04 00:50:40 +01:00
.concat(self.masking_nonce)
.concat(self.masked_response.serialize())
2022-04-02 01:10:00 +02:00
.concat(self.ke2_message.serialize())
}
2022-01-04 00:50:40 +01:00
pub(crate) fn serialize_without_ke<'a>(
2022-02-25 07:13:22 +01:00
beta: &'a GenericArray<u8, <OprfGroup<CS> as Group>::ElemLen>,
2022-01-04 00:50:40 +01:00
masking_nonce: &'a GenericArray<u8, NonceLen>,
masked_response: &'a MaskedResponse<CS>,
) -> impl Iterator<Item = &'a [u8]> {
2022-01-06 00:10:57 +01:00
[beta.as_slice(), masking_nonce.as_slice()]
2022-01-04 00:50:40 +01:00
.into_iter()
.chain(masked_response.iter())
2020-11-16 14:05:43 -08:00
}
/// Deserialization from bytes
pub fn deserialize(input: &[u8]) -> Result<Self, ProtocolError> {
2022-02-25 07:13:22 +01:00
let elem_len = <OprfGroup<CS> as Group>::ElemLen::USIZE;
2021-10-25 02:54:32 -07:00
let key_len = <CS::KeGroup as KeGroup>::PkLen::USIZE;
2022-02-25 07:13:22 +01:00
let nonce_len = NonceLen::USIZE;
let envelope_len = Envelope::<CS>::len();
let masked_response_len = key_len + envelope_len;
2022-02-25 07:13:22 +01:00
let ke2_message_len = Ke2MessageLen::<CS>::USIZE;
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
2022-01-06 06:19:02 +01:00
// Check that the message is actually containing an element of the correct
// subgroup
2020-11-16 14:05:43 -08:00
let beta_bytes = &checked_slice[..elem_len];
2022-04-02 01:10:00 +02:00
let evaluation_element = voprf::EvaluationElement::<CS::OprfCs>::deserialize(beta_bytes)?;
2020-11-16 14:05:43 -08:00
// Throw an error if the identity group element is encountered
2022-02-25 07:13:22 +01:00
if bool::from(<OprfGroup<CS> as Group>::identity_elem().ct_eq(&evaluation_element.value()))
{
2021-08-22 12:28:19 -07:00
return Err(ProtocolError::IdentityGroupElementError);
}
2022-01-04 00:50:40 +01:00
let masking_nonce =
GenericArray::clone_from_slice(&checked_slice[elem_len..elem_len + nonce_len]);
let masked_response = MaskedResponse::deserialize(
&checked_slice[elem_len + nonce_len..elem_len + nonce_len + masked_response_len],
);
2020-11-16 14:05:43 -08:00
let ke2_message =
2022-04-02 01:10:00 +02:00
<CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE2Message::deserialize(
&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)]
2022-01-06 06:19:02 +01:00
/// Only used for tests, where we can set the beta value to test for the
/// reflection error case
2022-02-25 07:13:22 +01:00
pub fn set_evaluation_element_for_testing(&self, beta: <OprfGroup<CS> as Group>::Elem) -> 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),
2022-01-04 00:50:40 +01:00
masking_nonce: self.masking_nonce,
2021-07-12 12:33:19 -07:00
masked_response: self.masked_response.clone(),
ke2_message: self.ke2_message.clone(),
}
}
2020-11-16 14:05:43 -08:00
}
2022-01-04 00:50:40 +01:00
/// Length of [`CredentialFinalization`] in bytes for serialization.
pub type CredentialFinalizationLen<CS: CipherSuite> = Ke3MessageLen<CS>;
2022-01-06 00:10:57 +01:00
impl<CS: CipherSuite> CredentialFinalization<CS>
where
2022-02-25 07:13:22 +01:00
<OprfHash<CS> as OutputSizeUser>::OutputSize:
IsLess<U256> + IsLessOrEqual<<OprfHash<CS> as BlockSizeUser>::BlockSize>,
OprfHash<CS>: Hash,
<OprfHash<CS> as CoreProxy>::Core: ProxyHash,
<<OprfHash<CS> as CoreProxy>::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<<OprfHash<CS> as CoreProxy>::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
2022-01-06 00:10:57 +01:00
{
2020-11-16 14:05:43 -08:00
/// Serialization into bytes
2022-01-04 00:50:40 +01:00
pub fn serialize(&self) -> GenericArray<u8, CredentialFinalizationLen<CS>> {
2022-04-02 01:10:00 +02:00
self.ke3_message.serialize()
2020-11-16 14:05:43 -08:00
}
/// Deserialization from bytes
pub fn deserialize(input: &[u8]) -> Result<Self, ProtocolError> {
let ke3_message =
2022-04-02 01:10:00 +02:00
<CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE3Message::deserialize(
2022-02-25 07:13:22 +01:00
input,
)?;
Ok(Self { ke3_message })
2020-11-16 14:05:43 -08:00
}
}