2021-10-25 02:54:32 -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.
|
2021-10-25 02:54:32 -07:00
|
|
|
|
2022-01-06 06:19:02 +01:00
|
|
|
//! Includes the KeGroup trait and definitions for the key exchange groups
|
2021-10-25 02:54:32 -07:00
|
|
|
|
2022-02-25 07:13:22 +01:00
|
|
|
mod elliptic_curve;
|
|
|
|
|
#[cfg(feature = "ristretto255")]
|
|
|
|
|
pub mod ristretto255;
|
|
|
|
|
#[cfg(feature = "x25519")]
|
|
|
|
|
pub mod x25519;
|
|
|
|
|
|
|
|
|
|
use digest::core_api::BlockSizeUser;
|
|
|
|
|
use digest::Digest;
|
|
|
|
|
use generic_array::typenum::{IsLess, IsLessOrEqual, U256};
|
2021-10-25 02:54:32 -07:00
|
|
|
use generic_array::{ArrayLength, GenericArray};
|
|
|
|
|
use rand::{CryptoRng, RngCore};
|
|
|
|
|
|
2022-01-06 06:19:02 +01:00
|
|
|
use crate::errors::InternalError;
|
|
|
|
|
|
2021-10-25 02:54:32 -07:00
|
|
|
/// A group representation for use in the key exchange
|
2022-02-25 07:13:22 +01:00
|
|
|
pub trait KeGroup {
|
|
|
|
|
/// Public key
|
|
|
|
|
type Pk: Clone;
|
2021-10-25 02:54:32 -07:00
|
|
|
/// Length of the public key
|
2022-02-25 07:13:22 +01:00
|
|
|
type PkLen: ArrayLength<u8>;
|
|
|
|
|
/// Secret key
|
|
|
|
|
type Sk: Clone;
|
2021-10-25 02:54:32 -07:00
|
|
|
/// Length of the secret key
|
2022-02-25 07:13:22 +01:00
|
|
|
type SkLen: ArrayLength<u8>;
|
|
|
|
|
|
|
|
|
|
/// Serializes `self`
|
|
|
|
|
fn serialize_pk(pk: &Self::Pk) -> GenericArray<u8, Self::PkLen>;
|
2021-10-25 02:54:32 -07:00
|
|
|
|
|
|
|
|
/// Return a public key from its fixed-length bytes representation
|
2022-02-25 07:13:22 +01:00
|
|
|
fn deserialize_pk(bytes: &GenericArray<u8, Self::PkLen>) -> Result<Self::Pk, InternalError>;
|
2021-10-25 02:54:32 -07:00
|
|
|
|
|
|
|
|
/// Generate a random secret key
|
2022-02-25 07:13:22 +01:00
|
|
|
fn random_sk<R: RngCore + CryptoRng>(rng: &mut R) -> Self::Sk;
|
|
|
|
|
|
|
|
|
|
/// Hashes a slice of pseudo-random bytes to a scalar
|
|
|
|
|
///
|
|
|
|
|
/// # Errors
|
|
|
|
|
/// [`InternalError::HashToScalar`] if the `input` is empty or longer then
|
|
|
|
|
/// [`u16::MAX`].
|
|
|
|
|
fn hash_to_scalar<H>(input: &[&[u8]], dst: &[u8]) -> Result<Self::Sk, InternalError>
|
|
|
|
|
where
|
|
|
|
|
H: Digest + BlockSizeUser,
|
|
|
|
|
H::OutputSize: IsLess<U256> + IsLessOrEqual<H::BlockSize>;
|
2021-10-25 02:54:32 -07:00
|
|
|
|
|
|
|
|
/// Return a public key from its secret key
|
2022-02-25 07:13:22 +01:00
|
|
|
fn public_key(sk: &Self::Sk) -> Self::Pk;
|
|
|
|
|
|
|
|
|
|
/// Diffie-Hellman key exchange
|
|
|
|
|
fn diffie_hellman(pk: &Self::Pk, sk: &Self::Sk) -> GenericArray<u8, Self::PkLen>;
|
|
|
|
|
|
|
|
|
|
/// Zeroize secret key on drop.
|
|
|
|
|
fn zeroize_sk_on_drop(sk: &mut Self::Sk);
|
2021-10-25 02:54:32 -07:00
|
|
|
|
|
|
|
|
/// Serializes `self`
|
2022-02-25 07:13:22 +01:00
|
|
|
fn serialize_sk(sk: &Self::Sk) -> GenericArray<u8, Self::SkLen>;
|
2021-10-25 02:54:32 -07:00
|
|
|
|
2022-02-25 07:13:22 +01:00
|
|
|
/// Return a public key from its fixed-length bytes representation
|
|
|
|
|
fn deserialize_sk(bytes: &GenericArray<u8, Self::SkLen>) -> Result<Self::Sk, InternalError>;
|
2021-10-25 02:54:32 -07:00
|
|
|
}
|