This repository has been archived by the owner on Feb 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
siv_test.go
230 lines (194 loc) · 5.56 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Written in 2015 by Dmitry Chestnykh.
//
// To the extent possible under law, the author have dedicated all copyright
// and related and neighboring rights to this software to the public domain
// worldwide. This software is distributed without any warranty.
// http://creativecommons.org/publicdomain/zero/1.0/
package miscreant
import (
"bytes"
"encoding/hex"
"encoding/json"
"io/ioutil"
"testing"
)
type aesSIVExample struct {
name string
key []byte
ad [][]byte
plaintext []byte
ciphertext []byte
}
// Load AES-SIV test vectors from aes_siv.tjson
// TODO: switch to a native Go TJSON parser when available
func loadAESSIVExamples(filename string) []aesSIVExample {
var examplesJSON map[string]interface{}
exampleData, err := ioutil.ReadFile("vectors/" + filename)
if err != nil {
panic(err)
}
if err = json.Unmarshal(exampleData, &examplesJSON); err != nil {
panic(err)
}
examplesArray := examplesJSON["examples:A<O>"].([]interface{})
if examplesArray == nil {
panic("no toplevel 'examples:A<O>' key in " + filename)
}
result := make([]aesSIVExample, len(examplesArray))
for i, exampleJSON := range examplesArray {
example := exampleJSON.(map[string]interface{})
name := example["name:s"].(string)
keyHex := example["key:d16"].(string)
key := make([]byte, hex.DecodedLen(len(keyHex)))
if _, err := hex.Decode(key, []byte(keyHex)); err != nil {
panic(err)
}
adHeaders := example["ad:A<d16>"].([]interface{})
ad := make([][]byte, len(adHeaders))
for j, adHeader := range adHeaders {
adHeaderHex := adHeader.(string)
adDecoded := make([]byte, hex.DecodedLen(len(adHeaderHex)))
if _, err := hex.Decode(adDecoded, []byte(adHeaderHex)); err != nil {
panic(err)
}
ad[j] = adDecoded
}
plaintextHex := example["plaintext:d16"].(string)
plaintext := make([]byte, hex.DecodedLen(len(plaintextHex)))
if _, err := hex.Decode(plaintext, []byte(plaintextHex)); err != nil {
panic(err)
}
ciphertextHex := example["ciphertext:d16"].(string)
ciphertext := make([]byte, hex.DecodedLen(len(ciphertextHex)))
if _, err := hex.Decode(ciphertext, []byte(ciphertextHex)); err != nil {
panic(err)
}
result[i] = aesSIVExample{name, key, ad, plaintext, ciphertext}
}
return result
}
func TestAESCMACSIV(t *testing.T) {
for i, v := range loadAESSIVExamples("aes_siv.tjson") {
c, err := NewAESCMACSIV(v.key)
if err != nil {
t.Errorf("NewAESCMACSIV: %d: %s", i, err)
}
ct, err := c.Seal(nil, v.plaintext, v.ad...)
if err != nil {
t.Errorf("Seal: %d: %s", i, err)
}
if !bytes.Equal(v.ciphertext, ct) {
t.Errorf("Seal: %d: expected: %x\ngot: %x", i, v.ciphertext, ct)
}
pt, err := c.Open(nil, ct, v.ad...)
if err != nil {
t.Errorf("Open: %d: %s", i, err)
}
if !bytes.Equal(v.plaintext, pt) {
t.Errorf("Open: %d: expected: %x\ngot: %x", i, v.plaintext, pt)
}
}
}
func TestAESPMACSIV(t *testing.T) {
for i, v := range loadAESSIVExamples("aes_pmac_siv.tjson") {
c, err := NewAESPMACSIV(v.key)
if err != nil {
t.Errorf("NewAESPMACSIV: %d: %s", i, err)
}
ct, err := c.Seal(nil, v.plaintext, v.ad...)
if err != nil {
t.Errorf("Seal: %d: %s", i, err)
}
if !bytes.Equal(v.ciphertext, ct) {
t.Errorf("Seal: %d: expected: %x\ngot: %x", i, v.ciphertext, ct)
}
pt, err := c.Open(nil, ct, v.ad...)
if err != nil {
t.Errorf("Open: %d: %s", i, err)
}
if !bytes.Equal(v.plaintext, pt) {
t.Errorf("Open: %d: expected: %x\ngot: %x", i, v.plaintext, pt)
}
}
}
func TestAESCMACSIVAppend(t *testing.T) {
v := loadAESSIVExamples("aes_siv.tjson")[0]
c, err := NewAESCMACSIV(v.key)
if err != nil {
t.Fatalf("NewAESCMACSIV: %s", err)
}
out := []byte{1, 2, 3, 4}
x, err := c.Seal(out, v.plaintext, v.ad...)
if err != nil {
t.Fatalf("Seal: %s", err)
}
if !bytes.Equal(x[:4], out[:4]) {
t.Fatalf("Seal: didn't correctly append")
}
out = make([]byte, 4, 100)
x, err = c.Seal(out, v.plaintext, v.ad...)
if err != nil {
t.Fatalf("Seal: %s", err)
}
if !bytes.Equal(x[:4], out[:4]) {
t.Fatalf("Seal: didn't correctly append with sufficient capacity")
}
out = make([]byte, 4)
x, err = c.Open(out, v.ciphertext, v.ad...)
if err != nil {
t.Fatalf("Open: %s", err)
}
if !bytes.Equal(x[:4], out[:4]) {
t.Fatalf("Open: didn't correctly append")
}
out = make([]byte, 4, 100)
x, err = c.Open(out, v.ciphertext, v.ad...)
if err != nil {
t.Fatalf("Open: %s", err)
}
if !bytes.Equal(x[:4], out[:4]) {
t.Fatalf("Open: didn't correctly append with sufficient capacity")
}
}
func BenchmarkSIVAES128_Seal_1K(b *testing.B) {
a := make([]byte, 64)
m := make([]byte, 1024)
c, _ := NewAESCMACSIV(make([]byte, 32))
out := make([]byte, 0, len(m)+c.Overhead())
b.SetBytes(int64(len(m)))
for i := 0; i < b.N; i++ {
_, _ = c.Seal(out, m, a)
}
}
func BenchmarkSIVAES128_Seal_8K(b *testing.B) {
a := make([]byte, 64)
m := make([]byte, 8192)
c, _ := NewAESCMACSIV(make([]byte, 32))
b.SetBytes(int64(len(m)))
out := make([]byte, 0, len(m)+c.Overhead())
for i := 0; i < b.N; i++ {
_, _ = c.Seal(out, m, a)
}
}
func BenchmarkSIVAES128_Open_1K(b *testing.B) {
a := make([]byte, 64)
m := make([]byte, 1024)
c, _ := NewAESCMACSIV(make([]byte, 32))
x, _ := c.Seal(nil, m, a)
out := make([]byte, 0, len(m))
b.SetBytes(int64(len(m)))
for i := 0; i < b.N; i++ {
_, _ = c.Open(out, x, a)
}
}
func BenchmarkSIVAES128_Open_8K(b *testing.B) {
a := make([]byte, 64)
m := make([]byte, 8192)
c, _ := NewAESCMACSIV(make([]byte, 32))
x, _ := c.Seal(nil, m, a)
out := make([]byte, 0, len(m))
b.SetBytes(int64(len(m)))
for i := 0; i < b.N; i++ {
_, _ = c.Open(out, x, a)
}
}