// Copyright (c) Meta Platforms, Inc. and affiliates. // // This source code is dual-licensed under either the MIT license found in the // LICENSE-MIT file in the root directory of this source tree or the Apache // License, Version 2.0 found in the LICENSE-APACHE file in the root directory // of this source tree. You may select, at your option, one of the above-listed // licenses. use digest::core_api::BlockSizeUser; use digest::Output; use generic_array::typenum::{IsLess, Le, NonZero, U256}; use generic_array::{ArrayLength, GenericArray}; use rand::{CryptoRng, RngCore}; use zeroize::ZeroizeOnDrop; use crate::ciphersuite::{CipherSuite, OprfHash}; use crate::errors::ProtocolError; use crate::hash::{Hash, ProxyHash}; use crate::key_exchange::group::KeGroup; use crate::keypair::{PrivateKey, PublicKey, SecretKey}; pub trait KeyExchange where D::Core: ProxyHash, ::BlockSize: IsLess, Le<::BlockSize, U256>: NonZero, { type KE1State: Deserialize + Serialize + ZeroizeOnDrop + Clone; type KE2State: Deserialize + Serialize + ZeroizeOnDrop + Clone; type KE1Message: Deserialize + Serialize + ZeroizeOnDrop + Clone; type KE2Message: Deserialize + Serialize + ZeroizeOnDrop + Clone; type KE3Message: Deserialize + Serialize + ZeroizeOnDrop + Clone; fn generate_ke1( rng: &mut R, ) -> Result<(Self::KE1State, Self::KE1Message), ProtocolError>; #[allow(clippy::too_many_arguments)] fn generate_ke2< 'a, 'b, 'c, 'd, OprfCs: voprf::CipherSuite, R: RngCore + CryptoRng, S: SecretKey, >( rng: &mut R, l1_bytes: impl Iterator, l2_bytes: impl Iterator, ke1_message: Self::KE1Message, client_s_pk: PublicKey, server_s_sk: S, id_u: impl Iterator, id_s: impl Iterator, context: &[u8], ) -> Result, ProtocolError>; #[allow(clippy::too_many_arguments)] fn generate_ke3<'a, 'b, 'c, 'd>( l2_component: impl Iterator, ke2_message: Self::KE2Message, ke1_state: &Self::KE1State, serialized_credential_request: impl Iterator, server_s_pk: PublicKey, client_s_sk: PrivateKey, id_u: impl Iterator, id_s: impl Iterator, context: &[u8], ) -> Result, ProtocolError>; fn finish_ke( ke3_message: Self::KE3Message, ke2_state: &Self::KE2State, ) -> Result, ProtocolError>; } pub trait Deserialize: Sized { fn deserialize(input: &[u8]) -> Result; } pub trait Serialize { type Len: ArrayLength; fn serialize(&self) -> GenericArray; } #[cfg(not(test))] pub type GenerateKe2Result = ( >::KE2State, >::KE2Message, ); #[cfg(test)] pub type GenerateKe2Result = ( >::KE2State, >::KE2Message, Output, Output, ); #[cfg(not(test))] pub type GenerateKe3Result = (Output, >::KE3Message); #[cfg(test)] pub type GenerateKe3Result = ( Output, >::KE3Message, Output, Output, ); pub type Ke1StateLen = <, CS::KeGroup>>::KE1State as Serialize>::Len; pub type Ke1MessageLen = <, CS::KeGroup>>::KE1Message as Serialize>::Len; pub type Ke2StateLen = <, CS::KeGroup>>::KE2State as Serialize>::Len; pub type Ke2MessageLen = <, CS::KeGroup>>::KE2Message as Serialize>::Len; pub type Ke3MessageLen = <, CS::KeGroup>>::KE3Message as Serialize>::Len;