internal/wycheproof: update TestEcdsa to use ecdsa.VerifyASN1
Change-Id: Ibd6ce156550615cb85c06e734641c34fca0cfcd0
Reviewed-on: https://go-review.googlesource.com/c/crypto/+/220697
Run-TryBot: Katie Hockman <katie@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Filippo Valsorda <filippo@golang.org>
diff --git a/internal/wycheproof/ecdsa_compat_test.go b/internal/wycheproof/ecdsa_compat_test.go
new file mode 100644
index 0000000..8edc10b
--- /dev/null
+++ b/internal/wycheproof/ecdsa_compat_test.go
@@ -0,0 +1,33 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build !go1.15
+
+// ecdsa.VerifyASN1 was added in Go 1.15.
+
+package wycheproof
+
+import (
+ "crypto/ecdsa"
+ "math/big"
+
+ "golang.org/x/crypto/cryptobyte"
+ "golang.org/x/crypto/cryptobyte/asn1"
+)
+
+func verifyASN1(pub *ecdsa.PublicKey, hash, sig []byte) bool {
+ var (
+ r, s = &big.Int{}, &big.Int{}
+ inner cryptobyte.String
+ )
+ input := cryptobyte.String(sig)
+ if !input.ReadASN1(&inner, asn1.SEQUENCE) ||
+ !input.Empty() ||
+ !inner.ReadASN1Integer(r) ||
+ !inner.ReadASN1Integer(s) ||
+ !inner.Empty() {
+ return false
+ }
+ return ecdsa.Verify(pub, hash, r, s)
+}
diff --git a/internal/wycheproof/ecdsa_go115_test.go b/internal/wycheproof/ecdsa_go115_test.go
new file mode 100644
index 0000000..8da3be5
--- /dev/null
+++ b/internal/wycheproof/ecdsa_go115_test.go
@@ -0,0 +1,15 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build go1.15
+
+package wycheproof
+
+import (
+ "crypto/ecdsa"
+)
+
+func verifyASN1(pub *ecdsa.PublicKey, hash, sig []byte) bool {
+ return ecdsa.VerifyASN1(pub, hash, sig)
+}
diff --git a/internal/wycheproof/ecdsa_test.go b/internal/wycheproof/ecdsa_test.go
index 00043a1..81731f7 100644
--- a/internal/wycheproof/ecdsa_test.go
+++ b/internal/wycheproof/ecdsa_test.go
@@ -7,8 +7,6 @@
import (
"crypto/ecdsa"
"testing"
-
- wecdsa "golang.org/x/crypto/internal/wycheproof/internal/ecdsa"
)
func TestEcdsa(t *testing.T) {
@@ -157,7 +155,7 @@
h.Reset()
h.Write(decodeHex(sig.Msg))
hashed := h.Sum(nil)
- got := wecdsa.VerifyASN1(pub, hashed, decodeHex(sig.Sig))
+ got := verifyASN1(pub, hashed, decodeHex(sig.Sig))
if want := shouldPass(sig.Result, sig.Flags, flagsShouldPass); got != want {
t.Errorf("tcid: %d, type: %s, comment: %q, wanted success: %t", sig.TcId, sig.Result, sig.Comment, want)
}
diff --git a/internal/wycheproof/internal/ecdsa/ecdsa.go b/internal/wycheproof/internal/ecdsa/ecdsa.go
deleted file mode 100644
index 4246c94..0000000
--- a/internal/wycheproof/internal/ecdsa/ecdsa.go
+++ /dev/null
@@ -1,33 +0,0 @@
-// Copyright 2019 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-// Package ecdsa provides an internal version of ecdsa.Verify
-// that is used for the Wycheproof tests.
-package ecdsa
-
-import (
- "crypto/ecdsa"
- "math/big"
-
- "golang.org/x/crypto/cryptobyte"
- "golang.org/x/crypto/cryptobyte/asn1"
-)
-
-// VerifyASN1 verifies the ASN1 encoded signature, sig, of hash using the
-// public key, pub. Its return value records whether the signature is valid.
-func VerifyASN1(pub *ecdsa.PublicKey, hash, sig []byte) bool {
- var (
- r, s = &big.Int{}, &big.Int{}
- inner cryptobyte.String
- )
- input := cryptobyte.String(sig)
- if !input.ReadASN1(&inner, asn1.SEQUENCE) ||
- !input.Empty() ||
- !inner.ReadASN1Integer(r) ||
- !inner.ReadASN1Integer(s) ||
- !inner.Empty() {
- return false
- }
- return ecdsa.Verify(pub, hash, r, s)
-}