-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
151 lines (131 loc) · 4.11 KB
/
main_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
package main
import (
"crypto/aes"
"crypto/cipher"
"fmt"
"runtime"
"strconv"
"testing"
"golang.org/x/crypto/chacha20poly1305"
)
func benchmarkAESGCMEncrypt(b *testing.B, buf []byte, keySize int) {
//b.ReportAllocs()
b.SetBytes(int64(len(buf)))
var key = make([]byte, keySize)
var nonce [12]byte
var ad [13]byte
aes, _ := aes.NewCipher(key[:])
aesgcm, _ := cipher.NewGCM(aes)
var out []byte
b.ResetTimer()
for i := 0; i < b.N; i++ {
out = aesgcm.Seal(out[:0], nonce[:], buf, ad[:])
}
}
func benchmarkAESGCMDecrypt(b *testing.B, buf []byte, keySize int) {
//b.ReportAllocs()
b.SetBytes(int64(len(buf)))
var key = make([]byte, keySize)
var nonce [12]byte
var ad [13]byte
aes, _ := aes.NewCipher(key[:])
aesgcm, _ := cipher.NewGCM(aes)
var out []byte
ct := aesgcm.Seal(nil, nonce[:], buf[:], ad[:])
b.ResetTimer()
for i := 0; i < b.N; i++ {
out, _ = aesgcm.Open(out[:0], nonce[:], ct, ad[:])
}
}
func benchmarkAESGCM(b *testing.B) {
for _, length := range []int{64, 256, 1024, 8 * 1024} {
b.Run("Decrypt-128-"+strconv.Itoa(length), func(b *testing.B) {
benchmarkAESGCMDecrypt(b, make([]byte, length), 128/8)
})
}
for _, length := range []int{64, 256, 1024, 8 * 1024} {
b.Run("Decrypt-256-"+strconv.Itoa(length), func(b *testing.B) {
benchmarkAESGCMDecrypt(b, make([]byte, length), 256/8)
})
}
for _, length := range []int{64, 256, 1024, 8 * 1024} {
b.Run("Encrypt-128-"+strconv.Itoa(length), func(b *testing.B) {
benchmarkAESGCMEncrypt(b, make([]byte, length), 128/8)
})
}
for _, length := range []int{64, 256, 1024, 8 * 1024} {
b.Run("Encrypt-256-"+strconv.Itoa(length), func(b *testing.B) {
benchmarkAESGCMEncrypt(b, make([]byte, length), 256/8)
})
}
}
func benchamarkChaCha20Poly1305Encrypt(b *testing.B, buf []byte, nonceSize int) {
//b.ReportAllocs()
b.SetBytes(int64(len(buf)))
var key [32]byte
var nonce = make([]byte, nonceSize)
var ad [13]byte
var out []byte
var aead cipher.AEAD
switch len(nonce) {
case chacha20poly1305.NonceSize:
aead, _ = chacha20poly1305.New(key[:])
case chacha20poly1305.NonceSizeX:
aead, _ = chacha20poly1305.NewX(key[:])
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
out = aead.Seal(out[:0], nonce[:], buf[:], ad[:])
}
}
func benchamarkChaCha20Poly1305Decrypt(b *testing.B, buf []byte, nonceSize int) {
//b.ReportAllocs()
b.SetBytes(int64(len(buf)))
var key [32]byte
var nonce = make([]byte, nonceSize)
var ad [13]byte
var ct []byte
var out []byte
var aead cipher.AEAD
switch len(nonce) {
case chacha20poly1305.NonceSize:
aead, _ = chacha20poly1305.New(key[:])
case chacha20poly1305.NonceSizeX:
aead, _ = chacha20poly1305.NewX(key[:])
}
ct = aead.Seal(ct[:0], nonce[:], buf[:], ad[:])
b.ResetTimer()
for i := 0; i < b.N; i++ {
out, _ = aead.Open(out[:0], nonce[:], ct[:], ad[:])
}
}
func benchmarkChacha20Poly1305(b *testing.B) {
for _, length := range []int{64, 256, 1024, 8 * 1024} {
b.Run("Decrypt-"+strconv.Itoa(length), func(b *testing.B) {
benchamarkChaCha20Poly1305Decrypt(b, make([]byte, length), chacha20poly1305.NonceSize)
})
}
for _, length := range []int{64, 256, 1024, 8 * 1024} {
b.Run("Decrypt-"+strconv.Itoa(length)+"-X", func(b *testing.B) {
benchamarkChaCha20Poly1305Decrypt(b, make([]byte, length), chacha20poly1305.NonceSizeX)
})
}
for _, length := range []int{64, 256, 1024, 8 * 1024} {
b.Run("Encrypt-"+strconv.Itoa(length), func(b *testing.B) {
benchamarkChaCha20Poly1305Encrypt(b, make([]byte, length), chacha20poly1305.NonceSize)
})
}
for _, length := range []int{64, 256, 1024, 8 * 1024} {
b.Run("Encrypt-"+strconv.Itoa(length)+"-X", func(b *testing.B) {
benchamarkChaCha20Poly1305Encrypt(b, make([]byte, length), chacha20poly1305.NonceSizeX)
})
}
}
func BenchmarkCrypto(b *testing.B) {
fmt.Printf("CryptoTestGO %s (%s, %s/%s)\n", Version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
fmt.Println("GitHub: https://github.com/H1JK/CryptoTestGO")
old, new := GetSecurityAutoType()
fmt.Printf("VMess AUTO will choose: Xray 1.7.2-/V2Fly 5.0.8-: %s, Xray 1.7.2+/V2Fly 5.0.8+: %s\n", old, new)
b.Run("AES-GCM", benchmarkAESGCM)
b.Run("ChaCha20-Poly1305", benchmarkChacha20Poly1305)
}