1
16
17 package jose
18
19 import (
20 "bytes"
21 "crypto/ecdsa"
22 "crypto/elliptic"
23 "crypto/rand"
24 "crypto/rsa"
25 "fmt"
26 "io"
27 "math/big"
28 "reflect"
29 "regexp"
30 "testing"
31
32 "golang.org/x/crypto/ed25519"
33 )
34
35
36 var rsaTestKey, _ = rsa.GenerateKey(rand.Reader, 2048)
37
38 var ecTestKey256, _ = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
39 var ecTestKey384, _ = ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
40 var ecTestKey521, _ = ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
41
42 var ed25519PublicKey, ed25519PrivateKey, _ = ed25519.GenerateKey(rand.Reader)
43
44 func RoundtripJWE(keyAlg KeyAlgorithm, encAlg ContentEncryption, compressionAlg CompressionAlgorithm, serializer func(*JSONWebEncryption) (string, error), corrupter func(*JSONWebEncryption) bool, aad []byte, encryptionKey interface{}, decryptionKey interface{}) error {
45 var rcpt Recipient
46 switch keyAlg {
47 case PBES2_HS256_A128KW, PBES2_HS384_A192KW, PBES2_HS512_A256KW:
48
49 rcpt = Recipient{Algorithm: keyAlg, Key: encryptionKey, PBES2Count: 1000}
50 default:
51 rcpt = Recipient{Algorithm: keyAlg, Key: encryptionKey}
52 }
53 enc, err := NewEncrypter(encAlg, rcpt, &EncrypterOptions{Compression: compressionAlg})
54 if err != nil {
55 return fmt.Errorf("error on new encrypter: %s", err)
56 }
57
58 input := []byte("Lorem ipsum dolor sit amet")
59 obj, err := enc.EncryptWithAuthData(input, aad)
60 if err != nil {
61 return fmt.Errorf("error in encrypt: %s", err)
62 }
63
64 msg, err := serializer(obj)
65 if err != nil {
66 return fmt.Errorf("error in serializer: %s", err)
67 }
68
69 parsed, err := ParseEncrypted(msg)
70 if err != nil {
71 return fmt.Errorf("error in parse: %s, on msg '%s'", err, msg)
72 }
73
74
75 skip := corrupter(parsed)
76 if skip {
77 return fmt.Errorf("corrupter indicated message should be skipped")
78 }
79
80 if bytes.Compare(parsed.GetAuthData(), aad) != 0 {
81 return fmt.Errorf("auth data in parsed object does not match")
82 }
83
84 output, err := parsed.Decrypt(decryptionKey)
85 if err != nil {
86 return fmt.Errorf("error on decrypt: %s", err)
87 }
88
89 if bytes.Compare(input, output) != 0 {
90 return fmt.Errorf("Decrypted output does not match input, got '%s' but wanted '%s'", output, input)
91 }
92
93 return nil
94 }
95
96 func TestRoundtripsJWE(t *testing.T) {
97
98 keyAlgs := []KeyAlgorithm{
99 DIRECT, ECDH_ES, ECDH_ES_A128KW, ECDH_ES_A192KW, ECDH_ES_A256KW, A128KW, A192KW, A256KW,
100 RSA1_5, RSA_OAEP, RSA_OAEP_256, A128GCMKW, A192GCMKW, A256GCMKW,
101 PBES2_HS256_A128KW, PBES2_HS384_A192KW, PBES2_HS512_A256KW,
102 }
103 encAlgs := []ContentEncryption{A128GCM, A192GCM, A256GCM, A128CBC_HS256, A192CBC_HS384, A256CBC_HS512}
104 zipAlgs := []CompressionAlgorithm{NONE, DEFLATE}
105
106 serializers := []func(*JSONWebEncryption) (string, error){
107 func(obj *JSONWebEncryption) (string, error) { return obj.CompactSerialize() },
108 func(obj *JSONWebEncryption) (string, error) { return obj.FullSerialize(), nil },
109 }
110
111 corrupter := func(obj *JSONWebEncryption) bool { return false }
112
113
114 aads := [][]byte{
115 nil,
116 []byte("Ut enim ad minim veniam"),
117 }
118
119
120 for _, alg := range keyAlgs {
121 for _, enc := range encAlgs {
122 for _, key := range generateTestKeys(alg, enc) {
123 for _, zip := range zipAlgs {
124 for i, serializer := range serializers {
125 err := RoundtripJWE(alg, enc, zip, serializer, corrupter, aads[i], key.enc, key.dec)
126 if err != nil {
127 t.Error(err, alg, enc, zip, i)
128 }
129 }
130 }
131 }
132 }
133 }
134 }
135
136 func TestRoundtripsJWECorrupted(t *testing.T) {
137
138 keyAlgs := []KeyAlgorithm{DIRECT, ECDH_ES, ECDH_ES_A128KW, A128KW, RSA1_5, RSA_OAEP, RSA_OAEP_256, A128GCMKW, PBES2_HS256_A128KW}
139 encAlgs := []ContentEncryption{A128GCM, A192GCM, A256GCM, A128CBC_HS256, A192CBC_HS384, A256CBC_HS512}
140 zipAlgs := []CompressionAlgorithm{NONE, DEFLATE}
141
142 serializers := []func(*JSONWebEncryption) (string, error){
143 func(obj *JSONWebEncryption) (string, error) { return obj.CompactSerialize() },
144 func(obj *JSONWebEncryption) (string, error) { return obj.FullSerialize(), nil },
145 }
146
147 bitflip := func(slice []byte) bool {
148 if len(slice) > 0 {
149 slice[0] ^= 0xFF
150 return false
151 }
152 return true
153 }
154
155 corrupters := []func(*JSONWebEncryption) bool{
156 func(obj *JSONWebEncryption) bool {
157
158 return bitflip(obj.ciphertext)
159 },
160 func(obj *JSONWebEncryption) bool {
161
162 return bitflip(obj.tag)
163 },
164 func(obj *JSONWebEncryption) bool {
165
166 return bitflip(obj.aad)
167 },
168 func(obj *JSONWebEncryption) bool {
169
170 return bitflip(obj.recipients[0].encryptedKey)
171 },
172 func(obj *JSONWebEncryption) bool {
173
174 tag, _ := obj.protected.getTag()
175 skip := bitflip(tag.bytes())
176 if skip {
177 return true
178 }
179 obj.protected.set(headerTag, tag)
180 return false
181 },
182 }
183
184
185 aads := [][]byte{
186 nil,
187 []byte("Ut enim ad minim veniam"),
188 }
189
190
191 for _, alg := range keyAlgs {
192 for _, enc := range encAlgs {
193 for _, key := range generateTestKeys(alg, enc) {
194 for _, zip := range zipAlgs {
195 for i, serializer := range serializers {
196 for j, corrupter := range corrupters {
197 err := RoundtripJWE(alg, enc, zip, serializer, corrupter, aads[i], key.enc, key.dec)
198 if err == nil {
199 t.Error("failed to detect corrupt data", err, alg, enc, zip, i, j)
200 }
201 }
202 }
203 }
204 }
205 }
206 }
207 }
208
209 func TestEncrypterWithJWKAndKeyID(t *testing.T) {
210 enc, err := NewEncrypter(A128GCM, Recipient{Algorithm: A128KW, Key: &JSONWebKey{
211 KeyID: "test-id",
212 Key: []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
213 }}, nil)
214 if err != nil {
215 t.Error(err)
216 }
217
218 ciphertext, _ := enc.Encrypt([]byte("Lorem ipsum dolor sit amet"))
219
220 serialized1, _ := ciphertext.CompactSerialize()
221 serialized2 := ciphertext.FullSerialize()
222
223 parsed1, _ := ParseEncrypted(serialized1)
224 parsed2, _ := ParseEncrypted(serialized2)
225
226 if parsed1.Header.KeyID != "test-id" {
227 t.Errorf("expected message to have key id from JWK, but found '%s' instead", parsed1.Header.KeyID)
228 }
229 if parsed2.Header.KeyID != "test-id" {
230 t.Errorf("expected message to have key id from JWK, but found '%s' instead", parsed2.Header.KeyID)
231 }
232 }
233
234 func TestEncrypterWithBrokenRand(t *testing.T) {
235 keyAlgs := []KeyAlgorithm{ECDH_ES_A128KW, A128KW, RSA1_5, RSA_OAEP, RSA_OAEP_256, A128GCMKW, PBES2_HS256_A128KW}
236 encAlgs := []ContentEncryption{A128GCM, A192GCM, A256GCM, A128CBC_HS256, A192CBC_HS384, A256CBC_HS512}
237
238 serializer := func(obj *JSONWebEncryption) (string, error) { return obj.CompactSerialize() }
239 corrupter := func(obj *JSONWebEncryption) bool { return false }
240
241
242 readers := []func() io.Reader{
243
244 func() io.Reader { return bytes.NewReader([]byte{}) },
245
246 func() io.Reader { return io.LimitReader(rand.Reader, 20) },
247 }
248
249 defer resetRandReader()
250
251 for _, alg := range keyAlgs {
252 for _, enc := range encAlgs {
253 for _, key := range generateTestKeys(alg, enc) {
254 for i, getReader := range readers {
255 RandReader = getReader()
256 err := RoundtripJWE(alg, enc, NONE, serializer, corrupter, nil, key.enc, key.dec)
257 if err == nil {
258 t.Error("encrypter should fail if rand is broken", i)
259 }
260 }
261 }
262 }
263 }
264 }
265
266 func TestNewEncrypterErrors(t *testing.T) {
267 _, err := NewEncrypter("XYZ", Recipient{}, nil)
268 if err == nil {
269 t.Error("was able to instantiate encrypter with invalid cipher")
270 }
271
272 _, err = NewMultiEncrypter("XYZ", []Recipient{}, nil)
273 if err == nil {
274 t.Error("was able to instantiate multi-encrypter with invalid cipher")
275 }
276
277 _, err = NewEncrypter(A128GCM, Recipient{Algorithm: DIRECT, Key: nil}, nil)
278 if err == nil {
279 t.Error("was able to instantiate encrypter with invalid direct key")
280 }
281
282 _, err = NewEncrypter(A128GCM, Recipient{Algorithm: ECDH_ES, Key: nil}, nil)
283 if err == nil {
284 t.Error("was able to instantiate encrypter with invalid EC key")
285 }
286 }
287
288 func TestMultiRecipientJWE(t *testing.T) {
289 sharedKey := []byte{
290 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
291 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
292 }
293
294 enc, err := NewMultiEncrypter(A128GCM, []Recipient{
295 {Algorithm: RSA_OAEP, Key: &rsaTestKey.PublicKey},
296 {Algorithm: A256GCMKW, Key: sharedKey},
297 }, nil)
298 if err != nil {
299 panic(err)
300 }
301
302 input := []byte("Lorem ipsum dolor sit amet")
303 obj, err := enc.Encrypt(input)
304 if err != nil {
305 t.Fatal("error in encrypt: ", err)
306 }
307
308 msg := obj.FullSerialize()
309
310 parsed, err := ParseEncrypted(msg)
311 if err != nil {
312 t.Fatal("error in parse: ", err)
313 }
314
315 i, _, output, err := parsed.DecryptMulti(rsaTestKey)
316 if err != nil {
317 t.Fatal("error on decrypt with RSA: ", err)
318 }
319
320 if i != 0 {
321 t.Fatal("recipient index should be 0 for RSA key")
322 }
323
324 if bytes.Compare(input, output) != 0 {
325 t.Fatal("Decrypted output does not match input: ", output, input)
326 }
327
328 i, _, output, err = parsed.DecryptMulti(sharedKey)
329 if err != nil {
330 t.Fatal("error on decrypt with AES: ", err)
331 }
332
333 if i != 1 {
334 t.Fatal("recipient index should be 1 for shared key")
335 }
336
337 if bytes.Compare(input, output) != 0 {
338 t.Fatal("Decrypted output does not match input", output, input)
339 }
340 }
341
342 func TestMultiRecipientErrors(t *testing.T) {
343 _, err := NewMultiEncrypter(A128GCM, []Recipient{}, nil)
344 if err == nil {
345 t.Error("should fail to instantiate with zero recipients")
346 }
347 }
348
349 func TestEncrypterOptions(t *testing.T) {
350 sharedKey := []byte{
351 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
352 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
353 }
354
355 opts := &EncrypterOptions{
356 Compression: DEFLATE,
357 }
358 opts.WithType("JWT")
359 opts.WithContentType("JWT")
360 enc, err := NewEncrypter(A256GCM, Recipient{Algorithm: A256GCMKW, Key: sharedKey}, opts)
361 if err != nil {
362 fmt.Println(err)
363 t.Error("Failed to create encrypter")
364 }
365
366 if !reflect.DeepEqual(*opts, enc.Options()) {
367 t.Error("Encrypter options do not match")
368 }
369 }
370
371
372 func TestEncrypterExtraHeaderInclusion(t *testing.T) {
373 sharedKey := []byte{
374 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
375 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
376 }
377
378 opts := &EncrypterOptions{
379 Compression: DEFLATE,
380 }
381 opts.WithType("JWT")
382 opts.WithContentType("JWT")
383 opts.WithHeader(HeaderKey("myCustomHeader"), "xyz")
384 enc, err := NewEncrypter(A256GCM, Recipient{Algorithm: A256GCMKW, Key: sharedKey}, opts)
385 if err != nil {
386 fmt.Println(err)
387 t.Error("Failed to create encrypter")
388 }
389
390 if !reflect.DeepEqual(*opts, enc.Options()) {
391 t.Error("Encrypter options do not match")
392 }
393
394 input := []byte("Lorem ipsum dolor sit amet")
395 obj, err := enc.Encrypt(input)
396 if err != nil {
397 t.Fatal("error in encrypt: ", err)
398 }
399
400 parsed, err := ParseEncrypted(obj.FullSerialize())
401 if err != nil {
402 t.Fatal("error in parse: ", err)
403 }
404
405 output, err := parsed.Decrypt(sharedKey)
406 if err != nil {
407 t.Fatal("error on decrypt: ", err)
408 }
409
410 if bytes.Compare(input, output) != 0 {
411 t.Fatal("Decrypted output does not match input: ", output, input)
412 }
413
414 if parsed.Header.ExtraHeaders[HeaderType] != "JWT" ||
415 parsed.Header.ExtraHeaders[HeaderContentType] != "JWT" ||
416 parsed.Header.ExtraHeaders[HeaderKey("myCustomHeader")] != "xyz" {
417 t.Fatalf("Mismatch in extra headers: %#v", parsed.Header.ExtraHeaders)
418 }
419 }
420
421
422
423 func TestPBES2JWKEncryption(t *testing.T) {
424 passphrase := []byte("Thus from my lips, by yours, my sin is purged.")
425
426 plaintext := []byte(`{
427 "kty":"RSA",
428 "kid":"juliet@capulet.lit",
429 "use":"enc",
430 "n":"t6Q8PWSi1dkJj9hTP8hNYFlvadM7DflW9mWepOJhJ66w7nyoK1gPNqFMSQRy
431 O125Gp-TEkodhWr0iujjHVx7BcV0llS4w5ACGgPrcAd6ZcSR0-Iqom-QFcNP
432 8Sjg086MwoqQU_LYywlAGZ21WSdS_PERyGFiNnj3QQlO8Yns5jCtLCRwLHL0
433 Pb1fEv45AuRIuUfVcPySBWYnDyGxvjYGDSM-AqWS9zIQ2ZilgT-GqUmipg0X
434 OC0Cc20rgLe2ymLHjpHciCKVAbY5-L32-lSeZO-Os6U15_aXrk9Gw8cPUaX1
435 _I8sLGuSiVdt3C_Fn2PZ3Z8i744FPFGGcG1qs2Wz-Q",
436 "e":"AQAB",
437 "d":"GRtbIQmhOZtyszfgKdg4u_N-R_mZGU_9k7JQ_jn1DnfTuMdSNprTeaSTyWfS
438 NkuaAwnOEbIQVy1IQbWVV25NY3ybc_IhUJtfri7bAXYEReWaCl3hdlPKXy9U
439 vqPYGR0kIXTQRqns-dVJ7jahlI7LyckrpTmrM8dWBo4_PMaenNnPiQgO0xnu
440 ToxutRZJfJvG4Ox4ka3GORQd9CsCZ2vsUDmsXOfUENOyMqADC6p1M3h33tsu
441 rY15k9qMSpG9OX_IJAXmxzAh_tWiZOwk2K4yxH9tS3Lq1yX8C1EWmeRDkK2a
442 hecG85-oLKQt5VEpWHKmjOi_gJSdSgqcN96X52esAQ",
443 "p":"2rnSOV4hKSN8sS4CgcQHFbs08XboFDqKum3sc4h3GRxrTmQdl1ZK9uw-PIHf
444 QP0FkxXVrx-WE-ZEbrqivH_2iCLUS7wAl6XvARt1KkIaUxPPSYB9yk31s0Q8
445 UK96E3_OrADAYtAJs-M3JxCLfNgqh56HDnETTQhH3rCT5T3yJws",
446 "q":"1u_RiFDP7LBYh3N4GXLT9OpSKYP0uQZyiaZwBtOCBNJgQxaj10RWjsZu0c6I
447 edis4S7B_coSKB0Kj9PaPaBzg-IySRvvcQuPamQu66riMhjVtG6TlV8CLCYK
448 rYl52ziqK0E_ym2QnkwsUX7eYTB7LbAHRK9GqocDE5B0f808I4s",
449 "dp":"KkMTWqBUefVwZ2_Dbj1pPQqyHSHjj90L5x_MOzqYAJMcLMZtbUtwKqvVDq3
450 tbEo3ZIcohbDtt6SbfmWzggabpQxNxuBpoOOf_a_HgMXK_lhqigI4y_kqS1w
451 Y52IwjUn5rgRrJ-yYo1h41KR-vz2pYhEAeYrhttWtxVqLCRViD6c",
452 "dq":"AvfS0-gRxvn0bwJoMSnFxYcK1WnuEjQFluMGfwGitQBWtfZ1Er7t1xDkbN9
453 GQTB9yqpDoYaN06H7CFtrkxhJIBQaj6nkF5KKS3TQtQ5qCzkOkmxIe3KRbBy
454 mXxkb5qwUpX5ELD5xFc6FeiafWYY63TmmEAu_lRFCOJ3xDea-ots",
455 "qi":"lSQi-w9CpyUReMErP1RsBLk7wNtOvs5EQpPqmuMvqW57NBUczScEoPwmUqq
456 abu9V0-Py4dQ57_bapoKRu1R90bvuFnU63SHWEFglZQvJDMeAvmj4sm-Fp0o
457 Yu_neotgQ0hzbI5gry7ajdYy9-2lNx_76aBZoOUu9HCJ-UsfSOI8"
458 }`)
459
460 serializationReference := `
461 eyJhbGciOiJQQkVTMi1IUzI1NitBMTI4S1ciLCJwMnMiOiIyV0NUY0paMVJ2ZF9DSn
462 VKcmlwUTF3IiwicDJjIjo0MDk2LCJlbmMiOiJBMTI4Q0JDLUhTMjU2IiwiY3R5Ijoi
463 andrK2pzb24ifQ.
464 TrqXOwuNUfDV9VPTNbyGvEJ9JMjefAVn-TR1uIxR9p6hsRQh9Tk7BA.
465 Ye9j1qs22DmRSAddIh-VnA.
466 AwhB8lxrlKjFn02LGWEqg27H4Tg9fyZAbFv3p5ZicHpj64QyHC44qqlZ3JEmnZTgQo
467 wIqZJ13jbyHB8LgePiqUJ1hf6M2HPLgzw8L-mEeQ0jvDUTrE07NtOerBk8bwBQyZ6g
468 0kQ3DEOIglfYxV8-FJvNBYwbqN1Bck6d_i7OtjSHV-8DIrp-3JcRIe05YKy3Oi34Z_
469 GOiAc1EK21B11c_AE11PII_wvvtRiUiG8YofQXakWd1_O98Kap-UgmyWPfreUJ3lJP
470 nbD4Ve95owEfMGLOPflo2MnjaTDCwQokoJ_xplQ2vNPz8iguLcHBoKllyQFJL2mOWB
471 wqhBo9Oj-O800as5mmLsvQMTflIrIEbbTMzHMBZ8EFW9fWwwFu0DWQJGkMNhmBZQ-3
472 lvqTc-M6-gWA6D8PDhONfP2Oib2HGizwG1iEaX8GRyUpfLuljCLIe1DkGOewhKuKkZ
473 h04DKNM5Nbugf2atmU9OP0Ldx5peCUtRG1gMVl7Qup5ZXHTjgPDr5b2N731UooCGAU
474 qHdgGhg0JVJ_ObCTdjsH4CF1SJsdUhrXvYx3HJh2Xd7CwJRzU_3Y1GxYU6-s3GFPbi
475 rfqqEipJDBTHpcoCmyrwYjYHFgnlqBZRotRrS95g8F95bRXqsaDY7UgQGwBQBwy665
476 d0zpvTasvfXf_c0MWAl-neFaKOW_Px6g4EUDjG1GWSXV9cLStLw_0ovdApDIFLHYHe
477 PyagyHjouQUuGiq7BsYwYrwaF06tgB8hV8omLNfMEmDPJaZUzMuHw6tBDwGkzD-tS_
478 ub9hxrpJ4UsOWnt5rGUyoN2N_c1-TQlXxm5oto14MxnoAyBQBpwIEgSH3Y4ZhwKBhH
479 PjSo0cdwuNdYbGPpb-YUvF-2NZzODiQ1OvWQBRHSbPWYz_xbGkgD504LRtqRwCO7CC
480 _CyyURi1sEssPVsMJRX_U4LFEOc82TiDdqjKOjRUfKK5rqLi8nBE9soQ0DSaOoFQZi
481 GrBrqxDsNYiAYAmxxkos-i3nX4qtByVx85sCE5U_0MqG7COxZWMOPEFrDaepUV-cOy
482 rvoUIng8i8ljKBKxETY2BgPegKBYCxsAUcAkKamSCC9AiBxA0UOHyhTqtlvMksO7AE
483 hNC2-YzPyx1FkhMoS4LLe6E_pFsMlmjA6P1NSge9C5G5tETYXGAn6b1xZbHtmwrPSc
484 ro9LWhVmAaA7_bxYObnFUxgWtK4vzzQBjZJ36UTk4OTB-JvKWgfVWCFsaw5WCHj6Oo
485 4jpO7d2yN7WMfAj2hTEabz9wumQ0TMhBduZ-QON3pYObSy7TSC1vVme0NJrwF_cJRe
486 hKTFmdlXGVldPxZCplr7ZQqRQhF8JP-l4mEQVnCaWGn9ONHlemczGOS-A-wwtnmwjI
487 B1V_vgJRf4FdpV-4hUk4-QLpu3-1lWFxrtZKcggq3tWTduRo5_QebQbUUT_VSCgsFc
488 OmyWKoj56lbxthN19hq1XGWbLGfrrR6MWh23vk01zn8FVwi7uFwEnRYSafsnWLa1Z5
489 TpBj9GvAdl2H9NHwzpB5NqHpZNkQ3NMDj13Fn8fzO0JB83Etbm_tnFQfcb13X3bJ15
490 Cz-Ww1MGhvIpGGnMBT_ADp9xSIyAM9dQ1yeVXk-AIgWBUlN5uyWSGyCxp0cJwx7HxM
491 38z0UIeBu-MytL-eqndM7LxytsVzCbjOTSVRmhYEMIzUAnS1gs7uMQAGRdgRIElTJE
492 SGMjb_4bZq9s6Ve1LKkSi0_QDsrABaLe55UY0zF4ZSfOV5PMyPtocwV_dcNPlxLgNA
493 D1BFX_Z9kAdMZQW6fAmsfFle0zAoMe4l9pMESH0JB4sJGdCKtQXj1cXNydDYozF7l8
494 H00BV_Er7zd6VtIw0MxwkFCTatsv_R-GsBCH218RgVPsfYhwVuT8R4HarpzsDBufC4
495 r8_c8fc9Z278sQ081jFjOja6L2x0N_ImzFNXU6xwO-Ska-QeuvYZ3X_L31ZOX4Llp-
496 7QSfgDoHnOxFv1Xws-D5mDHD3zxOup2b2TppdKTZb9eW2vxUVviM8OI9atBfPKMGAO
497 v9omA-6vv5IxUH0-lWMiHLQ_g8vnswp-Jav0c4t6URVUzujNOoNd_CBGGVnHiJTCHl
498 88LQxsqLHHIu4Fz-U2SGnlxGTj0-ihit2ELGRv4vO8E1BosTmf0cx3qgG0Pq0eOLBD
499 IHsrdZ_CCAiTc0HVkMbyq1M6qEhM-q5P6y1QCIrwg.
500 0HFmhOzsQ98nNWJjIHkR7A`
501
502
503 r := regexp.MustCompile(`\s`)
504 plaintext = r.ReplaceAll(plaintext, []byte(""))
505 serializationReference = r.ReplaceAllString(serializationReference, "")
506
507 rcpt := Recipient{
508 Algorithm: PBES2_HS256_A128KW,
509 Key: passphrase,
510 PBES2Count: 4096,
511 PBES2Salt: []byte{
512 217, 96, 147, 112, 150, 117, 70,
513 247, 127, 8, 155, 137, 174, 42, 80, 215,
514 },
515 }
516
517 enc, err := NewEncrypter(A128CBC_HS256, rcpt, nil)
518 if err != nil {
519 t.Fatal("error on NewEncrypter:", err)
520 }
521
522 obj, err := enc.Encrypt(plaintext)
523 if err != nil {
524 t.Fatal("error on new Encrypt:", err)
525 }
526
527 serialized, err := obj.CompactSerialize()
528 if err != nil {
529 t.Fatal("error on CompactSerialize")
530 }
531
532 jwe1, err := ParseEncrypted(serialized)
533 if err != nil {
534 t.Fatal("error in ParseEncrypted")
535 }
536
537 jwe2, err := ParseEncrypted(serializationReference)
538 if err != nil {
539 t.Fatal("error in ParseEncrypted")
540 }
541
542 original1, err := jwe1.Decrypt(passphrase)
543 if err != nil {
544 t.Fatal("error in Decrypt:", err)
545 }
546
547 original2, err := jwe2.Decrypt(passphrase)
548 if err != nil {
549 t.Fatal("error in Decrypt reference:", err)
550 }
551
552 if bytes.Compare(original1, original2) != 0 {
553 t.Error("decryption does not match reference decryption")
554 }
555
556 if bytes.Compare(plaintext, original1) != 0 {
557 t.Error("decryption does not match plaintext")
558 }
559
560 if bytes.Compare(plaintext, original2) != 0 {
561 t.Error("reference decryption does not match plaintext")
562 }
563 }
564
565 func TestEncrypterWithPBES2(t *testing.T) {
566 expected := []byte("Lorem ipsum dolor sit amet")
567 algs := []KeyAlgorithm{
568 PBES2_HS256_A128KW, PBES2_HS384_A192KW, PBES2_HS512_A256KW,
569 }
570
571
572 recipientKeys := []interface{}{"password", []byte("password")}
573 for _, key := range recipientKeys {
574 for _, alg := range algs {
575 enc, err := NewEncrypter(A128GCM, Recipient{Algorithm: alg, Key: &JSONWebKey{
576 KeyID: "test-id",
577 Key: key,
578 }}, nil)
579 if err != nil {
580 t.Error(err)
581 }
582
583 ciphertext, _ := enc.Encrypt(expected)
584
585 serialized1, _ := ciphertext.CompactSerialize()
586 serialized2 := ciphertext.FullSerialize()
587
588 parsed1, _ := ParseEncrypted(serialized1)
589 parsed2, _ := ParseEncrypted(serialized2)
590
591 actual1, err := parsed1.Decrypt("password")
592 if err != nil {
593 t.Fatal("error on Decrypt:", err)
594 }
595
596 actual2, err := parsed2.Decrypt([]byte("password"))
597 if err != nil {
598 t.Fatal("error on Decrypt:", err)
599 }
600
601 if bytes.Compare(actual1, expected) != 0 {
602 t.Errorf("error comparing decrypted message (%s) and expected (%s)", actual1, expected)
603 }
604
605 if bytes.Compare(actual2, expected) != 0 {
606 t.Errorf("error comparing decrypted message (%s) and expected (%s)", actual2, expected)
607 }
608 }
609 }
610 }
611
612 type testKey struct {
613 enc, dec interface{}
614 }
615
616 func symmetricTestKey(size int) []testKey {
617 key, _, _ := randomKeyGenerator{size: size}.genKey()
618
619 return []testKey{
620 {
621 enc: key,
622 dec: key,
623 },
624 {
625 enc: &JSONWebKey{KeyID: "test", Key: key},
626 dec: &JSONWebKey{KeyID: "test", Key: key},
627 },
628 }
629 }
630
631 func TestDirectEncryptionKeySizeCheck(t *testing.T) {
632
633 key16 := []byte("0123456789ABCDEF")
634
635
636 key32 := []byte("0123456789ABCDEF0123456789ABCDEF")
637
638
639 _, err := NewEncrypter(A128GCM, Recipient{Algorithm: DIRECT, Key: key32}, nil)
640 if err != ErrInvalidKeySize {
641 t.Error("Should reject AES-128 with 32-byte key")
642 }
643
644
645 _, err = NewEncrypter(A256GCM, Recipient{Algorithm: DIRECT, Key: key16}, nil)
646 if err != ErrInvalidKeySize {
647 t.Error("Should reject AES-256 with 16-byte key")
648 }
649 }
650
651 func generateTestKeys(keyAlg KeyAlgorithm, encAlg ContentEncryption) []testKey {
652 switch keyAlg {
653 case DIRECT:
654 return symmetricTestKey(getContentCipher(encAlg).keySize())
655 case ECDH_ES, ECDH_ES_A128KW, ECDH_ES_A192KW, ECDH_ES_A256KW:
656 return []testKey{
657 {
658 dec: ecTestKey256,
659 enc: &ecTestKey256.PublicKey,
660 },
661 {
662 dec: ecTestKey384,
663 enc: &ecTestKey384.PublicKey,
664 },
665 {
666 dec: ecTestKey521,
667 enc: &ecTestKey521.PublicKey,
668 },
669 {
670 dec: &JSONWebKey{KeyID: "test", Key: ecTestKey256},
671 enc: &JSONWebKey{KeyID: "test", Key: &ecTestKey256.PublicKey},
672 },
673 }
674 case A128GCMKW, A128KW:
675 return symmetricTestKey(16)
676 case A192GCMKW, A192KW:
677 return symmetricTestKey(24)
678 case A256GCMKW, A256KW:
679 return symmetricTestKey(32)
680 case RSA1_5, RSA_OAEP, RSA_OAEP_256:
681 return []testKey{{
682 dec: rsaTestKey,
683 enc: &rsaTestKey.PublicKey,
684 }}
685 case PBES2_HS256_A128KW, PBES2_HS384_A192KW, PBES2_HS512_A256KW:
686
687 i, err := rand.Int(rand.Reader, big.NewInt(64))
688 if err != nil {
689 panic(err)
690 }
691 return symmetricTestKey(int(i.Int64()))
692 }
693
694 panic("Must update test case")
695 }
696
697 func RunRoundtripsJWE(b *testing.B, alg KeyAlgorithm, enc ContentEncryption, zip CompressionAlgorithm, priv, pub interface{}) {
698 serializer := func(obj *JSONWebEncryption) (string, error) {
699 return obj.CompactSerialize()
700 }
701
702 corrupter := func(obj *JSONWebEncryption) bool { return false }
703
704 b.ResetTimer()
705 for i := 0; i < b.N; i++ {
706 err := RoundtripJWE(alg, enc, zip, serializer, corrupter, nil, pub, priv)
707 if err != nil {
708 b.Error(err)
709 }
710 }
711 }
712
713 var (
714 chunks = map[string][]byte{
715 "1B": make([]byte, 1),
716 "64B": make([]byte, 64),
717 "1KB": make([]byte, 1024),
718 "64KB": make([]byte, 65536),
719 "1MB": make([]byte, 1048576),
720 "64MB": make([]byte, 67108864),
721 }
722
723 symKey16, _, _ = randomKeyGenerator{size: 16}.genKey()
724 symKey32, _, _ = randomKeyGenerator{size: 32}.genKey()
725 symKey64, _, _ = randomKeyGenerator{size: 64}.genKey()
726
727 encrypters = map[string]Encrypter{
728 "OAEPAndGCM": mustEncrypter(RSA_OAEP, A128GCM, &rsaTestKey.PublicKey),
729 "PKCSAndGCM": mustEncrypter(RSA1_5, A128GCM, &rsaTestKey.PublicKey),
730 "OAEPAndCBC": mustEncrypter(RSA_OAEP, A128CBC_HS256, &rsaTestKey.PublicKey),
731 "PKCSAndCBC": mustEncrypter(RSA1_5, A128CBC_HS256, &rsaTestKey.PublicKey),
732 "DirectGCM128": mustEncrypter(DIRECT, A128GCM, symKey16),
733 "DirectCBC128": mustEncrypter(DIRECT, A128CBC_HS256, symKey32),
734 "DirectGCM256": mustEncrypter(DIRECT, A256GCM, symKey32),
735 "DirectCBC256": mustEncrypter(DIRECT, A256CBC_HS512, symKey64),
736 "AESKWAndGCM128": mustEncrypter(A128KW, A128GCM, symKey16),
737 "AESKWAndCBC256": mustEncrypter(A256KW, A256GCM, symKey32),
738 "ECDHOnP256AndGCM128": mustEncrypter(ECDH_ES, A128GCM, &ecTestKey256.PublicKey),
739 "ECDHOnP384AndGCM128": mustEncrypter(ECDH_ES, A128GCM, &ecTestKey384.PublicKey),
740 "ECDHOnP521AndGCM128": mustEncrypter(ECDH_ES, A128GCM, &ecTestKey521.PublicKey),
741 }
742 )
743
744 func BenchmarkEncrypt1BWithOAEPAndGCM(b *testing.B) { benchEncrypt("1B", "OAEPAndGCM", b) }
745 func BenchmarkEncrypt64BWithOAEPAndGCM(b *testing.B) { benchEncrypt("64B", "OAEPAndGCM", b) }
746 func BenchmarkEncrypt1KBWithOAEPAndGCM(b *testing.B) { benchEncrypt("1KB", "OAEPAndGCM", b) }
747 func BenchmarkEncrypt64KBWithOAEPAndGCM(b *testing.B) { benchEncrypt("64KB", "OAEPAndGCM", b) }
748 func BenchmarkEncrypt1MBWithOAEPAndGCM(b *testing.B) { benchEncrypt("1MB", "OAEPAndGCM", b) }
749 func BenchmarkEncrypt64MBWithOAEPAndGCM(b *testing.B) { benchEncrypt("64MB", "OAEPAndGCM", b) }
750
751 func BenchmarkEncrypt1BWithPKCSAndGCM(b *testing.B) { benchEncrypt("1B", "PKCSAndGCM", b) }
752 func BenchmarkEncrypt64BWithPKCSAndGCM(b *testing.B) { benchEncrypt("64B", "PKCSAndGCM", b) }
753 func BenchmarkEncrypt1KBWithPKCSAndGCM(b *testing.B) { benchEncrypt("1KB", "PKCSAndGCM", b) }
754 func BenchmarkEncrypt64KBWithPKCSAndGCM(b *testing.B) { benchEncrypt("64KB", "PKCSAndGCM", b) }
755 func BenchmarkEncrypt1MBWithPKCSAndGCM(b *testing.B) { benchEncrypt("1MB", "PKCSAndGCM", b) }
756 func BenchmarkEncrypt64MBWithPKCSAndGCM(b *testing.B) { benchEncrypt("64MB", "PKCSAndGCM", b) }
757
758 func BenchmarkEncrypt1BWithOAEPAndCBC(b *testing.B) { benchEncrypt("1B", "OAEPAndCBC", b) }
759 func BenchmarkEncrypt64BWithOAEPAndCBC(b *testing.B) { benchEncrypt("64B", "OAEPAndCBC", b) }
760 func BenchmarkEncrypt1KBWithOAEPAndCBC(b *testing.B) { benchEncrypt("1KB", "OAEPAndCBC", b) }
761 func BenchmarkEncrypt64KBWithOAEPAndCBC(b *testing.B) { benchEncrypt("64KB", "OAEPAndCBC", b) }
762 func BenchmarkEncrypt1MBWithOAEPAndCBC(b *testing.B) { benchEncrypt("1MB", "OAEPAndCBC", b) }
763 func BenchmarkEncrypt64MBWithOAEPAndCBC(b *testing.B) { benchEncrypt("64MB", "OAEPAndCBC", b) }
764
765 func BenchmarkEncrypt1BWithPKCSAndCBC(b *testing.B) { benchEncrypt("1B", "PKCSAndCBC", b) }
766 func BenchmarkEncrypt64BWithPKCSAndCBC(b *testing.B) { benchEncrypt("64B", "PKCSAndCBC", b) }
767 func BenchmarkEncrypt1KBWithPKCSAndCBC(b *testing.B) { benchEncrypt("1KB", "PKCSAndCBC", b) }
768 func BenchmarkEncrypt64KBWithPKCSAndCBC(b *testing.B) { benchEncrypt("64KB", "PKCSAndCBC", b) }
769 func BenchmarkEncrypt1MBWithPKCSAndCBC(b *testing.B) { benchEncrypt("1MB", "PKCSAndCBC", b) }
770 func BenchmarkEncrypt64MBWithPKCSAndCBC(b *testing.B) { benchEncrypt("64MB", "PKCSAndCBC", b) }
771
772 func BenchmarkEncrypt1BWithDirectGCM128(b *testing.B) { benchEncrypt("1B", "DirectGCM128", b) }
773 func BenchmarkEncrypt64BWithDirectGCM128(b *testing.B) { benchEncrypt("64B", "DirectGCM128", b) }
774 func BenchmarkEncrypt1KBWithDirectGCM128(b *testing.B) { benchEncrypt("1KB", "DirectGCM128", b) }
775 func BenchmarkEncrypt64KBWithDirectGCM128(b *testing.B) { benchEncrypt("64KB", "DirectGCM128", b) }
776 func BenchmarkEncrypt1MBWithDirectGCM128(b *testing.B) { benchEncrypt("1MB", "DirectGCM128", b) }
777 func BenchmarkEncrypt64MBWithDirectGCM128(b *testing.B) { benchEncrypt("64MB", "DirectGCM128", b) }
778
779 func BenchmarkEncrypt1BWithDirectCBC128(b *testing.B) { benchEncrypt("1B", "DirectCBC128", b) }
780 func BenchmarkEncrypt64BWithDirectCBC128(b *testing.B) { benchEncrypt("64B", "DirectCBC128", b) }
781 func BenchmarkEncrypt1KBWithDirectCBC128(b *testing.B) { benchEncrypt("1KB", "DirectCBC128", b) }
782 func BenchmarkEncrypt64KBWithDirectCBC128(b *testing.B) { benchEncrypt("64KB", "DirectCBC128", b) }
783 func BenchmarkEncrypt1MBWithDirectCBC128(b *testing.B) { benchEncrypt("1MB", "DirectCBC128", b) }
784 func BenchmarkEncrypt64MBWithDirectCBC128(b *testing.B) { benchEncrypt("64MB", "DirectCBC128", b) }
785
786 func BenchmarkEncrypt1BWithDirectGCM256(b *testing.B) { benchEncrypt("1B", "DirectGCM256", b) }
787 func BenchmarkEncrypt64BWithDirectGCM256(b *testing.B) { benchEncrypt("64B", "DirectGCM256", b) }
788 func BenchmarkEncrypt1KBWithDirectGCM256(b *testing.B) { benchEncrypt("1KB", "DirectGCM256", b) }
789 func BenchmarkEncrypt64KBWithDirectGCM256(b *testing.B) { benchEncrypt("64KB", "DirectGCM256", b) }
790 func BenchmarkEncrypt1MBWithDirectGCM256(b *testing.B) { benchEncrypt("1MB", "DirectGCM256", b) }
791 func BenchmarkEncrypt64MBWithDirectGCM256(b *testing.B) { benchEncrypt("64MB", "DirectGCM256", b) }
792
793 func BenchmarkEncrypt1BWithDirectCBC256(b *testing.B) { benchEncrypt("1B", "DirectCBC256", b) }
794 func BenchmarkEncrypt64BWithDirectCBC256(b *testing.B) { benchEncrypt("64B", "DirectCBC256", b) }
795 func BenchmarkEncrypt1KBWithDirectCBC256(b *testing.B) { benchEncrypt("1KB", "DirectCBC256", b) }
796 func BenchmarkEncrypt64KBWithDirectCBC256(b *testing.B) { benchEncrypt("64KB", "DirectCBC256", b) }
797 func BenchmarkEncrypt1MBWithDirectCBC256(b *testing.B) { benchEncrypt("1MB", "DirectCBC256", b) }
798 func BenchmarkEncrypt64MBWithDirectCBC256(b *testing.B) { benchEncrypt("64MB", "DirectCBC256", b) }
799
800 func BenchmarkEncrypt1BWithAESKWAndGCM128(b *testing.B) { benchEncrypt("1B", "AESKWAndGCM128", b) }
801 func BenchmarkEncrypt64BWithAESKWAndGCM128(b *testing.B) { benchEncrypt("64B", "AESKWAndGCM128", b) }
802 func BenchmarkEncrypt1KBWithAESKWAndGCM128(b *testing.B) { benchEncrypt("1KB", "AESKWAndGCM128", b) }
803 func BenchmarkEncrypt64KBWithAESKWAndGCM128(b *testing.B) { benchEncrypt("64KB", "AESKWAndGCM128", b) }
804 func BenchmarkEncrypt1MBWithAESKWAndGCM128(b *testing.B) { benchEncrypt("1MB", "AESKWAndGCM128", b) }
805 func BenchmarkEncrypt64MBWithAESKWAndGCM128(b *testing.B) { benchEncrypt("64MB", "AESKWAndGCM128", b) }
806
807 func BenchmarkEncrypt1BWithAESKWAndCBC256(b *testing.B) { benchEncrypt("1B", "AESKWAndCBC256", b) }
808 func BenchmarkEncrypt64BWithAESKWAndCBC256(b *testing.B) { benchEncrypt("64B", "AESKWAndCBC256", b) }
809 func BenchmarkEncrypt1KBWithAESKWAndCBC256(b *testing.B) { benchEncrypt("1KB", "AESKWAndCBC256", b) }
810 func BenchmarkEncrypt64KBWithAESKWAndCBC256(b *testing.B) { benchEncrypt("64KB", "AESKWAndCBC256", b) }
811 func BenchmarkEncrypt1MBWithAESKWAndCBC256(b *testing.B) { benchEncrypt("1MB", "AESKWAndCBC256", b) }
812 func BenchmarkEncrypt64MBWithAESKWAndCBC256(b *testing.B) { benchEncrypt("64MB", "AESKWAndCBC256", b) }
813
814 func BenchmarkEncrypt1BWithECDHOnP256AndGCM128(b *testing.B) {
815 benchEncrypt("1B", "ECDHOnP256AndGCM128", b)
816 }
817 func BenchmarkEncrypt64BWithECDHOnP256AndGCM128(b *testing.B) {
818 benchEncrypt("64B", "ECDHOnP256AndGCM128", b)
819 }
820 func BenchmarkEncrypt1KBWithECDHOnP256AndGCM128(b *testing.B) {
821 benchEncrypt("1KB", "ECDHOnP256AndGCM128", b)
822 }
823 func BenchmarkEncrypt64KBWithECDHOnP256AndGCM128(b *testing.B) {
824 benchEncrypt("64KB", "ECDHOnP256AndGCM128", b)
825 }
826 func BenchmarkEncrypt1MBWithECDHOnP256AndGCM128(b *testing.B) {
827 benchEncrypt("1MB", "ECDHOnP256AndGCM128", b)
828 }
829 func BenchmarkEncrypt64MBWithECDHOnP256AndGCM128(b *testing.B) {
830 benchEncrypt("64MB", "ECDHOnP256AndGCM128", b)
831 }
832
833 func BenchmarkEncrypt1BWithECDHOnP384AndGCM128(b *testing.B) {
834 benchEncrypt("1B", "ECDHOnP384AndGCM128", b)
835 }
836 func BenchmarkEncrypt64BWithECDHOnP384AndGCM128(b *testing.B) {
837 benchEncrypt("64B", "ECDHOnP384AndGCM128", b)
838 }
839 func BenchmarkEncrypt1KBWithECDHOnP384AndGCM128(b *testing.B) {
840 benchEncrypt("1KB", "ECDHOnP384AndGCM128", b)
841 }
842 func BenchmarkEncrypt64KBWithECDHOnP384AndGCM128(b *testing.B) {
843 benchEncrypt("64KB", "ECDHOnP384AndGCM128", b)
844 }
845 func BenchmarkEncrypt1MBWithECDHOnP384AndGCM128(b *testing.B) {
846 benchEncrypt("1MB", "ECDHOnP384AndGCM128", b)
847 }
848 func BenchmarkEncrypt64MBWithECDHOnP384AndGCM128(b *testing.B) {
849 benchEncrypt("64MB", "ECDHOnP384AndGCM128", b)
850 }
851
852 func BenchmarkEncrypt1BWithECDHOnP521AndGCM128(b *testing.B) {
853 benchEncrypt("1B", "ECDHOnP521AndGCM128", b)
854 }
855 func BenchmarkEncrypt64BWithECDHOnP521AndGCM128(b *testing.B) {
856 benchEncrypt("64B", "ECDHOnP521AndGCM128", b)
857 }
858 func BenchmarkEncrypt1KBWithECDHOnP521AndGCM128(b *testing.B) {
859 benchEncrypt("1KB", "ECDHOnP521AndGCM128", b)
860 }
861 func BenchmarkEncrypt64KBWithECDHOnP521AndGCM128(b *testing.B) {
862 benchEncrypt("64KB", "ECDHOnP521AndGCM128", b)
863 }
864 func BenchmarkEncrypt1MBWithECDHOnP521AndGCM128(b *testing.B) {
865 benchEncrypt("1MB", "ECDHOnP521AndGCM128", b)
866 }
867 func BenchmarkEncrypt64MBWithECDHOnP521AndGCM128(b *testing.B) {
868 benchEncrypt("64MB", "ECDHOnP521AndGCM128", b)
869 }
870
871 func benchEncrypt(chunkKey, primKey string, b *testing.B) {
872 data, ok := chunks[chunkKey]
873 if !ok {
874 b.Fatalf("unknown chunk size %s", chunkKey)
875 }
876
877 enc, ok := encrypters[primKey]
878 if !ok {
879 b.Fatalf("unknown encrypter %s", primKey)
880 }
881
882 b.SetBytes(int64(len(data)))
883 for i := 0; i < b.N; i++ {
884 enc.Encrypt(data)
885 }
886 }
887
888 var (
889 decryptionKeys = map[string]interface{}{
890 "OAEPAndGCM": rsaTestKey,
891 "PKCSAndGCM": rsaTestKey,
892 "OAEPAndCBC": rsaTestKey,
893 "PKCSAndCBC": rsaTestKey,
894
895 "DirectGCM128": symKey16,
896 "DirectCBC128": symKey32,
897 "DirectGCM256": symKey32,
898 "DirectCBC256": symKey64,
899
900 "AESKWAndGCM128": symKey16,
901 "AESKWAndCBC256": symKey32,
902
903 "ECDHOnP256AndGCM128": ecTestKey256,
904 "ECDHOnP384AndGCM128": ecTestKey384,
905 "ECDHOnP521AndGCM128": ecTestKey521,
906 }
907 )
908
909 func BenchmarkDecrypt1BWithOAEPAndGCM(b *testing.B) { benchDecrypt("1B", "OAEPAndGCM", b) }
910 func BenchmarkDecrypt64BWithOAEPAndGCM(b *testing.B) { benchDecrypt("64B", "OAEPAndGCM", b) }
911 func BenchmarkDecrypt1KBWithOAEPAndGCM(b *testing.B) { benchDecrypt("1KB", "OAEPAndGCM", b) }
912 func BenchmarkDecrypt64KBWithOAEPAndGCM(b *testing.B) { benchDecrypt("64KB", "OAEPAndGCM", b) }
913 func BenchmarkDecrypt1MBWithOAEPAndGCM(b *testing.B) { benchDecrypt("1MB", "OAEPAndGCM", b) }
914 func BenchmarkDecrypt64MBWithOAEPAndGCM(b *testing.B) { benchDecrypt("64MB", "OAEPAndGCM", b) }
915
916 func BenchmarkDecrypt1BWithPKCSAndGCM(b *testing.B) { benchDecrypt("1B", "PKCSAndGCM", b) }
917 func BenchmarkDecrypt64BWithPKCSAndGCM(b *testing.B) { benchDecrypt("64B", "PKCSAndGCM", b) }
918 func BenchmarkDecrypt1KBWithPKCSAndGCM(b *testing.B) { benchDecrypt("1KB", "PKCSAndGCM", b) }
919 func BenchmarkDecrypt64KBWithPKCSAndGCM(b *testing.B) { benchDecrypt("64KB", "PKCSAndGCM", b) }
920 func BenchmarkDecrypt1MBWithPKCSAndGCM(b *testing.B) { benchDecrypt("1MB", "PKCSAndGCM", b) }
921 func BenchmarkDecrypt64MBWithPKCSAndGCM(b *testing.B) { benchDecrypt("64MB", "PKCSAndGCM", b) }
922
923 func BenchmarkDecrypt1BWithOAEPAndCBC(b *testing.B) { benchDecrypt("1B", "OAEPAndCBC", b) }
924 func BenchmarkDecrypt64BWithOAEPAndCBC(b *testing.B) { benchDecrypt("64B", "OAEPAndCBC", b) }
925 func BenchmarkDecrypt1KBWithOAEPAndCBC(b *testing.B) { benchDecrypt("1KB", "OAEPAndCBC", b) }
926 func BenchmarkDecrypt64KBWithOAEPAndCBC(b *testing.B) { benchDecrypt("64KB", "OAEPAndCBC", b) }
927 func BenchmarkDecrypt1MBWithOAEPAndCBC(b *testing.B) { benchDecrypt("1MB", "OAEPAndCBC", b) }
928 func BenchmarkDecrypt64MBWithOAEPAndCBC(b *testing.B) { benchDecrypt("64MB", "OAEPAndCBC", b) }
929
930 func BenchmarkDecrypt1BWithPKCSAndCBC(b *testing.B) { benchDecrypt("1B", "PKCSAndCBC", b) }
931 func BenchmarkDecrypt64BWithPKCSAndCBC(b *testing.B) { benchDecrypt("64B", "PKCSAndCBC", b) }
932 func BenchmarkDecrypt1KBWithPKCSAndCBC(b *testing.B) { benchDecrypt("1KB", "PKCSAndCBC", b) }
933 func BenchmarkDecrypt64KBWithPKCSAndCBC(b *testing.B) { benchDecrypt("64KB", "PKCSAndCBC", b) }
934 func BenchmarkDecrypt1MBWithPKCSAndCBC(b *testing.B) { benchDecrypt("1MB", "PKCSAndCBC", b) }
935 func BenchmarkDecrypt64MBWithPKCSAndCBC(b *testing.B) { benchDecrypt("64MB", "PKCSAndCBC", b) }
936
937 func BenchmarkDecrypt1BWithDirectGCM128(b *testing.B) { benchDecrypt("1B", "DirectGCM128", b) }
938 func BenchmarkDecrypt64BWithDirectGCM128(b *testing.B) { benchDecrypt("64B", "DirectGCM128", b) }
939 func BenchmarkDecrypt1KBWithDirectGCM128(b *testing.B) { benchDecrypt("1KB", "DirectGCM128", b) }
940 func BenchmarkDecrypt64KBWithDirectGCM128(b *testing.B) { benchDecrypt("64KB", "DirectGCM128", b) }
941 func BenchmarkDecrypt1MBWithDirectGCM128(b *testing.B) { benchDecrypt("1MB", "DirectGCM128", b) }
942 func BenchmarkDecrypt64MBWithDirectGCM128(b *testing.B) { benchDecrypt("64MB", "DirectGCM128", b) }
943
944 func BenchmarkDecrypt1BWithDirectCBC128(b *testing.B) { benchDecrypt("1B", "DirectCBC128", b) }
945 func BenchmarkDecrypt64BWithDirectCBC128(b *testing.B) { benchDecrypt("64B", "DirectCBC128", b) }
946 func BenchmarkDecrypt1KBWithDirectCBC128(b *testing.B) { benchDecrypt("1KB", "DirectCBC128", b) }
947 func BenchmarkDecrypt64KBWithDirectCBC128(b *testing.B) { benchDecrypt("64KB", "DirectCBC128", b) }
948 func BenchmarkDecrypt1MBWithDirectCBC128(b *testing.B) { benchDecrypt("1MB", "DirectCBC128", b) }
949 func BenchmarkDecrypt64MBWithDirectCBC128(b *testing.B) { benchDecrypt("64MB", "DirectCBC128", b) }
950
951 func BenchmarkDecrypt1BWithDirectGCM256(b *testing.B) { benchDecrypt("1B", "DirectGCM256", b) }
952 func BenchmarkDecrypt64BWithDirectGCM256(b *testing.B) { benchDecrypt("64B", "DirectGCM256", b) }
953 func BenchmarkDecrypt1KBWithDirectGCM256(b *testing.B) { benchDecrypt("1KB", "DirectGCM256", b) }
954 func BenchmarkDecrypt64KBWithDirectGCM256(b *testing.B) { benchDecrypt("64KB", "DirectGCM256", b) }
955 func BenchmarkDecrypt1MBWithDirectGCM256(b *testing.B) { benchDecrypt("1MB", "DirectGCM256", b) }
956 func BenchmarkDecrypt64MBWithDirectGCM256(b *testing.B) { benchDecrypt("64MB", "DirectGCM256", b) }
957
958 func BenchmarkDecrypt1BWithDirectCBC256(b *testing.B) { benchDecrypt("1B", "DirectCBC256", b) }
959 func BenchmarkDecrypt64BWithDirectCBC256(b *testing.B) { benchDecrypt("64B", "DirectCBC256", b) }
960 func BenchmarkDecrypt1KBWithDirectCBC256(b *testing.B) { benchDecrypt("1KB", "DirectCBC256", b) }
961 func BenchmarkDecrypt64KBWithDirectCBC256(b *testing.B) { benchDecrypt("64KB", "DirectCBC256", b) }
962 func BenchmarkDecrypt1MBWithDirectCBC256(b *testing.B) { benchDecrypt("1MB", "DirectCBC256", b) }
963 func BenchmarkDecrypt64MBWithDirectCBC256(b *testing.B) { benchDecrypt("64MB", "DirectCBC256", b) }
964
965 func BenchmarkDecrypt1BWithAESKWAndGCM128(b *testing.B) { benchDecrypt("1B", "AESKWAndGCM128", b) }
966 func BenchmarkDecrypt64BWithAESKWAndGCM128(b *testing.B) { benchDecrypt("64B", "AESKWAndGCM128", b) }
967 func BenchmarkDecrypt1KBWithAESKWAndGCM128(b *testing.B) { benchDecrypt("1KB", "AESKWAndGCM128", b) }
968 func BenchmarkDecrypt64KBWithAESKWAndGCM128(b *testing.B) { benchDecrypt("64KB", "AESKWAndGCM128", b) }
969 func BenchmarkDecrypt1MBWithAESKWAndGCM128(b *testing.B) { benchDecrypt("1MB", "AESKWAndGCM128", b) }
970 func BenchmarkDecrypt64MBWithAESKWAndGCM128(b *testing.B) { benchDecrypt("64MB", "AESKWAndGCM128", b) }
971
972 func BenchmarkDecrypt1BWithAESKWAndCBC256(b *testing.B) { benchDecrypt("1B", "AESKWAndCBC256", b) }
973 func BenchmarkDecrypt64BWithAESKWAndCBC256(b *testing.B) { benchDecrypt("64B", "AESKWAndCBC256", b) }
974 func BenchmarkDecrypt1KBWithAESKWAndCBC256(b *testing.B) { benchDecrypt("1KB", "AESKWAndCBC256", b) }
975 func BenchmarkDecrypt64KBWithAESKWAndCBC256(b *testing.B) { benchDecrypt("64KB", "AESKWAndCBC256", b) }
976 func BenchmarkDecrypt1MBWithAESKWAndCBC256(b *testing.B) { benchDecrypt("1MB", "AESKWAndCBC256", b) }
977 func BenchmarkDecrypt64MBWithAESKWAndCBC256(b *testing.B) { benchDecrypt("64MB", "AESKWAndCBC256", b) }
978
979 func BenchmarkDecrypt1BWithECDHOnP256AndGCM128(b *testing.B) {
980 benchDecrypt("1B", "ECDHOnP256AndGCM128", b)
981 }
982 func BenchmarkDecrypt64BWithECDHOnP256AndGCM128(b *testing.B) {
983 benchDecrypt("64B", "ECDHOnP256AndGCM128", b)
984 }
985 func BenchmarkDecrypt1KBWithECDHOnP256AndGCM128(b *testing.B) {
986 benchDecrypt("1KB", "ECDHOnP256AndGCM128", b)
987 }
988 func BenchmarkDecrypt64KBWithECDHOnP256AndGCM128(b *testing.B) {
989 benchDecrypt("64KB", "ECDHOnP256AndGCM128", b)
990 }
991 func BenchmarkDecrypt1MBWithECDHOnP256AndGCM128(b *testing.B) {
992 benchDecrypt("1MB", "ECDHOnP256AndGCM128", b)
993 }
994 func BenchmarkDecrypt64MBWithECDHOnP256AndGCM128(b *testing.B) {
995 benchDecrypt("64MB", "ECDHOnP256AndGCM128", b)
996 }
997
998 func BenchmarkDecrypt1BWithECDHOnP384AndGCM128(b *testing.B) {
999 benchDecrypt("1B", "ECDHOnP384AndGCM128", b)
1000 }
1001 func BenchmarkDecrypt64BWithECDHOnP384AndGCM128(b *testing.B) {
1002 benchDecrypt("64B", "ECDHOnP384AndGCM128", b)
1003 }
1004 func BenchmarkDecrypt1KBWithECDHOnP384AndGCM128(b *testing.B) {
1005 benchDecrypt("1KB", "ECDHOnP384AndGCM128", b)
1006 }
1007 func BenchmarkDecrypt64KBWithECDHOnP384AndGCM128(b *testing.B) {
1008 benchDecrypt("64KB", "ECDHOnP384AndGCM128", b)
1009 }
1010 func BenchmarkDecrypt1MBWithECDHOnP384AndGCM128(b *testing.B) {
1011 benchDecrypt("1MB", "ECDHOnP384AndGCM128", b)
1012 }
1013 func BenchmarkDecrypt64MBWithECDHOnP384AndGCM128(b *testing.B) {
1014 benchDecrypt("64MB", "ECDHOnP384AndGCM128", b)
1015 }
1016
1017 func BenchmarkDecrypt1BWithECDHOnP521AndGCM128(b *testing.B) {
1018 benchDecrypt("1B", "ECDHOnP521AndGCM128", b)
1019 }
1020 func BenchmarkDecrypt64BWithECDHOnP521AndGCM128(b *testing.B) {
1021 benchDecrypt("64B", "ECDHOnP521AndGCM128", b)
1022 }
1023 func BenchmarkDecrypt1KBWithECDHOnP521AndGCM128(b *testing.B) {
1024 benchDecrypt("1KB", "ECDHOnP521AndGCM128", b)
1025 }
1026 func BenchmarkDecrypt64KBWithECDHOnP521AndGCM128(b *testing.B) {
1027 benchDecrypt("64KB", "ECDHOnP521AndGCM128", b)
1028 }
1029 func BenchmarkDecrypt1MBWithECDHOnP521AndGCM128(b *testing.B) {
1030 benchDecrypt("1MB", "ECDHOnP521AndGCM128", b)
1031 }
1032 func BenchmarkDecrypt64MBWithECDHOnP521AndGCM128(b *testing.B) {
1033 benchDecrypt("64MB", "ECDHOnP521AndGCM128", b)
1034 }
1035
1036 func benchDecrypt(chunkKey, primKey string, b *testing.B) {
1037 chunk, ok := chunks[chunkKey]
1038 if !ok {
1039 b.Fatalf("unknown chunk size %s", chunkKey)
1040 }
1041
1042 enc, ok := encrypters[primKey]
1043 if !ok {
1044 b.Fatalf("unknown encrypter %s", primKey)
1045 }
1046
1047 dec, ok := decryptionKeys[primKey]
1048 if !ok {
1049 b.Fatalf("unknown decryption key %s", primKey)
1050 }
1051
1052 data, err := enc.Encrypt(chunk)
1053 if err != nil {
1054 b.Fatal(err)
1055 }
1056
1057 b.SetBytes(int64(len(chunk)))
1058 b.ResetTimer()
1059 for i := 0; i < b.N; i++ {
1060 data.Decrypt(dec)
1061 }
1062 }
1063
1064 func mustEncrypter(keyAlg KeyAlgorithm, encAlg ContentEncryption, encryptionKey interface{}) Encrypter {
1065 enc, err := NewEncrypter(encAlg, Recipient{Algorithm: keyAlg, Key: encryptionKey}, nil)
1066 if err != nil {
1067 panic(err)
1068 }
1069 return enc
1070 }
1071
View as plain text