@@ -38,27 +38,43 @@ mod signing;
38
38
#[ cfg( feature = "arithmetic" ) ]
39
39
mod verifying;
40
40
41
+ #[ cfg( feature = "der" ) ]
42
+ mod der;
43
+
44
+ #[ cfg( feature = "der" ) ]
45
+ pub use der:: Signature as DerSignature ;
46
+
41
47
pub use signature;
42
48
43
49
#[ cfg( feature = "arithmetic" ) ]
44
50
pub use self :: { signing:: SigningKey , verifying:: VerifyingKey } ;
45
51
46
- use crate :: { FieldBytes , NonZeroScalar , Sm2 } ;
47
- use core:: fmt:: { self , Debug } ;
52
+ use crate :: { Array , FieldBytes , FieldBytesSize , NonZeroScalar , Sm2 } ;
53
+ use core:: {
54
+ fmt:: { self , Debug } ,
55
+ ops:: Add ,
56
+ } ;
57
+
48
58
use signature:: { Error , Result , SignatureEncoding } ;
49
59
50
60
#[ cfg( feature = "alloc" ) ]
51
61
use alloc:: vec:: Vec ;
52
62
53
63
#[ cfg( feature = "pkcs8" ) ]
54
64
use crate :: pkcs8:: {
55
- AlgorithmIdentifierRef , ObjectIdentifier , der:: AnyRef , spki:: AssociatedAlgorithmIdentifier ,
65
+ AlgorithmIdentifierRef , ObjectIdentifier ,
66
+ der:: { self as der_core, AnyRef , asn1:: BitString } ,
67
+ spki:: AssociatedAlgorithmIdentifier ,
56
68
} ;
69
+
57
70
#[ cfg( all( feature = "alloc" , feature = "pkcs8" ) ) ]
58
- use crate :: pkcs8:: { der , spki:: SignatureBitStringEncoding } ;
71
+ use crate :: pkcs8:: spki:: SignatureBitStringEncoding ;
59
72
60
- /// SM2DSA signature serialized as bytes.
61
- pub type SignatureBytes = [ u8 ; Signature :: BYTE_SIZE ] ;
73
+ /// SM2DSA signature size.
74
+ pub type SignatureSize = <FieldBytesSize as Add >:: Output ;
75
+
76
+ /// SM2DSA signature bytes.
77
+ pub type SignatureBytes = Array < u8 , SignatureSize > ;
62
78
63
79
/// Primitive scalar type (works without the `arithmetic` feature).
64
80
type ScalarPrimitive = elliptic_curve:: ScalarPrimitive < Sm2 > ;
@@ -101,9 +117,15 @@ impl Signature {
101
117
Self :: try_from ( r. into ( ) . concat ( s. into ( ) ) . as_slice ( ) )
102
118
}
103
119
120
+ /// Parse a signature from ASN.1 DER.
121
+ #[ cfg( feature = "der" ) ]
122
+ pub fn from_der ( bytes : & [ u8 ] ) -> Result < Self > {
123
+ DerSignature :: try_from ( bytes) . and_then ( Self :: try_from)
124
+ }
125
+
104
126
/// Serialize this signature as bytes.
105
127
pub fn to_bytes ( & self ) -> SignatureBytes {
106
- let mut ret = [ 0 ; Self :: BYTE_SIZE ] ;
128
+ let mut ret = SignatureBytes :: default ( ) ;
107
129
let ( r_bytes, s_bytes) = ret. split_at_mut ( Self :: BYTE_SIZE / 2 ) ;
108
130
r_bytes. copy_from_slice ( & self . r . to_bytes ( ) ) ;
109
131
s_bytes. copy_from_slice ( & self . s . to_bytes ( ) ) ;
@@ -120,6 +142,12 @@ impl Signature {
120
142
self . s . to_bytes ( )
121
143
}
122
144
145
+ /// Serialize this signature as ASN.1 DER.
146
+ #[ cfg( feature = "der" ) ]
147
+ pub fn to_der ( & self ) -> DerSignature {
148
+ DerSignature :: from_components ( & self . r_bytes ( ) , & self . s_bytes ( ) ) . expect ( "DER encoding error" )
149
+ }
150
+
123
151
/// Convert this signature into a byte vector.
124
152
#[ cfg( feature = "alloc" ) ]
125
153
pub fn to_vec ( & self ) -> Vec < u8 > {
@@ -207,8 +235,8 @@ impl TryFrom<&[u8]> for Signature {
207
235
208
236
#[ cfg( all( feature = "alloc" , feature = "pkcs8" ) ) ]
209
237
impl SignatureBitStringEncoding for Signature {
210
- fn to_bitstring ( & self ) -> der :: Result < der :: asn1 :: BitString > {
211
- der :: asn1 :: BitString :: new ( 0 , self . to_vec ( ) )
238
+ fn to_bitstring ( & self ) -> der_core :: Result < BitString > {
239
+ BitString :: new ( 0 , self . to_vec ( ) )
212
240
}
213
241
}
214
242
0 commit comments