-
Notifications
You must be signed in to change notification settings - Fork 16
/
aead_test.go
88 lines (68 loc) · 2.89 KB
/
aead_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
package minq
import (
"crypto/cipher"
"testing"
)
var kTestKey1 = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
var kTestIV1 = []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
var kTestKey2 = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
var kTestIV2 = []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13}
var ktestAeadHdr1 = []byte{1, 2, 3}
var ktestAeadHdr2 = []byte{1, 2, 4}
var ktestAeadBody1 = []byte{5, 6, 7}
var ktestAeadBody2 = []byte{5, 6, 8}
var kNonce0 = []byte{0, 0, 0, 0, 0, 0, 0, 0}
var kNonce1 = []byte{0, 0, 0, 0, 0, 0, 0, 1}
func testAeadSuccess(t *testing.T, aead cipher.AEAD) {
ct := aead.Seal(nil, kNonce0, ktestAeadBody1, ktestAeadHdr1)
pt, err := aead.Open(nil, kNonce0, ct, ktestAeadHdr1)
assertNotError(t, err, "Could not unprotect")
assertByteEquals(t, pt, ktestAeadBody1)
}
func testAeadWrongPacketNumber(t *testing.T, aead cipher.AEAD) {
ct := aead.Seal(nil, kNonce0, ktestAeadBody1, ktestAeadHdr1)
_, err := aead.Open(nil, kNonce1, ct, ktestAeadHdr1)
assertError(t, err, "Shouldn't have unprotected")
}
func testAeadWrongHeader(t *testing.T, aead cipher.AEAD) {
ct := aead.Seal(nil, kNonce0, ktestAeadBody1, ktestAeadHdr1)
_, err := aead.Open(nil, kNonce0, ct, ktestAeadHdr2)
assertError(t, err, "Shouldn't have unprotected")
}
func testAeadCorruptCT(t *testing.T, aead cipher.AEAD) {
ct := aead.Seal(nil, kNonce0, ktestAeadBody1, ktestAeadHdr1)
ct[0]++
_, err := aead.Open(nil, kNonce0, ct, ktestAeadHdr1)
assertError(t, err, "Shouldn't have unprotected")
}
func testAeadCorruptTag(t *testing.T, aead cipher.AEAD) {
ct := aead.Seal(nil, kNonce0, ktestAeadBody1, ktestAeadHdr1)
ct[len(ct)-1]++
_, err := aead.Open(nil, kNonce0, ct, ktestAeadHdr1)
assertError(t, err, "Shouldn't have unprotected")
}
func testAeadWrongAead(t *testing.T, aead cipher.AEAD, aead2 cipher.AEAD) {
ct := aead.Seal(nil, kNonce0, ktestAeadBody1, ktestAeadHdr1)
_, err := aead2.Open(nil, kNonce0, ct, ktestAeadHdr1)
assertError(t, err, "Shouldn't have unprotected")
}
func testAeadAll(t *testing.T, aead cipher.AEAD) {
t.Run("Success", func(t *testing.T) { testAeadSuccess(t, aead) })
t.Run("WrongHeader", func(t *testing.T) { testAeadWrongHeader(t, aead) })
t.Run("CorruptCT", func(t *testing.T) { testAeadCorruptCT(t, aead) })
t.Run("CorruptTag", func(t *testing.T) { testAeadCorruptTag(t, aead) })
}
func makeWrappedAead(t *testing.T, key []byte, iv []byte) cipher.AEAD {
a, err := newWrappedAESGCM(key, iv)
assertNotError(t, err, "Couldn't make AEAD")
return a
}
func TestAeadAES128GCM(t *testing.T) {
a1 := makeWrappedAead(t, kTestKey1, kTestIV1)
a2 := makeWrappedAead(t, kTestKey2, kTestIV1)
a3 := makeWrappedAead(t, kTestKey1, kTestIV2)
testAeadAll(t, a1)
t.Run("WrongKey", func(t *testing.T) { testAeadWrongAead(t, a1, a2) })
t.Run("WrongIV", func(t *testing.T) { testAeadWrongAead(t, a1, a3) })
t.Run("WrongPacketNumber", func(t *testing.T) { testAeadWrongPacketNumber(t, a1) })
}