1use std::sync::Arc;
21
22use crate::{CryptoError, SignatureScheme, SigningKey};
23
24pub const DETERMINISTIC_ECDSA: bool = cfg!(feature = "ecdsa-rfc6979");
26
27pub const DETERMINISTIC_ECDSA_SCHEMES: [SignatureScheme; 2] = [
30 SignatureScheme::EcdsaP256Sha256,
31 SignatureScheme::EcdsaP384Sha384,
32];
33
34pub fn deterministic_ecdsa(
45 key: Arc<dyn SigningKey>,
46) -> Result<Option<Arc<dyn SigningKey>>, CryptoError> {
47 let Some(scheme) = DETERMINISTIC_ECDSA_SCHEMES
48 .into_iter()
49 .find(|scheme| key.supports(*scheme))
50 else {
51 return Ok(None);
52 };
53 imp::wrap(key, scheme).map(Some)
54}
55
56#[cfg(not(feature = "ecdsa-rfc6979"))]
57mod imp {
58 use super::*;
59
60 pub(super) fn wrap(
61 _key: Arc<dyn SigningKey>,
62 _scheme: SignatureScheme,
63 ) -> Result<Arc<dyn SigningKey>, CryptoError> {
64 Err(CryptoError::Provider(
65 "deterministic ECDSA (RFC 6979) needs the `ecdsa-rfc6979` feature of rtci-crypto \
66 (`rtci` forwards it)"
67 .into(),
68 ))
69 }
70}
71
72#[cfg(feature = "ecdsa-rfc6979")]
73pub use imp::DeterministicEcdsaKey;
74
75#[cfg(feature = "ecdsa-rfc6979")]
76mod imp {
77 use super::*;
78 use crate::{CryptoAlgorithm, PublicKey, PublicKeyEncoding, SecretVec};
79 use p256::ecdsa::signature::Signer;
80 use p256::pkcs8::DecodePrivateKey;
81
82 enum Curve {
85 P256(p256::ecdsa::SigningKey),
86 P384(p384::ecdsa::SigningKey),
87 }
88
89 pub struct DeterministicEcdsaKey {
91 inner: Arc<dyn SigningKey>,
92 curve: Curve,
93 }
94
95 pub(super) fn wrap(
96 inner: Arc<dyn SigningKey>,
97 scheme: SignatureScheme,
98 ) -> Result<Arc<dyn SigningKey>, CryptoError> {
99 let pkcs8: SecretVec = inner.to_pkcs8_der()?.ok_or_else(|| {
100 CryptoError::Provider(
101 "deterministic ECDSA (RFC 6979) needs the key's PKCS#8 form; this key does \
102 not export it"
103 .into(),
104 )
105 })?;
106 let (curve, public) = match scheme {
107 SignatureScheme::EcdsaP256Sha256 => {
108 let key = p256::ecdsa::SigningKey::from_pkcs8_der(pkcs8.as_ref())
109 .map_err(|_| CryptoError::InvalidPrivateKey)?;
110 let public = key.verifying_key().to_sec1_point(false).as_bytes().to_vec();
111 (Curve::P256(key), public)
112 }
113 SignatureScheme::EcdsaP384Sha384 => {
114 let key = p384::ecdsa::SigningKey::from_pkcs8_der(pkcs8.as_ref())
115 .map_err(|_| CryptoError::InvalidPrivateKey)?;
116 let public = key.verifying_key().to_sec1_point(false).as_bytes().to_vec();
117 (Curve::P384(key), public)
118 }
119 _ => {
120 return Err(CryptoError::UnsupportedAlgorithm(
121 CryptoAlgorithm::Signature(scheme),
122 ));
123 }
124 };
125 let PublicKey { encoding, bytes } = inner.public_key();
128 if encoding != PublicKeyEncoding::EcUncompressedPoint || bytes != public.as_slice() {
129 return Err(CryptoError::InvalidPrivateKey);
130 }
131 Ok(Arc::new(DeterministicEcdsaKey { inner, curve }))
132 }
133
134 impl SigningKey for DeterministicEcdsaKey {
135 fn supports(&self, scheme: SignatureScheme) -> bool {
136 self.inner.supports(scheme)
137 }
138
139 fn public_key(&self) -> PublicKey<'_> {
140 self.inner.public_key()
141 }
142
143 fn sign(&self, scheme: SignatureScheme, message: &[u8]) -> Result<Vec<u8>, CryptoError> {
144 let failed = |_| CryptoError::Provider("signature generation failed".into());
145 match (&self.curve, scheme) {
146 (Curve::P256(key), SignatureScheme::EcdsaP256Sha256) => {
147 let signature: p256::ecdsa::DerSignature =
148 key.try_sign(message).map_err(failed)?;
149 Ok(signature.as_bytes().to_vec())
150 }
151 (Curve::P384(key), SignatureScheme::EcdsaP384Sha384) => {
152 let signature: p384::ecdsa::DerSignature =
153 key.try_sign(message).map_err(failed)?;
154 Ok(signature.as_bytes().to_vec())
155 }
156 _ => self.inner.sign(scheme, message),
157 }
158 }
159
160 fn to_pkcs8_der(&self) -> Result<Option<SecretVec>, CryptoError> {
161 self.inner.to_pkcs8_der()
162 }
163 }
164}
165
166#[cfg(test)]
167#[path = "rfc6979_test.rs"]
168mod rfc6979_test;