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)]
1516pub use signature::{self, Error};
1718#[cfg(feature = "digest")]
19pub use signature::digest::{self, Digest};
2021#[cfg(feature = "rand_core")]
22use signature::rand_core::CryptoRngCore;
2324/// 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.
35async fn sign_async(&self, msg: &[u8]) -> Result<S, Error>;
36}
3738impl<S, T> AsyncSigner<S> for T
39where
40T: signature::Signer<S>,
41{
42async fn sign_async(&self, msg: &[u8]) -> Result<S, Error> {
43self.try_sign(msg)
44 }
45}
4647/// 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
54D: 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.
58async fn sign_digest_async(&self, digest: D) -> Result<S, Error>;
59}
6061#[cfg(feature = "digest")]
62impl<D, S, T> AsyncDigestSigner<D, S> for T
63where
64D: Digest,
65 T: signature::DigestSigner<D, S>,
66{
67async fn sign_digest_async(&self, digest: D) -> Result<S, Error> {
68self.try_sign_digest(digest)
69 }
70}
7172/// 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
77async fn sign_with_rng_async(&self, rng: &mut impl CryptoRngCore, msg: &[u8]) -> S {
78self.try_sign_with_rng_async(rng, msg)
79 .await
80.expect("signature operation failed")
81 }
8283/// 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.
88async fn try_sign_with_rng_async(
89&self,
90 rng: &mut impl CryptoRngCore,
91 msg: &[u8],
92 ) -> Result<S, Error>;
93}
9495#[cfg(feature = "rand_core")]
96impl<S, T> AsyncRandomizedSigner<S> for T
97where
98T: signature::RandomizedSigner<S>,
99{
100async fn try_sign_with_rng_async(
101&self,
102 rng: &mut impl CryptoRngCore,
103 msg: &[u8],
104 ) -> Result<S, Error> {
105self.try_sign_with_rng(rng, msg)
106 }
107}