@@ -6,9 +6,11 @@ package chacha20poly1305
66
77import  (
88	"bytes" 
9+ 	"crypto/cipher" 
910	cr "crypto/rand" 
1011	"encoding/hex" 
1112	mr "math/rand" 
13+ 	"strconv" 
1214	"testing" 
1315)
1416
@@ -19,7 +21,18 @@ func TestVectors(t *testing.T) {
1921		ad , _  :=  hex .DecodeString (test .aad )
2022		plaintext , _  :=  hex .DecodeString (test .plaintext )
2123
22- 		aead , err  :=  New (key )
24+ 		var  (
25+ 			aead  cipher.AEAD 
26+ 			err   error 
27+ 		)
28+ 		switch  len (nonce ) {
29+ 		case  NonceSize :
30+ 			aead , err  =  New (key )
31+ 		case  NonceSizeX :
32+ 			aead , err  =  NewX (key )
33+ 		default :
34+ 			t .Fatalf ("#%d: wrong nonce length: %d" , i , len (nonce ))
35+ 		}
2336		if  err  !=  nil  {
2437			t .Fatal (err )
2538		}
@@ -68,87 +81,117 @@ func TestVectors(t *testing.T) {
6881
6982func  TestRandom (t  * testing.T ) {
7083	// Some random tests to verify Open(Seal) == Plaintext 
71- 	for  i  :=  0 ; i  <  256 ; i ++  {
72- 		var  nonce  [12 ]byte 
73- 		var  key  [32 ]byte 
74- 
75- 		al  :=  mr .Intn (128 )
76- 		pl  :=  mr .Intn (16384 )
77- 		ad  :=  make ([]byte , al )
78- 		plaintext  :=  make ([]byte , pl )
79- 		cr .Read (key [:])
80- 		cr .Read (nonce [:])
81- 		cr .Read (ad )
82- 		cr .Read (plaintext )
83- 
84- 		aead , err  :=  New (key [:])
85- 		if  err  !=  nil  {
86- 			t .Fatal (err )
87- 		}
84+ 	f  :=  func (t  * testing.T , nonceSize  int ) {
85+ 		for  i  :=  0 ; i  <  256 ; i ++  {
86+ 			var  nonce  =  make ([]byte , nonceSize )
87+ 			var  key  [32 ]byte 
88+ 
89+ 			al  :=  mr .Intn (128 )
90+ 			pl  :=  mr .Intn (16384 )
91+ 			ad  :=  make ([]byte , al )
92+ 			plaintext  :=  make ([]byte , pl )
93+ 			cr .Read (key [:])
94+ 			cr .Read (nonce [:])
95+ 			cr .Read (ad )
96+ 			cr .Read (plaintext )
97+ 
98+ 			var  (
99+ 				aead  cipher.AEAD 
100+ 				err   error 
101+ 			)
102+ 			switch  len (nonce ) {
103+ 			case  NonceSize :
104+ 				aead , err  =  New (key [:])
105+ 			case  NonceSizeX :
106+ 				aead , err  =  NewX (key [:])
107+ 			default :
108+ 				t .Fatalf ("#%d: wrong nonce length: %d" , i , len (nonce ))
109+ 			}
110+ 			if  err  !=  nil  {
111+ 				t .Fatal (err )
112+ 			}
88113
89- 		ct  :=  aead .Seal (nil , nonce [:], plaintext , ad )
114+ 			 ct  :=  aead .Seal (nil , nonce [:], plaintext , ad )
90115
91- 		plaintext2 , err  :=  aead .Open (nil , nonce [:], ct , ad )
92- 		if  err  !=  nil  {
93- 			t .Errorf ("Random #%d: Open failed" , i )
94- 			continue 
95- 		}
116+ 			 plaintext2 , err  :=  aead .Open (nil , nonce [:], ct , ad )
117+ 			 if  err  !=  nil  {
118+ 				 t .Errorf ("Random #%d: Open failed" , i )
119+ 				 continue 
120+ 			 }
96121
97- 		if  ! bytes .Equal (plaintext , plaintext2 ) {
98- 			t .Errorf ("Random #%d: plaintext's don't match: got %x vs %x" , i , plaintext2 , plaintext )
99- 			continue 
100- 		}
122+ 			 if  ! bytes .Equal (plaintext , plaintext2 ) {
123+ 				 t .Errorf ("Random #%d: plaintext's don't match: got %x vs %x" , i , plaintext2 , plaintext )
124+ 				 continue 
125+ 			 }
101126
102- 		if  len (ad ) >  0  {
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 )
127+ 			if  len (ad ) >  0  {
128+ 				alterAdIdx  :=  mr .Intn (len (ad ))
129+ 				ad [alterAdIdx ] ^=  0x80 
130+ 				if  _ , err  :=  aead .Open (nil , nonce [:], ct , ad ); err  ==  nil  {
131+ 					t .Errorf ("Random #%d: Open was successful after altering additional data" , i )
132+ 				}
133+ 				ad [alterAdIdx ] ^=  0x80 
107134			}
108- 			ad [alterAdIdx ] ^=  0x80 
109- 		}
110135
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 )
115- 		}
116- 		nonce [alterNonceIdx ] ^=  0x80 
136+ 			 alterNonceIdx  :=  mr .Intn (aead .NonceSize ())
137+ 			 nonce [alterNonceIdx ] ^=  0x80 
138+ 			 if  _ , err  :=  aead .Open (nil , nonce [:], ct , ad ); err  ==  nil  {
139+ 				 t .Errorf ("Random #%d: Open was successful after altering nonce" , i )
140+ 			 }
141+ 			 nonce [alterNonceIdx ] ^=  0x80 
117142
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 )
143+ 			alterCtIdx  :=  mr .Intn (len (ct ))
144+ 			ct [alterCtIdx ] ^=  0x80 
145+ 			if  _ , err  :=  aead .Open (nil , nonce [:], ct , ad ); err  ==  nil  {
146+ 				t .Errorf ("Random #%d: Open was successful after altering ciphertext" , i )
147+ 			}
148+ 			ct [alterCtIdx ] ^=  0x80 
122149		}
123- 		ct [alterCtIdx ] ^=  0x80 
124150	}
151+ 	t .Run ("Standard" , func (t  * testing.T ) { f (t , NonceSize ) })
152+ 	t .Run ("X" , func (t  * testing.T ) { f (t , NonceSizeX ) })
125153}
126154
127- func  benchamarkChaCha20Poly1305Seal (b  * testing.B , buf  []byte ) {
155+ func  benchamarkChaCha20Poly1305Seal (b  * testing.B , buf  []byte , nonceSize  int ) {
156+ 	b .ReportAllocs ()
128157	b .SetBytes (int64 (len (buf )))
129158
130159	var  key  [32 ]byte 
131- 	var  nonce  [ 12 ]byte 
160+ 	var  nonce  =   make ([ ]byte ,  nonceSize ) 
132161	var  ad  [13 ]byte 
133162	var  out  []byte 
134163
135- 	aead , _  :=  New (key [:])
164+ 	var  aead  cipher.AEAD 
165+ 	switch  len (nonce ) {
166+ 	case  NonceSize :
167+ 		aead , _  =  New (key [:])
168+ 	case  NonceSizeX :
169+ 		aead , _  =  NewX (key [:])
170+ 	}
171+ 
136172	b .ResetTimer ()
137173	for  i  :=  0 ; i  <  b .N ; i ++  {
138174		out  =  aead .Seal (out [:0 ], nonce [:], buf [:], ad [:])
139175	}
140176}
141177
142- func  benchamarkChaCha20Poly1305Open (b  * testing.B , buf  []byte ) {
178+ func  benchamarkChaCha20Poly1305Open (b  * testing.B , buf  []byte , nonceSize  int ) {
179+ 	b .ReportAllocs ()
143180	b .SetBytes (int64 (len (buf )))
144181
145182	var  key  [32 ]byte 
146- 	var  nonce  [ 12 ]byte 
183+ 	var  nonce  =   make ([ ]byte ,  nonceSize ) 
147184	var  ad  [13 ]byte 
148185	var  ct  []byte 
149186	var  out  []byte 
150187
151- 	aead , _  :=  New (key [:])
188+ 	var  aead  cipher.AEAD 
189+ 	switch  len (nonce ) {
190+ 	case  NonceSize :
191+ 		aead , _  =  New (key [:])
192+ 	case  NonceSizeX :
193+ 		aead , _  =  NewX (key [:])
194+ 	}
152195	ct  =  aead .Seal (ct [:0 ], nonce [:], buf [:], ad [:])
153196
154197	b .ResetTimer ()
@@ -157,26 +200,20 @@ func benchamarkChaCha20Poly1305Open(b *testing.B, buf []byte) {
157200	}
158201}
159202
160- func  BenchmarkChacha20Poly1305Open_64 (b  * testing.B ) {
161- 	benchamarkChaCha20Poly1305Open (b , make ([]byte , 64 ))
162- }
163- 
164- func  BenchmarkChacha20Poly1305Seal_64 (b  * testing.B ) {
165- 	benchamarkChaCha20Poly1305Seal (b , make ([]byte , 64 ))
166- }
167- 
168- func  BenchmarkChacha20Poly1305Open_1350 (b  * testing.B ) {
169- 	benchamarkChaCha20Poly1305Open (b , make ([]byte , 1350 ))
170- }
171- 
172- func  BenchmarkChacha20Poly1305Seal_1350 (b  * testing.B ) {
173- 	benchamarkChaCha20Poly1305Seal (b , make ([]byte , 1350 ))
174- }
175- 
176- func  BenchmarkChacha20Poly1305Open_8K (b  * testing.B ) {
177- 	benchamarkChaCha20Poly1305Open (b , make ([]byte , 8 * 1024 ))
178- }
179- 
180- func  BenchmarkChacha20Poly1305Seal_8K (b  * testing.B ) {
181- 	benchamarkChaCha20Poly1305Seal (b , make ([]byte , 8 * 1024 ))
203+ func  BenchmarkChacha20Poly1305 (b  * testing.B ) {
204+ 	for  _ , length  :=  range  []int {64 , 1350 , 8  *  1024 } {
205+ 		b .Run ("Open-" + strconv .Itoa (length ), func (b  * testing.B ) {
206+ 			benchamarkChaCha20Poly1305Open (b , make ([]byte , length ), NonceSize )
207+ 		})
208+ 		b .Run ("Seal-" + strconv .Itoa (length ), func (b  * testing.B ) {
209+ 			benchamarkChaCha20Poly1305Seal (b , make ([]byte , length ), NonceSize )
210+ 		})
211+ 
212+ 		b .Run ("Open-" + strconv .Itoa (length )+ "-X" , func (b  * testing.B ) {
213+ 			benchamarkChaCha20Poly1305Open (b , make ([]byte , length ), NonceSizeX )
214+ 		})
215+ 		b .Run ("Seal-" + strconv .Itoa (length )+ "-X" , func (b  * testing.B ) {
216+ 			benchamarkChaCha20Poly1305Seal (b , make ([]byte , length ), NonceSizeX )
217+ 		})
218+ 	}
182219}
0 commit comments