async_signature/
lib.rs

1#![no_std]
2#![doc = include_str!("../README.md")]
3#![doc(
4    html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg",
5    html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg"
6)]
7#![cfg_attr(docsrs, feature(doc_auto_cfg))]
8#![forbid(unsafe_code)]
9#![warn(
10    missing_docs,
11    rust_2018_idioms,
12    unused_qualifications,
13    missing_debug_implementations
14)]
15
16pub use signature::{self, Error};
17
18#[cfg(feature = "digest")]
19pub use signature::digest::{self, Digest};
20
21#[cfg(feature = "rand_core")]
22use signature::rand_core::CryptoRngCore;
23
24/// Asynchronously sign the provided message bytestring using `Self`
25/// (e.g. client for a Cloud KMS or HSM), returning a digital signature.
26///
27/// This trait is an async equivalent of the [`signature::Signer`] trait.
28#[allow(async_fn_in_trait)]
29pub trait AsyncSigner<S> {
30    /// Attempt to sign the given message, returning a digital signature on
31    /// success, or an error if something went wrong.
32    ///
33    /// The main intended use case for signing errors is when communicating
34    /// with external signers, e.g. cloud KMS, HSMs, or other hardware tokens.
35    async fn sign_async(&self, msg: &[u8]) -> Result<S, Error>;
36}
37
38impl<S, T> AsyncSigner<S> for T
39where
40    T: signature::Signer<S>,
41{
42    async fn sign_async(&self, msg: &[u8]) -> Result<S, Error> {
43        self.try_sign(msg)
44    }
45}
46
47/// Asynchronously sign the given prehashed message [`Digest`] using `Self`.
48///
49/// This trait is an async equivalent of the [`signature::DigestSigner`] trait.
50#[cfg(feature = "digest")]
51#[allow(async_fn_in_trait)]
52pub trait AsyncDigestSigner<D, S>
53where
54    D: Digest,
55{
56    /// Attempt to sign the given prehashed message [`Digest`], returning a
57    /// digital signature on success, or an error if something went wrong.
58    async fn sign_digest_async(&self, digest: D) -> Result<S, Error>;
59}
60
61#[cfg(feature = "digest")]
62impl<D, S, T> AsyncDigestSigner<D, S> for T
63where
64    D: Digest,
65    T: signature::DigestSigner<D, S>,
66{
67    async fn sign_digest_async(&self, digest: D) -> Result<S, Error> {
68        self.try_sign_digest(digest)
69    }
70}
71
72/// Sign the given message using the provided external randomness source.
73#[cfg(feature = "rand_core")]
74#[allow(async_fn_in_trait)]
75pub trait AsyncRandomizedSigner<S> {
76    /// Sign the given message and return a digital signature
77    async fn sign_with_rng_async(&self, rng: &mut impl CryptoRngCore, msg: &[u8]) -> S {
78        self.try_sign_with_rng_async(rng, msg)
79            .await
80            .expect("signature operation failed")
81    }
82
83    /// Attempt to sign the given message, returning a digital signature on
84    /// success, or an error if something went wrong.
85    ///
86    /// The main intended use case for signing errors is when communicating
87    /// with external signers, e.g. cloud KMS, HSMs, or other hardware tokens.
88    async fn try_sign_with_rng_async(
89        &self,
90        rng: &mut impl CryptoRngCore,
91        msg: &[u8],
92    ) -> Result<S, Error>;
93}
94
95#[cfg(feature = "rand_core")]
96impl<S, T> AsyncRandomizedSigner<S> for T
97where
98    T: signature::RandomizedSigner<S>,
99{
100    async fn try_sign_with_rng_async(
101        &self,
102        rng: &mut impl CryptoRngCore,
103        msg: &[u8],
104    ) -> Result<S, Error> {
105        self.try_sign_with_rng(rng, msg)
106    }
107}