...
1
2
3
4
5 package wycheproof
6
7 import (
8 "crypto/ecdsa"
9 "math/big"
10 "testing"
11
12 "golang.org/x/crypto/cryptobyte"
13 "golang.org/x/crypto/cryptobyte/asn1"
14 )
15
16 func TestECDSA(t *testing.T) {
17 type ASNSignatureTestVector struct {
18
19 Comment string `json:"comment"`
20
21 Flags []string `json:"flags"`
22
23 Msg string `json:"msg"`
24
25 Result string `json:"result"`
26
27 Sig string `json:"sig"`
28
29 TcID int `json:"tcId"`
30 }
31
32 type ECPublicKey struct {
33
34 Curve interface{} `json:"curve"`
35 }
36
37 type ECDSATestGroup struct {
38
39 Key *ECPublicKey `json:"key"`
40
41 KeyDER string `json:"keyDer"`
42
43 SHA string `json:"sha"`
44 Tests []*ASNSignatureTestVector `json:"tests"`
45 }
46
47 type Root struct {
48 TestGroups []*ECDSATestGroup `json:"testGroups"`
49 }
50
51 flagsShouldPass := map[string]bool{
52
53
54 "MissingZero": false,
55
56
57
58 "WeakHash": true,
59 }
60
61
62
63 supportedCurves := map[string]bool{
64 "secp224r1": true,
65 "secp256r1": true,
66 "secp384r1": true,
67 "secp521r1": true,
68 }
69
70 var root Root
71 readTestVector(t, "ecdsa_test.json", &root)
72 for _, tg := range root.TestGroups {
73 curve := tg.Key.Curve.(string)
74 if !supportedCurves[curve] {
75 continue
76 }
77 pub := decodePublicKey(tg.KeyDER).(*ecdsa.PublicKey)
78 h := parseHash(tg.SHA).New()
79 for _, sig := range tg.Tests {
80 h.Reset()
81 h.Write(decodeHex(sig.Msg))
82 hashed := h.Sum(nil)
83 sigBytes := decodeHex(sig.Sig)
84 got := ecdsa.VerifyASN1(pub, hashed, sigBytes)
85 if want := shouldPass(sig.Result, sig.Flags, flagsShouldPass); got != want {
86 t.Errorf("tcid: %d, type: %s, comment: %q, VerifyASN1 wanted success: %t", sig.TcID, sig.Result, sig.Comment, want)
87 }
88
89 var r, s big.Int
90 var inner cryptobyte.String
91 input := cryptobyte.String(sigBytes)
92 if !input.ReadASN1(&inner, asn1.SEQUENCE) ||
93 !input.Empty() ||
94 !inner.ReadASN1Integer(&r) ||
95 !inner.ReadASN1Integer(&s) ||
96 !inner.Empty() {
97 continue
98 }
99 got = ecdsa.Verify(pub, hashed, &r, &s)
100 if want := shouldPass(sig.Result, sig.Flags, flagsShouldPass); got != want {
101 t.Errorf("tcid: %d, type: %s, comment: %q, Verify wanted success: %t", sig.TcID, sig.Result, sig.Comment, want)
102 }
103 }
104 }
105 }
106
View as plain text