-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaead_test.go
81 lines (72 loc) · 1.92 KB
/
aead_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
package natrium
import (
"crypto/cipher"
"crypto/aes"
"testing"
)
func Test_AEAD_Basic(t *testing.T) {
tocrypt := []byte("Hello World")
key := []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
thingy := AEAD(key)
crypted := thingy.Seal(make([]byte, 0), make([]byte, thingy.NonceSize()),
tocrypt, nil)
decrypted, err := thingy.Open(make([]byte, 0), make([]byte, thingy.NonceSize()),
crypted, nil)
if err != nil {
t.FailNow()
}
for i := 0; i < len(decrypted); i++ {
if tocrypt[i] != decrypted[i] {
t.FailNow()
}
}
}
func Test_AEAD_NoData_Fail(t *testing.T) {
tocrypt := []byte("Hello World")
key := []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
thingy := AEAD(key)
crypted := thingy.Seal(make([]byte, 0), make([]byte, thingy.NonceSize()),
tocrypt, nil)
// Modify crypted
crypted[3] = 100
_, err := thingy.Open(make([]byte, 0), make([]byte, thingy.NonceSize()),
crypted, nil)
if err == nil {
t.FailNow()
}
}
func Test_AEAD_Data_Fail(t *testing.T) {
tocrypt := []byte("Hello World")
key := []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
thingy := AEAD(key)
dat := []byte("hello world")
crypted := thingy.Seal(make([]byte, 0), make([]byte, thingy.NonceSize()),
tocrypt, dat)
dat[3] = 100
_, err := thingy.Open(make([]byte, 0), make([]byte, thingy.NonceSize()),
crypted, dat)
if err == nil {
t.FailNow()
}
}
func BenchmarkAEAD(b *testing.B) {
tocrypt := make([]byte, 1024)
key := []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
thingy := AEAD(key)
nonce := make([]byte, thingy.NonceSize())
b.ResetTimer()
for i := 0; i < b.N; i++ {
thingy.Seal(make([]byte, 0), nonce, tocrypt, nil)
}
}
func BenchmarkAEAD_AES(b *testing.B) {
tocrypt := make([]byte, 1024)
key := []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
raw, _ := aes.NewCipher(key)
thingy, _ := cipher.NewGCM(raw)
nonce := make([]byte, thingy.NonceSize())
b.ResetTimer()
for i := 0; i < b.N; i++ {
thingy.Seal(make([]byte, 0), nonce, tocrypt, nil)
}
}