1
2
3
4
5 package wycheproof
6
7 import (
8 "bytes"
9 "crypto/rsa"
10 "crypto/x509"
11 "fmt"
12 "testing"
13 )
14
15 func TestRSAOAEPDecrypt(t *testing.T) {
16
17 type Notes struct {
18 }
19
20
21 type RsaesOaepTestVector struct {
22
23
24 Comment string `json:"comment,omitempty"`
25
26
27 Ct string `json:"ct,omitempty"`
28
29
30 Flags []string `json:"flags,omitempty"`
31
32
33 Label string `json:"label,omitempty"`
34
35
36 Msg string `json:"msg,omitempty"`
37
38
39 Result string `json:"result,omitempty"`
40
41
42 TcId int `json:"tcId,omitempty"`
43 }
44
45
46 type RsaesOaepTestGroup struct {
47
48
49 D string `json:"d,omitempty"`
50
51
52 E string `json:"e,omitempty"`
53
54
55 Mgf string `json:"mgf,omitempty"`
56
57
58 MgfSha string `json:"mgfSha,omitempty"`
59
60
61 N string `json:"n,omitempty"`
62
63
64 PrivateKeyPem string `json:"privateKeyPem,omitempty"`
65
66
67 PrivateKeyPkcs8 string `json:"privateKeyPkcs8,omitempty"`
68
69
70 Sha string `json:"sha,omitempty"`
71 Tests []*RsaesOaepTestVector `json:"tests,omitempty"`
72 Type interface{} `json:"type,omitempty"`
73 }
74
75
76 type Root struct {
77
78
79 Algorithm string `json:"algorithm,omitempty"`
80
81
82 GeneratorVersion string `json:"generatorVersion,omitempty"`
83
84
85 Header []string `json:"header,omitempty"`
86
87
88 Notes *Notes `json:"notes,omitempty"`
89
90
91 NumberOfTests int `json:"numberOfTests,omitempty"`
92 Schema interface{} `json:"schema,omitempty"`
93 TestGroups []*RsaesOaepTestGroup `json:"testGroups,omitempty"`
94 }
95
96
97
98
99
100
101 files := []string{
102 "rsa_oaep_2048_sha1_mgf1sha1_test.json",
103 "rsa_oaep_2048_sha224_mgf1sha224_test.json",
104 "rsa_oaep_2048_sha256_mgf1sha256_test.json",
105 "rsa_oaep_2048_sha384_mgf1sha384_test.json",
106 "rsa_oaep_2048_sha512_mgf1sha512_test.json",
107 "rsa_oaep_3072_sha256_mgf1sha256_test.json",
108 "rsa_oaep_3072_sha512_mgf1sha512_test.json",
109 "rsa_oaep_4096_sha256_mgf1sha256_test.json",
110 "rsa_oaep_4096_sha512_mgf1sha512_test.json",
111 "rsa_oaep_misc_test.json",
112 }
113
114 flagsShouldPass := map[string]bool{
115
116 "SmallModulus": true,
117 }
118
119 for _, f := range files {
120 var root Root
121 readTestVector(t, f, &root)
122 for _, tg := range root.TestGroups {
123 if tg.MgfSha != tg.Sha {
124 continue
125 }
126 priv, err := x509.ParsePKCS8PrivateKey(decodeHex(tg.PrivateKeyPkcs8))
127 if err != nil {
128 t.Fatalf("%s failed to parse PKCS #8 private key: %s", f, err)
129 }
130 hash := parseHash(tg.Sha)
131 for _, tv := range tg.Tests {
132 t.Run(fmt.Sprintf("%s #%d", f, tv.TcId), func(t *testing.T) {
133 wantPass := shouldPass(tv.Result, tv.Flags, flagsShouldPass)
134 plaintext, err := rsa.DecryptOAEP(hash.New(), nil, priv.(*rsa.PrivateKey), decodeHex(tv.Ct), decodeHex(tv.Label))
135 if wantPass {
136 if err != nil {
137 t.Fatalf("comment: %s, expected success: %s", tv.Comment, err)
138 }
139 if !bytes.Equal(plaintext, decodeHex(tv.Msg)) {
140 t.Errorf("comment: %s, unexpected plaintext: got %x, want %s", tv.Comment, plaintext, tv.Msg)
141 }
142 } else if err == nil {
143 t.Errorf("comment: %s, expected failure", tv.Comment)
144 }
145 })
146 }
147 }
148 }
149 }
150
View as plain text