2020-07-13 15:23:29 -07: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-07-13 15:23:29 -07:00
|
|
|
|
2022-01-06 00:10:57 +01:00
|
|
|
use digest::core_api::BlockSizeUser;
|
|
|
|
|
use digest::Output;
|
|
|
|
|
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};
|
|
|
|
|
|
2021-07-30 12:54:16 +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,
|
2022-01-06 00:10:57 +01:00
|
|
|
Output<D>,
|
|
|
|
|
Output<D>,
|
2021-07-30 12:54:16 +02:00
|
|
|
);
|
|
|
|
|
#[cfg(not(test))]
|
2022-01-06 00:10:57 +01:00
|
|
|
pub type GenerateKe3Result<K, D, G> = (Output<D>, <K as KeyExchange<D, G>>::KE3Message);
|
2021-07-30 12:54:16 +02:00
|
|
|
#[cfg(test)]
|
2021-07-30 13:51:02 +02:00
|
|
|
pub type GenerateKe3Result<K, D, G> = (
|
2022-01-06 00:10:57 +01:00
|
|
|
Output<D>,
|
2021-07-30 13:51:02 +02:00
|
|
|
<K as KeyExchange<D, G>>::KE3Message,
|
2022-01-06 00:10:57 +01:00
|
|
|
Output<D>,
|
|
|
|
|
Output<D>,
|
2021-07-30 13:51:02 +02:00
|
|
|
);
|
2021-07-30 12:54:16 +02:00
|
|
|
|
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-02-25 07:13:22 +01:00
|
|
|
type KE1State: FromBytes + ToBytes + ZeroizeOnDrop + Clone;
|
|
|
|
|
type KE2State: FromBytes + ToBytes + ZeroizeOnDrop + Clone;
|
|
|
|
|
type KE1Message: FromBytes + ToBytes + ZeroizeOnDrop + Clone;
|
|
|
|
|
type KE2Message: FromBytes + ToBytes + ZeroizeOnDrop + Clone;
|
|
|
|
|
type KE3Message: FromBytes + ToBytes + ZeroizeOnDrop + Clone;
|
2020-07-13 15:23:29 -07:00
|
|
|
|
2020-09-19 19:16:35 -04:00
|
|
|
fn generate_ke1<R: RngCore + CryptoRng>(
|
2020-07-13 15:23:29 -07:00
|
|
|
rng: &mut R,
|
|
|
|
|
) -> Result<(Self::KE1State, Self::KE1Message), ProtocolError>;
|
|
|
|
|
|
2022-02-25 07:13:22 +01:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2022-01-04 00:50:40 +01:00
|
|
|
fn generate_ke2<'a, 'b, 'c, 'd, 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>,
|
2021-07-16 13:54:10 +02:00
|
|
|
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],
|
2021-07-30 12:54:16 +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
|
|
|
}
|
|
|
|
|
|
2021-06-14 21:22:21 -07:00
|
|
|
pub trait FromBytes: Sized {
|
2022-01-04 00:50:40 +01:00
|
|
|
fn from_bytes(input: &[u8]) -> Result<Self, ProtocolError>;
|
2021-06-14 21:22:21 -07:00
|
|
|
}
|
|
|
|
|
|
2020-07-13 15:23:29 -07:00
|
|
|
pub trait ToBytes {
|
2022-01-04 00:50:40 +01:00
|
|
|
type Len: ArrayLength<u8>;
|
|
|
|
|
|
|
|
|
|
fn to_bytes(&self) -> GenericArray<u8, Self::Len>;
|
2020-07-13 15:23:29 -07:00
|
|
|
}
|
2022-01-04 00:50:40 +01:00
|
|
|
|
|
|
|
|
pub type Ke1StateLen<CS: CipherSuite> =
|
2022-02-25 07:13:22 +01:00
|
|
|
<<CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE1State as ToBytes>::Len;
|
2022-01-04 00:50:40 +01:00
|
|
|
pub type Ke1MessageLen<CS: CipherSuite> =
|
2022-02-25 07:13:22 +01:00
|
|
|
<<CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE1Message as ToBytes>::Len;
|
2022-01-04 00:50:40 +01:00
|
|
|
pub type Ke2StateLen<CS: CipherSuite> =
|
2022-02-25 07:13:22 +01:00
|
|
|
<<CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE2State as ToBytes>::Len;
|
2022-01-04 00:50:40 +01:00
|
|
|
pub type Ke2MessageLen<CS: CipherSuite> =
|
2022-02-25 07:13:22 +01:00
|
|
|
<<CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE2Message as ToBytes>::Len;
|
2022-01-04 00:50:40 +01:00
|
|
|
pub type Ke3MessageLen<CS: CipherSuite> =
|
2022-02-25 07:13:22 +01:00
|
|
|
<<CS::KeyExchange as KeyExchange<OprfHash<CS>, CS::KeGroup>>::KE3Message as ToBytes>::Len;
|