2020-07-13 15:23:29 -07:00
|
|
|
// 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::{
|
|
|
|
|
errors::{InternalPakeError, ProtocolError},
|
2020-07-27 15:25:04 -07:00
|
|
|
hash::Hash,
|
2020-07-13 15:23:29 -07:00
|
|
|
keypair::{Key, KeyPair},
|
|
|
|
|
};
|
|
|
|
|
use rand_core::{CryptoRng, RngCore};
|
|
|
|
|
|
|
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
|
2020-09-19 19:16:35 -04:00
|
|
|
pub trait KeyExchange<D: Hash, KeyFormat: KeyPair<Repr = Key>> {
|
2020-07-13 15:23:29 -07:00
|
|
|
type KE1State: TryFrom<Vec<u8>, Error = InternalPakeError> + ToBytes;
|
|
|
|
|
type KE2State: TryFrom<Vec<u8>, Error = ProtocolError> + ToBytes;
|
|
|
|
|
type KE1Message: TryFrom<Vec<u8>, Error = InternalPakeError> + ToBytes;
|
|
|
|
|
type KE2Message: TryFrom<Vec<u8>, Error = ProtocolError> + ToBytes;
|
|
|
|
|
type KE3Message: TryFrom<Vec<u8>, Error = ProtocolError> + ToBytes;
|
|
|
|
|
|
2020-09-19 19:16:35 -04:00
|
|
|
fn generate_ke1<R: RngCore + CryptoRng>(
|
2020-07-13 15:23:29 -07:00
|
|
|
l1_component: Vec<u8>,
|
|
|
|
|
rng: &mut R,
|
|
|
|
|
) -> Result<(Self::KE1State, Self::KE1Message), ProtocolError>;
|
|
|
|
|
|
2020-09-19 19:16:35 -04:00
|
|
|
fn generate_ke2<R: RngCore + CryptoRng>(
|
2020-07-13 15:23:29 -07:00
|
|
|
rng: &mut R,
|
|
|
|
|
l1_bytes: Vec<u8>,
|
|
|
|
|
l2_bytes: Vec<u8>,
|
|
|
|
|
ke1_message: Self::KE1Message,
|
|
|
|
|
client_s_pk: KeyFormat::Repr,
|
|
|
|
|
server_s_sk: KeyFormat::Repr,
|
|
|
|
|
) -> Result<(Self::KE2State, Self::KE2Message), ProtocolError>;
|
|
|
|
|
|
2020-09-19 19:16:35 -04:00
|
|
|
fn generate_ke3(
|
2020-07-13 15:23:29 -07:00
|
|
|
l2_component: Vec<u8>,
|
|
|
|
|
ke2_message: Self::KE2Message,
|
|
|
|
|
ke1_state: &Self::KE1State,
|
|
|
|
|
server_s_pk: KeyFormat::Repr,
|
|
|
|
|
client_s_sk: KeyFormat::Repr,
|
|
|
|
|
) -> Result<(Vec<u8>, Self::KE3Message), ProtocolError>;
|
|
|
|
|
|
|
|
|
|
fn finish_ke(
|
|
|
|
|
ke3_message: Self::KE3Message,
|
|
|
|
|
ke2_state: &Self::KE2State,
|
|
|
|
|
) -> Result<Vec<u8>, ProtocolError>;
|
|
|
|
|
|
|
|
|
|
fn ke1_state_size() -> usize;
|
|
|
|
|
|
|
|
|
|
fn ke2_message_size() -> usize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub trait ToBytes {
|
|
|
|
|
fn to_bytes(&self) -> Vec<u8>;
|
|
|
|
|
}
|