Skip to content

Commit 17cde86

Browse files
committed
pkcrypto: use ASN1 funcs
│ OLD │ NEW │ │ sec/op │ sec/op vs base │ VerifyUplinkOrderSignature-32 75.74µ ± 1% 75.05µ ± 1% -0.91% (p=0.009 n=10) VerifyUplinkPieceHashSignature-32 77.60µ ± 1% 76.63µ ± 3% ~ (p=0.089 n=10) VerifyOrderLimitSignature-32 86.57µ ± 1% 83.32µ ± 0% -3.76% (p=0.000 n=10) geomean 79.83µ 78.25µ -1.98% │ OLD │ NEW │ │ B/op │ B/op vs base │ VerifyUplinkOrderSignature-32 776.0 ± 0% 776.0 ± 0% ~ (p=1.000 n=10) ¹ VerifyUplinkPieceHashSignature-32 992.0 ± 0% 992.0 ± 0% ~ (p=1.000 n=10) ¹ VerifyOrderLimitSignature-32 2.742Ki ± 0% 2.000Ki ± 0% -27.07% (p=0.000 n=10) geomean 1.263Ki 1.137Ki -9.99% ¹ all samples are equal │ OLD │ NEW │ │ allocs/op │ allocs/op vs base │ VerifyUplinkOrderSignature-32 20.00 ± 0% 20.00 ± 0% ~ (p=1.000 n=10) ¹ VerifyUplinkPieceHashSignature-32 24.00 ± 0% 24.00 ± 0% ~ (p=1.000 n=10) ¹ VerifyOrderLimitSignature-32 65.00 ± 0% 43.00 ± 0% -33.85% (p=0.000 n=10) geomean 31.48 27.43 -12.87% Change-Id: I4f643e446f8af7e47416bcea87f9b4e921396cb4
1 parent 3f709c7 commit 17cde86

File tree

2 files changed

+3
-28
lines changed

2 files changed

+3
-28
lines changed

pkcrypto/encoding.go

-18
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ package pkcrypto
66
import (
77
"crypto"
88
"crypto/x509"
9-
"encoding/asn1"
109
"encoding/pem"
1110
"io"
12-
"math/big"
1311

1412
"github.com/zeebo/errs"
1513
)
@@ -208,22 +206,6 @@ func (e *encodedChain) Parse() ([]*x509.Certificate, error) {
208206
return chain, nil
209207
}
210208

211-
type ecdsaSignature struct {
212-
R, S *big.Int
213-
}
214-
215-
func marshalECDSASignature(r, s *big.Int) ([]byte, error) {
216-
return asn1.Marshal(ecdsaSignature{R: r, S: s})
217-
}
218-
219-
func unmarshalECDSASignature(signatureBytes []byte) (r, s *big.Int, err error) {
220-
var signature ecdsaSignature
221-
if _, err = asn1.Unmarshal(signatureBytes, &signature); err != nil {
222-
return nil, nil, err
223-
}
224-
return signature.R, signature.S, nil
225-
}
226-
227209
// ecPrivateKeyFromASN1 parses a private key from the special Elliptic Curve
228210
// Private Key ASN.1 structure. This is here only for backward compatibility.
229211
// Use PKCS#8 instead.

pkcrypto/signing.go

+3-10
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,7 @@ func verifyECDSASignatureWithoutHashing(pubKey *ecdsa.PublicKey, digest, signatu
7979
race2.ReadSlice(digest)
8080
race2.ReadSlice(signatureBytes)
8181

82-
r, s, err := unmarshalECDSASignature(signatureBytes)
83-
if err != nil {
84-
return ErrVerifySignature.New("unable to unmarshal ecdsa signature: %v", err)
85-
}
86-
if !ecdsa.Verify(pubKey, digest, r, s) {
82+
if !ecdsa.VerifyASN1(pubKey, digest, signatureBytes) {
8783
return ErrVerifySignature.New("signature is not valid")
8884
}
8985
return nil
@@ -165,11 +161,8 @@ func VerifyHMACSHA256(privKey crypto.PrivateKey, data, signature []byte) error {
165161
func signECDSAWithoutHashing(privKey *ecdsa.PrivateKey, digest []byte) ([]byte, error) {
166162
race2.ReadSlice(digest)
167163

168-
r, s, err := ecdsa.Sign(rand.Reader, privKey, digest)
169-
if err != nil {
170-
return nil, ErrSign.Wrap(err)
171-
}
172-
return marshalECDSASignature(r, s)
164+
sig, err := ecdsa.SignASN1(rand.Reader, privKey, digest)
165+
return sig, ErrSign.Wrap(err)
173166
}
174167

175168
func signRSAWithoutHashing(privKey *rsa.PrivateKey, digest []byte) ([]byte, error) {

0 commit comments

Comments
 (0)