Skip to content

Commit 80558df

Browse files
committed
Convert signature to ecdsa::Signature
Signed-off-by: Arthur Gautier <[email protected]>
1 parent f14826c commit 80558df

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

tss-esapi/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ regex = "1.3.9"
2828
zeroize = { version = "1.5.7", features = ["zeroize_derive"] }
2929
tss-esapi-sys = { path = "../tss-esapi-sys", version = "0.5.0" }
3030
x509-cert = { version = "0.2.0", optional = true }
31+
ecdsa = { version = "0.16.9", optional = true }
3132
elliptic-curve = { version = "0.13.8", optional = true, features = ["alloc", "pkcs8"] }
3233
p192 = { version = "0.13.0", optional = true }
3334
p224 = { version = "0.13.2", optional = true }
@@ -58,5 +59,5 @@ semver = "1.0.7"
5859
[features]
5960
default = ["abstraction"]
6061
generate-bindings = ["tss-esapi-sys/generate-bindings"]
61-
abstraction = ["elliptic-curve", "rsa", "x509-cert", "p192", "p224", "p256", "p384", "p521", "sha1", "sha2", "sha3", "sm2", "sm3"]
62+
abstraction = ["ecdsa", "elliptic-curve", "rsa", "x509-cert", "p192", "p224", "p256", "p384", "p521", "sha1", "sha2", "sha3", "sm2", "sm3"]
6263
integration-tests = ["strum", "strum_macros"]

tss-esapi/src/structures/signatures.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ use crate::{
99
use log::error;
1010
use std::convert::{TryFrom, TryInto};
1111

12+
#[cfg(feature = "abstraction")]
13+
use {
14+
ecdsa::SignatureSize,
15+
elliptic_curve::{
16+
generic_array::{typenum::Unsigned, ArrayLength},
17+
FieldBytes, FieldBytesSize, PrimeCurve,
18+
},
19+
};
20+
1221
/// Type holding RSA signature information.
1322
///
1423
/// For more information about the contents of `signature` see Annex B
@@ -143,3 +152,31 @@ impl TryFrom<TPMS_SIGNATURE_ECC> for EccSignature {
143152
})
144153
}
145154
}
155+
156+
#[cfg(feature = "abstraction")]
157+
impl<C> TryFrom<EccSignature> for ecdsa::Signature<C>
158+
where
159+
C: PrimeCurve,
160+
SignatureSize<C>: ArrayLength<u8>,
161+
{
162+
type Error = Error;
163+
164+
fn try_from(signature: EccSignature) -> Result<Self> {
165+
let r = signature.signature_r().as_slice();
166+
let s = signature.signature_s().as_slice();
167+
168+
if r.len() != FieldBytesSize::<C>::USIZE {
169+
return Err(Error::local_error(WrapperErrorKind::InvalidParam));
170+
}
171+
if s.len() != FieldBytesSize::<C>::USIZE {
172+
return Err(Error::local_error(WrapperErrorKind::InvalidParam));
173+
}
174+
175+
let signature = ecdsa::Signature::from_scalars(
176+
FieldBytes::<C>::from_slice(r).clone(),
177+
FieldBytes::<C>::from_slice(s).clone(),
178+
)
179+
.map_err(|_| Error::local_error(WrapperErrorKind::InvalidParam))?;
180+
Ok(signature)
181+
}
182+
}

0 commit comments

Comments
 (0)