Skip to main content

crypto_dispatch/
traits.rs

1// SPDX-FileCopyrightText: Copyright © 2026 ReallyMe LLC. All rights reserved
2//
3// SPDX-License-Identifier: Apache-2.0
4
5use zeroize::Zeroizing;
6
7use crate::AlgorithmError;
8use crypto_core::Algorithm;
9
10/// Adapter contract for a detached-signature algorithm.
11pub trait SignatureAlgorithm {
12    /// The algorithm selector this adapter implements.
13    const ALG: Algorithm;
14
15    /// Generate a keypair, returning `(public_key, secret_key)`; the secret
16    /// zeroizes on drop.
17    fn generate_keypair() -> Result<(Vec<u8>, Zeroizing<Vec<u8>>), AlgorithmError>;
18    /// Reconstruct a keypair from existing secret material.
19    ///
20    /// Algorithms define the exact secret shape: Ed25519, ML-DSA, and similar
21    /// seed-form keys import a seed, while elliptic-curve algorithms import a
22    /// private scalar. This path is not password-based key generation.
23    fn derive_keypair(secret: &[u8]) -> Result<(Vec<u8>, Zeroizing<Vec<u8>>), AlgorithmError> {
24        let _ = secret;
25        Err(AlgorithmError::UnsupportedAlgorithm(Self::ALG))
26    }
27    /// Sign `msg` with `secret`, returning the detached signature bytes.
28    fn sign(secret: &[u8], msg: &[u8]) -> Result<Vec<u8>, AlgorithmError>;
29    /// Verify `sig` over `msg` against `public`; invalid signatures fail closed.
30    fn verify(public: &[u8], msg: &[u8], sig: &[u8]) -> Result<(), AlgorithmError>;
31}