1
1
// Copyright 2024 Contributors to the Parsec project.
2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
- use crate :: { structures:: EccSignature , Error , Result , WrapperErrorKind } ;
4
+ use crate :: {
5
+ structures:: { EccSignature , Signature } ,
6
+ Error , Result , WrapperErrorKind ,
7
+ } ;
5
8
6
9
use std:: convert:: TryFrom ;
7
10
@@ -11,17 +14,14 @@ use elliptic_curve::{
11
14
FieldBytes , FieldBytesSize , PrimeCurve ,
12
15
} ;
13
16
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 >
18
18
where
19
19
C : PrimeCurve ,
20
20
SignatureSize < C > : ArrayLength < u8 > ,
21
21
{
22
22
type Error = Error ;
23
23
24
- fn try_from ( signature : EccSignature ) -> Result < Self > {
24
+ fn try_from ( signature : & EccSignature ) -> Result < Self > {
25
25
let r = signature. signature_r ( ) . as_slice ( ) ;
26
26
let s = signature. signature_s ( ) . as_slice ( ) ;
27
27
@@ -33,21 +33,36 @@ where
33
33
}
34
34
35
35
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 ) ,
38
38
)
39
39
. map_err ( |_| Error :: local_error ( WrapperErrorKind :: InvalidParam ) ) ?;
40
40
Ok ( signature)
41
41
}
42
42
}
43
43
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
+
44
59
// Note: this does not implement `TryFrom<RsaSignature>` because `RsaSignature` does not carry the
45
60
// information whether the signatures was generated using PKCS#1v1.5 or PSS.
46
61
#[ cfg( feature = "rsa" ) ]
47
- impl TryFrom < Signature > for rsa:: pkcs1v15:: Signature {
62
+ impl TryFrom < & Signature > for rsa:: pkcs1v15:: Signature {
48
63
type Error = Error ;
49
64
50
- fn try_from ( signature : Signature ) -> Result < Self > {
65
+ fn try_from ( signature : & Signature ) -> Result < Self > {
51
66
let Signature :: RsaSsa ( signature) = signature else {
52
67
return Err ( Error :: local_error ( WrapperErrorKind :: InvalidParam ) ) ;
53
68
} ;
@@ -60,10 +75,10 @@ impl TryFrom<Signature> for rsa::pkcs1v15::Signature {
60
75
// Note: this does not implement `TryFrom<RsaSignature>` because `RsaSignature` does not carry the
61
76
// information whether the signatures was generated using PKCS#1v1.5 or PSS.
62
77
#[ cfg( feature = "rsa" ) ]
63
- impl TryFrom < Signature > for rsa:: pss:: Signature {
78
+ impl TryFrom < & Signature > for rsa:: pss:: Signature {
64
79
type Error = Error ;
65
80
66
- fn try_from ( signature : Signature ) -> Result < Self > {
81
+ fn try_from ( signature : & Signature ) -> Result < Self > {
67
82
let Signature :: RsaPss ( signature) = signature else {
68
83
return Err ( Error :: local_error ( WrapperErrorKind :: InvalidParam ) ) ;
69
84
} ;
0 commit comments