Skip to content

Commit

Permalink
Convert signature to ecdsa::Signature
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Gautier <[email protected]>
  • Loading branch information
baloo committed Aug 4, 2024
1 parent f14826c commit ec0d9d9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions tss-esapi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
37 changes: 37 additions & 0 deletions tss-esapi/src/structures/signatures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -143,3 +152,31 @@ impl TryFrom<TPMS_SIGNATURE_ECC> for EccSignature {
})
}
}

#[cfg(feature = "abstraction")]
impl<C> TryFrom<EccSignature> for ecdsa::Signature<C>
where
C: PrimeCurve,
SignatureSize<C>: ArrayLength<u8>,
{
type Error = Error;

fn try_from(signature: EccSignature) -> Result<Self> {
let r = signature.signature_r().as_slice();
let s = signature.signature_s().as_slice();

if r.len() != FieldBytesSize::<C>::USIZE {
return Err(Error::local_error(WrapperErrorKind::InvalidParam));
}
if s.len() != FieldBytesSize::<C>::USIZE {
return Err(Error::local_error(WrapperErrorKind::InvalidParam));
}

let signature = ecdsa::Signature::from_scalars(
FieldBytes::<C>::from_slice(r).clone(),
FieldBytes::<C>::from_slice(s).clone(),
)
.map_err(|_| Error::local_error(WrapperErrorKind::InvalidParam))?;
Ok(signature)
}
}

0 comments on commit ec0d9d9

Please sign in to comment.