@@ -33,10 +33,12 @@ import (
33
33
"crypto/elliptic"
34
34
"crypto/internal/randutil"
35
35
"crypto/sha512"
36
- "encoding/asn1"
37
36
"errors"
38
37
"io"
39
38
"math/big"
39
+
40
+ "golang.org/x/crypto/cryptobyte"
41
+ "golang.org/x/crypto/cryptobyte/asn1"
40
42
)
41
43
42
44
// A invertible implements fast inverse mod Curve.Params().N
@@ -66,10 +68,6 @@ type PrivateKey struct {
66
68
D * big.Int
67
69
}
68
70
69
- type ecdsaSignature struct {
70
- R , S * big.Int
71
- }
72
-
73
71
// Public returns the public key corresponding to priv.
74
72
func (priv * PrivateKey ) Public () crypto.PublicKey {
75
73
return & priv .PublicKey
@@ -88,7 +86,12 @@ func (priv *PrivateKey) Sign(rand io.Reader, digest []byte, opts crypto.SignerOp
88
86
return nil , err
89
87
}
90
88
91
- return asn1 .Marshal (ecdsaSignature {r , s })
89
+ var b cryptobyte.Builder
90
+ b .AddASN1 (asn1 .SEQUENCE , func (b * cryptobyte.Builder ) {
91
+ b .AddASN1BigInt (r )
92
+ b .AddASN1BigInt (s )
93
+ })
94
+ return b .Bytes ()
92
95
}
93
96
94
97
var one = new (big.Int ).SetInt64 (1 )
@@ -159,7 +162,7 @@ var errZeroParam = errors.New("zero parameter")
159
162
160
163
// Sign signs a hash (which should be the result of hashing a larger message)
161
164
// using the private key, priv. If the hash is longer than the bit-length of the
162
- // private key's curve order, the hash will be truncated to that length. It
165
+ // private key's curve order, the hash will be truncated to that length. It
163
166
// returns the signature as a pair of integers. The security of the private key
164
167
// depends on the entropy of rand.
165
168
func Sign (rand io.Reader , priv * PrivateKey , hash []byte ) (r , s * big.Int , err error ) {
@@ -238,6 +241,15 @@ func Sign(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err err
238
241
return
239
242
}
240
243
244
+ // SignASN1 signs a hash (which should be the result of hashing a larger message)
245
+ // using the private key, priv. If the hash is longer than the bit-length of the
246
+ // private key's curve order, the hash will be truncated to that length. It
247
+ // returns the ASN.1 encoded signature. The security of the private key
248
+ // depends on the entropy of rand.
249
+ func SignASN1 (rand io.Reader , priv * PrivateKey , hash []byte ) ([]byte , error ) {
250
+ return priv .Sign (rand , hash , nil )
251
+ }
252
+
241
253
// Verify verifies the signature in r, s of hash using the public key, pub. Its
242
254
// return value records whether the signature is valid.
243
255
func Verify (pub * PublicKey , hash []byte , r , s * big.Int ) bool {
@@ -282,6 +294,24 @@ func Verify(pub *PublicKey, hash []byte, r, s *big.Int) bool {
282
294
return x .Cmp (r ) == 0
283
295
}
284
296
297
+ // VerifyASN1 verifies the ASN.1 encoded signature, sig, of hash using the
298
+ // public key, pub. Its return value records whether the signature is valid.
299
+ func VerifyASN1 (pub * PublicKey , hash , sig []byte ) bool {
300
+ var (
301
+ r , s = & big.Int {}, & big.Int {}
302
+ inner cryptobyte.String
303
+ )
304
+ input := cryptobyte .String (sig )
305
+ if ! input .ReadASN1 (& inner , asn1 .SEQUENCE ) ||
306
+ ! input .Empty () ||
307
+ ! inner .ReadASN1Integer (r ) ||
308
+ ! inner .ReadASN1Integer (s ) ||
309
+ ! inner .Empty () {
310
+ return false
311
+ }
312
+ return Verify (pub , hash , r , s )
313
+ }
314
+
285
315
type zr struct {
286
316
io.Reader
287
317
}
0 commit comments