2026-07-01 20:48:44 +02:00
|
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
|
// Copyright (c) VexaHub and contributors.
|
2025-05-19 22:56:25 +02:00
|
|
|
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
|
|
|
|
|
|
|
|
//! PureEdDSA implementation for [`SigmaI`](crate::SigmaI). Currently only
|
|
|
|
|
//! supports [`Ed25519`](crate::Ed25519).
|
|
|
|
|
|
|
|
|
|
use core::marker::PhantomData;
|
|
|
|
|
|
|
|
|
|
use generic_array::GenericArray;
|
2026-07-01 11:52:12 +02:00
|
|
|
use rand::{CryptoRng, Rng};
|
2025-05-19 22:56:25 +02:00
|
|
|
use zeroize::Zeroize;
|
|
|
|
|
|
|
|
|
|
use self::implementation::PureEddsaImpl;
|
|
|
|
|
use super::{Message, MessageBuilder, SignatureProtocol};
|
|
|
|
|
use crate::ciphersuite::CipherSuite;
|
|
|
|
|
use crate::errors::ProtocolError;
|
|
|
|
|
use crate::key_exchange::group::Group;
|
|
|
|
|
use crate::key_exchange::sigma_i::CachedMessage;
|
|
|
|
|
|
|
|
|
|
/// PureEdDSA for [`SigmaI`](crate::SigmaI).
|
|
|
|
|
///
|
|
|
|
|
/// The ["verification state"](Self::VerifyState) is a [`CachedMessage`],
|
|
|
|
|
/// created by calling [`Message::to_cached()`].
|
|
|
|
|
pub struct PureEddsa<G>(PhantomData<G>);
|
|
|
|
|
|
|
|
|
|
impl<G: PureEddsaImpl> SignatureProtocol for PureEddsa<G> {
|
|
|
|
|
type Group = G;
|
|
|
|
|
type Signature = G::Signature;
|
|
|
|
|
type SignatureLen = G::SignatureLen;
|
|
|
|
|
type VerifyState<CS: CipherSuite, KE: Group> = CachedMessage<CS, KE>;
|
|
|
|
|
|
2026-07-01 11:52:12 +02:00
|
|
|
fn sign<'a, R: CryptoRng + Rng, CS: CipherSuite, KE: Group>(
|
2025-05-19 22:56:25 +02:00
|
|
|
sk: &G::Sk,
|
|
|
|
|
_: &mut R,
|
|
|
|
|
message: &Message<CS, KE>,
|
|
|
|
|
) -> (Self::Signature, Self::VerifyState<CS, KE>) {
|
|
|
|
|
G::sign(sk, message)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn verify<CS: CipherSuite, KE: Group>(
|
|
|
|
|
pk: &G::Pk,
|
|
|
|
|
message_builder: MessageBuilder<'_, CS>,
|
|
|
|
|
state: Self::VerifyState<CS, KE>,
|
|
|
|
|
signature: &Self::Signature,
|
|
|
|
|
) -> Result<(), ProtocolError> {
|
|
|
|
|
G::verify(pk, message_builder, state, signature)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn serialize_signature(signature: &Self::Signature) -> GenericArray<u8, Self::SignatureLen> {
|
|
|
|
|
G::serialize_signature(signature)
|
|
|
|
|
}
|
2026-07-01 11:52:12 +02:00
|
|
|
|
|
|
|
|
fn deserialize_take_signature(bytes: &mut &[u8]) -> Result<Self::Signature, ProtocolError> {
|
|
|
|
|
G::deserialize_take_signature(bytes)
|
|
|
|
|
}
|
2025-05-19 22:56:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(in super::super) mod implementation {
|
|
|
|
|
use generic_array::ArrayLength;
|
|
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
pub trait PureEddsaImpl: Group {
|
|
|
|
|
type Signature: Clone + Zeroize;
|
2026-07-01 11:52:12 +02:00
|
|
|
type SignatureLen: ArrayLength;
|
2025-05-19 22:56:25 +02:00
|
|
|
|
|
|
|
|
fn sign<CS: CipherSuite, KE: Group>(
|
|
|
|
|
sk: &Self::Sk,
|
|
|
|
|
message: &Message<CS, KE>,
|
|
|
|
|
) -> (Self::Signature, CachedMessage<CS, KE>);
|
|
|
|
|
|
|
|
|
|
fn verify<CS: CipherSuite, KE: Group>(
|
|
|
|
|
pk: &Self::Pk,
|
|
|
|
|
message_builder: MessageBuilder<'_, CS>,
|
|
|
|
|
state: CachedMessage<CS, KE>,
|
|
|
|
|
signature: &Self::Signature,
|
|
|
|
|
) -> Result<(), ProtocolError>;
|
|
|
|
|
|
|
|
|
|
fn deserialize_take_signature(bytes: &mut &[u8]) -> Result<Self::Signature, ProtocolError>;
|
|
|
|
|
|
|
|
|
|
fn serialize_signature(signature: &Self::Signature)
|
2025-07-10 21:55:41 +02:00
|
|
|
-> GenericArray<u8, Self::SignatureLen>;
|
2025-05-19 22:56:25 +02:00
|
|
|
}
|
|
|
|
|
}
|