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::{
|
2021-06-14 21:22:21 -07:00
|
|
|
ciphersuite::CipherSuite,
|
2020-11-16 11:49:27 -08:00
|
|
|
errors::{PakeError, ProtocolError},
|
2021-01-25 13:20:36 -08:00
|
|
|
group::Group,
|
2020-07-27 15:25:04 -07:00
|
|
|
hash::Hash,
|
2021-06-15 10:48:29 +02:00
|
|
|
keypair::{PrivateKey, PublicKey},
|
2020-07-13 15:23:29 -07:00
|
|
|
};
|
2021-02-11 18:10:48 -08:00
|
|
|
use rand::{CryptoRng, RngCore};
|
2021-06-04 16:29:58 -07:00
|
|
|
use zeroize::Zeroize;
|
2020-07-13 15:23:29 -07:00
|
|
|
|
2021-01-25 13:20:36 -08:00
|
|
|
pub trait KeyExchange<D: Hash, G: Group> {
|
2021-06-15 00:36:17 +02:00
|
|
|
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;
|
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>;
|
|
|
|
|
|
2020-11-16 14:05:43 -08:00
|
|
|
#[allow(clippy::too_many_arguments, clippy::type_complexity)]
|
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,
|
2021-06-15 10:48:29 +02:00
|
|
|
client_s_pk: PublicKey,
|
|
|
|
|
server_s_sk: PrivateKey,
|
2020-11-16 14:05:43 -08:00
|
|
|
id_u: Vec<u8>,
|
|
|
|
|
id_s: Vec<u8>,
|
2021-04-30 15:56:51 -07:00
|
|
|
context: Vec<u8>,
|
|
|
|
|
) -> Result<(Self::KE2State, Self::KE2Message), ProtocolError>;
|
2020-07-13 15:23:29 -07:00
|
|
|
|
2020-11-16 14:05:43 -08:00
|
|
|
#[allow(clippy::too_many_arguments, clippy::type_complexity)]
|
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,
|
2021-02-02 12:12:20 -08:00
|
|
|
serialized_credential_request: &[u8],
|
2021-06-15 10:48:29 +02:00
|
|
|
server_s_pk: PublicKey,
|
|
|
|
|
client_s_sk: PrivateKey,
|
2020-11-16 14:05:43 -08:00
|
|
|
id_u: Vec<u8>,
|
|
|
|
|
id_s: Vec<u8>,
|
2021-04-30 15:56:51 -07:00
|
|
|
context: Vec<u8>,
|
|
|
|
|
) -> Result<(Vec<u8>, Self::KE3Message), 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,
|
2021-01-14 15:30:37 -08:00
|
|
|
) -> Result<Vec<u8>, ProtocolError>;
|
2020-07-13 15:23:29 -07:00
|
|
|
|
|
|
|
|
fn ke2_message_size() -> usize;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-14 21:22:21 -07:00
|
|
|
pub trait FromBytes: Sized {
|
|
|
|
|
fn from_bytes<CS: CipherSuite>(input: &[u8]) -> Result<Self, PakeError>;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-13 15:23:29 -07:00
|
|
|
pub trait ToBytes {
|
|
|
|
|
fn to_bytes(&self) -> Vec<u8>;
|
|
|
|
|
}
|
2021-06-04 16:29:58 -07:00
|
|
|
|
|
|
|
|
pub trait ToBytesWithPointers {
|
|
|
|
|
fn to_bytes(&self) -> Vec<u8>;
|
|
|
|
|
|
|
|
|
|
// Only used for tests to grab raw pointers to data
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
fn as_byte_ptrs(&self) -> Vec<(*const u8, usize)>;
|
|
|
|
|
}
|