...
1
2
3
4
5 package wycheproof
6
7 import (
8 "crypto/dsa"
9 "testing"
10
11 wdsa "golang.org/x/crypto/internal/wycheproof/internal/dsa"
12 )
13
14 func TestDsa(t *testing.T) {
15
16 type AsnSignatureTestVector struct {
17
18
19 Comment string `json:"comment,omitempty"`
20
21
22 Flags []string `json:"flags,omitempty"`
23
24
25 Msg string `json:"msg,omitempty"`
26
27
28 Result string `json:"result,omitempty"`
29
30
31 Sig string `json:"sig,omitempty"`
32
33
34 TcId int `json:"tcId,omitempty"`
35 }
36
37
38 type DsaPublicKey struct {
39
40
41 G string `json:"g,omitempty"`
42
43
44 KeySize int `json:"keySize,omitempty"`
45
46
47 P string `json:"p,omitempty"`
48
49
50 Q string `json:"q,omitempty"`
51
52
53 Type string `json:"type,omitempty"`
54
55
56 Y string `json:"y,omitempty"`
57 }
58
59
60 type DsaTestGroup struct {
61
62
63 Key *DsaPublicKey `json:"key,omitempty"`
64
65
66 KeyDer string `json:"keyDer,omitempty"`
67
68
69 KeyPem string `json:"keyPem,omitempty"`
70
71
72 Sha string `json:"sha,omitempty"`
73 Tests []*AsnSignatureTestVector `json:"tests,omitempty"`
74 Type interface{} `json:"type,omitempty"`
75 }
76
77
78 type Notes struct {
79 }
80
81
82 type Root struct {
83
84
85 Algorithm string `json:"algorithm,omitempty"`
86
87
88 GeneratorVersion string `json:"generatorVersion,omitempty"`
89
90
91 Header []string `json:"header,omitempty"`
92
93
94 Notes *Notes `json:"notes,omitempty"`
95
96
97 NumberOfTests int `json:"numberOfTests,omitempty"`
98 Schema interface{} `json:"schema,omitempty"`
99 TestGroups []*DsaTestGroup `json:"testGroups,omitempty"`
100 }
101
102 flagsShouldPass := map[string]bool{
103
104 "NoLeadingZero": false,
105 }
106
107 var root Root
108 readTestVector(t, "dsa_test.json", &root)
109 for _, tg := range root.TestGroups {
110 pub := decodePublicKey(tg.KeyDer).(*dsa.PublicKey)
111 h := parseHash(tg.Sha).New()
112 for _, sig := range tg.Tests {
113 h.Reset()
114 h.Write(decodeHex(sig.Msg))
115 hashed := h.Sum(nil)
116 hashed = hashed[:pub.Q.BitLen()/8]
117 got := wdsa.VerifyASN1(pub, hashed, decodeHex(sig.Sig))
118 if want := shouldPass(sig.Result, sig.Flags, flagsShouldPass); got != want {
119 t.Errorf("tcid: %d, type: %s, comment: %q, wanted success: %t", sig.TcId, sig.Result, sig.Comment, want)
120 }
121 }
122 }
123 }
124
View as plain text