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

60 lines
1.9 KiB
Rust
Raw Normal View History

// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (c) VexaHub and contributors.
2023-05-22 23:04:26 -07:00
// Copyright (c) Meta Platforms, Inc. and affiliates.
2021-10-25 02:54:32 -07:00
2025-05-19 22:56:25 +02:00
//! Includes the [`Group`] trait and definitions for the key exchange groups
2021-10-25 02:54:32 -07:00
2022-12-19 18:03:11 +01:00
#[cfg(feature = "curve25519")]
pub mod curve25519;
2025-05-19 22:56:25 +02:00
#[cfg(feature = "ed25519")]
pub mod ed25519;
pub mod elliptic_curve;
2022-02-25 07:13:22 +01:00
#[cfg(feature = "ristretto255")]
pub mod ristretto255;
2021-10-25 02:54:32 -07:00
use generic_array::{ArrayLength, GenericArray};
use hybrid_array::ArraySize;
use rand::{CryptoRng, Rng};
2025-07-17 22:15:30 +02:00
use zeroize::ZeroizeOnDrop;
2021-10-25 02:54:32 -07:00
use crate::errors::{InternalError, ProtocolError};
2022-01-06 06:19:02 +01:00
const STR_OPAQUE_DERIVE_AUTH_KEY_PAIR: [u8; 33] = *b"OPAQUE-DeriveDiffieHellmanKeyPair";
2021-10-25 02:54:32 -07:00
/// A group representation for use in the key exchange
2025-05-19 22:56:25 +02:00
pub trait Group {
2022-02-25 07:13:22 +01:00
/// Public key
2025-07-17 22:15:30 +02:00
type Pk: Clone;
2021-10-25 02:54:32 -07:00
/// Length of the public key
type PkLen: ArrayLength + ArraySize;
2022-02-25 07:13:22 +01:00
/// Secret key
2025-07-17 22:15:30 +02:00
type Sk: Clone + ZeroizeOnDrop;
2021-10-25 02:54:32 -07:00
/// Length of the secret key
type SkLen: ArrayLength + ArraySize;
2022-02-25 07:13:22 +01:00
/// Serializes `self`
2025-07-17 22:15:30 +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
2025-05-19 22:56:25 +02:00
///
/// The deserialized bytes must be taken from `bytes`.
fn deserialize_take_pk(bytes: &mut &[u8]) -> Result<Self::Pk, ProtocolError>;
2021-10-25 02:54:32 -07:00
/// Generate a random secret key
fn random_sk<R: Rng + CryptoRng>(rng: &mut R) -> Self::Sk;
2022-02-25 07:13:22 +01:00
2025-05-19 22:56:25 +02:00
/// Deterministically derive a [`Self::Sk`] from `seed`.
fn derive_scalar(seed: GenericArray<u8, Self::SkLen>) -> Result<Self::Sk, InternalError>;
2022-04-17 16:23:31 -07:00
2021-10-25 02:54:32 -07:00
/// Return a public key from its secret key
2025-07-17 22:15:30 +02:00
fn public_key(sk: &Self::Sk) -> Self::Pk;
2022-02-25 07:13:22 +01:00
2021-10-25 02:54:32 -07:00
/// Serializes `self`
2025-07-17 22:15:30 +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
2025-05-19 22:56:25 +02:00
///
/// The deserialized bytes must be taken from `bytes`.
fn deserialize_take_sk(bytes: &mut &[u8]) -> Result<Self::Sk, ProtocolError>;
2022-04-17 16:23:31 -07:00
}