2026-07-01 20:48:44 +02:00
|
|
|
// 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};
|
2026-07-01 11:52:12 +02:00
|
|
|
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
|
|
|
|
2025-04-22 09:08:17 +02:00
|
|
|
use crate::errors::{InternalError, ProtocolError};
|
2022-01-06 06:19:02 +01:00
|
|
|
|
2023-05-27 17:39:04 -05: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
|
2026-07-01 11:52:12 +02:00
|
|
|
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
|
2026-07-01 11:52:12 +02:00
|
|
|
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
|
2026-07-01 11:52:12 +02:00
|
|
|
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
|
|
|
}
|