This repository has been archived by the owner on Apr 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
siv_test.go
130 lines (102 loc) · 3.44 KB
/
siv_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
package siv
import (
"bytes"
"crypto/aes"
"encoding/hex"
"testing"
)
func TestBadKeySize(t *testing.T) {
aead, err := New(make([]byte, 16), aes.NewCipher)
if err == nil {
t.Fatalf("AEAD returned instead of error: %v", aead)
}
}
func TestNoNonceRequired(t *testing.T) {
aead, _ := New(make([]byte, 32), aes.NewCipher)
if v, want := aead.NonceSize(), 0; v != want {
t.Errorf("Nonce size was %d, but expected %d", v, want)
}
}
func TestOverhead(t *testing.T) {
aead, _ := New(make([]byte, 32), aes.NewCipher)
if v, want := aead.Overhead(), aes.BlockSize; v != want {
t.Errorf("Overhead was %d, but expected %d", v, want)
}
}
func TestDeterministicEncryption(t *testing.T) {
// https://tools.ietf.org/html/rfc5297#appendix-A.1
key, _ := hex.DecodeString("fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff")
data, _ := hex.DecodeString("101112131415161718191a1b1c1d1e1f2021222324252627")
plaintext, _ := hex.DecodeString("112233445566778899aabbccddee")
ciphertext, _ := hex.DecodeString("85632d07c6e8f37f950acd320a2ecc9340c02b9690c4dc04daef7f6afe5c")
aead, err := New(key, aes.NewCipher)
if err != nil {
t.Fatal(err)
}
actual := aead.Seal(nil, nil, plaintext, data)
if !bytes.Equal(actual, ciphertext) {
t.Errorf("Ciphertext was %x, but expected %x", actual, ciphertext)
}
}
func TestRoundTrip(t *testing.T) {
key, _ := hex.DecodeString("fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff")
data, _ := hex.DecodeString("101112131415161718191a1b1c1d1e1f2021222324252627")
plaintext, _ := hex.DecodeString("112233445566778899aabbccddee")
aead, err := New(key, aes.NewCipher)
if err != nil {
t.Fatal(err)
}
ciphertext := aead.Seal(nil, nil, plaintext, data)
actual, err := aead.Open(nil, nil, ciphertext, data)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(actual, plaintext) {
t.Errorf("Plaintext was %x, but expected %x", actual, plaintext)
}
}
func TestRoundTripBadData(t *testing.T) {
key, _ := hex.DecodeString("fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff")
data, _ := hex.DecodeString("101112131415161718191a1b1c1d1e1f2021222324252627")
plaintext, _ := hex.DecodeString("112233445566778899aabbccddee")
aead, err := New(key, aes.NewCipher)
if err != nil {
t.Fatal(err)
}
ciphertext := aead.Seal(nil, nil, plaintext, data)
data[0] ^= 1
actual, err := aead.Open(nil, nil, ciphertext, data)
if err == nil {
t.Fatalf("Plaintext returned instead of error: %x", actual)
}
}
func TestRoundTripBadCiphertext(t *testing.T) {
key, _ := hex.DecodeString("fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff")
data, _ := hex.DecodeString("101112131415161718191a1b1c1d1e1f2021222324252627")
plaintext, _ := hex.DecodeString("112233445566778899aabbccddee")
aead, err := New(key, aes.NewCipher)
if err != nil {
t.Fatal(err)
}
ciphertext := aead.Seal(nil, nil, plaintext, data)
ciphertext[0] ^= 1
actual, err := aead.Open(nil, nil, ciphertext, data)
if err == nil {
t.Fatalf("Plaintext returned instead of error: %x", actual)
}
}
func BenchmarkSeal(b *testing.B) {
key, _ := hex.DecodeString("fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff")
data, _ := hex.DecodeString("101112131415161718191a1b1c1d1e1f2021222324252627")
plaintext := make([]byte, 1024)
aead, err := New(key, aes.NewCipher)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
b.ReportAllocs()
b.SetBytes(1024)
for i := 0; i < b.N; i++ {
aead.Seal(nil, nil, plaintext, data)
}
}