forked from karlmcguire/ige
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathencrypt_test.go
138 lines (111 loc) · 2.53 KB
/
encrypt_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 TestNewIGEEncrypter(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")
}
}()
_ = NewIGEEncrypter(c, []byte{})
}
func TestEncrypterBlockSize(t *testing.T) {
c, err := aes.NewCipher(make([]byte, 16))
if err != nil {
t.Fatal(err)
}
i := NewIGEEncrypter(c, make([]byte, 32))
if i.BlockSize() != 16 {
t.Fatalf("encrypter.BlockSize() != 16, got %d instead\n", i.BlockSize())
}
}
func TestEncrypterCryptBlocks(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 := NewIGEEncrypter(c, v.IV)
i.CryptBlocks(out, v.Plaintext)
if !bytes.Equal(out, v.Ciphertext) {
t.Fatalf("test vector %d has wrong ciphertext\n", a+1)
}
}
}
func TestEncryptBlocks(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)
}
EncryptBlocks(c, v.IV, out, v.Plaintext)
if !bytes.Equal(out, v.Ciphertext) {
t.Fatalf("test vector %d has wrong ciphertext\n", a+1)
}
}
}
func TestEncryptCryptBlocksPanicSrc(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("encrypt.CryptBlocks() not panicking with bad src")
}
}()
i := NewIGEEncrypter(c, make([]byte, 32))
i.CryptBlocks(make([]byte, 16), make([]byte, 1))
}
func TestEncryptCryptBlocksPanicDst(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("encrypt.CryptBlocks() not panicking with bad dst")
}
}()
i := NewIGEEncrypter(c, make([]byte, 32))
i.CryptBlocks(make([]byte, 1), make([]byte, 16))
}
func BenchmarkEncryptBlocks(b *testing.B) {
for _, payload := range []int{
16,
128,
1024,
8192,
64 * 1024,
512 * 1024,
} {
b.Run(fmt.Sprintf("%d", payload), benchEncrypt(payload))
}
}
func benchEncrypt(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)
}
EncryptBlocks(c, TestVectors[0].IV, dst, src)
}
}
}