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
69use 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 >
1818where
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 } ;
0 commit comments