@@ -121,4 +121,93 @@ mod test {
121121
122122 assert_eq ! ( [ x0, x1] , target) ;
123123 }
124+
125+ #[ test]
126+ fn test_chacha_true_values_a ( ) {
127+ // Test vectors 1 and 2 from
128+ // https://tools.ietf.org/html/draft-nir-cfrg-chacha20-poly1305-04
129+ // but the expected values are replaced with the ones produced
130+ // by Chacha12 (as generated by rand_chacha).
131+ let seed = [ 0u8 ; 32 ] ;
132+ let mut rng = StdRng :: from_seed ( seed) ;
133+
134+ let mut results = [ 0u32 ; 16 ] ;
135+ for i in results. iter_mut ( ) {
136+ * i = rng. next_u32 ( ) ;
137+ }
138+ let expected = [
139+ 1788540059 , 1408849159 , 315498369 , 3582142047 , 3150129412 , 343203913 , 2777219198 ,
140+ 1595256366 , 2046321669 , 3236133586 , 875096108 , 2039282348 , 748642874 , 426368672 ,
141+ 803736417 , 3190166337 ,
142+ ] ;
143+ assert_eq ! ( results, expected) ;
144+
145+ for i in results. iter_mut ( ) {
146+ * i = rng. next_u32 ( ) ;
147+ }
148+ let expected = [
149+ 1099486475 , 4269030944 , 863108230 , 1024974988 , 3085641926 , 3435904281 , 562369813 ,
150+ 3028926540 , 3448579394 , 301026465 , 2545418950 , 1137216539 , 3393593065 , 3226554768 ,
151+ 2963686439 , 378791447 ,
152+ ] ;
153+ assert_eq ! ( results, expected) ;
154+ }
155+
156+ #[ test]
157+ fn test_chacha_true_values_b ( ) {
158+ // Test vector 3 from
159+ // https://tools.ietf.org/html/draft-nir-cfrg-chacha20-poly1305-04
160+ // but the expected values are replaced with the ones produced
161+ // by Chacha12 (as generated by rand_chacha).
162+ let seed = [
163+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
164+ 0 , 0 , 1 ,
165+ ] ;
166+ let mut rng = StdRng :: from_seed ( seed) ;
167+
168+ // Skip block 0
169+ for _ in 0 ..16 {
170+ rng. next_u32 ( ) ;
171+ }
172+
173+ let mut results = [ 0u32 ; 16 ] ;
174+ for i in results. iter_mut ( ) {
175+ * i = rng. next_u32 ( ) ;
176+ }
177+ let expected = [
178+ 1149489484 , 1453872210 , 205606722 , 2561053166 , 4209612569 , 2282920975 , 2059979989 ,
179+ 3051314295 , 4135011526 , 3904759261 , 3697107527 , 2431012387 , 1801979597 , 1269452364 ,
180+ 3197257745 , 2154707421 ,
181+ ] ;
182+ assert_eq ! ( results, expected) ;
183+ }
184+
185+ #[ test]
186+ fn test_chacha_true_values_c ( ) {
187+ // Test vector 4 from
188+ // https://tools.ietf.org/html/draft-nir-cfrg-chacha20-poly1305-04
189+ // but the expected values are replaced with the ones produced
190+ // by Chacha12 (as generated by rand_chacha).
191+ let seed = [
192+ 0 , 0xff , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
193+ 0 , 0 , 0 , 0 ,
194+ ] ;
195+ let expected = [
196+ 1614024310 , 613097064 , 193455541 , 494774344 , 2671734178 , 1658534307 , 1449802595 ,
197+ 1853279994 , 129091709 , 2135285029 , 2884964524 , 3629948889 , 2335941772 , 2471549797 ,
198+ 887756884 , 3388159322 ,
199+ ] ;
200+ let expected_end = 3 * 16 ;
201+ let mut results = [ 0u32 ; 16 ] ;
202+
203+ // Test block 2 by skipping block 0 and 1
204+ let mut rng1 = StdRng :: from_seed ( seed) ;
205+ for _ in 0 ..32 {
206+ rng1. next_u32 ( ) ;
207+ }
208+ for i in results. iter_mut ( ) {
209+ * i = rng1. next_u32 ( ) ;
210+ }
211+ assert_eq ! ( results, expected) ;
212+ }
124213}
0 commit comments