1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package chacha20poly1305
15 func TestVectors(t *testing.T) {
16 for i, test := range chacha20Poly1305Tests {
17 key, _ := hex.DecodeString(test.key)
18 nonce, _ := hex.DecodeString(test.nonce)
19 ad, _ := hex.DecodeString(test.aad)
20 plaintext, _ := hex.DecodeString(test.plaintext)
27 ct := aead.Seal(nil, nonce, plaintext, ad)
28 if ctHex := hex.EncodeToString(ct); ctHex != test.out {
29 t.Errorf("#%d: got %s, want %s", i, ctHex, test.out)
33 plaintext2, err := aead.Open(nil, nonce, ct, ad)
35 t.Errorf("#%d: Open failed", i)
39 if !bytes.Equal(plaintext, plaintext2) {
40 t.Errorf("#%d: plaintext's don't match: got %x vs %x", i, plaintext2, plaintext)
45 alterAdIdx := mr.Intn(len(ad))
46 ad[alterAdIdx] ^= 0x80
47 if _, err := aead.Open(nil, nonce, ct, ad); err == nil {
48 t.Errorf("#%d: Open was successful after altering additional data", i)
50 ad[alterAdIdx] ^= 0x80
53 alterNonceIdx := mr.Intn(aead.NonceSize())
54 nonce[alterNonceIdx] ^= 0x80
55 if _, err := aead.Open(nil, nonce, ct, ad); err == nil {
56 t.Errorf("#%d: Open was successful after altering nonce", i)
58 nonce[alterNonceIdx] ^= 0x80
60 alterCtIdx := mr.Intn(len(ct))
61 ct[alterCtIdx] ^= 0x80
62 if _, err := aead.Open(nil, nonce, ct, ad); err == nil {
63 t.Errorf("#%d: Open was successful after altering ciphertext", i)
65 ct[alterCtIdx] ^= 0x80
69 func TestRandom(t *testing.T) {
70 // Some random tests to verify Open(Seal) == Plaintext
71 for i := 0; i < 256; i++ {
77 ad := make([]byte, al)
78 plaintext := make([]byte, pl)
84 aead, err := New(key[:])
89 ct := aead.Seal(nil, nonce[:], plaintext, ad)
91 plaintext2, err := aead.Open(nil, nonce[:], ct, ad)
93 t.Errorf("Random #%d: Open failed", i)
97 if !bytes.Equal(plaintext, plaintext2) {
98 t.Errorf("Random #%d: plaintext's don't match: got %x vs %x", i, plaintext2, plaintext)
103 alterAdIdx := mr.Intn(len(ad))
104 ad[alterAdIdx] ^= 0x80
105 if _, err := aead.Open(nil, nonce[:], ct, ad); err == nil {
106 t.Errorf("Random #%d: Open was successful after altering additional data", i)
108 ad[alterAdIdx] ^= 0x80
111 alterNonceIdx := mr.Intn(aead.NonceSize())
112 nonce[alterNonceIdx] ^= 0x80
113 if _, err := aead.Open(nil, nonce[:], ct, ad); err == nil {
114 t.Errorf("Random #%d: Open was successful after altering nonce", i)
116 nonce[alterNonceIdx] ^= 0x80
118 alterCtIdx := mr.Intn(len(ct))
119 ct[alterCtIdx] ^= 0x80
120 if _, err := aead.Open(nil, nonce[:], ct, ad); err == nil {
121 t.Errorf("Random #%d: Open was successful after altering ciphertext", i)
123 ct[alterCtIdx] ^= 0x80
127 func benchamarkChaCha20Poly1305Seal(b *testing.B, buf []byte) {
128 b.SetBytes(int64(len(buf)))
135 aead, _ := New(key[:])
137 for i := 0; i < b.N; i++ {
138 out = aead.Seal(out[:0], nonce[:], buf[:], ad[:])
142 func benchamarkChaCha20Poly1305Open(b *testing.B, buf []byte) {
143 b.SetBytes(int64(len(buf)))
151 aead, _ := New(key[:])
152 ct = aead.Seal(ct[:0], nonce[:], buf[:], ad[:])
155 for i := 0; i < b.N; i++ {
156 out, _ = aead.Open(out[:0], nonce[:], ct[:], ad[:])
160 func BenchmarkChacha20Poly1305Open_64(b *testing.B) {
161 benchamarkChaCha20Poly1305Open(b, make([]byte, 64))
164 func BenchmarkChacha20Poly1305Seal_64(b *testing.B) {
165 benchamarkChaCha20Poly1305Seal(b, make([]byte, 64))
168 func BenchmarkChacha20Poly1305Open_1350(b *testing.B) {
169 benchamarkChaCha20Poly1305Open(b, make([]byte, 1350))
172 func BenchmarkChacha20Poly1305Seal_1350(b *testing.B) {
173 benchamarkChaCha20Poly1305Seal(b, make([]byte, 1350))
176 func BenchmarkChacha20Poly1305Open_8K(b *testing.B) {
177 benchamarkChaCha20Poly1305Open(b, make([]byte, 8*1024))
180 func BenchmarkChacha20Poly1305Seal_8K(b *testing.B) {
181 benchamarkChaCha20Poly1305Seal(b, make([]byte, 8*1024))