Skip to content

Commit 9a6a370

Browse files
committed
Add test vectors RustCrypto#1 - RustCrypto#3 from RFC 7539 section A.1
1 parent 4969237 commit 9a6a370

File tree

1 file changed

+84
-1
lines changed

1 file changed

+84
-1
lines changed

chacha20/tests/chacha20.rs

+84-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ extern crate block_cipher_trait;
22
extern crate chacha20;
33
extern crate stream_cipher;
44

5-
use block_cipher_trait::generic_array::{typenum::U8, GenericArray};
5+
use block_cipher_trait::generic_array::{
6+
typenum::{U12, U8},
7+
GenericArray,
8+
};
69
use chacha20::ChaCha20;
710
use stream_cipher::NewStreamCipher;
811
use stream_cipher::StreamCipher;
@@ -14,6 +17,9 @@ const KEY_BYTES: usize = 32;
1417
#[cfg(test)]
1518
const IV_BYTES: usize = 8;
1619

20+
#[cfg(test)]
21+
const LONG_IV_BYTES: usize = 12;
22+
1723
#[cfg(test)]
1824
const IETF_KEY0: [u8; KEY_BYTES] = [0; KEY_BYTES];
1925

@@ -35,6 +41,9 @@ const IETF_IV0: [u8; IV_BYTES] = [0; IV_BYTES];
3541
#[cfg(test)]
3642
const IETF_IV1: [u8; IV_BYTES] = [0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
3743

44+
#[cfg(test)]
45+
const IETF_IV2: [u8; LONG_IV_BYTES] = [0; LONG_IV_BYTES];
46+
3847
#[cfg(test)]
3948
const IETF_IVHI: [u8; IV_BYTES] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01];
4049

@@ -73,6 +82,30 @@ const EXPECTED_IETF_KEY0_IVHI: [u8; 60] = [
7382
0x52, 0x77, 0x06, 0x2e, 0xb7, 0xa0, 0x43, 0x3e, 0x44, 0x5f, 0x41, 0xe3,
7483
];
7584

85+
#[cfg(test)]
86+
const EXPECTED_IETF_KEY0_IV2: [u8; 64] = [
87+
0x76, 0xb8, 0xe0, 0xad, 0xa0, 0xf1, 0x3d, 0x90, 0x40, 0x5d, 0x6a, 0xe5, 0x53, 0x86, 0xbd, 0x28,
88+
0xbd, 0xd2, 0x19, 0xb8, 0xa0, 0x8d, 0xed, 0x1a, 0xa8, 0x36, 0xef, 0xcc, 0x8b, 0x77, 0x0d, 0xc7,
89+
0xda, 0x41, 0x59, 0x7c, 0x51, 0x57, 0x48, 0x8d, 0x77, 0x24, 0xe0, 0x3f, 0xb8, 0xd8, 0x4a, 0x37,
90+
0x6a, 0x43, 0xb8, 0xf4, 0x15, 0x18, 0xa1, 0x1c, 0xc3, 0x87, 0xb6, 0x69, 0xb2, 0xee, 0x65, 0x86,
91+
];
92+
93+
#[cfg(test)]
94+
const EXPECTED_IETF_KEY0_IV2_COUNTER1: [u8; 64] = [
95+
0x9f, 0x07, 0xe7, 0xbe, 0x55, 0x51, 0x38, 0x7a, 0x98, 0xba, 0x97, 0x7c, 0x73, 0x2d, 0x08, 0x0d,
96+
0xcb, 0x0f, 0x29, 0xa0, 0x48, 0xe3, 0x65, 0x69, 0x12, 0xc6, 0x53, 0x3e, 0x32, 0xee, 0x7a, 0xed,
97+
0x29, 0xb7, 0x21, 0x76, 0x9c, 0xe6, 0x4e, 0x43, 0xd5, 0x71, 0x33, 0xb0, 0x74, 0xd8, 0x39, 0xd5,
98+
0x31, 0xed, 0x1f, 0x28, 0x51, 0x0a, 0xfb, 0x45, 0xac, 0xe1, 0x0a, 0x1f, 0x4b, 0x79, 0x4d, 0x6f,
99+
];
100+
101+
#[cfg(test)]
102+
const EXPECTED_IETF_KEY1_IV2_COUNTER1: [u8; 64] = [
103+
0x3a, 0xeb, 0x52, 0x24, 0xec, 0xf8, 0x49, 0x92, 0x9b, 0x9d, 0x82, 0x8d, 0xb1, 0xce, 0xd4, 0xdd,
104+
0x83, 0x20, 0x25, 0xe8, 0x01, 0x8b, 0x81, 0x60, 0xb8, 0x22, 0x84, 0xf3, 0xc9, 0x49, 0xaa, 0x5a,
105+
0x8e, 0xca, 0x00, 0xbb, 0xb4, 0xa7, 0x3b, 0xda, 0xd1, 0x92, 0xb5, 0xc4, 0x2f, 0x73, 0xf2, 0xfd,
106+
0x4e, 0x27, 0x36, 0x44, 0xc8, 0xb3, 0x61, 0x25, 0xa6, 0x4a, 0xdd, 0xeb, 0x00, 0x6c, 0x13, 0xa0,
107+
];
108+
76109
#[cfg(test)]
77110
const EXPECTED_LONG: [u8; 256] = [
78111
0xde, 0xeb, 0x6b, 0x9d, 0x06, 0xdf, 0xf3, 0xe0, 0x91, 0xbf, 0x3a, 0xd4, 0xf4, 0xd4, 0x92, 0xb6,
@@ -153,6 +186,56 @@ fn chacha20_ietf_key0_ivhi() {
153186
}
154187
}
155188

189+
#[test]
190+
fn chacha20_ietf_key0_iv2() {
191+
let mut cipher: ChaCha20<U12> = ChaCha20::new(
192+
&GenericArray::from(IETF_KEY0),
193+
&GenericArray::from(IETF_IV2),
194+
);
195+
196+
let mut buf = [0; 64];
197+
198+
cipher.encrypt(&mut buf);
199+
200+
for i in 0..64 {
201+
assert_eq!(buf[i], EXPECTED_IETF_KEY0_IV2[i])
202+
}
203+
}
204+
205+
#[test]
206+
fn chacha20_ietf_key0_iv2_counter1() {
207+
let mut cipher: ChaCha20<U12> = ChaCha20::new(
208+
&GenericArray::from(IETF_KEY0),
209+
&GenericArray::from(IETF_IV2),
210+
);
211+
cipher.seek(1 << 6);
212+
213+
let mut buf = [0; 64];
214+
215+
cipher.encrypt(&mut buf);
216+
217+
for i in 0..64 {
218+
assert_eq!(buf[i], EXPECTED_IETF_KEY0_IV2_COUNTER1[i])
219+
}
220+
}
221+
222+
#[test]
223+
fn chacha20_ietf_key1_iv2_counter1() {
224+
let mut cipher: ChaCha20<U12> = ChaCha20::new(
225+
&GenericArray::from(IETF_KEY1),
226+
&GenericArray::from(IETF_IV2),
227+
);
228+
cipher.seek(1 << 6);
229+
230+
let mut buf = [0; 64];
231+
232+
cipher.encrypt(&mut buf);
233+
234+
for i in 0..64 {
235+
assert_eq!(buf[i], EXPECTED_IETF_KEY1_IV2_COUNTER1[i])
236+
}
237+
}
238+
156239
#[test]
157240
fn chacha20_long() {
158241
let mut cipher: ChaCha20<U8> =

0 commit comments

Comments
 (0)