Skip to content

Commit 8c09e8a

Browse files
committed
crypto/ecdsa: add SignASN1, VerifyASN1
Update the Example in the crypto/ecdsa package for signing and verifying signatures to use these new functions. This also changes (*PrivateKey).Sign to use x/crypto/cryptobyte/asn1 instead of encoding/asn1 to marshal the signature. Fixes #20544 Change-Id: I3423cfc4d7f9e1748fbed5a631438c8a3b280df4 Reviewed-on: https://go-review.googlesource.com/c/go/+/217940 TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]>
1 parent f5ff005 commit 8c09e8a

File tree

4 files changed

+75
-12
lines changed

4 files changed

+75
-12
lines changed

Diff for: src/crypto/ecdsa/ecdsa.go

+37-7
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ import (
3333
"crypto/elliptic"
3434
"crypto/internal/randutil"
3535
"crypto/sha512"
36-
"encoding/asn1"
3736
"errors"
3837
"io"
3938
"math/big"
39+
40+
"golang.org/x/crypto/cryptobyte"
41+
"golang.org/x/crypto/cryptobyte/asn1"
4042
)
4143

4244
// A invertible implements fast inverse mod Curve.Params().N
@@ -66,10 +68,6 @@ type PrivateKey struct {
6668
D *big.Int
6769
}
6870

69-
type ecdsaSignature struct {
70-
R, S *big.Int
71-
}
72-
7371
// Public returns the public key corresponding to priv.
7472
func (priv *PrivateKey) Public() crypto.PublicKey {
7573
return &priv.PublicKey
@@ -88,7 +86,12 @@ func (priv *PrivateKey) Sign(rand io.Reader, digest []byte, opts crypto.SignerOp
8886
return nil, err
8987
}
9088

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()
9295
}
9396

9497
var one = new(big.Int).SetInt64(1)
@@ -159,7 +162,7 @@ var errZeroParam = errors.New("zero parameter")
159162

160163
// Sign signs a hash (which should be the result of hashing a larger message)
161164
// 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
163166
// returns the signature as a pair of integers. The security of the private key
164167
// depends on the entropy of rand.
165168
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
238241
return
239242
}
240243

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+
241253
// Verify verifies the signature in r, s of hash using the public key, pub. Its
242254
// return value records whether the signature is valid.
243255
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 {
282294
return x.Cmp(r) == 0
283295
}
284296

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+
285315
type zr struct {
286316
io.Reader
287317
}

Diff for: src/crypto/ecdsa/ecdsa_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,36 @@ func TestSignAndVerify(t *testing.T) {
131131
testSignAndVerify(t, elliptic.P521(), "p521")
132132
}
133133

134+
func testSignAndVerifyASN1(t *testing.T, c elliptic.Curve, tag string) {
135+
priv, _ := GenerateKey(c, rand.Reader)
136+
137+
hashed := []byte("testing")
138+
sig, err := SignASN1(rand.Reader, priv, hashed)
139+
if err != nil {
140+
t.Errorf("%s: error signing: %s", tag, err)
141+
return
142+
}
143+
144+
if !VerifyASN1(&priv.PublicKey, hashed, sig) {
145+
t.Errorf("%s: VerifyASN1 failed", tag)
146+
}
147+
148+
hashed[0] ^= 0xff
149+
if VerifyASN1(&priv.PublicKey, hashed, sig) {
150+
t.Errorf("%s: VerifyASN1 always works!", tag)
151+
}
152+
}
153+
154+
func TestSignAndVerifyASN1(t *testing.T) {
155+
testSignAndVerifyASN1(t, elliptic.P224(), "p224")
156+
if testing.Short() {
157+
return
158+
}
159+
testSignAndVerifyASN1(t, elliptic.P256(), "p256")
160+
testSignAndVerifyASN1(t, elliptic.P384(), "p384")
161+
testSignAndVerifyASN1(t, elliptic.P521(), "p521")
162+
}
163+
134164
func testNonceSafety(t *testing.T, c elliptic.Curve, tag string) {
135165
priv, _ := GenerateKey(c, rand.Reader)
136166

Diff for: src/crypto/ecdsa/example_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ func Example() {
2121
msg := "hello, world"
2222
hash := sha256.Sum256([]byte(msg))
2323

24-
r, s, err := ecdsa.Sign(rand.Reader, privateKey, hash[:])
24+
sig, err := ecdsa.SignASN1(rand.Reader, privateKey, hash[:])
2525
if err != nil {
2626
panic(err)
2727
}
28-
fmt.Printf("signature: (0x%x, 0x%x)\n", r, s)
28+
fmt.Printf("signature: %x\n", sig)
2929

30-
valid := ecdsa.Verify(&privateKey.PublicKey, hash[:], r, s)
30+
valid := ecdsa.VerifyASN1(&privateKey.PublicKey, hash[:], sig)
3131
fmt.Println("signature verified:", valid)
3232
}

Diff for: src/go/build/deps_test.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,11 @@ var pkgDeps = map[string][]string{
383383

384384
// Mathematical crypto: dependencies on fmt (L4) and math/big.
385385
// We could avoid some of the fmt, but math/big imports fmt anyway.
386-
"crypto/dsa": {"L4", "CRYPTO", "math/big"},
387-
"crypto/ecdsa": {"L4", "CRYPTO", "crypto/elliptic", "math/big", "encoding/asn1"},
386+
"crypto/dsa": {"L4", "CRYPTO", "math/big"},
387+
"crypto/ecdsa": {
388+
"L4", "CRYPTO", "crypto/elliptic", "math/big",
389+
"golang.org/x/crypto/cryptobyte", "golang.org/x/crypto/cryptobyte/asn1",
390+
},
388391
"crypto/elliptic": {"L4", "CRYPTO", "math/big"},
389392
"crypto/rsa": {"L4", "CRYPTO", "crypto/rand", "math/big"},
390393

0 commit comments

Comments
 (0)