Files
opaque-vx/src/key_exchange/traits.rs
T

120 lines
4.2 KiB
Rust
Raw Normal View History

2023-05-22 23:04:26 -07:00
// Copyright (c) Meta Platforms, Inc. and affiliates.
2020-07-13 15:23:29 -07:00
//
2023-05-22 23:04:26 -07:00
// 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
2021-12-03 14:38:11 -08:00
// License, Version 2.0 found in the LICENSE-APACHE file in the root directory
2023-05-22 23:04:26 -07:00
// of this source tree. You may select, at your option, one of the above-listed
// licenses.
2020-07-13 15:23:29 -07:00
2025-04-15 22:31:37 +02:00
use digest::core_api::BlockSizeUser;
2022-01-06 00:10:57 +01:00
use digest::Output;
2025-04-15 22:31:37 +02:00
use generic_array::typenum::{IsLess, Le, NonZero, U256};
2022-01-04 00:50:40 +01:00
use generic_array::{ArrayLength, GenericArray};
2021-02-11 18:10:48 -08:00
use rand::{CryptoRng, RngCore};
2022-02-25 07:13:22 +01:00
use zeroize::ZeroizeOnDrop;
2020-07-13 15:23:29 -07:00
2022-02-25 07:13:22 +01:00
use crate::ciphersuite::{CipherSuite, OprfHash};
2022-01-06 06:19:02 +01:00
use crate::errors::ProtocolError;
use crate::hash::{Hash, ProxyHash};
use crate::key_exchange::group::KeGroup;
use crate::keypair::{PrivateKey, PublicKey, SecretKey};
2022-01-06 00:10:57 +01:00
pub trait KeyExchange<D: Hash, G: KeGroup>
where
D::Core: ProxyHash,
<D::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
{
2022-04-02 01:10:00 +02:00
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;
2020-07-13 15:23:29 -07:00
fn generate_ke1<OprfCs: voprf::CipherSuite, R: RngCore + CryptoRng>(
2020-07-13 15:23:29 -07:00
rng: &mut R,
2025-04-15 22:31:37 +02:00
) -> Result<(Self::KE1State, Self::KE1Message), ProtocolError>;
2020-07-13 15:23:29 -07:00
2022-02-25 07:13:22 +01:00
#[allow(clippy::too_many_arguments)]
fn generate_ke2<
'a,
'b,
'c,
'd,
OprfCs: voprf::CipherSuite,
R: RngCore + CryptoRng,
S: SecretKey<G>,
>(
2020-07-13 15:23:29 -07:00
rng: &mut R,
2022-01-04 00:50:40 +01:00
l1_bytes: impl Iterator<Item = &'a [u8]>,
l2_bytes: impl Iterator<Item = &'b [u8]>,
2020-07-13 15:23:29 -07:00
ke1_message: Self::KE1Message,
2021-07-06 13:27:13 +02:00
client_s_pk: PublicKey<G>,
server_s_sk: S,
2022-01-04 00:50:40 +01:00
id_u: impl Iterator<Item = &'c [u8]>,
id_s: impl Iterator<Item = &'d [u8]>,
context: &[u8],
2025-04-15 22:31:37 +02:00
) -> Result<GenerateKe2Result<Self, D, G>, ProtocolError<S::Error>>;
2020-07-13 15:23:29 -07:00
2022-02-25 07:13:22 +01:00
#[allow(clippy::too_many_arguments)]
2022-01-04 00:50:40 +01:00
fn generate_ke3<'a, 'b, 'c, 'd>(
l2_component: impl Iterator<Item = &'a [u8]>,
2020-07-13 15:23:29 -07:00
ke2_message: Self::KE2Message,
ke1_state: &Self::KE1State,
2022-01-04 00:50:40 +01:00
serialized_credential_request: impl Iterator<Item = &'b [u8]>,
2021-07-06 13:27:13 +02:00
server_s_pk: PublicKey<G>,
client_s_sk: PrivateKey<G>,
2022-01-04 00:50:40 +01:00
id_u: impl Iterator<Item = &'c [u8]>,
id_s: impl Iterator<Item = &'d [u8]>,
context: &[u8],
2021-07-30 12:54:16 +02:00
) -> Result<GenerateKe3Result<Self, D, G>, ProtocolError>;
2020-07-13 15:23:29 -07:00
fn finish_ke(
ke3_message: Self::KE3Message,
ke2_state: &Self::KE2State,
2022-01-06 00:10:57 +01:00
) -> Result<Output<D>, ProtocolError>;
2020-07-13 15:23:29 -07:00
}
2022-04-02 01:10:00 +02:00
pub trait Deserialize: Sized {
fn deserialize(input: &[u8]) -> Result<Self, ProtocolError>;
}
2022-04-02 01:10:00 +02:00
pub trait Serialize {
2022-01-04 00:50:40 +01:00
type Len: ArrayLength<u8>;
2022-04-02 01:10:00 +02:00
fn serialize(&self) -> GenericArray<u8, Self::Len>;
2020-07-13 15:23:29 -07:00
}
2022-01-04 00:50:40 +01:00
2022-04-02 01:10:00 +02:00
#[cfg(not(test))]
pub type GenerateKe2Result<K, D, G> = (
<K as KeyExchange<D, G>>::KE2State,
<K as KeyExchange<D, G>>::KE2Message,
);
#[cfg(test)]
pub type GenerateKe2Result<K, D, G> = (
<K as KeyExchange<D, G>>::KE2State,
<K as KeyExchange<D, G>>::KE2Message,
Output<D>,
Output<D>,
);
#[cfg(not(test))]
pub type GenerateKe3Result<K, D, G> = (Output<D>, <K as KeyExchange<D, G>>::KE3Message);
#[cfg(test)]
pub type GenerateKe3Result<K, D, G> = (
Output<D>,
<K as KeyExchange<D, G>>::KE3Message,
Output<D>,
Output<D>,
);
2022-01-04 00:50:40 +01:00
pub type Ke1StateLen<CS: CipherSuite> =
2022-04-02 01:10:00 +02:00
<<CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE1State as Serialize>::Len;
2022-01-04 00:50:40 +01:00
pub type Ke1MessageLen<CS: CipherSuite> =
2022-04-02 01:10:00 +02:00
<<CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE1Message as Serialize>::Len;
2022-01-04 00:50:40 +01:00
pub type Ke2StateLen<CS: CipherSuite> =
2022-04-02 01:10:00 +02:00
<<CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE2State as Serialize>::Len;
2022-01-04 00:50:40 +01:00
pub type Ke2MessageLen<CS: CipherSuite> =
2022-04-02 01:10:00 +02:00
<<CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE2Message as Serialize>::Len;
2022-01-04 00:50:40 +01:00
pub type Ke3MessageLen<CS: CipherSuite> =
2022-04-02 01:10:00 +02:00
<<CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE3Message as Serialize>::Len;