-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
131 lines (101 loc) · 2.89 KB
/
main.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
package main
import (
"crypto/aes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"crypto/sha512"
"flag"
"fmt"
"time"
"golang.org/x/crypto/sha3"
)
type Test struct {
Name string
Func func()
}
func NewRand(len int) []byte {
data := make([]byte, len)
actual, err := rand.Read(data)
if err != nil {
panic(err.Error())
}
if actual != len {
panic(fmt.Sprintf("short read"))
}
return data
}
func NewAES(payloadLen int) func() {
cipher, _ := aes.NewCipher(NewRand(32))
input := NewRand(payloadLen)
output := make([]byte, payloadLen)
return func() { cipher.Encrypt(output, input) }
}
func NewSHA256(payloadLen int) func() {
input := NewRand(payloadLen)
return func() { sha256.Sum256(input) }
}
func NewSHA3Shake256(payloadLen int) func() {
input := NewRand(payloadLen)
var hash = make([]byte, 64)
return func() {
sha3.ShakeSum256(hash, input)
}
}
func NewSHA512256(payloadLen int) func() {
input := NewRand(payloadLen)
return func() { sha512.Sum512_256(input) }
}
func NewECDSASign(payloadLen int) func() {
pubkeyCurve := elliptic.P256()
privatekey := new(ecdsa.PrivateKey)
privatekey, _ = ecdsa.GenerateKey(pubkeyCurve, rand.Reader) // this generates a public & private key pair
input := NewRand(payloadLen)
digest := sha256.Sum256(input)
return func() {
ecdsa.Sign(rand.Reader, privatekey, digest[:])
}
}
func NewECDSAVerify(payloadLen int) func() {
pubkeyCurve := elliptic.P256()
privatekey := new(ecdsa.PrivateKey)
privatekey, _ = ecdsa.GenerateKey(pubkeyCurve, rand.Reader) // this generates a public & private key pair
input := NewRand(payloadLen)
digest := sha256.Sum256(input)
sigA, sigB, _ := ecdsa.Sign(rand.Reader, privatekey, digest[:])
return func() {
ecdsa.Verify(&privatekey.PublicKey, digest[:], sigA, sigB)
}
}
func main() {
iterations := flag.Int("iterations", 100, "the number of iterations per test")
payloadLen := flag.Int("payload", 1*1024, "the size of the payload to use")
flag.Parse()
tests := []Test{
Test{"null", func() {}},
//Test{"timer validation", func() { time.Sleep(100 * time.Microsecond)} },
Test{"AES", NewAES(*payloadLen)},
Test{"SHA256", NewSHA256(*payloadLen)},
Test{"SHA3 SHAKE256", NewSHA3Shake256(*payloadLen)},
Test{"SHA512/256", NewSHA512256(*payloadLen)},
Test{"ECDSA sign", NewECDSASign(*payloadLen)},
Test{"ECDSA verify", NewECDSAVerify(*payloadLen)},
Test{"UDS", NewUDS(*payloadLen)},
Test{"TLS", NewTLS(*payloadLen)},
}
fmt.Printf("iterations: %d payloadLen: %d\n", *iterations, *payloadLen)
for _, test := range tests {
fmt.Print("Running test \"" + test.Name + "\"...")
t0 := time.Now()
for i := 0; i < *iterations; i++ {
test.Func()
}
t1 := time.Now()
delta := t1.Sub(t0).Nanoseconds()
rtt := int(delta) / (*iterations)
ops := 1000000000 / rtt
bandwidth := ops * *payloadLen
fmt.Printf("done: %dns/iteration, %d ops/sec, %d bytes/sec\n", rtt, ops, bandwidth)
}
}