Skip to content

Commit 38a3558

Browse files
authored
Merge pull request #573 from baloo/baloo/signature/try-from-ref
rework TryFrom converting signatures to accept a ref
2 parents dedbdaa + bfb3123 commit 38a3558

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

tss-esapi/src/abstraction/no_tpm/quote.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ where
4545
SignatureSize<C>: ArrayLength<u8>,
4646
FieldBytesSize<C>: ModulusSize,
4747
{
48-
let Ok(signature) = ecdsa::Signature::<C>::try_from(signature.clone()) else {
48+
let Ok(signature) = ecdsa::Signature::<C>::try_from(signature) else {
4949
return Ok(false);
5050
};
5151
let Ok(public) = elliptic_curve::PublicKey::<C>::try_from(public) else {
@@ -342,7 +342,7 @@ pub fn checkquote(
342342
}
343343
#[cfg(feature = "rsa")]
344344
(Public::Rsa { .. }, sig @ Signature::RsaSsa(pkcs_sig)) => {
345-
let Ok(sig) = pkcs1v15::Signature::try_from(sig.clone()) else {
345+
let Ok(sig) = pkcs1v15::Signature::try_from(sig) else {
346346
return Err(Error::WrapperError(WrapperErrorKind::UnsupportedParam));
347347
};
348348

@@ -353,7 +353,7 @@ pub fn checkquote(
353353
}
354354
#[cfg(feature = "rsa")]
355355
(Public::Rsa { .. }, sig @ Signature::RsaPss(pkcs_sig)) => {
356-
let Ok(sig) = pss::Signature::try_from(sig.clone()) else {
356+
let Ok(sig) = pss::Signature::try_from(sig) else {
357357
return Err(Error::WrapperError(WrapperErrorKind::UnsupportedParam));
358358
};
359359

tss-esapi/src/abstraction/signatures.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// Copyright 2024 Contributors to the Parsec project.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use crate::{structures::EccSignature, Error, Result, WrapperErrorKind};
4+
use crate::{
5+
structures::{EccSignature, Signature},
6+
Error, Result, WrapperErrorKind,
7+
};
58

69
use std::convert::TryFrom;
710

@@ -11,17 +14,14 @@ use elliptic_curve::{
1114
FieldBytes, FieldBytesSize, PrimeCurve,
1215
};
1316

14-
#[cfg(feature = "rsa")]
15-
use crate::structures::Signature;
16-
17-
impl<C> TryFrom<EccSignature> for ecdsa::Signature<C>
17+
impl<C> TryFrom<&EccSignature> for ecdsa::Signature<C>
1818
where
1919
C: PrimeCurve,
2020
SignatureSize<C>: ArrayLength<u8>,
2121
{
2222
type Error = Error;
2323

24-
fn try_from(signature: EccSignature) -> Result<Self> {
24+
fn try_from(signature: &EccSignature) -> Result<Self> {
2525
let r = signature.signature_r().as_slice();
2626
let s = signature.signature_s().as_slice();
2727

@@ -33,21 +33,36 @@ where
3333
}
3434

3535
let signature = ecdsa::Signature::from_scalars(
36-
FieldBytes::<C>::from_slice(r).clone(),
37-
FieldBytes::<C>::from_slice(s).clone(),
36+
FieldBytes::<C>::clone_from_slice(r),
37+
FieldBytes::<C>::clone_from_slice(s),
3838
)
3939
.map_err(|_| Error::local_error(WrapperErrorKind::InvalidParam))?;
4040
Ok(signature)
4141
}
4242
}
4343

44+
impl<C> TryFrom<&Signature> for ecdsa::Signature<C>
45+
where
46+
C: PrimeCurve,
47+
SignatureSize<C>: ArrayLength<u8>,
48+
{
49+
type Error = Error;
50+
51+
fn try_from(signature: &Signature) -> Result<Self> {
52+
let Signature::EcDsa(signature) = signature else {
53+
return Err(Error::local_error(WrapperErrorKind::InvalidParam));
54+
};
55+
Self::try_from(signature)
56+
}
57+
}
58+
4459
// Note: this does not implement `TryFrom<RsaSignature>` because `RsaSignature` does not carry the
4560
// information whether the signatures was generated using PKCS#1v1.5 or PSS.
4661
#[cfg(feature = "rsa")]
47-
impl TryFrom<Signature> for rsa::pkcs1v15::Signature {
62+
impl TryFrom<&Signature> for rsa::pkcs1v15::Signature {
4863
type Error = Error;
4964

50-
fn try_from(signature: Signature) -> Result<Self> {
65+
fn try_from(signature: &Signature) -> Result<Self> {
5166
let Signature::RsaSsa(signature) = signature else {
5267
return Err(Error::local_error(WrapperErrorKind::InvalidParam));
5368
};
@@ -60,10 +75,10 @@ impl TryFrom<Signature> for rsa::pkcs1v15::Signature {
6075
// Note: this does not implement `TryFrom<RsaSignature>` because `RsaSignature` does not carry the
6176
// information whether the signatures was generated using PKCS#1v1.5 or PSS.
6277
#[cfg(feature = "rsa")]
63-
impl TryFrom<Signature> for rsa::pss::Signature {
78+
impl TryFrom<&Signature> for rsa::pss::Signature {
6479
type Error = Error;
6580

66-
fn try_from(signature: Signature) -> Result<Self> {
81+
fn try_from(signature: &Signature) -> Result<Self> {
6782
let Signature::RsaPss(signature) = signature else {
6883
return Err(Error::local_error(WrapperErrorKind::InvalidParam));
6984
};

tss-esapi/src/abstraction/signer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ where
253253
)));
254254
};
255255

256-
let signature = Signature::try_from(signature).map_err(SigError::from_source)?;
256+
let signature = Signature::try_from(&signature).map_err(SigError::from_source)?;
257257

258258
Ok(signature)
259259
}
@@ -453,7 +453,7 @@ mod rsa {
453453
let signature = self.context.sign(digest).map_err(SigError::from_source)?;
454454

455455
let signature =
456-
pkcs1v15::Signature::try_from(signature).map_err(SigError::from_source)?;
456+
pkcs1v15::Signature::try_from(&signature).map_err(SigError::from_source)?;
457457

458458
Ok(signature)
459459
}
@@ -581,7 +581,7 @@ mod rsa {
581581

582582
let signature = self.context.sign(digest).map_err(SigError::from_source)?;
583583

584-
let signature = pss::Signature::try_from(signature).map_err(SigError::from_source)?;
584+
let signature = pss::Signature::try_from(&signature).map_err(SigError::from_source)?;
585585

586586
Ok(signature)
587587
}

0 commit comments

Comments
 (0)