rust_bottle/
signing.rs

1use crate::errors::Result;
2use rand::RngCore;
3
4/// Trait for types that can sign data.
5///
6/// This trait is implemented by all key types that support signing operations,
7/// such as `Ed25519Key` and `EcdsaP256Key`. The `sign` method produces a
8/// cryptographic signature of the message.
9///
10/// # Example
11///
12/// ```rust
13/// use rust_bottle::signing::Sign;
14/// use rust_bottle::keys::Ed25519Key;
15/// use rand::rngs::OsRng;
16///
17/// let rng = &mut OsRng;
18/// let key = Ed25519Key::generate(rng);
19/// let message = b"Test message";
20///
21/// let signature = key.sign(rng, message).unwrap();
22/// ```
23pub trait Sign {
24    /// Sign the given message.
25    ///
26    /// # Arguments
27    ///
28    /// * `rng` - A random number generator (may be used for non-deterministic signing)
29    /// * `message` - The message to sign
30    ///
31    /// # Returns
32    ///
33    /// * `Ok(Vec<u8>)` - Signature bytes
34    /// * `Err(BottleError::VerifyFailed)` - If signing fails
35    fn sign(&self, rng: &mut dyn RngCore, message: &[u8]) -> Result<Vec<u8>>;
36}
37
38/// Trait for types that can verify signatures.
39///
40/// This trait is implemented by all key types that support signature verification.
41/// The `verify` method checks that a signature is valid for a given message.
42///
43/// # Example
44///
45/// ```rust
46/// use rust_bottle::signing::{Sign, Verify};
47/// use rust_bottle::keys::Ed25519Key;
48/// use rand::rngs::OsRng;
49///
50/// let rng = &mut OsRng;
51/// let key = Ed25519Key::generate(rng);
52/// let message = b"Test message";
53///
54/// let signature = key.sign(rng, message).unwrap();
55/// assert!(key.verify(message, &signature).is_ok());
56/// ```
57pub trait Verify {
58    /// Verify a signature against a message.
59    ///
60    /// # Arguments
61    ///
62    /// * `message` - The original message
63    /// * `signature` - The signature to verify
64    ///
65    /// # Returns
66    ///
67    /// * `Ok(())` - Signature is valid
68    /// * `Err(BottleError::VerifyFailed)` - If signature verification fails
69    fn verify(&self, message: &[u8], signature: &[u8]) -> Result<()>;
70}
71
72/// Generic sign function that works with any signer.
73///
74/// This is a convenience function that calls the `sign` method on any type
75/// implementing the `Sign` trait.
76///
77/// # Arguments
78///
79/// * `rng` - A random number generator
80/// * `signer` - A signer implementing the `Sign` trait
81/// * `message` - The message to sign
82///
83/// # Returns
84///
85/// * `Ok(Vec<u8>)` - Signature bytes
86/// * `Err(BottleError::VerifyFailed)` - If signing fails
87pub fn sign<R: RngCore, S: Sign>(rng: &mut R, signer: &S, message: &[u8]) -> Result<Vec<u8>> {
88    signer.sign(rng, message)
89}
90
91/// Generic verify function that works with any verifier.
92///
93/// This is a convenience function that calls the `verify` method on any type
94/// implementing the `Verify` trait.
95///
96/// # Arguments
97///
98/// * `verifier` - A verifier implementing the `Verify` trait
99/// * `message` - The original message
100/// * `signature` - The signature to verify
101///
102/// # Returns
103///
104/// * `Ok(())` - Signature is valid
105/// * `Err(BottleError::VerifyFailed)` - If signature verification fails
106pub fn verify<V: Verify>(verifier: &V, message: &[u8], signature: &[u8]) -> Result<()> {
107    verifier.verify(message, signature)
108}