Skip to main content

signature/
signer.rs

1//! Traits for generating digital signatures.
2
3use crate::error::Error;
4
5#[cfg(feature = "digest")]
6use crate::digest::Update;
7
8#[cfg(feature = "rand_core")]
9use crate::rand_core::{CryptoRng, TryCryptoRng};
10
11/// Sign the provided message bytestring using `Self` (e.g. a cryptographic key or connection to an
12/// HSM), returning a digital signature.
13pub trait Signer<S> {
14    /// Sign the given message and return a digital signature.
15    fn sign(&self, msg: &[u8]) -> S {
16        self.try_sign(msg).expect("signature operation failed")
17    }
18
19    /// Attempt to sign the given message, returning a digital signature on success, or an error if
20    /// something went wrong.
21    ///
22    /// The main intended use case for signing errors is when communicating with external signers,
23    /// e.g. cloud KMS, HSMs, or other hardware tokens.
24    ///
25    /// # Errors
26    /// Returns implementation-specific errors in the event signing failed (e.g. KMS or HSM
27    /// communication error).
28    fn try_sign(&self, msg: &[u8]) -> Result<S, Error>;
29}
30
31/// Equivalent of [`Signer`] but the message is provided in non-contiguous byte slices.
32pub trait MultipartSigner<S> {
33    /// Equivalent of [`Signer::sign()`] but the message is provided in non-contiguous byte slices.
34    fn multipart_sign(&self, msg: &[&[u8]]) -> S {
35        self.try_multipart_sign(msg)
36            .expect("signature operation failed")
37    }
38
39    /// Equivalent of [`Signer::try_sign()`] but the message is provided in non-contiguous byte
40    /// slices.
41    ///
42    /// # Errors
43    /// Returns implementation-specific errors in the event signing failed (e.g. KMS or HSM
44    /// communication error).
45    fn try_multipart_sign(&self, msg: &[&[u8]]) -> Result<S, Error>;
46}
47
48/// Sign the provided message bytestring using `&mut Self` (e.g. an evolving cryptographic key such
49/// as a stateful hash-based signature), returning a digital signature.
50pub trait SignerMut<S> {
51    /// Sign the given message, update the state, and return a digital signature.
52    ///
53    /// # Panics
54    /// In the event of a signing error.
55    fn sign(&mut self, msg: &[u8]) -> S {
56        self.try_sign(msg).expect("signature operation failed")
57    }
58
59    /// Attempt to sign the given message, updating the state, and returning a digital signature on
60    /// success, or an error if something went wrong.
61    ///
62    /// # Errors
63    /// Signing can fail, e.g. if the number of time periods allowed by the current key is exceeded.
64    fn try_sign(&mut self, msg: &[u8]) -> Result<S, Error>;
65}
66
67/// Sign the given prehashed message `Digest` using `Self`.
68///
69/// ## Notes
70///
71/// This trait is primarily intended for signature algorithms based on the [Fiat-Shamir heuristic],
72/// a method for converting an interactive challenge/response-based proof-of-knowledge protocol into
73/// an offline digital signature through the use of a random oracle, i.e. a digest function.
74///
75/// The security of such protocols critically rests upon the inability of an attacker to solve for
76/// the output of the random oracle, as generally otherwise such signature algorithms are a system
77/// of linear equations and therefore doing so would allow the attacker to trivially forge
78/// signatures.
79///
80/// To prevent misuse which would potentially allow this to be possible, this API accepts a `Digest`
81/// instance, rather than a raw digest value.
82///
83/// [Fiat-Shamir heuristic]: https://en.wikipedia.org/wiki/Fiat%E2%80%93Shamir_heuristic
84#[cfg(feature = "digest")]
85pub trait DigestSigner<D: Update, S> {
86    /// Sign a message by updating the received `Digest` with it, returning a signature.
87    ///
88    /// The given function can be invoked multiple times. It is expected that in each invocation the
89    /// `Digest` is updated with the entire equal message.
90    ///
91    /// # Panics
92    /// In the event of a signing error.
93    fn sign_digest<F: Fn(&mut D)>(&self, f: F) -> S {
94        self.try_sign_digest(|digest| {
95            f(digest);
96            Ok(())
97        })
98        .expect("signature operation failed")
99    }
100
101    /// Attempt to sign a message by updating the received `Digest` with it, returning a digital
102    /// signature on success, or an error if something went wrong.
103    ///
104    /// The given function can be invoked multiple times. It is expected that in each invocation the
105    /// `Digest` is updated with the entire equal message.
106    ///
107    /// # Errors
108    /// Returns implementation-specific errors in the event signing failed (e.g. KMS or HSM
109    /// communication error).
110    fn try_sign_digest<F: Fn(&mut D) -> Result<(), Error>>(&self, f: F) -> Result<S, Error>;
111}
112
113/// Sign the given message using the provided external randomness source.
114#[cfg(feature = "rand_core")]
115pub trait RandomizedSigner<S> {
116    /// Sign the given message and return a digital signature
117    fn sign_with_rng<R: CryptoRng + ?Sized>(&self, rng: &mut R, msg: &[u8]) -> S {
118        self.try_sign_with_rng(rng, msg)
119            .expect("signature operation failed")
120    }
121
122    /// Attempt to sign the given message, returning a digital signature on success, or an error if
123    /// something went wrong.
124    ///
125    /// The main intended use case for signing errors is when communicating with external signers,
126    /// e.g. cloud KMS, HSMs, or other hardware tokens.
127    ///
128    /// # Errors
129    /// Returns implementation-specific errors in the event signing failed (e.g. KMS or HSM
130    /// communication error), or if the provided `rng` experiences an internal failure.
131    fn try_sign_with_rng<R: TryCryptoRng + ?Sized>(
132        &self,
133        rng: &mut R,
134        msg: &[u8],
135    ) -> Result<S, Error>;
136}
137
138/// Equivalent of [`RandomizedSigner`] but the message is provided in non-contiguous byte slices.
139#[cfg(feature = "rand_core")]
140pub trait RandomizedMultipartSigner<S> {
141    /// Equivalent of [`RandomizedSigner::sign_with_rng()`] but the message is provided in
142    /// non-contiguous byte slices.
143    fn multipart_sign_with_rng<R: CryptoRng + ?Sized>(&self, rng: &mut R, msg: &[&[u8]]) -> S {
144        self.try_multipart_sign_with_rng(rng, msg)
145            .expect("signature operation failed")
146    }
147
148    /// Equivalent of [`RandomizedSigner::try_sign_with_rng()`] but the message is provided in
149    /// non-contiguous byte slices.
150    ///
151    /// # Errors
152    /// Returns implementation-specific errors in the event signing failed (e.g. KMS or HSM
153    /// communication error), or if the provided `rng` experiences an internal failure.
154    fn try_multipart_sign_with_rng<R: TryCryptoRng + ?Sized>(
155        &self,
156        rng: &mut R,
157        msg: &[&[u8]],
158    ) -> Result<S, Error>;
159}
160
161/// Combination of [`DigestSigner`] and [`RandomizedSigner`] with support for computing a signature
162/// over a digest which requires entropy from an RNG.
163#[cfg(all(feature = "digest", feature = "rand_core"))]
164pub trait RandomizedDigestSigner<D: Update, S> {
165    /// Sign a message by updating the received `Digest` with it, returning a signature.
166    ///
167    /// The given function can be invoked multiple times. It is expected that in each invocation the
168    /// `Digest` is updated with the entire equal message.
169    ///
170    /// # Panics
171    /// In the event of a signing error.
172    fn sign_digest_with_rng<R: CryptoRng + ?Sized, F: Fn(&mut D)>(&self, rng: &mut R, f: F) -> S {
173        self.try_sign_digest_with_rng(rng, |digest| {
174            f(digest);
175            Ok(())
176        })
177        .expect("signature operation failed")
178    }
179
180    /// Attempt to sign a message by updating the received `Digest` with it, returning a digital
181    /// signature on success, or an error if something went wrong.
182    ///
183    /// The given function can be invoked multiple times. It is expected that in each invocation the
184    /// `Digest` is updated with the entire equal message.
185    ///
186    /// # Errors
187    /// Returns implementation-specific errors in the event signing failed (e.g. KMS or HSM
188    /// communication error), or if the provided `rng` experiences an internal failure.
189    fn try_sign_digest_with_rng<R: TryCryptoRng + ?Sized, F: Fn(&mut D) -> Result<(), Error>>(
190        &self,
191        rng: &mut R,
192        f: F,
193    ) -> Result<S, Error>;
194}
195
196/// Sign the provided message bytestring using `&mut Self` (e.g. an evolving cryptographic key such
197/// as a stateful hash-based signature), and a per-signature randomizer, returning a digital
198/// signature.
199#[cfg(feature = "rand_core")]
200pub trait RandomizedSignerMut<S> {
201    /// Sign the given message, update the state, and return a digital signature.
202    ///
203    /// # Panics
204    /// In the event of a signing error.
205    fn sign_with_rng<R: CryptoRng + ?Sized>(&mut self, rng: &mut R, msg: &[u8]) -> S {
206        self.try_sign_with_rng(rng, msg)
207            .expect("signature operation failed")
208    }
209
210    /// Attempt to sign the given message, updating the state, and returning a digital signature on
211    /// success, or an error if something went wrong.
212    ///
213    /// # Errors
214    /// Signing can fail, e.g. if the number of time periods allowed by the current key is exceeded,
215    /// or if the provided `rng` experiences an internal failure.
216    fn try_sign_with_rng<R: TryCryptoRng + ?Sized>(
217        &mut self,
218        rng: &mut R,
219        msg: &[u8],
220    ) -> Result<S, Error>;
221}
222
223/// Equivalent of [`RandomizedSignerMut`] but the message is provided in non-contiguous byte slices.
224#[cfg(feature = "rand_core")]
225pub trait RandomizedMultipartSignerMut<S> {
226    /// Equivalent of [`RandomizedSignerMut::sign_with_rng()`] but the message is provided in
227    /// non-contiguous byte slices.
228    ///
229    /// # Panics
230    /// In the event of a signing error.
231    fn multipart_sign_with_rng<R: CryptoRng + ?Sized>(&mut self, rng: &mut R, msg: &[&[u8]]) -> S {
232        self.try_multipart_sign_with_rng(rng, msg)
233            .expect("signature operation failed")
234    }
235
236    /// Equivalent of [`RandomizedSignerMut::try_sign_with_rng()`] but the message is provided in
237    /// non-contiguous byte slices.
238    ///
239    /// # Errors
240    /// Signing can fail, e.g. if the number of time periods allowed by the current key is exceeded,
241    /// or if the provided `rng` experiences an internal failure.
242    fn try_multipart_sign_with_rng<R: TryCryptoRng + ?Sized>(
243        &mut self,
244        rng: &mut R,
245        msg: &[&[u8]],
246    ) -> Result<S, Error>;
247}
248
249/// Blanket impl of [`RandomizedSignerMut`] for all [`RandomizedSigner`] types.
250#[cfg(feature = "rand_core")]
251impl<S, T: RandomizedSigner<S>> RandomizedSignerMut<S> for T {
252    fn try_sign_with_rng<R: TryCryptoRng + ?Sized>(
253        &mut self,
254        rng: &mut R,
255        msg: &[u8],
256    ) -> Result<S, Error> {
257        T::try_sign_with_rng(self, rng, msg)
258    }
259}
260
261/// Asynchronously sign the provided message bytestring using `Self` (e.g. client for a Cloud KMS or
262/// HSM), returning a digital signature.
263///
264/// This trait is an async equivalent of the [`Signer`] trait.
265pub trait AsyncSigner<S> {
266    /// Attempt to sign the given message, returning a digital signature on success, or an error if
267    /// something went wrong.
268    ///
269    /// The main intended use case for signing errors is when communicating with external signers,
270    /// e.g. cloud KMS, HSMs, or other hardware tokens.
271    ///
272    /// # Errors
273    /// Returns implementation-specific errors in the event signing failed (e.g. KMS or HSM
274    /// communication error).
275    async fn sign_async(&self, msg: &[u8]) -> Result<S, Error>;
276}
277
278impl<S, T> AsyncSigner<S> for T
279where
280    T: Signer<S>,
281{
282    async fn sign_async(&self, msg: &[u8]) -> Result<S, Error> {
283        self.try_sign(msg)
284    }
285}
286
287/// Asynchronously sign the given prehashed message `Digest` using `Self`.
288///
289/// This trait is an async equivalent of the [`DigestSigner`] trait.
290#[cfg(feature = "digest")]
291pub trait AsyncDigestSigner<D, S>
292where
293    D: Update,
294{
295    /// Attempt to sign a message by updating the received `Digest` with it, returning a digital
296    /// signature on success, or an error if something went wrong.
297    ///
298    /// The given function can be invoked multiple times. It is expected that in each invocation the
299    /// `Digest` is updated with the entire equal message.
300    ///
301    /// # Errors
302    /// Returns implementation-specific errors in the event signing failed (e.g. KMS or HSM
303    /// communication error).
304    async fn sign_digest_async<F: AsyncFn(&mut D) -> Result<(), Error>>(
305        &self,
306        f: F,
307    ) -> Result<S, Error>;
308}
309
310/// Sign the given message using the provided external randomness source.
311#[cfg(feature = "rand_core")]
312pub trait AsyncRandomizedSigner<S> {
313    /// Sign the given message and return a digital signature.
314    ///
315    /// # Panics
316    /// In the event of a signing error.
317    async fn sign_with_rng_async<R: CryptoRng + ?Sized>(&self, rng: &mut R, msg: &[u8]) -> S {
318        self.try_sign_with_rng_async(rng, msg)
319            .await
320            .expect("signature operation failed")
321    }
322
323    /// Attempt to sign the given message, returning a digital signature on success, or an error if
324    /// something went wrong.
325    ///
326    /// The main intended use case for signing errors is when communicating with external signers,
327    /// e.g. cloud KMS, HSMs, or other hardware tokens.
328    ///
329    /// # Errors
330    /// Returns implementation-specific errors in the event signing failed (e.g. KMS or HSM
331    /// communication error), or if the provided `rng` experiences an internal failure.
332    async fn try_sign_with_rng_async<R: TryCryptoRng + ?Sized>(
333        &self,
334        rng: &mut R,
335        msg: &[u8],
336    ) -> Result<S, Error>;
337}
338
339#[cfg(feature = "rand_core")]
340impl<S, T> AsyncRandomizedSigner<S> for T
341where
342    T: RandomizedSigner<S>,
343{
344    async fn try_sign_with_rng_async<R: TryCryptoRng + ?Sized>(
345        &self,
346        rng: &mut R,
347        msg: &[u8],
348    ) -> Result<S, Error> {
349        self.try_sign_with_rng(rng, msg)
350    }
351}