Debian Codegraph @

openpgp/packet/symmetrically_encrypted_test.go

raw
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package packet
6
7 import (
8 "bytes"
9 "crypto/rand"
10 "crypto/sha1"
11 "encoding/hex"
12 goerrors "errors"
13 "io"
14 "testing"
15
16 "github.com/ProtonMail/go-crypto/openpgp/errors"
17 )
18
19 // TestReader wraps a []byte and returns reads of a specific length.
20 type testReader struct {
21 data []byte
22 stride int
23 }
24
25 func (t *testReader) Read(buf []byte) (n int, err error) {
26 n = t.stride
27 if n > len(t.data) {
28 n = len(t.data)
29 }
30 if n > len(buf) {
31 n = len(buf)
32 }
33
34 copy(buf[:n], t.data)
35 t.data = t.data[n:]
36
37 if len(t.data) == 0 {
38 err = io.EOF
39 }
40
41 return
42 }
43
44 const mdcPlaintextHex = "cb1362000000000048656c6c6f2c20776f726c6421d314c23d643f478a9a2098811fcb191e7b24b80966a1"
45
46 func TestMDCReader(t *testing.T) {
47 mdcPlaintext, _ := hex.DecodeString(mdcPlaintextHex)
48 for stride := 1; stride < len(mdcPlaintext)/2; stride++ {
49 r := &testReader{data: mdcPlaintext, stride: stride}
50 mdcReader := &seMDCReader{in: r, h: sha1.New()}
51 body, err := io.ReadAll(mdcReader)
52 if err != nil {
53 t.Errorf("stride: %d, error: %s", stride, err)
54 continue
55 }
56 if !bytes.Equal(body, mdcPlaintext[:len(mdcPlaintext)-22]) {
57 t.Errorf("stride: %d: bad contents %x", stride, body)
58 continue
59 }
60
61 err = mdcReader.Close()
62 if err != nil {
63 t.Errorf("stride: %d, error on Close: %s", stride, err)
64 }
65 }
66
67 mdcPlaintext[15] ^= 80
68
69 r := &testReader{data: mdcPlaintext, stride: 2}
70 mdcReader := &seMDCReader{in: r, h: sha1.New()}
71 _, err := io.ReadAll(mdcReader)
72 if err != nil {
73 t.Errorf("corruption test, error: %s", err)
74 return
75 }
76 err = mdcReader.Close()
77 if err == nil {
78 t.Error("corruption: no error")
79 } else if !goerrors.Is(err, errors.ErrMDCHashMismatch) {
80 t.Errorf("corruption: expected SignatureError, got: %s", err)
81 }
82 }
83
84 func TestSerializeMdc(t *testing.T) {
85 buf := bytes.NewBuffer(nil)
86 c := CipherAES128
87 key := make([]byte, c.KeySize())
88
89 cipherSuite := CipherSuite{
90 Cipher: c,
91 Mode: AEADModeOCB,
92 }
93
94 w, err := SerializeSymmetricallyEncrypted(buf, c, false, cipherSuite, key, nil)
95 if err != nil {
96 t.Errorf("error from SerializeSymmetricallyEncrypted: %s", err)
97 return
98 }
99
100 contents := []byte("hello world\n")
101
102 if _, err := w.Write(contents); err != nil {
103 t.Error(err)
104 return
105 }
106 w.Close()
107
108 p, err := Read(buf)
109 if err != nil {
110 t.Errorf("error from Read: %s", err)
111 return
112 }
113
114 se, ok := p.(*SymmetricallyEncrypted)
115 if !ok {
116 t.Errorf("didn't read a *SymmetricallyEncrypted")
117 return
118 }
119
120 r, err := se.Decrypt(c, key)
121 if err != nil {
122 t.Errorf("error from Decrypt: %s", err)
123 return
124 }
125
126 contentsCopy := bytes.NewBuffer(nil)
127 _, err = io.Copy(contentsCopy, r)
128 if err != nil {
129 t.Errorf("error from io.Copy: %s", err)
130 return
131 }
132 if !bytes.Equal(contentsCopy.Bytes(), contents) {
133 t.Errorf("contents not equal got: %x want: %x", contentsCopy.Bytes(), contents)
134 }
135 }
136
137 const aeadHexKey = "1936fc8568980274bb900d8319360c77"
138 const aeadHexSeipd = "d26902070306fcb94490bcb98bbdc9d106c6090266940f72e89edc21b5596b1576b101ed0f9ffc6fc6d65bbfd24dcd0790966e6d1e85a30053784cb1d8b6a0699ef12155a7b2ad6258531b57651fd7777912fa95e35d9b40216f69a4c248db28ff4331f1632907399e6ff9"
139 const aeadHexPlainText = "cb1362000000000048656c6c6f2c20776f726c6421d50e1ce2269a9eddef81032172b7ed7c"
140 const aeadExpectedSalt = "fcb94490bcb98bbdc9d106c6090266940f72e89edc21b5596b1576b101ed0f9f"
141
142 func TestAeadRfcVector(t *testing.T) {
143 key, err := hex.DecodeString(aeadHexKey)
144 if err != nil {
145 t.Errorf("error in decoding key: %s", err)
146 }
147
148 packet, err := hex.DecodeString(aeadHexSeipd)
149 if err != nil {
150 t.Errorf("error in decoding packet: %s", err)
151 }
152
153 plainText, err := hex.DecodeString(aeadHexPlainText)
154 if err != nil {
155 t.Errorf("error in decoding plaintext: %s", err)
156 }
157
158 expectedSalt, err := hex.DecodeString(aeadExpectedSalt)
159 if err != nil {
160 t.Errorf("error in decoding salt: %s", err)
161 }
162
163 buf := bytes.NewBuffer(packet)
164 p, err := Read(buf)
165 if err != nil {
166 t.Errorf("error from Read: %s", err)
167 return
168 }
169
170 se, ok := p.(*SymmetricallyEncrypted)
171 if !ok {
172 t.Errorf("didn't read a *SymmetricallyEncrypted")
173 return
174 }
175
176 if se.Version != symmetricallyEncryptedVersionAead {
177 t.Errorf("found wrong version, want: %d, got: %d", symmetricallyEncryptedVersionAead, se.Version)
178 }
179
180 if se.Cipher != CipherAES128 {
181 t.Errorf("found wrong cipher, want: %d, got: %d", CipherAES128, se.Cipher)
182 }
183
184 if se.Mode != AEADModeGCM {
185 t.Errorf("found wrong mode, want: %d, got: %d", AEADModeGCM, se.Mode)
186 }
187
188 if !bytes.Equal(se.Salt[:], expectedSalt) {
189 t.Errorf("found wrong salt, want: %x, got: %x", expectedSalt, se.Salt)
190 }
191
192 if se.ChunkSizeByte != 0x06 {
193 t.Errorf("found wrong chunk size byte, want: %d, got: %d", 0x06, se.ChunkSizeByte)
194 }
195
196 aeadReader, err := se.Decrypt(CipherFunction(0), key)
197 if err != nil {
198 t.Errorf("error from Decrypt: %s", err)
199 return
200 }
201
202 decrypted, err := io.ReadAll(aeadReader)
203 if err != nil {
204 t.Errorf("error when reading: %s", err)
205 return
206 }
207
208 err = aeadReader.Close()
209 if err != nil {
210 t.Errorf("error when closing reader: %s", err)
211 return
212 }
213
214 if !bytes.Equal(decrypted, plainText) {
215 t.Errorf("contents not equal got: %x want: %x", decrypted, plainText)
216 }
217 }
218
219 func TestAeadEncryptDecrypt(t *testing.T) {
220 ciphers := map[string]CipherFunction{
221 "AES128": CipherAES128,
222 "AES192": CipherAES192,
223 "AES256": CipherAES256,
224 }
225
226 modes := map[string]AEADMode{
227 "EAX": AEADModeEAX,
228 "OCB": AEADModeOCB,
229 "GCM": AEADModeGCM,
230 }
231
232 for cipherName, cipher := range ciphers {
233 t.Run(cipherName, func(t *testing.T) {
234 for modeName, mode := range modes {
235 t.Run(modeName, func(t *testing.T) {
236 testSerializeAead(t, CipherSuite{Cipher: cipher, Mode: mode})
237 })
238 }
239 })
240 }
241 }
242
243 func testSerializeAead(t *testing.T, cipherSuite CipherSuite) {
244 buf := bytes.NewBuffer(nil)
245 key := make([]byte, cipherSuite.Cipher.KeySize())
246 _, _ = rand.Read(key)
247
248 w, err := SerializeSymmetricallyEncrypted(buf, CipherFunction(0), true, cipherSuite, key, &Config{AEADConfig: &AEADConfig{}})
249 if err != nil {
250 t.Errorf("error from SerializeSymmetricallyEncrypted: %s", err)
251 return
252 }
253
254 contents := []byte("hello world\n")
255
256 w.Write(contents)
257 w.Close()
258
259 p, err := Read(buf)
260 if err != nil {
261 t.Errorf("error from Read: %s", err)
262 return
263 }
264
265 se, ok := p.(*SymmetricallyEncrypted)
266 if !ok {
267 t.Errorf("didn't read a *SymmetricallyEncrypted")
268 return
269 }
270
271 if se.Version != symmetricallyEncryptedVersionAead {
272 t.Errorf("found wrong version, want: %d, got: %d", symmetricallyEncryptedVersionAead, se.Version)
273 }
274
275 if se.Cipher != cipherSuite.Cipher {
276 t.Errorf("found wrong cipher, want: %d, got: %d", cipherSuite.Cipher, se.Cipher)
277 }
278
279 if se.Mode != cipherSuite.Mode {
280 t.Errorf("found wrong mode, want: %d, got: %d", cipherSuite.Mode, se.Mode)
281 }
282
283 r, err := se.Decrypt(CipherFunction(0), key)
284 if err != nil {
285 t.Errorf("error from Decrypt: %s", err)
286 return
287 }
288
289 contentsCopy := bytes.NewBuffer(nil)
290 _, err = io.Copy(contentsCopy, r)
291 if err != nil {
292 t.Errorf("error from io.Copy: %s", err)
293 return
294 }
295 if !bytes.Equal(contentsCopy.Bytes(), contents) {
296 t.Errorf("contents not equal got: %x want: %x", contentsCopy.Bytes(), contents)
297 }
298 }