// 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. use crate::{ ciphersuite::CipherSuite, errors::{PakeError, ProtocolError}, group::Group, hash::Hash, keypair::{PrivateKey, PublicKey, SecretKey}, }; use rand::{CryptoRng, RngCore}; use zeroize::Zeroize; #[cfg(not(test))] pub type GenerateKe2Result = ( >::KE2State, >::KE2Message, ); #[cfg(test)] pub type GenerateKe2Result = ( >::KE2State, >::KE2Message, Vec, generic_array::GenericArray::OutputSize>, ); #[cfg(not(test))] pub type GenerateKe3Result = (Vec, >::KE3Message); #[cfg(test)] pub type GenerateKe3Result = ( Vec, >::KE3Message, Vec, generic_array::GenericArray::OutputSize>, ); pub trait KeyExchange { type KE1State: FromBytes + ToBytesWithPointers + Zeroize + Clone; type KE2State: FromBytes + ToBytesWithPointers + Zeroize + Clone; type KE1Message: FromBytes + ToBytes + Clone; type KE2Message: FromBytes + ToBytes + Clone; type KE3Message: FromBytes + ToBytes + Clone; fn generate_ke1( rng: &mut R, ) -> Result<(Self::KE1State, Self::KE1Message), ProtocolError>; #[allow(clippy::too_many_arguments, clippy::type_complexity)] fn generate_ke2>( rng: &mut R, l1_bytes: Vec, l2_bytes: Vec, ke1_message: Self::KE1Message, client_s_pk: PublicKey, server_s_sk: S, id_u: Vec, id_s: Vec, context: Vec, ) -> Result, ProtocolError>; #[allow(clippy::too_many_arguments, clippy::type_complexity)] fn generate_ke3( l2_component: Vec, ke2_message: Self::KE2Message, ke1_state: &Self::KE1State, serialized_credential_request: &[u8], server_s_pk: PublicKey, client_s_sk: PrivateKey, id_u: Vec, id_s: Vec, context: Vec, ) -> Result, ProtocolError>; #[allow(clippy::type_complexity)] fn finish_ke( ke3_message: Self::KE3Message, ke2_state: &Self::KE2State, ) -> Result, ProtocolError>; fn ke2_message_size() -> usize; } pub trait FromBytes: Sized { fn from_bytes(input: &[u8]) -> Result; } pub trait ToBytes { fn to_bytes(&self) -> Vec; } pub trait ToBytesWithPointers { fn to_bytes(&self) -> Vec; // Only used for tests to grab raw pointers to data #[cfg(test)] fn as_byte_ptrs(&self) -> Vec<(*const u8, usize)>; }