ssi_crypto/
signature.rs

1use crate::{Algorithm, AlgorithmInstance, SecretKey};
2
3#[derive(Debug, thiserror::Error)]
4pub enum SignatureError {
5    #[error("unsupported algorithm `{0}`")]
6    UnsupportedAlgorithm(Algorithm),
7
8    #[error("secret key is not compatible with the signature algorithm")]
9    IncompatibleKey,
10}
11
12impl AlgorithmInstance {
13    #[allow(unused)]
14    pub fn sign(&self, key: &SecretKey, signing_bytes: &[u8]) -> Result<Vec<u8>, SignatureError> {
15        match self {
16            #[cfg(feature = "ed25519")]
17            Self::EdDSA => match key {
18                SecretKey::Ed25519(key) => {
19                    use ed25519_dalek::Signer;
20                    Ok(key.sign(signing_bytes).to_bytes().to_vec())
21                }
22                #[allow(unreachable_patterns)]
23                _ => Err(SignatureError::IncompatibleKey),
24            },
25            #[cfg(feature = "secp256k1")]
26            Self::ES256K => {
27                match key {
28                    SecretKey::Secp256k1(key) => {
29                        use k256::ecdsa::{signature::Signer, Signature};
30                        let signing_key = k256::ecdsa::SigningKey::from(key);
31                        let signature: Signature = signing_key.try_sign(signing_bytes).unwrap(); // Uses SHA-256 by default.
32                        Ok(signature.to_bytes().to_vec())
33                    }
34                    #[allow(unreachable_patterns)]
35                    _ => Err(SignatureError::IncompatibleKey),
36                }
37            }
38            #[cfg(feature = "secp256r1")]
39            Self::ES256 => {
40                match key {
41                    SecretKey::P256(key) => {
42                        use p256::ecdsa::{signature::Signer, Signature};
43                        let signing_key = p256::ecdsa::SigningKey::from(key);
44                        let signature: Signature = signing_key.try_sign(signing_bytes).unwrap(); // Uses SHA-256 by default.
45                        Ok(signature.to_bytes().to_vec())
46                    }
47                    #[allow(unreachable_patterns)]
48                    _ => Err(SignatureError::IncompatibleKey),
49                }
50            }
51            #[cfg(feature = "secp384r1")]
52            Self::ES384 => {
53                match key {
54                    SecretKey::P384(key) => {
55                        use p384::ecdsa::{signature::Signer, Signature};
56                        let signing_key = p384::ecdsa::SigningKey::from(key);
57                        let signature: Signature = signing_key.try_sign(signing_bytes).unwrap(); // Uses SHA-384 by default.
58                        Ok(signature.to_bytes().to_vec())
59                    }
60                    #[allow(unreachable_patterns)]
61                    _ => Err(SignatureError::IncompatibleKey),
62                }
63            }
64            other => Err(SignatureError::UnsupportedAlgorithm(other.algorithm())),
65        }
66    }
67}