-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathadiantum_test.go
115 lines (105 loc) · 3.67 KB
/
adiantum_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
package adiantum
import (
"encoding/hex"
"encoding/json"
"os"
"testing"
"lukechampine.com/adiantum/hbsh"
)
func fromHex(s string) []byte {
b, _ := hex.DecodeString(s)
return b
}
type testVector struct {
Description string `json:"description"`
Input struct {
Key string `json:"key_hex"`
Tweak string `json:"tweak_hex"`
} `json:"input"`
Plaintext string `json:"plaintext_hex"`
Ciphertext string `json:"ciphertext_hex"`
}
func readTestVectors(t *testing.T, filename string) []testVector {
t.Helper()
js, err := os.ReadFile(filename)
if err != nil {
t.Fatal(err)
}
var tests []testVector
if err := json.Unmarshal(js, &tests); err != nil {
t.Fatal(err)
}
return tests
}
func TestAdiantum_XChaCha8_32_AES256(t *testing.T) {
tests := readTestVectors(t, "testdata/Adiantum_XChaCha8_32_AES256.json")
for i, test := range tests {
c := New8(fromHex(test.Input.Key))
ciphertext := c.Encrypt(fromHex(test.Plaintext), fromHex(test.Input.Tweak))
if hex.EncodeToString(ciphertext) != test.Ciphertext {
t.Fatalf("%v (%v): Encryption failed:\nexp: %v\ngot: %x", test.Description, i, test.Ciphertext, ciphertext)
}
plaintext := c.Decrypt(fromHex(test.Ciphertext), fromHex(test.Input.Tweak))
if hex.EncodeToString(plaintext) != test.Plaintext {
t.Fatalf("%v (%v): Decryption failed:\nexp: %v\ngot: %x", test.Description, i, test.Plaintext, plaintext)
}
}
}
func TestAdiantum_XChaCha12_32_AES256(t *testing.T) {
tests := readTestVectors(t, "testdata/Adiantum_XChaCha12_32_AES256.json")
for i, test := range tests {
c := New(fromHex(test.Input.Key))
ciphertext := c.Encrypt(fromHex(test.Plaintext), fromHex(test.Input.Tweak))
if hex.EncodeToString(ciphertext) != test.Ciphertext {
t.Fatalf("%v (%v): Encryption failed:\nexp: %v\ngot: %x", test.Description, i, test.Ciphertext, ciphertext)
}
plaintext := c.Decrypt(fromHex(test.Ciphertext), fromHex(test.Input.Tweak))
if hex.EncodeToString(plaintext) != test.Plaintext {
t.Fatalf("%v (%v): Decryption failed:\nexp: %v\ngot: %x", test.Description, i, test.Plaintext, plaintext)
}
}
}
func TestAdiantum_XChaCha20_32_AES256(t *testing.T) {
tests := readTestVectors(t, "testdata/Adiantum_XChaCha20_32_AES256.json")
for i, test := range tests {
c := New20(fromHex(test.Input.Key))
ciphertext := c.Encrypt(fromHex(test.Plaintext), fromHex(test.Input.Tweak))
if hex.EncodeToString(ciphertext) != test.Ciphertext {
t.Fatalf("%v (%v): Encryption failed:\nexp: %v\ngot: %x", test.Description, i, test.Ciphertext, ciphertext)
}
plaintext := c.Decrypt(fromHex(test.Ciphertext), fromHex(test.Input.Tweak))
if hex.EncodeToString(plaintext) != test.Plaintext {
t.Fatalf("%v (%v): Decryption failed:\nexp: %v\ngot: %x", test.Description, i, test.Plaintext, plaintext)
}
}
}
func BenchmarkAdiantum(b *testing.B) {
runEncrypt := func(c *hbsh.HBSH) func(*testing.B) {
return func(b *testing.B) {
block := make([]byte, 4096)
tweak := make([]byte, 12)
b.SetBytes(int64(len(block)))
b.ReportAllocs()
for i := 0; i < b.N; i++ {
c.Encrypt(block, tweak)
}
}
}
runDecrypt := func(c *hbsh.HBSH) func(*testing.B) {
return func(b *testing.B) {
block := make([]byte, 4096)
tweak := make([]byte, 12)
b.SetBytes(int64(len(block)))
b.ReportAllocs()
for i := 0; i < b.N; i++ {
c.Decrypt(block, tweak)
}
}
}
b.Run("XChaCha8_Encrypt", runEncrypt(New8(make([]byte, 32))))
b.Run("XChaCha8_Decrypt", runDecrypt(New8(make([]byte, 32))))
b.Run("XChaCha12_Encrypt", runEncrypt(New(make([]byte, 32))))
b.Run("XChaCha12_Decrypt", runDecrypt(New(make([]byte, 32))))
b.Run("XChaCha20_Encrypt", runEncrypt(New20(make([]byte, 32))))
b.Run("XChaCha20_Decrypt", runDecrypt(New20(make([]byte, 32))))
}