// Copyright (c) Meta Platforms, Inc. and affiliates. // // This source code is dual-licensed under either the MIT license found in the // LICENSE-MIT file in the root directory of this source tree or the Apache // License, Version 2.0 found in the LICENSE-APACHE file in the root directory // of this source tree. You may select, at your option, one of the above-listed // licenses. //! HashEdDSA implementation for [`SigmaI`](crate::SigmaI). Currently only //! supports [`Ed25519`](crate::Ed25519). use core::marker::PhantomData; use generic_array::GenericArray; use rand::{CryptoRng, Rng}; use zeroize::Zeroize; use self::implementation::HashEddsaImpl; use super::{Message, MessageBuilder, SignatureProtocol}; use crate::ciphersuite::CipherSuite; use crate::errors::ProtocolError; use crate::key_exchange::group::Group; /// HashEdDSA for [`SigmaI`](crate::SigmaI). /// /// The ["verification state"](Self::VerifyState) is the pre-hash for the /// message to be verified. pub struct HashEddsa(PhantomData); impl SignatureProtocol for HashEddsa { type Group = G; type Signature = G::Signature; type SignatureLen = G::SignatureLen; type VerifyState = G::VerifyState; fn sign<'a, R: CryptoRng + Rng, CS: CipherSuite, KE: Group>( sk: &::Sk, _: &mut R, message: &Message, ) -> (Self::Signature, Self::VerifyState) { G::sign(sk, message) } fn verify( pk: &::Pk, _: MessageBuilder<'_, CS>, state: Self::VerifyState, signature: &Self::Signature, ) -> Result<(), ProtocolError> { G::verify(pk, state, signature) } fn serialize_signature(signature: &Self::Signature) -> GenericArray { G::serialize_signature(signature) } fn deserialize_take_signature(bytes: &mut &[u8]) -> Result { G::deserialize_take_signature(bytes) } } pub(in super::super) mod implementation { use generic_array::ArrayLength; use super::*; pub trait HashEddsaImpl: Group { type Signature: Clone + Zeroize; type SignatureLen: ArrayLength; type VerifyState: Clone + Zeroize; fn sign( sk: &Self::Sk, message: &Message, ) -> (Self::Signature, Self::VerifyState); fn verify( pk: &Self::Pk, state: Self::VerifyState, signature: &Self::Signature, ) -> Result<(), ProtocolError>; fn deserialize_take_signature(bytes: &mut &[u8]) -> Result; fn serialize_signature(signature: &Self::Signature) -> GenericArray; } }