Files
opaque-vx/src/key_exchange/group/mod.rs
T

67 lines
2.2 KiB
Rust
Raw Normal View History

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-04-02 01:10:00 +02:00
use zeroize::Zeroize;
2021-10-25 02:54:32 -07:00
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
2022-04-02 01:10:00 +02:00
type Pk: Copy + Zeroize;
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
2022-04-02 01:10:00 +02:00
type Sk: Copy + Zeroize;
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`
2022-04-02 01:10:00 +02:00
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-04-02 01:10:00 +02:00
fn deserialize_pk(bytes: &[u8]) -> 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-04-02 01:10:00 +02:00
fn public_key(sk: Self::Sk) -> Self::Pk;
2022-02-25 07:13:22 +01:00
/// Diffie-Hellman key exchange
2022-04-02 01:10:00 +02:00
fn diffie_hellman(pk: Self::Pk, sk: Self::Sk) -> GenericArray<u8, Self::PkLen>;
2021-10-25 02:54:32 -07:00
/// Serializes `self`
2022-04-02 01:10:00 +02: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
2022-04-02 01:10:00 +02:00
fn deserialize_sk(bytes: &[u8]) -> Result<Self::Sk, InternalError>;
2021-10-25 02:54:32 -07:00
}