diff --git a/tss-esapi/Cargo.toml b/tss-esapi/Cargo.toml index 41e55d63..c26571c3 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 } @@ -58,5 +59,5 @@ semver = "1.0.7" [features] default = ["abstraction"] generate-bindings = ["tss-esapi-sys/generate-bindings"] -abstraction = ["elliptic-curve", "rsa", "x509-cert", "p192", "p224", "p256", "p384", "p521", "sha1", "sha2", "sha3", "sm2", "sm3"] +abstraction = ["ecdsa", "elliptic-curve", "rsa", "x509-cert", "p192", "p224", "p256", "p384", "p521", "sha1", "sha2", "sha3", "sm2", "sm3"] integration-tests = ["strum", "strum_macros"] diff --git a/tss-esapi/src/abstraction/mod.rs b/tss-esapi/src/abstraction/mod.rs index ab10fe85..ec2cdd5d 100644 --- a/tss-esapi/src/abstraction/mod.rs +++ b/tss-esapi/src/abstraction/mod.rs @@ -10,6 +10,7 @@ pub mod public; pub mod transient; mod hashing; +mod signatures; pub use hashing::AssociatedHashingAlgorithm; use std::convert::TryFrom; diff --git a/tss-esapi/src/abstraction/signatures.rs b/tss-esapi/src/abstraction/signatures.rs new file mode 100644 index 00000000..b39b7107 --- /dev/null +++ b/tss-esapi/src/abstraction/signatures.rs @@ -0,0 +1,42 @@ +// Copyright 2024 Contributors to the Parsec project. +// SPDX-License-Identifier: Apache-2.0 + +use crate::{structures::EccSignature, Error, Result, WrapperErrorKind}; + +use std::convert::TryFrom; + +use ecdsa::SignatureSize; +use elliptic_curve::{ + generic_array::{typenum::Unsigned, ArrayLength}, + FieldBytes, FieldBytesSize, PrimeCurve, +}; + +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) + } +} + +// TODO(baloo): impl TryFrom for rsa::pkcs1v15::Signature +// TODO(baloo): impl TryFrom for rsa::pss::Signature