1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#[cfg(all(feature = "digest", feature = "sha2"))]
use super::digest::DigestSigner;
use crate::{error::Error, signature::Signature};
#[cfg(all(feature = "digest", feature = "sha2"))]
use digest::Digest;
#[cfg(all(feature = "digest", feature = "sha2"))]
use sha2::{Sha256, Sha384, Sha512};

// TODO: define these using a macro?

/// Signer which computes SHA-256 digests of messages
pub trait Sha256Signer<S>: Send + Sync
where
    S: Signature,
{
    /// Compute a signature of the SHA-256 digest of a message
    fn sign_sha256(&self, msg: &[u8]) -> Result<S, Error>;
}

#[cfg(all(feature = "digest", feature = "sha2"))]
impl<S, T> Sha256Signer<S> for T
where
    S: Signature,
    T: DigestSigner<Sha256, S>,
{
    fn sign_sha256(&self, msg: &[u8]) -> Result<S, Error> {
        self.sign(Sha256::new().chain(msg))
    }
}

/// Signer which computes SHA-384 digests of messages
pub trait Sha384Signer<S>: Send + Sync
where
    S: Signature,
{
    /// Compute a signature of the SHA-384 digest of a message
    fn sign_sha384(&self, msg: &[u8]) -> Result<S, Error>;
}

#[cfg(all(feature = "digest", feature = "sha2"))]
impl<S, T> Sha384Signer<S> for T
where
    S: Signature,
    T: DigestSigner<Sha384, S>,
{
    fn sign_sha384(&self, msg: &[u8]) -> Result<S, Error> {
        self.sign(Sha384::new().chain(msg))
    }
}

/// Signer which computes SHA-512 digests of messages
pub trait Sha512Signer<S>: Send + Sync
where
    S: Signature,
{
    /// Compute a signature of the SHA-512 digest of a message
    fn sign_sha512(&self, msg: &[u8]) -> Result<S, Error>;
}

#[cfg(all(feature = "digest", feature = "sha2"))]
impl<S, T> Sha512Signer<S> for T
where
    S: Signature,
    T: DigestSigner<Sha512, S>,
{
    fn sign_sha512(&self, msg: &[u8]) -> Result<S, Error> {
        self.sign(Sha512::new().chain(msg))
    }
}

/// Compute SHA-256 of the given message and then sign the resulting digest.
/// This can be used to avoid importing the `Signer` and `Signature` traits
pub fn sign_sha256<S>(signer: &dyn Sha256Signer<S>, msg: &[u8]) -> Result<S, Error>
where
    S: Signature,
{
    signer.sign_sha256(msg)
}

/// Compute SHA-384 of the given message and then sign the resulting digest.
/// This can be used to avoid importing the `Signer` and `Signature` traits
pub fn sign_sha384<S>(signer: &dyn Sha384Signer<S>, msg: &[u8]) -> Result<S, Error>
where
    S: Signature,
{
    signer.sign_sha384(msg)
}

/// Compute SHA-512 of the given message and then sign the resulting digest.
/// This can be used to avoid importing the `Signer` and `Signature` traits
pub fn sign_sha512<S>(signer: &dyn Sha512Signer<S>, msg: &[u8]) -> Result<S, Error>
where
    S: Signature,
{
    signer.sign_sha512(msg)
}