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

117 lines
4.1 KiB
Rust
Raw Normal View History

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
2021-10-25 02:54:32 -07:00
use crate::key_exchange::group::KeGroup;
2020-07-13 15:23:29 -07:00
use crate::{
ciphersuite::CipherSuite,
2021-08-22 12:28:19 -07:00
errors::ProtocolError,
2020-07-27 15:25:04 -07:00
hash::Hash,
keypair::{PrivateKey, PublicKey, SecretKey},
2020-07-13 15:23:29 -07:00
};
2022-01-04 00:50:40 +01:00
use digest::Digest;
use generic_array::{ArrayLength, GenericArray};
2021-02-11 18:10:48 -08:00
use rand::{CryptoRng, RngCore};
use zeroize::Zeroize;
2020-07-13 15:23:29 -07:00
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-04 00:50:40 +01:00
GenericArray<u8, <D as Digest>::OutputSize>,
GenericArray<u8, <D as Digest>::OutputSize>,
2021-07-30 12:54:16 +02:00
);
#[cfg(not(test))]
2022-01-04 00:50:40 +01:00
pub type GenerateKe3Result<K, D, G> = (
GenericArray<u8, <D as Digest>::OutputSize>,
<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-04 00:50:40 +01:00
GenericArray<u8, <D as Digest>::OutputSize>,
2021-07-30 13:51:02 +02:00
<K as KeyExchange<D, G>>::KE3Message,
2022-01-04 00:50:40 +01:00
GenericArray<u8, <D as Digest>::OutputSize>,
GenericArray<u8, <D as Digest>::OutputSize>,
2021-07-30 13:51:02 +02:00
);
2021-07-30 12:54:16 +02:00
2021-10-25 02:54:32 -07:00
pub trait KeyExchange<D: Hash, G: KeGroup> {
type KE1State: FromBytes + ToBytes + Zeroize + Clone;
type KE2State: FromBytes + ToBytes + Zeroize + Clone;
2022-01-04 00:50:40 +01:00
type KE1Message: FromBytes + ToBytes + Zeroize + Clone;
2021-06-15 00:36:17 +02:00
type KE2Message: FromBytes + ToBytes + Clone;
type KE3Message: FromBytes + ToBytes + Clone;
2020-07-13 15:23:29 -07:00
fn generate_ke1<R: RngCore + CryptoRng>(
2020-07-13 15:23:29 -07:00
rng: &mut R,
) -> Result<(Self::KE1State, Self::KE1Message), ProtocolError>;
2020-11-16 14:05:43 -08:00
#[allow(clippy::too_many_arguments, clippy::type_complexity)]
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>,
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
2020-11-16 14:05:43 -08:00
#[allow(clippy::too_many_arguments, clippy::type_complexity)]
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
2020-11-16 14:05:43 -08:00
#[allow(clippy::type_complexity)]
2020-07-13 15:23:29 -07:00
fn finish_ke(
ke3_message: Self::KE3Message,
ke2_state: &Self::KE2State,
2022-01-04 00:50:40 +01:00
) -> Result<GenericArray<u8, D::OutputSize>, ProtocolError>;
2020-07-13 15:23:29 -07:00
fn ke2_message_size() -> usize;
}
pub trait FromBytes: Sized {
2022-01-04 00:50:40 +01:00
fn from_bytes(input: &[u8]) -> Result<Self, ProtocolError>;
}
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
#[allow(dead_code, type_alias_bounds)]
pub type Ke1StateLen<CS: CipherSuite> =
<<CS::KeyExchange as KeyExchange<CS::Hash, CS::KeGroup>>::KE1State as ToBytes>::Len;
#[allow(type_alias_bounds)]
pub type Ke1MessageLen<CS: CipherSuite> =
<<CS::KeyExchange as KeyExchange<CS::Hash, CS::KeGroup>>::KE1Message as ToBytes>::Len;
#[allow(type_alias_bounds)]
#[allow(type_alias_bounds)]
pub type Ke2StateLen<CS: CipherSuite> =
<<CS::KeyExchange as KeyExchange<CS::Hash, CS::KeGroup>>::KE2State as ToBytes>::Len;
#[allow(type_alias_bounds)]
pub type Ke2MessageLen<CS: CipherSuite> =
<<CS::KeyExchange as KeyExchange<CS::Hash, CS::KeGroup>>::KE2Message as ToBytes>::Len;
#[allow(type_alias_bounds)]
pub type Ke3MessageLen<CS: CipherSuite> =
<<CS::KeyExchange as KeyExchange<CS::Hash, CS::KeGroup>>::KE3Message as ToBytes>::Len;