| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package chacha20 |
| 5 |
|
| 6 |
import "testing" |
| 7 |
|
| 8 |
var recFunc = func(t *testing.T, msg string) { |
| 9 |
if recover() == nil { |
| 10 |
t.Fatalf("Expected error: %s", msg) |
| 11 |
} |
| 12 |
} |
| 13 |
|
| 14 |
func TestNewChaCha20Poly1305WithTagSize(t *testing.T) { |
| 15 |
var key [32]byte |
| 16 |
_, err := NewChaCha20Poly1305WithTagSize(&key, 0) |
| 17 |
if err == nil { |
| 18 |
t.Fatalf("NewChaCha20Poly1305WithTagSize accepted invalid tagsize: %d", 0) |
| 19 |
} |
| 20 |
|
| 21 |
_, err = NewChaCha20Poly1305WithTagSize(&key, 17) |
| 22 |
if err == nil { |
| 23 |
t.Fatalf("NewChaCha20Poly1305WithTagSize accepted invalid tagsize: %d", 0) |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
func TestOverhead(t *testing.T) { |
| 28 |
var key [32]byte |
| 29 |
c := NewChaCha20Poly1305(&key) |
| 30 |
|
| 31 |
if o := c.Overhead(); o != TagSize { |
| 32 |
t.Fatalf("Expected %d but Overhead() returned %d", TagSize, o) |
| 33 |
} |
| 34 |
|
| 35 |
c, err := NewChaCha20Poly1305WithTagSize(&key, 12) |
| 36 |
if err != nil { |
| 37 |
t.Fatalf("Failed to create ChaCha20Poly1305 instance: %s", err) |
| 38 |
} |
| 39 |
if o := c.Overhead(); o != 12 { |
| 40 |
t.Fatalf("Expected %d but Overhead() returned %d", 12, o) |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
func TestNonceSize(t *testing.T) { |
| 45 |
var key [32]byte |
| 46 |
c := NewChaCha20Poly1305(&key) |
| 47 |
if n := c.NonceSize(); n != NonceSize { |
| 48 |
t.Fatalf("Expected %d but NonceSize() returned %d", TagSize, n) |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
func TestSeal(t *testing.T) { |
| 53 |
var key [32]byte |
| 54 |
c := NewChaCha20Poly1305(&key) |
| 55 |
|
| 56 |
var ( |
| 57 |
nonce [NonceSize]byte |
| 58 |
src [64]byte |
| 59 |
dst [64 + TagSize]byte |
| 60 |
) |
| 61 |
|
| 62 |
mustFail := func(msg string, dst, nonce, src []byte) { |
| 63 |
defer recFunc(t, msg) |
| 64 |
c.Seal(dst, nonce, src, nil) |
| 65 |
} |
| 66 |
|
| 67 |
mustFail("nonce size is invalid", dst[:], nonce[:NonceSize-1], src[:]) |
| 68 |
|
| 69 |
mustFail("dst length invalid", dst[:len(dst)-2], nonce[:], src[:]) |
| 70 |
} |
| 71 |
|
| 72 |
func TestOpen(t *testing.T) { |
| 73 |
var key [32]byte |
| 74 |
c := NewChaCha20Poly1305(&key) |
| 75 |
|
| 76 |
var ( |
| 77 |
nonce [NonceSize]byte |
| 78 |
src [64]byte |
| 79 |
dst [64 + TagSize]byte |
| 80 |
) |
| 81 |
|
| 82 |
_, err := c.Open(dst[:], nonce[:NonceSize-1], src[:], nil) |
| 83 |
if err == nil { |
| 84 |
t.Fatal("Open() accepted invalid nonce size") |
| 85 |
} |
| 86 |
|
| 87 |
_, err = c.Open(dst[:], nonce[:], src[:TagSize-1], nil) |
| 88 |
if err == nil { |
| 89 |
t.Fatal("Open() accepted invalid ciphertext length") |
| 90 |
} |
| 91 |
|
| 92 |
mustFail := func(msg string, dst, nonce, src []byte) { |
| 93 |
defer recFunc(t, msg) |
| 94 |
c.Open(dst, nonce, src, nil) |
| 95 |
} |
| 96 |
|
| 97 |
mustFail("dst length invalid", dst[:len(src)-TagSize-1], nonce[:], src[:]) |
| 98 |
|
| 99 |
|
| 100 |
c.Seal(dst[:], nonce[:], src[:], nil) |
| 101 |
dst[len(src)+1] += 1 |
| 102 |
|
| 103 |
_, err = c.Open(src[:], nonce[:], dst[:], nil) |
| 104 |
if err == nil { |
| 105 |
t.Fatal("Open() accepted invalid auth. tag") |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
func BenchmarkSeal64B(b *testing.B) { |
| 110 |
var key [32]byte |
| 111 |
var nonce [12]byte |
| 112 |
c := NewChaCha20Poly1305(&key) |
| 113 |
|
| 114 |
msg := make([]byte, 64) |
| 115 |
dst := make([]byte, len(msg)+TagSize) |
| 116 |
data := make([]byte, 32) |
| 117 |
|
| 118 |
b.SetBytes(int64(len(msg))) |
| 119 |
for i := 0; i < b.N; i++ { |
| 120 |
dst = c.Seal(dst, nonce[:], msg, data) |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
func BenchmarkSeal1K(b *testing.B) { |
| 125 |
var key [32]byte |
| 126 |
var nonce [12]byte |
| 127 |
c := NewChaCha20Poly1305(&key) |
| 128 |
|
| 129 |
msg := make([]byte, 1024) |
| 130 |
dst := make([]byte, len(msg)+TagSize) |
| 131 |
data := make([]byte, 32) |
| 132 |
|
| 133 |
b.SetBytes(int64(len(msg))) |
| 134 |
for i := 0; i < b.N; i++ { |
| 135 |
dst = c.Seal(dst, nonce[:], msg, data) |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
func BenchmarkSeal64K(b *testing.B) { |
| 140 |
var key [32]byte |
| 141 |
var nonce [12]byte |
| 142 |
c := NewChaCha20Poly1305(&key) |
| 143 |
|
| 144 |
msg := make([]byte, 64*1024) |
| 145 |
dst := make([]byte, len(msg)+TagSize) |
| 146 |
data := make([]byte, 32) |
| 147 |
|
| 148 |
b.SetBytes(int64(len(msg))) |
| 149 |
for i := 0; i < b.N; i++ { |
| 150 |
dst = c.Seal(dst, nonce[:], msg, data) |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
func BenchmarkOpen64B(b *testing.B) { |
| 155 |
var key [32]byte |
| 156 |
var nonce [12]byte |
| 157 |
c := NewChaCha20Poly1305(&key) |
| 158 |
|
| 159 |
msg := make([]byte, 64) |
| 160 |
dst := make([]byte, len(msg)) |
| 161 |
ciphertext := make([]byte, len(msg)+TagSize) |
| 162 |
data := make([]byte, 32) |
| 163 |
ciphertext = c.Seal(ciphertext, nonce[:], msg, data) |
| 164 |
|
| 165 |
b.SetBytes(int64(len(msg))) |
| 166 |
for i := 0; i < b.N; i++ { |
| 167 |
dst, _ = c.Open(dst, nonce[:], ciphertext, data) |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
func BenchmarkOpen1K(b *testing.B) { |
| 172 |
var key [32]byte |
| 173 |
var nonce [12]byte |
| 174 |
c := NewChaCha20Poly1305(&key) |
| 175 |
|
| 176 |
msg := make([]byte, 1024) |
| 177 |
dst := make([]byte, len(msg)) |
| 178 |
ciphertext := make([]byte, len(msg)+TagSize) |
| 179 |
data := make([]byte, 32) |
| 180 |
ciphertext = c.Seal(ciphertext, nonce[:], msg, data) |
| 181 |
|
| 182 |
b.SetBytes(int64(len(msg))) |
| 183 |
for i := 0; i < b.N; i++ { |
| 184 |
dst, _ = c.Open(dst, nonce[:], ciphertext, data) |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
func BenchmarkOpen64K(b *testing.B) { |
| 189 |
var key [32]byte |
| 190 |
var nonce [12]byte |
| 191 |
c := NewChaCha20Poly1305(&key) |
| 192 |
|
| 193 |
msg := make([]byte, 64*1024) |
| 194 |
dst := make([]byte, len(msg)) |
| 195 |
ciphertext := make([]byte, len(msg)+TagSize) |
| 196 |
data := make([]byte, 32) |
| 197 |
ciphertext = c.Seal(ciphertext, nonce[:], msg, data) |
| 198 |
|
| 199 |
b.SetBytes(int64(len(msg))) |
| 200 |
for i := 0; i < b.N; i++ { |
| 201 |
dst, _ = c.Open(dst, nonce[:], ciphertext, data) |
| 202 |
} |
| 203 |
} |