forked from karlmcguire/ige
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdecrypt_test.go
138 lines (111 loc) · 2.53 KB
/
decrypt_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package ige
import (
"bytes"
"crypto/aes"
"fmt"
"testing"
)
func TestNewIGEDecrypter(t *testing.T) {
c, err := aes.NewCipher(make([]byte, 16))
if err != nil {
t.Fatal(err)
}
defer func() {
if r := recover(); r == nil {
t.Fatal("NewIGEEncrypter didn't panic with bad iv")
}
}()
_ = NewIGEDecrypter(c, []byte{})
}
func TestDecrypterBlockSize(t *testing.T) {
c, err := aes.NewCipher(make([]byte, 16))
if err != nil {
t.Fatal(err)
}
i := NewIGEDecrypter(c, make([]byte, 32))
if i.BlockSize() != 16 {
t.Fatalf("decrypter.BlockSize() != 16, got %d instead\n", i.BlockSize())
}
}
func TestDecrypterCryptBlocks(t *testing.T) {
for a, v := range TestVectors {
out := make([]byte, len(v.Ciphertext))
c, err := aes.NewCipher(v.Key)
if err != nil {
t.Fatal(err)
}
i := NewIGEDecrypter(c, v.IV)
i.CryptBlocks(out, v.Ciphertext)
if !bytes.Equal(out, v.Plaintext) {
t.Fatalf("test vector %d has wrong ciphertext\n", a+1)
}
}
}
func TestDecryptBlocks(t *testing.T) {
for a, v := range TestVectors {
out := make([]byte, len(v.Ciphertext))
c, err := aes.NewCipher(v.Key)
if err != nil {
t.Fatal(err)
}
DecryptBlocks(c, v.IV, out, v.Ciphertext)
if !bytes.Equal(out, v.Plaintext) {
t.Fatalf("test vector %d has wrong ciphertext\n", a+1)
}
}
}
func TestDecryptCryptBlocksPanicSrc(t *testing.T) {
c, err := aes.NewCipher(make([]byte, 16))
if err != nil {
t.Fatal(err)
}
defer func() {
if r := recover(); r == nil {
t.Fatal("decrypt.CryptBlocks() not panicking with bad src")
}
}()
i := NewIGEDecrypter(c, make([]byte, 32))
i.CryptBlocks(make([]byte, 16), make([]byte, 1))
}
func TestDecryptCryptBlocksPanicDst(t *testing.T) {
c, err := aes.NewCipher(make([]byte, 16))
if err != nil {
t.Fatal(err)
}
defer func() {
if r := recover(); r == nil {
t.Fatal("decrypt.CryptBlocks() not panicking with bad dst")
}
}()
i := NewIGEDecrypter(c, make([]byte, 32))
i.CryptBlocks(make([]byte, 1), make([]byte, 16))
}
func BenchmarkDecryptBlocks(b *testing.B) {
for _, payload := range []int{
16,
128,
1024,
8192,
64 * 1024,
512 * 1024,
} {
b.Run(fmt.Sprintf("%d", payload), benchDecrypt(payload))
}
}
func benchDecrypt(n int) func(b *testing.B) {
return func(b *testing.B) {
b.Helper()
src := make([]byte, n)
dst := make([]byte, n)
b.ReportAllocs()
b.SetBytes(int64(n))
b.ResetTimer()
for i := 0; i < b.N; i++ {
c, err := aes.NewCipher(TestVectors[0].Key)
if err != nil {
b.Fatal(err)
}
DecryptBlocks(c, TestVectors[0].IV, dst, src)
}
}
}