use std::sync::Arc;
use crate::{CryptoError, SignatureScheme, SigningKey};
pub const DETERMINISTIC_ECDSA: bool = cfg!(feature = "ecdsa-rfc6979");
pub const DETERMINISTIC_ECDSA_SCHEMES: [SignatureScheme; 2] = [
SignatureScheme::EcdsaP256Sha256,
SignatureScheme::EcdsaP384Sha384,
];
pub fn deterministic_ecdsa(
key: Arc<dyn SigningKey>,
) -> Result<Option<Arc<dyn SigningKey>>, CryptoError> {
let Some(scheme) = DETERMINISTIC_ECDSA_SCHEMES
.into_iter()
.find(|scheme| key.supports(*scheme))
else {
return Ok(None);
};
imp::wrap(key, scheme).map(Some)
}
#[cfg(not(feature = "ecdsa-rfc6979"))]
mod imp {
use super::*;
pub(super) fn wrap(
_key: Arc<dyn SigningKey>,
_scheme: SignatureScheme,
) -> Result<Arc<dyn SigningKey>, CryptoError> {
Err(CryptoError::Provider(
"deterministic ECDSA (RFC 6979) needs the `ecdsa-rfc6979` feature of rtci-crypto \
(`rtci` forwards it)"
.into(),
))
}
}
#[cfg(feature = "ecdsa-rfc6979")]
pub use imp::DeterministicEcdsaKey;
#[cfg(feature = "ecdsa-rfc6979")]
mod imp {
use super::*;
use crate::{CryptoAlgorithm, PublicKey, PublicKeyEncoding, SecretVec};
use p256::ecdsa::signature::Signer;
use p256::pkcs8::DecodePrivateKey;
enum Curve {
P256(p256::ecdsa::SigningKey),
P384(p384::ecdsa::SigningKey),
}
pub struct DeterministicEcdsaKey {
inner: Arc<dyn SigningKey>,
curve: Curve,
}
pub(super) fn wrap(
inner: Arc<dyn SigningKey>,
scheme: SignatureScheme,
) -> Result<Arc<dyn SigningKey>, CryptoError> {
let pkcs8: SecretVec = inner.to_pkcs8_der()?.ok_or_else(|| {
CryptoError::Provider(
"deterministic ECDSA (RFC 6979) needs the key's PKCS#8 form; this key does \
not export it"
.into(),
)
})?;
let (curve, public) = match scheme {
SignatureScheme::EcdsaP256Sha256 => {
let key = p256::ecdsa::SigningKey::from_pkcs8_der(pkcs8.as_ref())
.map_err(|_| CryptoError::InvalidPrivateKey)?;
let public = key.verifying_key().to_sec1_point(false).as_bytes().to_vec();
(Curve::P256(key), public)
}
SignatureScheme::EcdsaP384Sha384 => {
let key = p384::ecdsa::SigningKey::from_pkcs8_der(pkcs8.as_ref())
.map_err(|_| CryptoError::InvalidPrivateKey)?;
let public = key.verifying_key().to_sec1_point(false).as_bytes().to_vec();
(Curve::P384(key), public)
}
_ => {
return Err(CryptoError::UnsupportedAlgorithm(
CryptoAlgorithm::Signature(scheme),
));
}
};
let PublicKey { encoding, bytes } = inner.public_key();
if encoding != PublicKeyEncoding::EcUncompressedPoint || bytes != public.as_slice() {
return Err(CryptoError::InvalidPrivateKey);
}
Ok(Arc::new(DeterministicEcdsaKey { inner, curve }))
}
impl SigningKey for DeterministicEcdsaKey {
fn supports(&self, scheme: SignatureScheme) -> bool {
self.inner.supports(scheme)
}
fn public_key(&self) -> PublicKey<'_> {
self.inner.public_key()
}
fn sign(&self, scheme: SignatureScheme, message: &[u8]) -> Result<Vec<u8>, CryptoError> {
let failed = |_| CryptoError::Provider("signature generation failed".into());
match (&self.curve, scheme) {
(Curve::P256(key), SignatureScheme::EcdsaP256Sha256) => {
let signature: p256::ecdsa::DerSignature =
key.try_sign(message).map_err(failed)?;
Ok(signature.as_bytes().to_vec())
}
(Curve::P384(key), SignatureScheme::EcdsaP384Sha384) => {
let signature: p384::ecdsa::DerSignature =
key.try_sign(message).map_err(failed)?;
Ok(signature.as_bytes().to_vec())
}
_ => self.inner.sign(scheme, message),
}
}
fn to_pkcs8_der(&self) -> Result<Option<SecretVec>, CryptoError> {
self.inner.to_pkcs8_der()
}
}
}
#[cfg(test)]
#[path = "rfc6979_test.rs"]
mod rfc6979_test;