diff --git a/tss-esapi/Cargo.toml b/tss-esapi/Cargo.toml index 41e55d63..f039646c 100644 --- a/tss-esapi/Cargo.toml +++ b/tss-esapi/Cargo.toml @@ -28,6 +28,7 @@ regex = "1.3.9" zeroize = { version = "1.5.7", features = ["zeroize_derive"] } tss-esapi-sys = { path = "../tss-esapi-sys", version = "0.5.0" } x509-cert = { version = "0.2.0", optional = true } +ecdsa = { version = "0.16.9", optional = true } elliptic-curve = { version = "0.13.8", optional = true, features = ["alloc", "pkcs8"] } p192 = { version = "0.13.0", optional = true } p224 = { version = "0.13.2", optional = true } diff --git a/tss-esapi/src/structures/signatures.rs b/tss-esapi/src/structures/signatures.rs index 043bb74f..bd5ec99a 100644 --- a/tss-esapi/src/structures/signatures.rs +++ b/tss-esapi/src/structures/signatures.rs @@ -9,6 +9,15 @@ use crate::{ use log::error; use std::convert::{TryFrom, TryInto}; +#[cfg(feature = "abstraction")] +use { + ecdsa::SignatureSize, + elliptic_curve::{ + generic_array::{typenum::Unsigned, ArrayLength}, + FieldBytes, FieldBytesSize, PrimeCurve, + }, +}; + /// Type holding RSA signature information. /// /// For more information about the contents of `signature` see Annex B @@ -143,3 +152,31 @@ impl TryFrom for EccSignature { }) } } + +#[cfg(feature = "abstraction")] +impl TryFrom for ecdsa::Signature +where + C: PrimeCurve, + SignatureSize: ArrayLength, +{ + type Error = Error; + + fn try_from(signature: EccSignature) -> Result { + let r = signature.signature_r().as_slice(); + let s = signature.signature_s().as_slice(); + + if r.len() != FieldBytesSize::::USIZE { + return Err(Error::local_error(WrapperErrorKind::InvalidParam)); + } + if s.len() != FieldBytesSize::::USIZE { + return Err(Error::local_error(WrapperErrorKind::InvalidParam)); + } + + let signature = ecdsa::Signature::from_scalars( + FieldBytes::::from_slice(r).clone(), + FieldBytes::::from_slice(s).clone(), + ) + .map_err(|_| Error::local_error(WrapperErrorKind::InvalidParam))?; + Ok(signature) + } +}