Debian Codegraph @

debian/rust-vendor/chacha20/tests/kats.rs

raw
1 //! Tests for ChaCha20 (IETF and "djb" versions) as well as XChaCha20
2 #[cfg(feature = "cipher")]
3 use chacha20::ChaCha20;
4
5 #[cfg(feature = "legacy")]
6 use chacha20::ChaCha20Legacy;
7
8 #[cfg(feature = "xchacha")]
9 use chacha20::XChaCha20;
10
11 // IETF version of ChaCha20 (96-bit nonce)
12 #[cfg(feature = "cipher")]
13 cipher::stream_cipher_test!(chacha20_core, "chacha20", ChaCha20);
14 #[cfg(feature = "cipher")]
15 cipher::stream_cipher_seek_test!(chacha20_seek, ChaCha20);
16 #[cfg(feature = "xchacha")]
17 cipher::stream_cipher_seek_test!(xchacha20_seek, XChaCha20);
18 #[cfg(feature = "legacy")]
19 cipher::stream_cipher_seek_test!(chacha20legacy_seek, ChaCha20Legacy);
20
21 #[cfg(feature = "cipher")]
22 mod chacha20test {
23 use chacha20::{ChaCha20, KeyIvInit};
24 use cipher::StreamCipher;
25 use hex_literal::hex;
26
27 //
28 // ChaCha20 test vectors from:
29 // <https://datatracker.ietf.org/doc/html/rfc8439#section-2.4.2>
30 //
31
32 const KEY: [u8; 32] = hex!("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f");
33
34 const IV: [u8; 12] = hex!("000000000000004a00000000");
35
36 const PLAINTEXT: [u8; 114] = hex!(
37 "
38 4c616469657320616e642047656e746c
39 656d656e206f662074686520636c6173
40 73206f66202739393a20496620492063
41 6f756c64206f6666657220796f75206f
42 6e6c79206f6e652074697020666f7220
43 746865206675747572652c2073756e73
44 637265656e20776f756c642062652069
45 742e
46 "
47 );
48
49 const KEYSTREAM: [u8; 114] = hex!(
50 "
51 224f51f3401bd9e12fde276fb8631ded8c131f823d2c06
52 e27e4fcaec9ef3cf788a3b0aa372600a92b57974cded2b
53 9334794cba40c63e34cdea212c4cf07d41b769a6749f3f
54 630f4122cafe28ec4dc47e26d4346d70b98c73f3e9c53a
55 c40c5945398b6eda1a832c89c167eacd901d7e2bf363
56 "
57 );
58
59 const CIPHERTEXT: [u8; 114] = hex!(
60 "
61 6e2e359a2568f98041ba0728dd0d6981
62 e97e7aec1d4360c20a27afccfd9fae0b
63 f91b65c5524733ab8f593dabcd62b357
64 1639d624e65152ab8f530c359f0861d8
65 07ca0dbf500d6a6156a38e088a22b65e
66 52bc514d16ccf806818ce91ab7793736
67 5af90bbf74a35be6b40b8eedf2785e42
68 874d
69 "
70 );
71
72 #[test]
73 fn chacha20_keystream() {
74 let mut cipher = ChaCha20::new(&KEY.into(), &IV.into());
75
76 // The test vectors omit the first 64-bytes of the keystream
77 let mut prefix = [0u8; 64];
78 cipher.apply_keystream(&mut prefix);
79
80 let mut buf = [0u8; 114];
81 cipher.apply_keystream(&mut buf);
82 assert_eq!(&buf[..], &KEYSTREAM[..]);
83 }
84
85 #[test]
86 fn chacha20_encryption() {
87 let mut cipher = ChaCha20::new(&KEY.into(), &IV.into());
88 let mut buf = PLAINTEXT;
89
90 // The test vectors omit the first 64-bytes of the keystream
91 let mut prefix = [0u8; 64];
92 cipher.apply_keystream(&mut prefix);
93
94 cipher.apply_keystream(&mut buf);
95 assert_eq!(&buf[..], &CIPHERTEXT[..]);
96 }
97 }
98
99 #[rustfmt::skip]
100 #[cfg(feature = "xchacha")]
101 mod xchacha20 {
102 use chacha20::{Key, XChaCha20, XNonce};
103 use cipher::{KeyIvInit, StreamCipher};
104 use hex_literal::hex;
105
106 cipher::stream_cipher_seek_test!(xchacha20_seek, XChaCha20);
107
108 //
109 // XChaCha20 test vectors from:
110 // <https://datatracker.ietf.org/doc/html/draft-arciszewski-xchacha-03#appendix-A.2>
111 //
112
113 const KEY: [u8; 32] = hex!("
114 808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f
115 ");
116
117 const IV: [u8; 24] = hex!("
118 404142434445464748494a4b4c4d4e4f5051525354555658
119 ");
120
121 const PLAINTEXT: [u8; 304] = hex!("
122 5468652064686f6c65202870726f6e6f756e6365642022646f6c652229206973
123 20616c736f206b6e6f776e2061732074686520417369617469632077696c6420
124 646f672c2072656420646f672c20616e642077686973746c696e6720646f672e
125 2049742069732061626f7574207468652073697a65206f662061204765726d61
126 6e20736865706865726420627574206c6f6f6b73206d6f7265206c696b652061
127 206c6f6e672d6c656767656420666f782e205468697320686967686c7920656c
128 757369766520616e6420736b696c6c6564206a756d70657220697320636c6173
129 736966696564207769746820776f6c7665732c20636f796f7465732c206a6163
130 6b616c732c20616e6420666f78657320696e20746865207461786f6e6f6d6963
131 2066616d696c792043616e696461652e
132 ");
133
134 const KEYSTREAM: [u8; 304] = hex!("
135 29624b4b1b140ace53740e405b2168540fd7d630c1f536fecd722fc3cddba7f4
136 cca98cf9e47e5e64d115450f9b125b54449ff76141ca620a1f9cfcab2a1a8a25
137 5e766a5266b878846120ea64ad99aa479471e63befcbd37cd1c22a221fe46221
138 5cf32c74895bf505863ccddd48f62916dc6521f1ec50a5ae08903aa259d9bf60
139 7cd8026fba548604f1b6072d91bc91243a5b845f7fd171b02edc5a0a84cf28dd
140 241146bc376e3f48df5e7fee1d11048c190a3d3deb0feb64b42d9c6fdeee290f
141 a0e6ae2c26c0249ea8c181f7e2ffd100cbe5fd3c4f8271d62b15330cb8fdcf00
142 b3df507ca8c924f7017b7e712d15a2eb5c50484451e54e1b4b995bd8fdd94597
143 bb94d7af0b2c04df10ba0890899ed9293a0f55b8bafa999264035f1d4fbe7fe0
144 aafa109a62372027e50e10cdfecca127
145 ");
146
147 const CIPHERTEXT: [u8; 304] = hex!("
148 7d0a2e6b7f7c65a236542630294e063b7ab9b555a5d5149aa21e4ae1e4fbce87
149 ecc8e08a8b5e350abe622b2ffa617b202cfad72032a3037e76ffdcdc4376ee05
150 3a190d7e46ca1de04144850381b9cb29f051915386b8a710b8ac4d027b8b050f
151 7cba5854e028d564e453b8a968824173fc16488b8970cac828f11ae53cabd201
152 12f87107df24ee6183d2274fe4c8b1485534ef2c5fbc1ec24bfc3663efaa08bc
153 047d29d25043532db8391a8a3d776bf4372a6955827ccb0cdd4af403a7ce4c63
154 d595c75a43e045f0cce1f29c8b93bd65afc5974922f214a40b7c402cdb91ae73
155 c0b63615cdad0480680f16515a7ace9d39236464328a37743ffc28f4ddb324f4
156 d0f5bbdc270c65b1749a6efff1fbaa09536175ccd29fb9e6057b307320d31683
157 8a9c71f70b5b5907a66f7ea49aadc409
158 ");
159
160 #[test]
161 fn xchacha20_keystream() {
162 let mut cipher = XChaCha20::new(&Key::from(KEY), &XNonce::from(IV));
163
164 // The test vectors omit the first 64-bytes of the keystream
165 let mut prefix = [0u8; 64];
166 cipher.apply_keystream(&mut prefix);
167
168 let mut buf = [0u8; 304];
169 cipher.apply_keystream(&mut buf);
170 assert_eq!(&buf[..], &KEYSTREAM[..]);
171 }
172
173 #[test]
174 fn xchacha20_encryption() {
175 let mut cipher = XChaCha20::new(&Key::from(KEY), &XNonce::from(IV));
176 let mut buf = PLAINTEXT;
177
178 // The test vectors omit the first 64-bytes of the keystream
179 let mut prefix = [0u8; 64];
180 cipher.apply_keystream(&mut prefix);
181
182 cipher.apply_keystream(&mut buf);
183 assert_eq!(&buf[..], &CIPHERTEXT[..]);
184 }
185 }
186
187 // Legacy "djb" version of ChaCha20 (64-bit nonce)
188 #[cfg(feature = "legacy")]
189 #[rustfmt::skip]
190 mod legacy {
191 use chacha20::{ChaCha20Legacy, LegacyNonce};
192 use cipher::{StreamCipher, StreamCipherSeek, KeyIvInit};
193 use hex_literal::hex;
194
195 cipher::stream_cipher_test!(chacha20_legacy_core, "chacha20-legacy", ChaCha20Legacy);
196 cipher::stream_cipher_seek_test!(chacha20_legacy_seek, ChaCha20Legacy);
197
198 const KEY_LONG: [u8; 32] = hex!("
199 0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20
200 ");
201
202 const IV_LONG: [u8; 8] = hex!("0301040105090206");
203
204 const EXPECTED_LONG: [u8; 256] = hex!("
205 deeb6b9d06dff3e091bf3ad4f4d492b6dd98246f69691802e466e03bad235787
206 0f1c6c010b6c2e650c4bf58d2d35c72ab639437069a384e03100078cc1d735a0
207 db4e8f474ee6291460fd9197c77ed87b4c64e0d9ac685bd1c56cce021f3819cd
208 13f49c9a3053603602582a060e59c2fbee90ab0bf7bb102d819ced03969d3bae
209 71034fe598246583336aa744d8168e5dfff5c6d10270f125a4130e719717e783
210 c0858b6f7964437173ea1d7556c158bc7a99e74a34d93da6bf72ac9736a215ac
211 aefd4ec031f3f13f099e3d811d83a2cf1d544a68d2752409cc6be852b0511a2e
212 32f69aa0be91b30981584a1c56ce7546cca24d8cfdfca525d6b15eea83b6b686
213 ");
214
215 #[test]
216 #[ignore]
217 fn chacha20_offsets() {
218 for idx in 0..256 {
219 for middle in idx..256 {
220 for last in middle..256 {
221 let mut cipher =
222 ChaCha20Legacy::new(&KEY_LONG.into(), &LegacyNonce::from(IV_LONG));
223 let mut buf = [0; 256];
224
225 cipher.seek(idx as u64);
226 cipher.apply_keystream(&mut buf[idx..middle]);
227 cipher.apply_keystream(&mut buf[middle..last]);
228
229 for k in idx..last {
230 assert_eq!(buf[k], EXPECTED_LONG[k]);
231 }
232 }
233 }
234 }
235 }
236 }