...
1
2
3
4
5 package wycheproof
6
7 import (
8 "crypto/rsa"
9 "testing"
10 )
11
12 func TestRsa(t *testing.T) {
13
14 type KeyJwk struct {
15 }
16
17
18 type Notes struct {
19 }
20
21
22 type SignatureTestVector struct {
23
24
25 Comment string `json:"comment,omitempty"`
26
27
28 Flags []string `json:"flags,omitempty"`
29
30
31 Msg string `json:"msg,omitempty"`
32
33
34 Result string `json:"result,omitempty"`
35
36
37 Sig string `json:"sig,omitempty"`
38
39
40 TcId int `json:"tcId,omitempty"`
41 }
42
43
44 type RsassaPkcs1TestGroup struct {
45
46
47 D string `json:"d,omitempty"`
48
49
50 E string `json:"e,omitempty"`
51
52
53 KeyAsn string `json:"keyAsn,omitempty"`
54
55
56 KeyDer string `json:"keyDer,omitempty"`
57
58
59 KeyJwk *KeyJwk `json:"keyJwk,omitempty"`
60
61
62 KeyPem string `json:"keyPem,omitempty"`
63
64
65 KeySize int `json:"keySize,omitempty"`
66
67
68 N string `json:"n,omitempty"`
69
70
71 Sha string `json:"sha,omitempty"`
72 Tests []*SignatureTestVector `json:"tests,omitempty"`
73 Type interface{} `json:"type,omitempty"`
74 }
75
76
77 type Root struct {
78
79
80 Algorithm string `json:"algorithm,omitempty"`
81
82
83 GeneratorVersion string `json:"generatorVersion,omitempty"`
84
85
86 Header []string `json:"header,omitempty"`
87
88
89 Notes *Notes `json:"notes,omitempty"`
90
91
92 NumberOfTests int `json:"numberOfTests,omitempty"`
93 Schema interface{} `json:"schema,omitempty"`
94 TestGroups []*RsassaPkcs1TestGroup `json:"testGroups,omitempty"`
95 }
96
97 flagsShouldPass := map[string]bool{
98
99 "MissingNull": false,
100
101 "SmallModulus": true,
102
103 "SmallPublicKey": true,
104 }
105
106 var root Root
107 readTestVector(t, "rsa_signature_test.json", &root)
108 for _, tg := range root.TestGroups {
109 pub := decodePublicKey(tg.KeyDer).(*rsa.PublicKey)
110 ch := parseHash(tg.Sha)
111 h := ch.New()
112 for _, sig := range tg.Tests {
113 h.Reset()
114 h.Write(decodeHex(sig.Msg))
115 hashed := h.Sum(nil)
116 err := rsa.VerifyPKCS1v15(pub, ch, hashed, decodeHex(sig.Sig))
117 want := shouldPass(sig.Result, sig.Flags, flagsShouldPass)
118 if (err == nil) != 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