-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaes_gcm_test.go
151 lines (136 loc) · 5.01 KB
/
aes_gcm_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
// Copyright (c) 2018 Andreas Auernhammer. All rights reserved.
// Use of this source code is governed by a license that can be
// found in the LICENSE file.
package siv
import (
"bytes"
"encoding/hex"
"testing"
"golang.org/x/sys/cpu"
)
func TestAESGCM(t *testing.T) {
hasAES, hashGHASH := cpu.X86.HasAES, cpu.X86.HasPCLMULQDQ
defer func(hasAES, hashGHASH bool) { cpu.X86.HasAES, cpu.X86.HasPCLMULQDQ = hasAES, hashGHASH }(hasAES, hashGHASH)
if hasAES && hashGHASH {
t.Run("Asm", testAESGCM)
cpu.X86.HasAES, cpu.X86.HasPCLMULQDQ = false, false
}
t.Run("Generic", testAESGCM)
}
func testAESGCM(t *testing.T) {
for i, v := range aesGcmSivTests {
c, err := NewGCM(v.Key())
if err != nil {
t.Errorf("Test %d: Failed to create AES_SIV: %v", i, err)
continue
}
ciphertext := c.Seal(nil, v.Nonce(), v.Plaintext(), v.AdditionalData())
if !bytes.Equal(ciphertext, v.Ciphertext()) {
t.Errorf("Test %d: Seal - ciphertext mismatch: %s - %s", i, v.ciphertext, hex.EncodeToString(ciphertext))
}
plaintext, err := c.Open(ciphertext[:0], v.Nonce(), ciphertext, v.AdditionalData())
if err != nil {
t.Errorf("Test %d: Open failed - %v", i, err)
}
if !bytes.Equal(plaintext, v.Plaintext()) {
t.Errorf("Test %d: Open - plaintext mismatch", i)
}
}
}
func TestAESGCMAssembler(t *testing.T) {
if !cpu.X86.HasAES || !cpu.X86.HasPCLMULQDQ {
t.Skip("No assembler implementation / AES hardware support")
}
keys := [][]byte{make([]byte, 16), make([]byte, 32)}
for i := range keys {
for j := range keys[i] {
keys[i][j] = byte(i*j + len(keys))
}
}
nonce := make([]byte, 12)
for i := range nonce {
nonce[i] = byte(i)
}
plaintext := make([]byte, 1024)
ciphertext := make([]byte, len(plaintext)+16)
for i := range keys {
for j := range plaintext {
plaintext[i] = byte(j + i)
testAESGCMAssmebler(i, ciphertext[:16+j], nonce, plaintext[:j], plaintext[j:], keys[i], t)
}
}
}
func testAESGCMAssmebler(i int, ciphertext, nonce, plaintext, additionalData, key []byte, t *testing.T) {
hasAES, hashGHASH := cpu.X86.HasAES, cpu.X86.HasPCLMULQDQ
defer func(hasAES, hashGHASH bool) { cpu.X86.HasAES, cpu.X86.HasPCLMULQDQ = hasAES, hashGHASH }(hasAES, hashGHASH)
c, err := NewGCM(key)
if err != nil {
t.Fatalf("Test %d: failed to create AES-GCM-SIV: %v", i, err)
}
ciphertext = c.Seal(ciphertext[:0], nonce, plaintext, additionalData)
asmPlaintext, err := c.Open(nil, nonce, ciphertext, additionalData)
if err != nil {
t.Fatalf("Test %d: Open failed: %v", i, err)
}
if !bytes.Equal(plaintext, asmPlaintext) {
t.Fatalf("Test %d: plaintext mismatch", i)
}
cpu.X86.HasAES, cpu.X86.HasPCLMULQDQ = false, false // Disable AES assembler implementations
c, err = NewGCM(key)
if err != nil {
t.Fatalf("Test %d: failed to create AES-GCM-SIV: %v", i, err)
}
refCiphertext := c.Seal(nil, nonce, plaintext, additionalData)
if !bytes.Equal(refCiphertext, ciphertext) {
t.Fatalf("Test %d: ciphertext mismatch", i)
}
refPlaintext, err := c.Open(ciphertext[:0], nonce, ciphertext, additionalData)
if err != nil {
t.Fatalf("Test %d: Open failed: %v", i, err)
}
if !bytes.Equal(plaintext, refPlaintext) {
t.Fatalf("Test %d: plaintext mismatch", i)
}
}
func BenchmarkAES128GCMSeal64(b *testing.B) { benchmarkAESGCMSeal(make([]byte, 16), 64, b) }
func BenchmarkAES128GCMSeal1K(b *testing.B) { benchmarkAESGCMSeal(make([]byte, 16), 1024, b) }
func BenchmarkAES128GCMSeal8K(b *testing.B) { benchmarkAESGCMSeal(make([]byte, 16), 8*1024, b) }
func BenchmarkAES128GCMOpen64(b *testing.B) { benchmarkAESGCMOpen(make([]byte, 16), 64, b) }
func BenchmarkAES128GCMOpen1K(b *testing.B) { benchmarkAESGCMOpen(make([]byte, 16), 1024, b) }
func BenchmarkAES128GCMOpen8K(b *testing.B) { benchmarkAESGCMOpen(make([]byte, 16), 8*1024, b) }
func BenchmarkAES256GCMSeal64(b *testing.B) { benchmarkAESGCMSeal(make([]byte, 32), 64, b) }
func BenchmarkAES256GCMSeal1K(b *testing.B) { benchmarkAESGCMSeal(make([]byte, 32), 1024, b) }
func BenchmarkAES256GCMSeal8K(b *testing.B) { benchmarkAESGCMSeal(make([]byte, 32), 8*1024, b) }
func BenchmarkAES256GCMOpen64(b *testing.B) { benchmarkAESGCMOpen(make([]byte, 32), 64, b) }
func BenchmarkAES256GCMOpen1K(b *testing.B) { benchmarkAESGCMOpen(make([]byte, 32), 1024, b) }
func BenchmarkAES256GCMOpen8K(b *testing.B) { benchmarkAESGCMOpen(make([]byte, 32), 8*1024, b) }
func benchmarkAESGCMSeal(key []byte, size int64, b *testing.B) {
c, err := NewGCM(key)
if err != nil {
b.Fatal(err)
}
nonce := make([]byte, c.NonceSize())
plaintext := make([]byte, size)
ciphertext := make([]byte, len(plaintext)+16)
b.ResetTimer()
b.SetBytes(size)
for i := 0; i < b.N; i++ {
c.Seal(ciphertext[:0], nonce, plaintext, nil)
}
}
func benchmarkAESGCMOpen(key []byte, size int64, b *testing.B) {
c, err := NewGCM(key)
if err != nil {
b.Fatal(err)
}
nonce := make([]byte, c.NonceSize())
plaintext := make([]byte, size)
ciphertext := c.Seal(nil, nonce, plaintext, nil)
b.ResetTimer()
b.SetBytes(size)
for i := 0; i < b.N; i++ {
if _, err := c.Open(plaintext[:0], nonce, ciphertext, nil); err != nil {
panic(err)
}
}
}