-
Notifications
You must be signed in to change notification settings - Fork 4
/
macdaddy_test.go
165 lines (129 loc) · 4.04 KB
/
macdaddy_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package macdaddy
import (
"bytes"
"testing"
. "github.com/bsm/ginkgo/v2"
. "github.com/bsm/gomega"
)
var _ = Describe("MAC", func() {
var key = []byte("THISisOURverySECRET32byteTESTkey")
var src = "plaintext"
It("should fail to init on bad keys", func() {
_, err := New([]byte("too short"), 1, 0)
Expect(err).To(MatchError("chacha20poly1305: bad key length"))
})
It("should have a epoch", func() {
mac, err := New(key, 321, 100)
Expect(err).NotTo(HaveOccurred())
Expect(mac.Epoch()).To(Equal(uint32(321)))
})
It("should declare overhead", func() {
mac, err := New(key, 1, 100)
Expect(err).NotTo(HaveOccurred())
Expect(mac.Overhead()).To(Equal(32))
})
It("should encrypt", func() {
mac, err := New(key, 1, 100)
Expect(err).NotTo(HaveOccurred())
Expect(mac.Encrypt(nil, nil)).To(HaveLen(32))
m1 := mac.Encrypt(nil, []byte("plaintext"))
m2 := mac.Encrypt(nil, []byte("plaintext"))
Expect(m1).To(HaveLen(41))
Expect(m2).To(HaveLen(41))
Expect(m1).NotTo(Equal(m2))
})
It("should open", func() {
mac, err := New(key, 1, 100)
Expect(err).NotTo(HaveOccurred())
m1 := mac.Encrypt(nil, []byte(src))
m2 := mac.Encrypt(nil, []byte(src))
Expect(m1).NotTo(Equal(m2))
p1, err := mac.Decrypt(nil, m1)
Expect(err).NotTo(HaveOccurred())
Expect(string(p1)).To(Equal(src))
p2, err := mac.Decrypt(nil, m2)
Expect(err).NotTo(HaveOccurred())
Expect(string(p2)).To(Equal(src))
})
It("should encrypt/open independently of seeds", func() {
mac1, err := New(key, 1, 100)
Expect(err).NotTo(HaveOccurred())
mac2, err := New(key, 1, 200)
Expect(err).NotTo(HaveOccurred())
m1 := mac1.Encrypt(nil, []byte(src))
m2 := mac2.Encrypt(nil, []byte(src))
Expect(m1).NotTo(Equal(m2))
p1, err := mac2.Decrypt(nil, m1)
Expect(err).NotTo(HaveOccurred())
Expect(string(p1)).To(Equal(src))
p2, err := mac1.Decrypt(nil, m2)
Expect(err).NotTo(HaveOccurred())
Expect(string(p2)).To(Equal(src))
})
It("should require consistent keys", func() {
mac1, err := New(key, 1, 100)
Expect(err).NotTo(HaveOccurred())
mac2, err := New(bytes.ToUpper(key), 1, 200)
Expect(err).NotTo(HaveOccurred())
m1 := mac1.Encrypt(nil, []byte(src))
m2 := mac2.Encrypt(nil, []byte(src))
Expect(m1).NotTo(Equal(m2))
_, err = mac2.Decrypt(nil, m1)
Expect(err).To(Equal(ErrBadToken))
_, err = mac1.Decrypt(nil, m2)
Expect(err).To(Equal(ErrBadToken))
})
It("should require consistent epochs", func() {
mac1, err := New(key, 1, 100)
Expect(err).NotTo(HaveOccurred())
mac2, err := New(key, 2, 200)
Expect(err).NotTo(HaveOccurred())
m1 := mac1.Encrypt(nil, []byte(src))
m2 := mac2.Encrypt(nil, []byte(src))
Expect(m1).NotTo(Equal(m2))
_, err = mac2.Decrypt(nil, m1)
Expect(err).To(Equal(ErrUnknownEpoch))
_, err = mac1.Decrypt(nil, m2)
Expect(err).To(Equal(ErrUnknownEpoch))
})
})
// ------------------------------------------------------------------------
func TestSuite(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "macdaddy")
}
func BenchmarkMAC_Encrypt_64(b *testing.B) { benchmarkMACEncrypt(b, 64) }
func BenchmarkMAC_Encrypt_1k(b *testing.B) { benchmarkMACEncrypt(b, 1024) }
func BenchmarkMAC_Encrypt_1M(b *testing.B) { benchmarkMACEncrypt(b, 1024*024) }
func BenchmarkMAC_Decrypt_64(b *testing.B) { benchmarkMACDecrypt(b, 64) }
func BenchmarkMAC_Decrypt_1k(b *testing.B) { benchmarkMACDecrypt(b, 1024) }
func BenchmarkMAC_Decrypt_1M(b *testing.B) { benchmarkMACDecrypt(b, 1024*1024) }
func benchmarkMACEncrypt(b *testing.B, n int) {
key := bytes.Repeat([]byte{'x'}, 32)
msg := bytes.Repeat([]byte{'x'}, n)
mac, err := New(key, 0, 0)
if err != nil {
b.Fatal(err)
}
var dst []byte
b.ResetTimer()
for i := 0; i < b.N; i++ {
dst = mac.Encrypt(dst[:0], msg)
}
}
func benchmarkMACDecrypt(b *testing.B, n int) {
key := bytes.Repeat([]byte{'x'}, 32)
mac, err := New(key, 0, 0)
if err != nil {
b.Fatal(err)
}
msg := mac.Encrypt(nil, bytes.Repeat([]byte{'x'}, n))
var dst []byte
b.ResetTimer()
for i := 0; i < b.N; i++ {
dst, err = mac.Decrypt(dst[:0], msg)
if err != nil {
b.Fatal(err)
}
}
}